任务4
源代码
#include<stdio.h> #define N 30 int main(){ char s[N]; int i,n; int line=0,word=0; FILE *fin; fin = fopen("data4.txt","r"); if(!fin){ printf("fail to open the file"); return 0; } while(!feof(fin)){ fgets(s,N,fin); line++; i = 0; while(s[i]!='\0'){ if(s[i]!=' '&&s[i]!='\n') word++; i++; } } fclose(fin); printf("data4.txt统计结果:\n"); printf("行数:%d\n",line); printf("字符数(不计空白符):%d\n",word); return 0; }
运行结果
任务5
源代码
#include <stdio.h> #include <string.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); 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("examine.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("data5.txt", "w"); if(fp == NULL){ printf("fail to open file to write"); return; } for(i = 0;i < n; i++) fprintf(fp, "%-20ld%-8s%-15.2f%-15.2f%-15.2f%-10s\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, j = 0, cnt = 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[j++] = st[i]; cnt++; } else { strcpy(st[i].result, "不通过"); } } return cnt; }
运行结果
任务6
必做
源代码
#include<stdio.h> #include<time.h> #include<stdlib.h> #include<string.h> #define N 1000 typedef struct student{ int id; char name[N]; char cla[N]; }STU; void read (STU st[]); void process (STU st[]); int main(){ STU s[80]; read(s); process(s); return 0; } void read (STU st[]){ int i; FILE *fp; fp = fopen("list.txt","r"); if(fp == NULL){ printf("fail to open the file"); return; } while(!feof(fp)){ for (i = 0;i < 80; i++){ fscanf(fp,"%ld %s %s",&st[i].id,st[i].name,st[i].cla); } } fclose(fp); } void process (STU st[]){ srand(time(NULL)); int i = 0,temp,count = 0; int sel[5]; char time[1000]; while(count < 5){ int sign = 1; temp = rand() % 80; for(i = 0;i < count; i++){ if(sel[i] == temp) count--; } sel[i] = temp; count++; } printf("-------------随机抽点名单-------------\n"); for(i = 0;i < 5; i++){ printf("%d\t%s\t\t%s\t\n",st[sel[i]].id,st[sel[i]].name,st[sel[i]].cla); } printf("--------------保存到文件---------------\n"); printf("输入文件名:"); scanf("%s",time); FILE *fp; fp = fopen(time,"w"); if(fp == NULL){ printf("fail to open the file"); return; } for(i = 0;i < 5; i++){ fprintf(fp,"%d\t\t%s\t\t%s\t\n",st[sel[i]].id,st[sel[i]].name,st[sel[i]].cla); } fclose(fp); printf("保存成功!"); }
运行结果
选做
源代码
#include<stdio.h> #include<time.h> #include<stdlib.h> #include<string.h> #define N 1000 typedef struct student{ int id; char name[N]; char cla[N]; }STU; void read (STU st[]); void process (STU st[]); int main(){ STU s[80]; read(s); process(s); return 0; } void read (STU st[]){ int i; FILE *fp; fp = fopen("list.txt","r"); if(fp == NULL){ printf("fail to open the file"); return; } while(!feof(fp)){ for (i = 0;i < 80; i++){ fscanf(fp,"%ld %s %s",&st[i].id,st[i].name,st[i].cla); } } fclose(fp); } void process (STU st[]){ srand(time(NULL)); int i,j,t; int temp,count = 0; int sel[5]; while(count < 5){ int sign = 1; temp = rand() % 80; for(i = 0;i < count; i++){ if(sel[i] == temp) count--; } sel[i] = temp; count++; } for(i = 0; i < 5; i++) for(j = 0; j < 4-i; j++){ if(sel[i] > sel[i+1]){ t = sel[i]; sel[i] = sel[i+1]; sel[i+1] = t; } } printf("-------------随机抽点名单-------------\n"); for(i = 0;i < 5; i++){ printf("%d\t%s\t\t%s\t\n",st[sel[i]].id,st[sel[i]].name,st[sel[i]].cla); } time_t rawtime; struct tm *timein; char name[100]; time(&rawtime); timein = localtime(&rawtime); strftime(name,sizeof(name),"%Y%m%d.txt",timein); FILE *fp; fp = fopen(name,"w"); if(fp == NULL){ printf("fail to open the file"); return; } for(i = 0;i < 5; i++){ fprintf(fp,"%d\t\t%s\t\t%s\t\n",st[sel[i]].id,st[sel[i]].name,st[sel[i]].cla); } fclose(fp); printf("文件保存成功!"); }
运行结果
标签:fp,int,st,STU,实验,printf,sel From: https://www.cnblogs.com/syqsyqsyqsyq/p/18638571