catalogue
Question 1: document content reproduction
Question 2: analyze students' grades
Question 3: count the number of times each letter appears in the text
Question 1: document content reproduction
First, manually create a TXT file in your editor directory and name it input_1.txt, the contents of the file are as follows:
Gently I left,
As I come gently;
I waved gently,
Farewell to the clouds in the West.
The golden willow by the river,
Is the bride in the sunset;
The shadow in the wave light,
Rippling in my heart.
💬 Title Requirements: create {output through code_ 1. Txt and use the file operation function to manually create the input just now_ Copy all contents in 1.txt file to output_1.txt.
💡 Reference answer:
#include <stdio.h> int main() { // Read the file just created FILE* input_1 = fopen("input_1.txt", "r"); if (input_1 == NULL) { perror("fopen"); return 1; } // Create file (copy destination) FILE* output_1 = fopen("output_1.txt", "w"); if (input_1 == NULL) { perror("fopen"); return 1; } // Start operation char ch = 0; while (1) { // copy ch = fgetc(input_1); if (ch == EOF) { break; } // paste fputc(ch, output_1); } // Close file fclose(input_1); fclose(output_1); input_1 = NULL; output_1 = NULL; return 0; }
🚩 Completion result:
Question 2: analyze students' grades
First, manually create a TXT file in your editor directory and name it input_2.txt, the contents of the file are as follows:
Hellen 90 89.2
Amy 100 52
Jack 22.3 64
Gordon 47.2 100
Brian 56 25.3
💬 Title Requirements: create {output through code_ 2.txt, and according to input_ 2. The content in txt generates the content shown in the following figure. The first line is composed of student name, average score and qualified. Calculate their average score and judge whether they are qualified, which is displayed in output_2.txt, it is required to achieve the effect shown in the above figure (indent freely).
If the average score of the student's mid-term and final grades is higher than or equal to the average score of the whole class, it is pass, and if it is lower than the average score of the whole class, it is fail.
Student names are limited to a maximum of 15 characters.
💡 Reference answer:
#include <stdio.h> int main(void) { char name[15] = { 0 }; // Student name (length limit 15) float mid; // Midterm examination results float final; // Final exam results float sum = 0.0; // Summation float avg = 0.0; // average float class_avg = 0.0; // Class average int cnt = 0; // Statistics int res = 0; // Read the file just created FILE* input_2 = fopen("input_2.txt", "r"); if (input_2 == NULL) { perror("fopen"); return 1; } // Create file (target) FILE* output_2 = fopen("output_2.txt", "w"); if (output_2 == NULL) { perror("fopen"); return 1; } // Read student names, midterm grades, and final grades while (1) { res = fscanf(input_2, "%s%f%f", name, &mid, &final); if (res == EOF) { break; } sum += mid + final; cnt += 2; } // Close file fclose(input_2); // Open again input_2 = fopen("input_2.txt", "r"); if (input_2 == NULL) { perror("fopen"); return 1; } // Calculate class average class_avg = sum / cnt; // Write first line title fprintf(output_2, "strudent average grade\n"); // Write data while (1) { res = fscanf(input_2, "%s%f%f", name, &mid, &final); if (res == EOF) { break; } // Calculate personal average avg = (mid + final) / 2; // pass or fail if (avg >= class_avg) { fprintf(output_2, "%-10s %.2f pass\n", name, avg); } else { fprintf(output_2, "%-10s %.2f fail\n", name, avg); } } fprintf(output_2, "Class average: %.2f", class_avg); // Close file fclose(input_2); fclose(output_2); input_2 = NULL; output_2 = NULL; return 0; }
🚩 Completion result:
Question 3: count the number of times each letter appears in the text
First, manually create a TXT file in your editor directory and name it input_3.txt, the contents of the file are as follows:
Overwatch is a multiplayer team-based first-person shooter developed and published by Blizzard Entertainment.
In a time of global crisis, an international task force of heroes banded together to restore peace to a war-torn world.
This organization, known as Overwatch, ended the crisis and helped maintain peace for a generation,
inspiring an era of exploration, innovation, and discovery. After many years, Overwatch's influence waned and it was eventually disbanded.
Now in the wake of its dismantling, conflict is rising once again. Overwatch may be gone... but the world still needs heroes.
Choose A Hero: Overwatch features a wide array of unique Heroes, ranging from a time-jumping adventurer, to an armored,
rocket-hammer-wielding warrior, to a transcendent robot monk. Every hero plays differently,
and mastering their Abilities is the key to unlocking their potential.No two heroes are the same.
💬 Title Requirements: read input_3.txt, count the occurrence of Chinese and English letters A~Z in the whole text for several times, ignore the case and only count the letters, and print them (the format is not required). The output results are as follows:
💡 Reference answer:
① Directly use library functions for case conversion:
#include <stdio.h> #include <ctype.h> int main(void) { int alp[26] = { 0 }; // alphabet // Read the file just created FILE* input_3 = fopen("input_3.txt", "r"); if (input_3 == NULL) { perror("fopen"); return 1; } int ch = 0; while (ch = fgetc(input_3), ch != EOF) { // Case conversion (Title requires ignoring case) if (isupper(ch)) { ch = tolower(ch); } if (islower(ch)) { alp[ch - 'a'] += 1; } } //Print results int i = 0; for (i = 0; i < 26; i++) { printf("[%c]:%d\t", 'A' + i, alp[i]); } // Close file fclose(input_3); input_3 = NULL; return 0; }
② Case conversion with the characteristics of ASCII code:
#include <stdio.h> int main(void) { int alp[26] = { 0 }; // alphabet // Read the file just created FILE* input_3 = fopen("input_3.txt", "r"); if (input_3 == NULL) { perror("fopen"); return 1; } int ch = 0; while (ch = fgetc(input_3), ch != EOF) { // Case conversion (Title requires ignoring case) if ('A' <= ch && ch <= 'Z') { ch -= 'A' - 'a'; } if ('a' <= ch && ch <= 'z') { alp[ch - 'a'] += 1; } } //Print results int i = 0; for (i = 0; i < 26; i++) { printf("[%c]:%d\t", 'A' + i, alp[i]); } // Close file fclose(input_3); input_3 = NULL; return 0; }
reference material:
Microsoft. MSDN(Microsoft Developer Network)[EB/OL]. []. .
Baidu Encyclopedia [EB / OL] []. https://baike.baidu.com/.
📌 Author: Wang Yiyou
📃 Update: December 2021 thirteen
❌ Corrigendum: None
📜 Statement: due to the limited level of the author, it is inevitable that there are errors and inaccuracies in this article. I also want to know these errors. I sincerely hope the readers can criticize and correct them.