实验七
任务四:
1 #include<stdio.h> 2 #include<stdlib.h> 3 4 void write(); 5 6 void read(int *x,int *y); 7 int main() { 8 int x = 0, y = 0; 9 write(); 10 read( &x, &y); 11 printf("date4.txt统计结果:\n行数:%18d\n字符数(不计空白符):%2d\n", x, y); 12 system("pause"); 13 return 0; 14 } 15 void write() { 16 int i = 0; 17 char* s[] = { 18 "0123456789-0123456789", 19 "nuist2024", 20 "cosmos galaxy" }; 21 FILE* fp; 22 fp = fopen("date4.txt", "w"); 23 if (!fp) { 24 printf("fail to open\n"); 25 return; 26 } 27 for (i = 0; i < 3; i++) { 28 fputs(s[i], fp); 29 fputs("\n",fp); 30 } 31 fclose(fp); 32 } 33 34 void read(int *x,int *y) { 35 char s; 36 int a=0,b=0,c=0; 37 FILE* fp; 38 fp = fopen("date4.txt", "r"); 39 if (!fp) { 40 printf("fail to open\n"); 41 return; 42 } 43 while (!feof(fp)) { 44 s = fgetc(fp); 45 if (s == '\n') { 46 a++; 47 } 48 if(s==EOF){ 49 break; 50 } 51 else if(s==' '){ 52 c++; 53 } 54 else{ 55 b++; 56 } 57 } 58 *x = a; 59 *y = b-a ; 60 fclose(fp); 61 62 }
任务五:
#include <stdio.h> #include <string.h> #include<stdlib.h> #define N 10 typedef struct { long id; char name[20]; float objective; float subjective; float sum; char result[10]; } STU; void read(STU st[], int n); void write(STU st[], int n); void output(STU st[], int n); int process(STU st[], int n, STU st_pass[]); int main() { STU stu[N], stu_pass[N]; int cnt; double pass_rate; printf("从文件读入%d个考生信息...\n", N); read(stu, N); printf("\n对考生成绩进行统计...\n"); cnt = process(stu, N, stu_pass); printf("\n通过考试的名单:\n"); output(stu, N); // 输出所有考生完整信息到屏幕 write(stu_pass, cnt); pass_rate = 1.0 * cnt / N; printf("\n本次等级考试通过率: %.2f%%\n", pass_rate * 100); system("pause"); return 0; } void output(STU st[], int n) { int i; printf("准考证号\t姓名\t客观题得分\t操作题得分\t总分\t\t结果\n"); for (i = 0; i < n; i++) printf("%ld\t\t%s\t%.2f\t\t%.2f\t\t%.2f\t\t%s\n", st[i].id, st[i].name, st[i].objective, st[i].subjective, st[i].sum, st[i].result); } void read(STU st[], int n) { int i; FILE* fin; fin = fopen("examinee.txt", "r"); if (!fin) { printf("fail to open file\n"); return; } while (!feof(fin)) { for (i = 0; i < n; i++) fscanf(fin, "%ld %s %f %f", &st[i].id, st[i].name, &st[i].objective, &st[i].subjective); } fclose(fin); } void write(STU s[], int n) { int i; FILE* fp; fp = fopen("list_pass.txt", "w"); for (i = 0; i < n; i++) { fprintf(fp, "%6ld %6s %6.1f %6.1f %6.1f %6s \n",s[i].id,s[i].name,s[i].objective,s[i].subjective,s[i].sum,s[i].result); } fclose(fp); } int process(STU st[], int n, STU st_pass[]) { int i = 0,count = 0; for (i = 0; i < n; i++) { st[i].sum = st[i].objective + st[i].subjective; if (st[i].sum >= 60) { strcpy(st[i].result, "通过"); st_pass[count] = st[i]; count++; } else { strcpy(st[i].result, "未通过"); } } return count; }
任务六:
1 #include<stdio.h> 2 #include<stdlib.h> 3 #include<time.h> 4 typedef struct { 5 long id; 6 char name[10]; 7 char bj[20]; 8 }ST; 9 void read(ST s[]) { 10 int i = 0; 11 FILE* fp; 12 fp = fopen("list.txt", "r"); 13 if (!fp) { 14 printf("fail to open\n"); 15 return; 16 } 17 for (i = 0; i < 80; i++) { 18 fscanf(fp,"%ld %s %s", &s[i].id, s[i].name, s[i].bj);20 } 21 } 22 23 void chose(ST s[], ST t[]) { 24 int a,c[5],h=0,i=0,j=0,k=0; 25 srand(time(NULL)); 26 a = rand() % 80; 27 c[0] = a; 28 for(h=0;h<4;h++){ 29 c[h+1] = choose(c[h]); 30 for(i=0;i<h+2;i++){ 31 k=0; 32 for(j=0;j<h+2;j++){ 33 if(c[i]==c[j]){ 34 k++; 35 } 36 } 37 if(k!=1){ 38 h--; 39 break; 40 } 41 } 42 } 43 for(i=0;i<4;i++){ 44 for(j=0;j<4;j++){ 45 if(c[j]>c[j+1]){ 46 k = c[j]; 47 c[j] = c[j+1]; 48 c[j+1]= k; 49 } 50 } 51 } 52 for(i=0;i<5;i++){ 53 t[i] = s[c[i]]; 54 } 55 } 56 int choose(int a){ 57 int b; 58 srand(a); 59 b = rand() % 80; 60 return b; 61 } 62 void put(ST t[]) { 63 int i=0; 64 char s[20]; 65 FILE *fp; 66 printf("----------随机抽点人名单----------\n"); 67 for(i=0;i<5;i++){ 68 printf("%-15ld %-15s %-15s\n", t[i].id, t[i].name, t[i].bj); 69 } 70 printf("----------保存到文件----------\n"); 71 printf("输入文件名:"); 72 scanf("%s",s); 73 fp = fopen(s,"w"); 74 for(i=0;i<5;i++){ 75 fprintf(fp,"%-10ld %-10s %-20s\n",t[i].id,t[i].name,t[i].bj); 76 } 77 fclose(fp); 78 } 79 int main() { 80 ST s[80],t[5]; 81 long a; 82 read(s); 83 chose(s, t); 84 put(t); 85 system("pause"); 86 return 0; 87 }
选做:
#include<stdio.h> #include<stdlib.h> #include<time.h> typedef struct { long id; char name[10]; char bj[20]; }ST; void read(ST s[]) { int i = 0; FILE* fp; fp = fopen("list.txt", "r"); if (!fp) { printf("fail to open\n"); return; } for (i = 0; i < 80; i++) { fscanf(fp,"%ld %s %s", &s[i].id, s[i].name, s[i].bj); } } void chose(ST s[], ST t[]) { int a,c[5],h=0,i=0,j=0,k=0; srand(time(NULL)); a = rand() % 80; c[0] = a; for(h=0;h<4;h++){ c[h+1] = choose(c[h]); for(i=0;i<h+2;i++){ k=0; for(j=0;j<h+2;j++){ if(c[i]==c[j]){ k++; } } if(k!=1){ h--; break; } } } for(i=0;i<4;i++){ for(j=0;j<4;j++){ if(c[j]>c[j+1]){ k = c[j]; c[j] = c[j+1]; c[j+1]= k; } } } for(i=0;i<5;i++){ t[i] = s[c[i]]; } } int choose(int a){ int b; srand(a); b = rand() % 80; return b; } void settime(){ FILE *fp; const time_t t=time(NULL); struct tm* info=NULL; fp = fopen("time.txt","w"); info = gmtime(&t); fprintf(fp,"%d%02d%02d.txt", info->tm_year + 1900,info->tm_mon + 1,info->tm_mday); fclose(fp); } void put(ST t[]) { int i=0; char s[20]; FILE *fp,*tm; settime(); tm = fopen("time.txt","r"); fscanf(tm,"%s",s); printf("----------%.8s名单----------\n",s); for(i=0;i<5;i++){ printf("%-15ld %-15s %-15s\n", t[i].id, t[i].name, t[i].bj); } fp = fopen(s,"w"); for(i=0;i<5;i++){ fprintf(fp,"%-10ld %-10s %-20s\n",t[i].id,t[i].name,t[i].bj); } printf("文件保存成功!"); fclose(fp); fclose(tm); } int main() { ST s[80],t[5]; long a; read(s); chose(s, t); put(t); system("pause"); return 0; }