8.編寫一個程序,以一個字符和任意文件名作為命令行參數。如果字符后面沒有參數,該程序讀取標
淮輸入;否則,程序依次打開每個文件并報告每個文件中該字符出現的次數。文件名和字符本身也要一同報告。程序應包含錯誤檢查,以確定參數數量是否正確和是否能打開文件。如果無法打開文件,程序應報告這一情況,然后繼續處理下一個文件。
int main(int argc, char* argv[])
{system("chcp 65001");if (argc < 2){fprintf(stderr, "用法: %s <文件1> [文件2] [文件3] ...\n", argv[0]);}else{if (argc == 2){char buf[1024];printf("請輸入字符串:\n");scanf_s("%s", buf, 1024);int count = 0;for (int i = 0;i < strlen(buf);i++){if(buf[i] == argv[1][0])count++;}printf("字符%c出現的次數為%d\n", argv[1][0], count);}else{for (int i = 2;i < argc;i++){FILE* fp = NULL;fopen_s(&fp, argv[i], "rb");if(fp == NULL)printf("文件%s打開失敗\n", argv[i]);else{int count = 0;char buf[1024];while (fgets(buf, sizeof(buf), fp) != NULL){for (int i = 0;i < strlen(buf);i++){if (buf[i] == argv[1][0])count++;}}printf("字符%c再文件%s中出現的次數為%d\n", argv[1][0], argv[i], count);fclose(fp);}}}}return 0;
}
9.修改程序清單 13.3 中的程序,從1開始,根據加入列表的順序為每個單詞編號。當程序下次運行時,確保新的單詞編號接著上次的編號開始。
#define MAX 41
int main(void)
{FILE *fp;char words[MAX];int lineNo = 0;fopen_s(&fp, "wordy", "r");if (fp){while (fgets(words, MAX, fp) != NULL){ lineNo++;}fclose(fp);}fopen_s(&fp, "wordy", "a+");if (fp == NULL)fprintf(stdout, "Can't open \"wordy\" file.\n");else{puts("Enter words to add to the file; press the #");puts("Key at the beginning of a line to terminate.");while ((fscanf_s(stdin, "%40s", words, 41) == 1) && (words[0] != '#'))fprintf(fp, "%d %s\n", ++lineNo, words);rewind(fp);while (fscanf_s(fp, "%d %s", &lineNo, words, 41) == 2)printf("%d %s\n", lineNo, words);puts("Done.");fclose(fp);}
}
10.編寫一個程序打開一個文本文件,通過交互方式獲得文件名。通過一個循環,提示用戶輸入一個文件位置。然后該程序打印從該位置開始到下一個換行符之前的內容。用戶輸入負數或非數值字符可以結束輸入循環。
int main()
{system("chcp 65001");puts("請輸入文件名:");char filename[256];FILE *fp;scanf_s("%s", filename, 256);fopen_s(&fp, filename, "r");if (fp == NULL){puts("無法打開文件!");}else{puts("請輸入要打印的位置(負數或非數字退出):");int pos;while (scanf_s("%d", &pos) == 1 && pos >= 0){fseek(fp, pos, SEEK_SET);puts(fgets(filename, 256, fp));puts("請輸入要打印的位置(負數或非數字退出):");}}fclose(fp);return 0;
}
11.編寫一個程序,接受兩個命令行參數。第1個參數是一個字符串,第2個參數是一個文件名。然后該程序查找該文件,打印文件中包含該字符串的所有行。因為該任務是面向行而不是面向字符的,所以要使用fgets()而不是getc()。使用標準C庫函數strstr()(11.5.7節簡要介紹過)在每一行中查找指定字符串。假設文件中的所有行都不超過255個字符。
int main(int argc, char* argv[])
{system("chcp 65001");if (argc < 2){fprintf(stderr, "用法: %s <字符串> <文件名>\n", argv[0]);}else{FILE *fp;fopen_s(&fp, argv[2], "rb");if (fp){ char buf[1024];while (fgets(buf, sizeof(buf), fp)){if (strstr(buf, argv[1])){printf("%s\n", buf);}}fclose(fp);}elsefprintf(stderr, "無法打開文件 %s\n", argv[2]);}return 0;
}
12.創建一個文本文件,內含20行,每行30個整數。這些整數都在0~9之間,用空格分開。該文件是用數字表示一張圖片,0~9表示逐漸增加的灰度。編寫一個程序,把文件中的內容讀入一個20X30的int數組中。一種把這些數字轉換為圖片的粗略方法是:該程序使用數組中的值初始化一個20X31的字符數組,用值0對應空格字符,1對應點字符,以此類推。數字越大表示字得所占的空間越大。例如,用#表示9。每行的最后一個字符(第31個)是空字符,這樣該數組包含了20個字符串。最后,程序顯示最終的圖片(即,打印所有的字符串),并將給果存儲在文本文件中。例如,下面是開始的數據:
0 0 9 0 0 0 0 0 0 0 0 0 5 8 9 9 8 5 2 0 0 0 0 0 0 0 0 0 0 0
0 0 0 0 9 0 0 0 0 0 0 0 5 8 9 9 8 5 5 2 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0 0 0 0 0 0 0 0
0 0 0 0 0 0 0 0 0 0 0 0 5 8 9 9 8 5 0 0 0 0 0 0 0 0 0 0 0 0
根據以上描述選擇特定的輸出字符,最終輸入如下:
# ? ? ? ? *%##%*'
# ? ? ? *%##%**'
*%.#%*~*'
# ? ? ? *%##%* ~*'
# ? ? ? ? *%##%* ?~*'
*%.#%* ? ~*'
*%.#%* ? ?~*'
*************%##%*************
%%%%%%%%%%%%*%##%*%%%%%%%%%%%%
#### ################:########
%%%%%%%%%%%%*%##%*%%%%%%%%%%%%
*************%##%*************
*%##%*
*%##%* ? ?==
'' ? ? ?*%##%* ?*= ?=*
:: ? ? ?*%##%* *=....=*
~~ ? ? ?*%##%* ?*= ?=*
** ? ? ?*%##%* ? ?==
*%##%*
*%##%*
int main(int argc, char* argv[])
{const char imgChar[] = { ' ', '.', '\'', ':', '~', '*', '=', '-', '%', '#'};char arr[20][31] = { 0 };FILE* fp;fopen_s(&fp, "img.txt", "rb");if(fp == NULL)printf("open file error!");else{char ch;int row = 0;int col = 0;while ((ch = getc(fp)) != EOF){if (ch >= '0' && ch <= '9'){arr[row][col++] = ch;}else if (ch == '\n'){row++;col = 0;}}fclose(fp);fopen_s(&fp, "imgout.txt", "wb+");for (int i = 0; i < 20; i++){for (int j = 0; j < 30; j++){arr[i][j] = imgChar[arr[i][j] - '0'];printf("%c", arr[i][j]);if (fp){fputc(arr[i][j], fp);}}printf("\n");if(fp){fputc('\n', fp);}}if(fp)fclose(fp);}return 0;
}
13.用變長數組(VLA)代替標準數組,完成編程練習12。
int main(int argc, char* argv[])
{const char imgChar[] = { ' ', '.', '\'', ':', '~', '*', '=', '-', '%', '#'};char **arr;FILE* fp;fopen_s(&fp, "img.txt", "rb");if(fp == NULL)printf("open file error!");else{int total_rows = 0;int max_cols = 0;char buf[1024];while (fgets(buf, 1024, fp)){total_rows++;int cols = strlen(buf) / 2;if (cols > max_cols)max_cols = cols;}rewind(fp);arr = (char**)malloc(total_rows * sizeof(char*));for (int i = 0; i < total_rows; i++){arr[i] = (char*)malloc(max_cols * sizeof(char));}char ch;int row = 0;int col = 0;while ((ch = getc(fp)) != EOF){if (ch >= '0' && ch <= '9'){arr[row][col++] = ch;}else if (ch == '\n'){row++;col = 0;}}fclose(fp);fopen_s(&fp, "imgout.txt", "wb+");for (int i = 0; i < total_rows; i++){for (int j = 0; j < max_cols; j++){arr[i][j] = imgChar[arr[i][j] - '0'];printf("%c", arr[i][j]);if (fp){fputc(arr[i][j], fp);}}printf("\n");if(fp){fputc('\n', fp);}}for (int i = 0; i < total_rows; i++){free(arr[i]);}free(arr);if(fp)fclose(fp);}return 0;
}
14.數字圖像,尤其是從字宙飛船發回的數字圖像,可能會包含一些失真。為編程練習12添加消除失真的函數。該函數把每個值與它上下左右相鄰的值作比較,如果該值與其周圍相鄰值的差都大于1,則用所有相鄰值的平均值(四舍五入為整數)代替該值。注意,與邊界上的點相鄰的點少于個,所以做特殊處理。
void remove_distortion(char** array, int rows, int cols) {// 創建臨時數組保存原始值int** temp = (int**)malloc(rows * sizeof(int*));for (int i = 0; i < rows; i++) {temp[i] = (int*)malloc(cols * sizeof(int));for (int j = 0; j < cols; j++) {temp[i][j] = array[i][j];}}// 遍歷所有點(包括邊界)for (int i = 0; i < rows; i++) {for (int j = 0; j < cols; j++) {int current = temp[i][j];int neighbor_count = 0; // 實際相鄰點數量int neighbor_sum = 0; // 相鄰點值總和int all_diff_gt1 = 1; // 假設所有差值>1// 檢查上鄰居if (i > 0) {int up = temp[i - 1][j];neighbor_sum += up;neighbor_count++;if (abs(current - up) <= 1) all_diff_gt1 = 0;}// 檢查下鄰居if (i < rows - 1) {int down = temp[i + 1][j];neighbor_sum += down;neighbor_count++;if (abs(current - down) <= 1) all_diff_gt1 = 0;}// 檢查左鄰居if (j > 0) {int left = temp[i][j - 1];neighbor_sum += left;neighbor_count++;if (abs(current - left) <= 1) all_diff_gt1 = 0;}// 檢查右鄰居if (j < cols - 1) {int right = temp[i][j + 1];neighbor_sum += right;neighbor_count++;if (abs(current - right) <= 1) all_diff_gt1 = 0;}// 如果存在相鄰點且所有差值>1,則替換當前值if (neighbor_count > 0 && all_diff_gt1) {// 計算平均值(四舍五入)double avg = (double)neighbor_sum / neighbor_count;array[i][j] = (int)round(avg);}}}// 釋放臨時數組內存for (int i = 0; i < rows; i++) {free(temp[i]);}free(temp);
}
int main(int argc, char* argv[])
{const char imgChar[] = { ' ', '.', '\'', ':', '~', '*', '=', '-', '%', '#'};char **arr;FILE* fp;fopen_s(&fp, "img.txt", "rb");if(fp == NULL)printf("open file error!");else{int total_rows = 0;int max_cols = 0;char buf[1024];while (fgets(buf, 1024, fp)){total_rows++;int cols = strlen(buf) / 2;if (cols > max_cols)max_cols = cols;}rewind(fp);arr = (char**)malloc(total_rows * sizeof(char*));for (int i = 0; i < total_rows; i++){arr[i] = (char*)malloc(max_cols * sizeof(char));}char ch;int row = 0;int col = 0;while ((ch = getc(fp)) != EOF){if (ch >= '0' && ch <= '9'){arr[row][col++] = ch;}else if (ch == '\n'){row++;col = 0;}}fclose(fp);remove_distortion(arr, total_rows, max_cols);fopen_s(&fp, "imgout.txt", "wb+");for (int i = 0; i < total_rows; i++){for (int j = 0; j < max_cols; j++){arr[i][j] = imgChar[arr[i][j] - '0'];printf("%c", arr[i][j]);if (fp){fputc(arr[i][j], fp);}}printf("\n");if(fp){fputc('\n', fp);}}if(fp)fclose(fp);}return 0;
}