2017年4月19日 星期三

C & cmd

命令列引數

C語言部份:
#include <stdio.h>
#include <stdlib.h>

/* run this program using the console pauser or add your own getch, system("pause") or input loop */

int main(int argc, char *argv[]) {

printf("testing now\n");
if(argc!=3){
printf("successful part1\n");
}


printf("%s",argv[0]);





return 0;
}
CMD部分:



PS:利用cmd開啟exe檔 並在其後輸入argc的值,可引入函數



重導

https://www.youtube.com/watch?v=xQXAnI9-Z2I&list=PLFI-nbR0eXR5x192m3XVGJzfJxbEcRo2Z&index=9

2017年4月10日 星期一

C&sql

使用C寫入資料庫

#include <stdio.h>
#include <stdlib.h>


int main(int argc, char *argv[]) {
int account;
char name[30];
double balance;
FILE *cfptr;
if((cfptr=fopen("clients2.dat","w"))==NULL){
printf("file could not be opened\n");

}
else{
printf("enter the account ,name,and balance.\n");
printf("enter EOF to end inpute.\n");
printf("?");
scanf("%d%s%.2f",&account,name,&balance);
while(!feof(stdin)){
fprintf(cfptr,"%d %s %.2f\n",account,name,balance);
printf("?");
scanf("%d%s%lf",&account,name,&balance);

}
fclose(cfptr);
}
return 0;
}


PS1:寫入同檔案夾中的資料庫,名字取決於此處clients2:if((cfptr=fopen("clients2.dat","w"))==NULL){
PS2:fopen(檔案名稱, 模式) 以指定模式開啟檔案 其中"W"代表寫入
*************************
使用C讀入資料庫
#include <stdio.h>
#include <stdlib.h>

int main(int argc, char *argv[]) {
int account;
char name[30];
double balance;
FILE *cfptr;
if((cfptr=fopen("clients.dat","r"))==NULL){
printf("could not open\n");

}
else {
printf("%-10s%-13s%s\n","account","name","balance");
fscanf(cfptr,"%d%s%lf",&account,name,&balance);
while(!feof(cfptr)){
printf("%-10d%-13s%7.2f\n",account,name,balance);
fscanf(cfptr,"%d%s%lf",&account,name,&balance);
}
fclose(cfptr);
}
return 0;
}

2017年3月29日 星期三

C語言 結構與回傳多個值

結構教學
http://programming.im.ncnu.edu.tw/Chapter13.htm
*********************************
結構基礎(回傳多重值:分母)
#include <stdio.h>
#include <stdlib.h>
struct ReturnValue {
int iVal1;
int iVal2;
};

struct ReturnValue TestReturn(){
struct ReturnValue rv;
rv.iVal1 = 19;
rv.iVal2 = 5;
return rv;
}
int main(){
struct ReturnValue rv;
rv = TestReturn( );
printf("%d/%d",rv.iVal1,rv.iVal2);
return 0;
}
***********************************
結構例子(分母乘法) (特殊運算子)
#include <stdio.h>
#include <stdlib.h>
struct fraction{
int a,b;
};

struct fraction multiply(int p,int o,int l,int k){
struct fraction st;
st.a=p*l;
st.b=o*k;
return st;
}
int main() {
struct fraction st1,st2,st3;
st1.a=5;
st1.b=3;
st2.a=4;
st2.b=1;
st3=multiply(st1.a,st1.b,st2.a,st2.b);
printf("%d/%d",st3.a,st3.b);
return 0;
}
**************************
結構例子(分母乘法) (特殊運算子) *改進*
#include <stdio.h>
#include <stdlib.h>
struct fraction {
int a, b;
};

typedef struct fraction Fraction; //以後可以用 Fraction 取代 struct fraction

Fraction multiply(Fraction f1, Fraction f2) {
Fraction st;
st.a = f1.a * f1.b;
//st.b = o*k; //類似
return st;
}
int main() {
Fraction st1, st2, st3;
st1.a = 5;
st1.b = 3;
st2.a = 4;
st2.b = 1;
st3 = multiply(st1, st2);
printf("%d/%d", st3.a, st3.b);
return 0;
}

2017年3月6日 星期一

C語言 進階字串

讓字串中的數字相加

#include <stdio.h>
#include <stdlib.h>


int main() {
char a[20];
    int i,k,x,m[20],p=0;
    printf("enter the word that include number\n");
    gets(a);
    for(i=0;a[i];i++)
        if(sscanf(a+i,"%d%n",&k,&x)){              
            m[i]=k;
            p=p+m[i];      
            i+=x;
        }
    printf("\n");
        printf("%d ",p);
return 0;
}
*******************************
讓英文字母開頭變大寫
#include <stdio.h>
#include <stdlib.h>


int main() {
char a[30];
    int i,k,x,m[20],p=0;
    printf("enter the word\n");
    gets(a);
    if( a[0]>='A' && a[0]<='Z')
    {
    a[0]=a[0];
    }
    else
    if( a[0]>='a' && a[0]<='z')
    {
     a[0]=a[0]-32;
    }
   
   
    for(i=0;i<=30;i++){
    if(a[i]==46){
    if( a[i+1]>='A' && a[i+1]<='Z')
           {
           a[i+1]=a[i+1];
           }
            else
            if( a[i+1]>='a' && a[i+1]<='z')
           {
            a[i+1]=a[i+1]-32;
           }
}
}



    printf("%s",a);
return 0;
}