实验任务4
程序代码:
#include <stdio.h> #include <stdlib.h> int main(){ FILE *fp; int i=0; char ch; fp=fopen("D:\\private\\learning\\data\\data4.txt","r"); if(fp==NULL){ printf("fail to open file"); system("pause"); return 1; } ch=fgetc(fp); while(ch!=EOF){ if(ch!=' '&&ch!='\n') i++; ch=fgetc(fp); } printf("data4.txt中共包含字符数(不计空白符):%d",i); system("pause"); return 0; }
运行结果:
实验任务5
程序代码:
#include <stdio.h> #include <string.h> #include <stdlib.h> #define N 10 typedef struct { long int id; char name[20]; float objective; float subjective; float sum; char level[10]; }STU; void input(STU s[], int n); void output(STU s[], int n); void process(STU s[], int n); int main() { STU stu[N]; printf("从文件中读入%d个考生信息:准考证号,姓名,客观题得分(<=40),操作题得分(<=60)\n",N); input(stu, N); printf("\n对考生信息进行处理: 计算总分,确定等级\n"); process(stu, N); printf("\n打印考生完整信息, 并保存到文件中"); output(stu, N); system("pause"); return 0; } void input(STU s[], int n) { int i; FILE *fin; fin = fopen("D:\\private\\learning\\data\\examinee.txt", "r"); if (fin == NULL) { printf("fail to open file\n"); exit(0); } while (!feof(fin)) { for (i = 0; i < n; i++) fscanf(fin, "%ld %s %f %f", &s[i].id, s[i].name, &s[i].objective, &s[i].subjective); } fclose(fin); } void output(STU s[], int n) { FILE *fout; int i; printf("\n"); 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", s[i].id, s[i].name, s[i].objective, s[i].subjective, s[i].sum, s[i].level); fout = fopen("D:\\private\\learning\\data\\result.txt", "w"); if (!fout) { printf("fail to open or create result.txt\n"); exit(0); } fprintf(fout, "准考证号\t\t姓名\t客观题得分\t操作题得分\t总分\t\t等级\n"); for (i = 0; i < n; i++) fprintf(fout, "%ld\t\t%s\t\t%.2f\t\t%.2f\t\t%.2f\t\t%s\n", s[i].id, s[i].name, s[i].objective, s[i].subjective, s[i].sum, s[i].level); fclose(fout); } void process(STU s[], int n) { int i,j,k; float min; STU t; for(i=0;i<N;i++) s[i].sum=s[i].objective+s[i].subjective; for(i=0;i<N;i++){ for(j=0,k=0,min=s[0].sum;j<N-i-1;j++) if(min>s[j].sum){ min=s[j].sum; k=j; } t=s[N-i-1]; s[N-i-1]=s[k]; s[k]=t; } for(i=0;i<N;i++){ if(i==0) strcpy(s[i].level,"优秀"); else{ if(i>0&&i<=4) strcpy(s[i].level,"合格"); else strcpy(s[i].level,"不合格"); } } }
运行结果:
实验任务6
程序代码:
#include <stdio.h> #include <stdlib.h> #include <time.h> int main(){ int i,j,k; typedef struct{ long int num; char name[10]; char clas[30]; }STU; FILE *fp1,*fp2; STU x[80],h[80]; printf("请输入抽取学生人数:"); scanf("%d",&k); fp1=fopen("D:\\private\\learning\\data\\list.txt","r"); if(fp1==NULL){ printf("fail to open file"); system("pause"); return 1; } i=0; while(!feof(fp1)){ fscanf(fp1,"%ld %s %s",&x[i].num,x[i].name,x[i].clas); i++; } srand(time(NULL)); for(i=0;i<k;i++){ j=rand()%80+1; h[i]=x[j]; } fp2=fopen("D:\\private\\learning\\data\\lucky.txt","w"); if(fp2==NULL){ printf("fail to open file"); system("pause"); return 1; } for(i=0;i<k;i++){ fprintf(fp2,"%ld %-20s %-20s\n",h[i].num,h[i].name,h[i].clas); printf("%ld %-20s %-20s\n",h[i].num,h[i].name,h[i].clas); } fclose(fp1); fclose(fp2); system("pause"); return 0; }
运行结果:
标签:ch,int,char,STU,实验,printf,include From: https://www.cnblogs.com/moreless-xu/p/17471379.html