首页 > 其他分享 >实验7

实验7

时间:2023-06-08 20:25:48浏览次数:35  
标签:10 20 int char STU 实验 include

 task4.c

 1 #include <stdio.h>
 2 #include <stdlib.h>
 3 #include <string.h>
 4 #define N 100 
 5 int main()
 6 {
 7     char s;
 8     int i=0;
 9     FILE *fp;
10     
11     fp=fopen("data4.txt","r");
12     
13     if (fp==NULL){
14         printf("fail to open");
15         return 1;
16         
17     }
18     while ((s=fgetc(fp))!=EOF)
19     {
20         if (s!=' '&&s!='\t'&&s!='\n')
21         {
22             i++;
23         }
24         
25     }
26  
27  printf("data4.txt中包含字符数(不计空白符):%d",i);
28  return 0;}

 

 

task5.c

  1 #include <stdio.h>
  2 #include <string.h>
  3 #include <stdlib.h>
  4 
  5 #define N 10
  6 
  7 typedef struct {
  8     long int id;
  9     char name[20];
 10     float objective;    // 客观题得分
 11     float subjective;   // 操作题得分
 12     float sum;
 13     char level[10];
 14 } STU;
 15 
 16 // 函数声明
 17 void input(STU s[], int n);
 18 void output(STU s[], int n);
 19 void process(STU s[], int n);
 20 
 21 int main() {
 22     STU stu[N];
 23 
 24     printf("从文件读入%d个考生信息: 准考证号,姓名,客观题得分(<=40),操作题得分(<=60)\n", N);
 25     input(stu, N);
 26 
 27     printf("\n对考生信息进行处理: 计算总分,确定等级\n");
 28     process(stu, N);
 29 
 30     printf("\n打印考生完整信息, 并保存到文件中");
 31     output(stu, N);
 32 
 33     return 0;
 34 }
 35 
 36 // 从文本文件examinee.txt读入考生信息:准考证号,姓名,客观题得分,操作题得分
 37 void input(STU s[], int n) {
 38     int i;
 39     FILE *fin;
 40 
 41     fin = fopen("examinee.txt", "r");
 42     if (fin == NULL) {
 43         printf("fail to open file\n");
 44         exit(0);
 45     }
 46 
 47     while (!feof(fin)) {
 48         for (i = 0; i < n; i++)
 49             fscanf(fin, "%ld %s %f %f", &s[i].id, s[i].name, &s[i].objective, &s[i].subjective);
 50     }
 51 
 52     fclose(fin);
 53 }
 54 
 55 // 输出考生完整信息: 准考证号,姓名,客观题得分,操作题得分,总分,等级
 56 // 不仅输出到屏幕上,还写到文本文件result.txt中
 57 void output(STU s[], int n) {
 58     FILE *fout;
 59     int i;
 60     
 61     // 输出到屏幕
 62     printf("\n");
 63     printf("准考证号\t姓名\t客观题得分\t操作题得分\t总分\t\t等级\n");
 64     for (i = 0; i < n; i++)
 65         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);
 66     
 67     // 保存到文件 
 68     fout = fopen("result.txt", "w");
 69     if (!fout) {
 70         printf("fail to open or create result.txt\n");
 71         exit(0);
 72     }
 73     
 74     fprintf(fout, "准考证号\t\t姓名\t客观题得分\t操作题得分\t总分\t\t等级\n");
 75 
 76     for (i = 0; i < n; i++)
 77         fprintf(fout, "%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);
 78 
 79     fclose(fout);
 80 }
 81 
 82 // 对考生信息进行处理:计算总分,排序,确定等级
 83 void process(STU s[], int n) {
 84    int i,j;
 85    STU temp;
 86    for (i=0;i<n;i++)
 87    {
 88        s[i].sum=s[i].objective+s[i].subjective;
 89        
 90    }
 91     for(i=0;i<n-1;i++)
 92     for (j=0;j<n-1;j++){
 93         if (s[j].sum<s[j+1].sum) {
 94             temp=s[j];
 95             s[j]=s[j+1];
 96             s[j+1]=temp;
 97         }
 98     }
 99 
100 for (i=0;i<n;i++)
101 {
102     if (i+1<=n*0.1) strcpy(s[i].level,"优秀");
103     else if (i+1>0.1*n&&i+1<=0.5*n) strcpy (s[i].level,"合格") ;
104      else strcpy (s[i].level,"不合格");
105      
106     }
107 }

 

task6.c

 

 1 #include <stdio.h>
 2 #include <stdlib.h>
 3 #include <time.h>
 4 #define N 80
 5 #define M 5
 6 typedef struct{
 7         long int id;
 8         char name[20];
 9         char classic[50];
10     }STU;
11     
12 int main()
13 {    STU s[N],r[M];
14     int i=0,n; 
15     FILE *fp1,*fp2;
16     if ((fp1=fopen("list.txt","r"))==NULL){
17         printf("fail to open file\n");
18         return 1;
19     }
20     while (!feof(fp1)){
21         fscanf(fp1,"%d%s%s",&s[i].id,s[i].name,s[i].classic);
22     i++;}
23 srand(time(0));
24 for (i=0;i<M;i++){
25     n=rand ()%N;
26     
27     r[i]=s[n];    
28 }
29 fclose(fp1);
30 if ((fp2=fopen("lucky.txt","w"))==NULL){
31     printf("fail to open the fail_2\n");
32     return 1;}
33 
34 for(i=0;i<M;i++){
35     printf("%ld\t%s\t%s\n",r[i].id,r[i].name,r[i].classic);
36     fprintf(fp2,"%ld\t%s\t%s\n",r[i].id,r[i].name,r[i].classic);}
37 
38 fclose(fp2);
39 
40 return 0;
41 }

 

标签:10,20,int,char,STU,实验,include
From: https://www.cnblogs.com/shaobky/p/17467354.html

相关文章

  • 五月二十五日实验
    实验一略实验二略实验三略实验四#include<stdio.h>#include<string.h>#defineN100typedefstruct{charnum[10];//学号ints1;//期末成绩ints2;//平时成绩doublesum;//总评charlevel[10];//等级}STU;intfun(STUa[],intn,STUh[]);//......
  • 实验6
    task1_1源代码:1fromturtleimport*23defmove(x,y):4'''画笔移动到坐标(x,y)处'''5penup()6goto(x,y)7pendown()89defdraw(n,size=100):10'''绘制边长为size的正n边形'''......
  • 实验七
    任务三#include<stdio.h>#include<stdlib.h>intmain(){FILE*fp;charch;intchar_count=0;fp=fopen("data4.txt","r");if(fp==NULL){printf("Couldnotopenfiledata4.txt.\n&q......
  • 实验7
    任务4源代码#include<stdio.h>#include<string.h>intmain(){FILE*fp;inti,num=0;chars[7][80];fp=fopen("data4.txt","r");if(fp==NULL){printf("failtoopenfp\n");......
  • 实验6 turtle绘图与python库应用编程体验
    task1-1fromturtleimport*defmove(x,y):penup()goto(x,y)pendown()defdraw(n,size=100):foriinrange(n):fd(size)left(360/n)defmain():pensize(2)pencolor('red')move(-200,0)draw......
  • 实验七
    实验任务4程序代码:#include<stdio.h>#include<string.h>#include<stdlib.h>intmain(){FILE*fp;charstr[100];inti,k=0,flag;while((fp=fopen("data4.txt","r"))==NULL)printf("failtoopen&quo......
  • 实验7
    实验任务4实验代码#include<stdio.h>#include<stdlib.h>#include<string.h>constintN=5,M=100;intmain(){charch[M];intn=0; FILE*fp; fp=fopen("data4.txt","r"); while(!feof(fp))......
  • 实验7
    #11#12classAccount:3def__init__(self,name,account_number,initial_amount=10):4self._name=name5self._card_no=account_number6self._balance=initial_amount78defdeposit(self,amount):9'......
  • 实验七
    classAccount:def__init__(self,name,account_number,initial_amount=10):self._name=nameself._card_no=account_numberself._balance=initial_amountdefdeposit(self,account):self._balance+=accountdefwithdraw(s......
  • 实验六
    task11fromturtleimport*2defmove(x,y):3penup()4goto(x,y)5pendown()6defdraw(n,size=100):7foriinrange(n):8fd(size)9left(360/n)10defmain():11pensize(2)12pencolor('red......