Github 備份:LINK
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
/* TQC+ C - 901_1 */ | |
#include<stdlib.h> | |
#include<stdio.h> | |
void exchange(int* ,int* ); | |
int main() | |
{ | |
int a,b; | |
printf("請輸入兩個數字(數字跟數字中間請以空白間隔)"); | |
scanf("%d %d",&a,&b); | |
printf("\na:%d b:%d",a,b); | |
exchange(&a,&b); | |
printf("\na:%d b:%d",a,b); | |
system("PAUSE"); | |
return 0; | |
} | |
void exchange(int* a,int* b){/* 將記憶體位址做調換的動作 */ | |
int temp; | |
temp = *a; | |
*a =*b ; | |
*b=temp; | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
/* TQC+ C - 901_2 */ | |
#include<stdlib.h> | |
#include<stdio.h> | |
void exchange(double* ,double* ); | |
int main() | |
{ | |
double a,b; | |
printf("請輸入兩個浮點數(數字跟數字中間請以空白間隔)"); | |
scanf("%lf %lf",&a,&b);/* 輸入double要用%lf */ | |
printf("\na:%f b:%f",a,b); | |
exchange(&a,&b); | |
printf("\na:%f b:%f",a,b); | |
system("PAUSE"); | |
return 0; | |
} | |
void exchange(double* a,double* b){/* 改成double */ | |
double temp; | |
temp = *a; | |
*a =*b ; | |
*b=temp; | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
/* TQC+ C - 902 */ | |
#include<stdlib.h> | |
#include<stdio.h> | |
int main() | |
{ | |
int i, c[6] ; | |
c[0]=c[1]=c[2]=c[3]=c[4]=c[5]=0;/* 先將他們初始值皆設定為0 */ | |
while( c[0]==c[1] && | |
c[1]==c[2] && | |
c[2]==c[3] && | |
c[3]==c[4] && | |
c[4]==c[5]) {/* 條件式判斷,是否有相同的數字 */ | |
for(i=0;i<6;i++) { | |
if(i%2==0)/* 其實這邊只是讓他的數字更亂而已 */ | |
c[i]=rand()%20+rand()%29+2; | |
else | |
c[i]=rand()%49+1; | |
} | |
} | |
printf("數字: "); | |
for(i=0;i<6;i++) | |
printf(" %d",c[i]); | |
system("PAUSE"); | |
return 0; | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
/* TQC+ C - 903 */ | |
#include<stdlib.h> | |
#include<stdio.h> | |
/* 宣告函數 */ | |
int greater60(int* ,int ); | |
int main() | |
{ | |
int data[6]; | |
int i; | |
for(i=0;i<6;i++){ | |
printf("請輸入第%d個分數:",(i+1)); | |
scanf("%d",&data[i]); | |
} | |
printf("您輸入的資料如下:\n"); | |
for(i=0;i<6;i++){ | |
printf("\ndata[%d]:%d",i,data[i]); | |
} | |
printf("\n大於60分得有:%d個",greater60(data,6)); | |
system("PAUSE"); | |
return 0; | |
} | |
int greater60(int* parr,int n) | |
{ | |
int i,c=0; | |
for(i=0;i<n;i++){ | |
if(parr[i]>=60) | |
c++; | |
} | |
return c; | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
/* TQC+ C - 904_1 */ | |
#include<stdlib.h> | |
#include<stdio.h> | |
int main() | |
{ | |
FILE *fi; | |
char name[20]; | |
int score; | |
fi=fopen("score.dat", "w"); /* 開啟檔案與寫入 */ | |
printf("請輸入學生的姓名(分數輸入負的分數時結束):"); | |
scanf("%s",name); | |
printf("請輸入此學生C語言分數:"); | |
scanf("%d",&score); | |
while(score>=0){ /* 條件是判斷,要數字非負數才會持續寫入 */ | |
fprintf(fi,"%s %d",name,score); | |
printf("請輸入學生的姓名(分數輸入負的分數時結束):"); | |
scanf("%s",name); | |
printf("請輸入此學生C語言分數:"); | |
scanf("%d",&score); | |
} | |
fclose(fi); | |
system("PAUSE"); | |
return 0; | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
/* TQC+ C - 904_2 */ | |
#include<stdlib.h> | |
#include<stdio.h> | |
int main(){ | |
FILE *fi; | |
char name[20]; | |
int score; | |
fi=fopen("score.dat", "r"); | |
while(fscanf(fi,"%s %d",name,&score)!=EOF){/* 判斷讀取是否為檔案結尾 */ | |
printf("%s的C語言分數是%d\n",name,score); | |
} | |
fclose(fi); | |
system("PAUSE"); | |
return 0; | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
/* TQC+ C - 905_1 */ | |
#include<stdlib.h> | |
#include<stdio.h> | |
int main(){ | |
FILE *fi; | |
double ss; | |
fi = fopen("dnumber.dat","w"); | |
printf("請輸入浮點數:"); | |
scanf("%lf",&ss); | |
while(ss!=-99.99){//當使用者輸入為-99.99時,停止寫入 | |
fprintf(fi," %lf ",ss); | |
printf("請輸入浮點數:"); | |
scanf("%lf",&ss); | |
} | |
fclose(fi); | |
system("PAUSE"); | |
return 0; | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
/* TQC+ C - 905_2 */ | |
#include<stdlib.h> | |
#include<stdio.h> | |
int main(){ | |
FILE *fi; | |
double ss; | |
fi = fopen("dnumber.dat","r"); | |
while(fscanf(fi," %lf ",&ss)!=EOF){ | |
printf("%.2lf\n",ss);//注意排版問題 | |
} | |
fclose(fi);//檔案不使用後,請關閉檔案,雖然程式結束後也會關閉檔案 | |
system("PAUSE"); | |
return 0; | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
/* TQC+ C - 905_1 */ | |
#include<stdlib.h> | |
#include<stdio.h> | |
/* 宣告一個結構 */ | |
struct student{ | |
char name[20]; | |
int score; | |
}; | |
int main(){ | |
struct student st[5]; | |
int i; | |
for(i=0;i<5;i++){ | |
printf("請輸入第%d位同學的姓名:",(i+1)); | |
scanf("%s",st[i].name); | |
printf("請輸入第%d位同學C語言的分數:",(i+1)); | |
scanf("%d",&st[i].score ); | |
} | |
for(i=0;i<5;i++){ | |
printf("%-10s %d\n",st[i].name ,st[i].score); | |
} | |
system("PAUSE"); | |
return 0; | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
/* TQC+ C - 907 */ | |
#include<stdlib.h> | |
#include<stdio.h> | |
char GPA(int); | |
int main(){ | |
struct student{ | |
char name[20]; | |
int score; | |
}; /* 宣告一個結構 */ | |
struct student st1,st2,st3; | |
printf("請輸入第一位學生姓名: "); | |
scanf("%s",st1.name); | |
printf("請輸入第一位學生成績: "); | |
scanf("%d",&st1.score); | |
printf("請輸入第二位學生姓名: "); | |
scanf("%s",st2.name); | |
printf("請輸入第二位學生成績: "); | |
scanf("%d",&st2.score); | |
printf("請輸入第一位學生姓名: "); | |
scanf("%s",st3.name); | |
printf("請輸入第一位學生成績: "); | |
scanf("%d",&st3.score); | |
printf("\n第一位學生: %s",st1.name); | |
printf("\n分數: %d",st1.score ); | |
printf ("\nGPA: %c",GPA(st1.score )); | |
printf("\n第二位學生: %s",st2.name); | |
printf("\n分數: %d",st2.score ); | |
printf ("\nGPA: %c",GPA(st2.score )); | |
printf("\n第三位學生: %s",st3.name); | |
printf("\n分數: %d",st3.score ); | |
printf ("\nGPA: %c",GPA(st3.score )); | |
system("PAUSE"); | |
return 0; | |
} | |
char GPA(int i){//GPA的函數 | |
char re; | |
if(i>=90) | |
re='A'; | |
else if(i>=80&&i<90)//區間判斷的寫法 | |
re='B'; | |
else if(i>=70&&i<80) | |
re='C'; | |
else | |
re='D'; | |
return re; | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
/* TQC+ C - 908 */ | |
#include<stdlib.h> | |
#include<stdio.h> | |
#include<time.h> /* 因為有用到時間函數要把它include */ | |
int main() | |
{ | |
int i ,j,a; | |
srand(time(NULL)); | |
/* 這個函數是要設定亂數的種子,因為其實亂數在產生時, | |
* 其實如果運算在一定時間內,產生的亂數不會差異太大。 | |
* 這邊我們讓種子的基底數,會隨時間而不一樣 | |
*/ | |
for(i=0;i<10;i++){ | |
for(j=0;j<10;j++){ | |
a=(rand()%1000)+1; /* 亂數區間設定方式 */ | |
printf(" %3d ",a); /* 排版要排好 */ | |
} | |
printf("\n"); | |
} | |
system("PAUSE"); | |
return 0; | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
/* TQC+ C - 909 */ | |
#include<stdlib.h> | |
#include<stdio.h> | |
#include<string.h> | |
int main(){ | |
char a[30],b[30],c[30],cmp; | |
printf("請輸入字串A:"); | |
gets(a); | |
/* 這邊在讀取字串時,因為要考量到使用者會輸入空白字元, | |
* 所以無法用scanf(),我們必須使用gets()的函數 | |
*/ | |
printf("字串A的長度:%d",strlen(a)); | |
strcpy(b,a); /* 複製字串的函數 */ | |
printf("\n字串B:%s",b); | |
printf("\n請輸入字串C:"); | |
gets(c); | |
cmp = strcmp(c,a); | |
/* 比較字串大小的函數 | |
* 前面大於後面,結果為1 | |
* 相等,結果為0 | |
* 前面大於後面,結果為-1 | |
*/ | |
if(cmp==1) | |
printf("字串A小於字串C"); | |
else if(cmp==-1) | |
printf("字串C小於字串A"); | |
else | |
printf("字串C等於字串A"); | |
system("PAUSE"); | |
return 0; | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
/* TQC+ C - 910 */ | |
#include<stdlib.h> | |
#include<stdio.h> | |
struct node{ | |
char name[10]; | |
int score; | |
struct node* next; | |
}; | |
int main() | |
{ | |
struct node *a,*b,*c,*current; | |
a=(struct node*)malloc(sizeof(struct node)); /* 配置動態記憶體大小方式 */ | |
printf("請輸入第一個學生姓名: "); | |
scanf("%s",a->name); | |
printf("分數: "); | |
scanf("%d",&a->score ); | |
a->next=NULL; /* 因為不確定後面是否還會有指標,所以先設定成NULL */ | |
b=(struct node*)malloc(sizeof(struct node)); | |
printf("請輸入第二個學生姓名: "); | |
scanf("%s",b->name); | |
printf("分數: "); | |
scanf("%d",&b->score ); | |
a->next=b; /* b設定完成後,便確定a之後有b */ | |
b->next=NULL; /* b之後不確定是否還有指標,設定成NULL */ | |
c=(struct node*)malloc(sizeof(struct node)); | |
printf("請輸入第三個學生姓名: "); | |
scanf("%s",c->name); | |
printf("分數: "); | |
scanf("%d",&c->score ); | |
b->next=c; | |
c->next=NULL; | |
printf("\n輸出...\n"); | |
current = a; | |
while(current!=NULL){ | |
printf("學生: %s\n",current->name); | |
printf("分數: %d\n\n",current->score); | |
current=current->next; /* 每顯示完畢後一次,將指標指給下一個 */ | |
} | |
free(a); /* 釋放記憶體 */ | |
free(b); /* 釋放記憶體 */ | |
free(c); /* 釋放記憶體 */ | |
system("PAUSE"); | |
return 0; | |
} |
TQC+ C 試題總覽:LINK
Github 備份:LINK
本篇教學的程式碼皆由筆者編輯,歡迎轉貼本教學,但請全文轉貼,謝啦~
沒有留言:
張貼留言