重點:整體架構理解、錯誤偵測
練習:找出並修正各種bug
功能:輸入學生資料,儲存至檔案,並計算平均分數
Bug 數量:3(陣列越界、檔案未關閉、平均計算錯誤)
#include <stdio.h>
#include <stdlib.h>
struct Student {
char name[20];
int score;
};
int main() {
int n;
printf("請輸入學生人數:");
scanf("%d", &n);
struct Student list[100];
for (int i = 0; i <= n; i++) { // Bug 1: 應為 i < n
printf("輸入姓名與分數:");
scanf("%s %d", list[i].name, &list[i].score);
}
FILE *fp = fopen("grades.txt", "w");
int total = 0;
for (int i = 0; i < n; i++) {
fprintf(fp, "%s %d\n", list[i].name, list[i].score);
total += list[i].score;
}
float avg = total / n; // Bug 3: 整數除法,應轉型
printf("平均分數:%.2f\n", avg);
// Bug 2: 檔案未關閉, 應為 fclose(fp);
return 0;
}
功能:讀取商品資料,查詢指定商品
Bug 數量:3(未初始化、字串比較錯誤、檔案開啟失敗未處理)
#include <stdio.h>
#include <string.h>
struct Product {
char name[20];
int stock;
};
int main() {
struct Product items[50];
int count = 0;
FILE *fp = fopen("inventory.txt", "r");
// Bug 1: 未檢查 fp 是否為 NULL
while (fscanf(fp, "%s %d", items[count].name, &items[count].stock) == 2) {
count++;
}
fclose(fp);
char query[20];
printf("請輸入要查詢的商品名稱:");
scanf("%s", query);
for (int i = 0; i < count; i++) {
if (items[i].name == query) { // Bug 2: 字串比較錯誤
printf("庫存:%d\n", items[i].stock);
}
}
return 0;
}
功能:輸入分數並由高至低排序
Bug 數量:3(指標錯誤、交換邏輯錯誤、未釋放記憶體)
#include <stdio.h>
#include <stdlib.h>
void sort(int *arr, int n) {
for (int i = 0; i < n - 1; i++) {
for (int j = i + 1; j < n; j++) {
if (arr[i] < arr[j]) {
int *temp = arr[i]; // Bug 1: 應為 int temp = arr[i];
arr[i] = arr[j];
arr[j] = temp; // Bug 2: temp 是指標
}
}
}
}
int main() {
int n;
printf("請輸入分數數量:");
scanf("%d", &n);
int *scores = malloc(n * sizeof(int));
for (int i = 0; i < n; i++) {
scanf("%d", &scores[i]);
}
sort(scores, n);
for (int i = 0; i < n; i++) {
printf("%d ", scores[i]);
}
printf("\n");
// Bug 3: 未釋放記憶體, 應為 free(scores);
return 0;
}
功能:使用 malloc 儲存分數並計算平均
Bug 數量:3(未檢查 malloc、越界存取、未釋放記憶體)
#include <stdio.h>
#include <stdlib.h>
int main() {
int n;
printf("請輸入分數數量:");
scanf("%d", &n);
int *scores = malloc(n * sizeof(int));
// Bug 1: 未檢查 scores 是否為 NULL
for (int i = 0; i <= n; i++) { // Bug 2: 應為 i < n
printf("請輸入第 %d 筆分數:", i + 1);
scanf("%d", &scores[i]);
}
int total = 0;
for (int i = 0; i < n; i++) {
total += scores[i];
}
float avg = (float)total / n;
printf("平均分數:%.2f\n", avg);
// Bug 3: 未釋放記憶體, 應為 free(scores);
return 0;
}
功能:根據工時與時薪計算薪資
Bug 數量:3(條件判斷錯誤、未處理負值、未使用函數)
#include <stdio.h>
float computeSalary(int hours, float rate) {
return hours * rate;
}
int main() {
int hours;
float rate;
printf("請輸入工時與時薪:");
scanf("%d %f", &hours, &rate);
if (hours >= 0 || rate >= 0) { // Bug 1: 應為 && 判斷
float salary = hours * rate; // Bug 2: 未使用函數
printf("薪資為 %.2f 元\n", salary);
} else {
printf("輸入錯誤\n");
}
return 0;
}
功能:儲存多本書籍資料並輸出
Bug 數量:3(陣列未初始化、結構欄位錯誤、資料遺失)
#include <stdio.h>
struct Book {
char title[30];
int pages;
};
int main() {
struct Book books[3];
for (int i = 0; i < 3; i++) {
printf("請輸入書名與頁數:");
scanf("%s %d", books[i].title, &books[i].pages);
}
for (int i = 0; i <= 3; i++) { // Bug 1: 應為 i < 3
printf("書名:%s,頁數:%d\n", books[i].name, books[i].pages); // Bug 2: 欄位錯誤
}
// Bug 3: 無法儲存空格書名,應使用 fgets
return 0;
}
功能:新增會員、查詢會員、儲存檔案
Bug 數量:4(陣列越界、查詢失敗、檔案未關閉、記憶體洩漏)
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
struct Member {
char name[20];
int age;
};
int main() {
struct Member *list = malloc(5 * sizeof(struct Member));
int count = 0;
for (int i = 0; i < 6; i++) { // Bug 1: 應為 i < 5
printf("請輸入姓名與年齡:");
scanf("%s %d", list[i].name, &list[i].age);
count++;
}
char query[20];
printf("查詢姓名:");
scanf("%s", query);
int found = 0;
for (int i = 0; i < count; i++) {
if (strcmp(list[i].name, query) == 0) {
printf("年齡:%d\n", list[i].age);
found = 1;
}
}
if (found = 0) { // Bug 2: 應為 ==,非賦值
printf("查無此人\n");
}
FILE *fp = fopen("members.txt", "w");
for (int i = 0; i < count; i++) {
fprintf(fp, "%s %d\n", list[i].name, list[i].age);
}
// Bug 3: 檔案未關閉, 應為 fclose(fp);
// Bug 4: 未釋放記憶體, 應為 free(list);
return 0;
}