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 - 301 */ | |
#include <stdio.h> | |
#include <stdlib.h> | |
void Star(); /* 記得補上分號 */ | |
/* C語言在設定函數時,在上方要先宣告一次,之後在main的下方撰寫函數的功能 | |
* 但如果是寫在main上方的函數,則無需先宣告一次 | |
*/ | |
int main () | |
{ | |
printf("呼叫印出星星的函數!!!\n"); | |
Star(); | |
printf("我喜歡學C程式語言\n"); | |
Star(); | |
printf("結束函數的呼叫!!!\n"); | |
system("PAUSE"); | |
return 0; | |
} | |
void Star() | |
{ | |
int i; | |
for(i = 1 ; i <= 30 ; i++) | |
printf("*"); | |
printf("\n"); | |
} |
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 - 302 */ | |
#include <stdio.h> | |
#include <stdlib.h> | |
int adjust(int); | |
int main () | |
{ | |
int score, final; | |
printf("請輸入您的分數: "); | |
scanf("%d", &score); | |
final = adjust(score); | |
/* 記得要將傳回來的分數寫入final變數中 */ | |
printf("調整後的分數: %d", final); | |
system("PAUSE"); | |
return 0; | |
} | |
int adjust(int score) | |
{ | |
int temp; /* 宣告變數 */ | |
if (score >= 60) | |
temp = score + 5; | |
else | |
temp = score + 10; | |
return 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 - 303 */ | |
#include <stdio.h> | |
#include <stdlib.h> | |
int num = 100; | |
void subFun(); | |
int main() | |
{ | |
int num = 200; /* 設定一個區域變數 */ | |
printf("在main函數中的num為%d\n", num); | |
subFun(); | |
system("PAUSE"); | |
return 0; | |
} | |
void subFun() /* 因為沒有要回傳,所以設定成void就好 */ | |
{ | |
printf("在subFun函數中的num為%d\n", num); | |
} |
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 - 304 */ | |
#include <stdio.h> | |
#include <stdlib.h> | |
void callFun(); | |
int main() | |
{ | |
int i; | |
for (i=1; i<=5; i++) | |
callFun(); | |
system("PAUSE"); | |
return 0; | |
} | |
void callFun() | |
{ | |
int ai = 100; | |
static int si = 100; | |
/* 在這邊將si變成static 全域變數, | |
* 讓他變成所有區域都看得懂, | |
* 所以宣告過後,再次宣告就沒意義, | |
* 因此會將它進行累加。 | |
*/ | |
ai++; | |
si++; | |
printf("ai=%d, si=%d\n", ai, si); /* 要補上換行符號 */ | |
} |
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 - 305_1 */ | |
/* CPD03-1.c code */ | |
#include <stdio.h> | |
#include <stdlib.h> | |
int x, y; | |
int callTotal(int,int); | |
int main() | |
{ | |
int tot; | |
printf("請輸入x, y的值: "); | |
scanf("%d %d", &x, &y); | |
tot=callTotal(x,y); | |
printf("%d+%d=%d\n", x, y, tot); | |
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 - 305_2 */ | |
/* CPD03-2.c code */ | |
int callTotal(int x,int y) | |
{ | |
int sum; | |
sum = x + y; | |
return sum; | |
} |
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 - 306 */ | |
#include <stdio.h> | |
#include <stdlib.h> | |
int main() | |
{ | |
int arr[6] = {10, 20, 30, 40, 50, 60}; /* 有六個元素,大小應設定為6 */ | |
int i, total = 0; | |
for (i = 0; i < 6; i++) { /* 陣列的index從0開始 */ | |
total += arr[i]; | |
} | |
printf("總和為%d\n", total); /* 顯示的數字為整數請用%d */ | |
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 - 307 */ | |
#include <stdio.h> | |
#include <stdlib.h> | |
int main() | |
{ | |
double arr2[2][3], total=0.0; | |
int i, j; | |
for (i=0; i<2; i++) /* 宣告大小為2時,裡面的index僅有0和1所以判斷時僅需要小於2即可 */ | |
for (j=0; j<3; j++) { /* 判斷式方法同上 */ | |
printf("請輸入arr[%d][%d]的值: ", i, j); | |
scanf("%lf", &arr2[i][j]); /* 輸入時%lf是輸入double用的,而且後面的參照必須要加上& */ | |
total += arr2[i][j]; | |
} | |
printf("總和為%.2f\n", total); | |
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 - 308 */ | |
#include <stdio.h> | |
#include <stdlib.h> | |
double sum(double a[], int n); | |
int main () | |
{ | |
double arr[5]; | |
int i; | |
double total=0; /* 改為double變數 */ | |
for(i=0; i<5; i++) { | |
printf("請輸入陣列arr[%d]元素值: ", i); | |
scanf("%lf", &arr[i]); /* 加上&參照 */ | |
} | |
for(i=0; i<5; i++) | |
printf("arr[%d]=%.2f\n", i, arr[i]); | |
total=sum(arr,i); | |
printf("此陣列的總和為%.2f\n", total); | |
system("PAUSE"); | |
return 0; | |
} | |
double sum(double a[], int n) /* 修改傳入參數的型態double */ | |
{ | |
int k; | |
double tot = 0; /* 設定加總初始值0 */ | |
for(k = 0; k < n; k++) | |
tot += a[k]; | |
return tot; | |
} |
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 - 309 */ | |
#include <stdio.h> | |
#include <stdlib.h> | |
int max(int a[], int n); | |
int main () | |
{ | |
int arr[5], i, maximum; | |
for(i = 0; i < 5; i++) { | |
printf("請輸入陣列arr[%d]元素值: ", i); | |
scanf("%d", &arr[i]); /* 建議把這邊的換行符號刪除 */ | |
} | |
for(i = 0; i < 5; i++) | |
printf("arr[%d]=%d\n", i, arr[i]); | |
maximum = max(arr, 5); | |
printf("此陣列最大值為%d\n", maximum); | |
system("PAUSE"); | |
return 0; | |
} | |
int max(int a[], int n) | |
{ | |
int k, M; | |
M = a[0]; | |
for(k=1; k < n; k++) { | |
if(a[k] > M) /* 如果大於臨時變數M,則替代M的值 */ | |
M=a[k]; | |
} | |
return M; | |
} |
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 - 310 */ | |
#include <stdio.h> | |
#include <stdlib.h> | |
int main () | |
{ | |
int arr[5]={100, 200, 300, 400, 500}; | |
int i; | |
printf("\n陣列元素的值如下:\n"); | |
for (i=0; i<5; i++) /* i的起始值為0 */ | |
printf("arr[%d]=%d\n", i, arr[i]); | |
/* %p是用來顯示位址的參照 */ | |
printf("\n陣列元素的位址如下:\n"); | |
for (i=0; i<5; i++) /* i的起始值為0 */ | |
printf("&arr[%d]=%p\n", i, &arr[i]); | |
/* 這邊是將每一個元素位址顯示出來 */ | |
printf("\n陣列元素的位址如下:\n"); | |
for (i=0; i<5; i++) /* i的起始值為0 */ | |
printf("&arr[%d]=%p\n", i, arr+i); | |
system("PAUSE"); | |
return 0; | |
} |
題目310解說:記憶體內的排列如下

TQC+ C 試題總覽:LINK
Github 備份:LINK
本篇教學的程式碼皆由筆者編輯,歡迎轉貼本教學,但請全文轉貼,謝啦~
沒有留言:
張貼留言