实验七

 1 #define _CRT_SECURE_NO_WARNINGS
 2 #include <stdio.h>
 3 #include <string.h>
 4 #include<stdlib.h>
 5 
 6 int main() {
 7     FILE* fp;
 8 
 9     int i = 0;
10     fp = fopen("D:\\ly\\data4.txt", "r");
11 
12     if (!fp) {
13         perror("文件打开失败");
14         return 3;
15 
16     }
17     int k = 0;
18 
19     char ch;
20     while ((ch = fgetc(fp)) != EOF) {
21         if (ch == '\n')
22             i++;
23         if(ch!=' '&&ch!='\n'&&ch!='\t')
24             k++;
25     }
26     if (k > 0 && ch != '\n')
27         i++;
28     printf("data4.txt统计结果\n");
29     printf("行数:%d\n", i);
30     printf("字符数%d\n", k);
31     fclose(fp);
32     return 0;
33 }

d7a34e046706067586e990ea9778b923

 1 #define _CRT_SECURE_NO_WARNINGS
 2 #include<stdio.h>
 3 #include<string.h>
 4 #define N 10
 5 typedef struct {
 6     long id;
 7     char name[20];
 8     float objective;
 9     float subjective;
10     float sum;
11     char result[10];
12 }STU;
13 void read(STU st[], int n);
14 void write(STU st[], int n);
15 void output(STU st[], int n);
16 int process(STU st[], int n, STU st_pass[]);
17 int main() {
18     STU stu[N], stu_pass[N];
19     int cnt;
20     double pass_rate;
21     printf("从文件读入%d个考生信息...\n",N);
22     read(stu, N);
23     printf("\n对考生成绩进行统计...\n");
24     cnt = process(stu, N, stu_pass);
25     printf("\n通过考试名单:\n");
26     output(stu, N);
27     write(stu, N);
28     pass_rate = 1.0 * cnt / N;
29     printf("\n本次考试通过率:%.2f%%\n", pass_rate * 100);
30     return 0;
31 }
32 void output(STU st[], int n) {
33     int i;
34 
35     printf("准考证号\t姓名\t客观题得分\t操作题得分\t总分\t\t结果\n");
36     for (i = 0; i < n; i++)
37         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);
38 }
39 void read(STU st[], int n) {
40     int i;
41     FILE* fin;
42 
43     fin = fopen("D:\\ly\\examinee.txt", "r");
44     if (!fin) {
45         printf("fail to open file\n");
46         return;
47     }
48 
49     for (i = 0; i < n; i++)
50         fscanf(fin, "%ld %s %f %f", &st[i].id, st[i].name, &st[i].objective, &st[i].subjective);
51 
52     fclose(fin);
53 }
54 int process(STU st[], int n, STU st_pass[]) {
55     int i;
56     int k=0;
57     for (i = 0; i < n; i++) {
58         st[i].sum = st[i].subjective + st[i].objective;
59         if (st[i].sum >= 60) {
60             strcpy(st[i].result, "通过");
61             st_pass[k++] = st[i];
62         }
63         else
64             strcpy(st[i].result, "不通过");
65     }
66     return k;
67 }
68 void write(STU st[], int n) {
69     FILE* fp;
70     int i;
71     fp = fopen("D:\\ly\\list_pass.txt", "w");
72     if (!fp) {
73         perror("list_pass.txt");
74         return 1;
75     }
76     fprintf(fp,"准考证号\t姓名\t客观题得分\t操作题得分\t总分\t\t结果\n");
77     for (i = 0; i < n; i++) {
78         if (st[i].sum >= 60)
79             fprintf(fp, "%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);
80     }
81     fclose(fp);
82 }

4f9127d3a55111786474768afc375992

  1 #include <stdio.h>
  2 #include <stdlib.h>
  3 #include <string.h>
  4 #include <time.h>
  5 
  6 #define MAX_CANDIDATES 80
  7 #define MAX_WINNERS 5
  8 #define MAX_NAME_LEN 50
  9 #define MAX_LINE_LEN 200
 10 
 11 // 候选名单数据(星际联盟2049(1)班)
 12 const char* candidates[MAX_CANDIDATES] = {
 13     "204942001\t抖森\t星际联盟2049(1)班",
 14     "204942002\t卷福\t星际联盟2049(1)班",
 15     "204942003\t毛怪\t星际联盟2049(1)班",
 16     "204942004\t大眼仔\t星际联盟2049(1)班",
 17     "204942005\t辛巴\t星际联盟2049(1)班",
 18     "204942006\t裘花\t星际联盟2049(1)班",
 19     "204942007\t小李子\t星际联盟2049(1)班",
 20     "204942008\t甜茶\t星际联盟2049(1)班",
 21     "204942009\t囧瑟夫\t星际联盟2049(1)班",
 22     "204942010\t霉霉\t星际联盟2049(1)班",
 23     "204942011\t大表姐\t星际联盟2049(1)班",
 24     "204942012\t大白\t星际联盟2049(1)班",
 25     "204942013\t灭霸\t星际联盟2049(1)班",
 26     "204942014\t桃乐丝\t星际联盟2049(1)班",
 27     "204942015\t稻草人\t星际联盟2049(1)班",
 28     "204942016\tMax\t星际联盟2049(1)班",
 29     "204942017\tchappie\t星际联盟2049(1)班",
 30     "204942018\t千寻\t星际联盟2049(1)班",
 31     "204942019\tSean\t星际联盟2049(1)班",
 32     "204942020\t白龙\t星际联盟2049(1)班",
 33     "204942021\t汤婆婆\t星际联盟2049(1)班",
 34     "204942022\t无脸男\t星际联盟2049(1)班",
 35     "204942023\t希达\t星际联盟2049(1)班",
 36     "204942024\t巴鲁\t星际联盟2049(1)班",
 37     "204942025\t朵拉\t星际联盟2049(1)班",
 38     "204942026\t普麻\t星际联盟2049(1)班",
 39     "204942027\t苏菲\t星际联盟2049(1)班",
 40     "204942028\t哈尔\t星际联盟2049(1)班",
 41     "204942029\t小海\t星际联盟2049(1)班",
 42     "204942030\t风间俊\t星际联盟2049(1)班",
 43     "204942031\t麦康纳\t星际联盟2049(1)班",
 44     "204942032\t德尼罗\t星际联盟2049(1)班",
 45     "204942033\t安德鲁\t星际联盟2049(1)班",
 46     "204942034\t西蒙斯\t星际联盟2049(1)班",
 47     "204942035\t伍德\t星际联盟2049(1)班",
 48     "204942036\t朱诺\t星际联盟2049(1)班",
 49     "204942037\t海蒂\t星际联盟2049(1)班",
 50     "204942038\t凯奇\t星际联盟2049(1)班",
 51     "204942039\t阿桑奇\t星际联盟2049(1)班",
 52     "204942040\t斯诺登\t星际联盟2049(1)班",
 53     "204942041\t斯沃茨\t星际联盟2049(1)班",
 54     "204942042\t小丑男\t星际联盟2049(1)班",
 55     "204942043\t维文\t星际联盟2049(1)班",
 56     "204942044\t瓦力\t星际联盟2049(1)班",
 57     "204942045\t伊娃\t星际联盟2049(1)班",
 58     "204942046\t七角怪\t星际联盟2049(1)班",
 59     "204942047\t废柴\t星际联盟2049(1)班",
 60     "204942048\t哆啦A梦\t星际联盟2049(1)班",
 61     "204942049\tSam\t星际联盟2049(1)班",
 62     "204942050\tFrank\t星际联盟2049(1)班",
 63     "204942051\t帕西诺\t星际联盟2049(1)班",
 64     "204942052\t罗宾森\t星际联盟2049(1)班",
 65     "204942053\t卡罗尔\t星际联盟2049(1)班",
 66     "204942054\t达西\t星际联盟2049(1)班",
 67     "204942055\t列维\t星际联盟2049(1)班",
 68     "204942056\t瑞秋\t星际联盟2049(1)班",
 69     "204942057\t吉尔\t星际联盟2049(1)班",
 70     "204942058\t伊内兹\t星际联盟2049(1)班",
 71     "204942059\t卡拉\t星际联盟2049(1)班",
 72     "204942060\t凯西\t星际联盟2049(1)班",
 73     "204942061\t德里安\t星际联盟2049(1)班",
 74     "204942062\t玛丽昂\t星际联盟2049(1)班",
 75     "204942063\t本杰明\t星际联盟2049(1)班",
 76     "204942064\t多姆\t星际联盟2049(1)班",
 77     "204942065\t比尔\t星际联盟2049(1)班",
 78     "204942066\t丽丝\t星际联盟2049(1)班",
 79     "204942067\t蒂安娜\t星际联盟2049(1)班",
 80     "204942068\t阿卢\t星际联盟2049(1)班",
 81     "204942069\t雷婆\t星际联盟2049(1)班",
 82     "204942070\t沙维\t星际联盟2049(1)班",
 83     "204942071\t艾佛特\t星际联盟2049(1)班",
 84     "204942072\t马特\t星际联盟2049(1)班",
 85     "204942073\t达克斯\t星际联盟2049(1)班",
 86     "204942074\t基廷\t星际联盟2049(1)班",
 87     "204942075\tShaw\t星际联盟2049(1)班",
 88     "204942076\tThomas\t星际联盟2049(1)班",
 89     "204942077\t维特\t星际联盟2049(1)班",
 90     "204942078\tJennie\t星际联盟2049(1)班",
 91     "204942079\tTibby\t星际联盟2049(1)班",
 92     "204942080\tVermont\t星际联盟2049(1)班"
 93 };
 94 
 95 int main() {
 96     int flag[MAX_CANDIDATES] = {0};  
 97     int winners[MAX_WINNERS];        
 98     int winner_count = 0;
 99     int random_index;
100     char filename[MAX_NAME_LEN];
101     char datetime_str[64];
102     time_t current_time;
103     struct tm *time_info;
104     srand((unsigned int)time(NULL));
105     
106     while (winner_count < MAX_WINNERS) {
107         random_index = rand() % MAX_CANDIDATES;
108         if (flag[random_index] == 0) {
109             flag[random_index] = 1;
110             winners[winner_count] = random_index;
111             winner_count++;
112         }
113     }
114     printf("------------中奖名单-----------\n");
115     for (int i = 0; i < MAX_WINNERS; i++) {
116         printf("%d. %s\n", i + 1, candidates[winners[i]]);
117     }
118     time(&current_time);
119     time_info = localtime(&current_time);
120     strftime(datetime_str, sizeof(datetime_str), "%Y%m%d_%H%M%S", time_info);
121     printf("------保存到文件------\n");
122     printf("输入文件名:"); 
123     fgets(filename, MAX_NAME_LEN, stdin);
124     filename[strcspn(filename, "\n")] = 0;
125 
126     if (strlen(filename) == 0) {
127         snprintf(filename, MAX_NAME_LEN, "抽奖结果_%s.txt", datetime_str);
128     } else {
129         if (strstr(filename, ".txt") == NULL) {
130             strncat(filename, ".txt", MAX_NAME_LEN - strlen(filename) - 1);
131         }
132     }
133 
134     FILE *fp = fopen(filename, "w");
135     if (fp == NULL) {
136         printf("\n 文件打开失败:%s\n", filename);
137         return 1;
138     }
139 
140     strftime(datetime_str, sizeof(datetime_str), "%Y-%m-%d %H:%M:%S", time_info);
141     
142     fprintf(fp, "星际联盟2049(1)班 - 随机抽奖结果\n");
143     fprintf(fp, "抽奖时间:%s\n", datetime_str);
144     fprintf(fp, "中奖人数:%d人\n", MAX_WINNERS);
145     fprintf(fp, "总候选人数:%d人\n", MAX_CANDIDATES);
146     fprintf(fp, "中奖名单如下:\n");
147     for (int i = 0; i < MAX_WINNERS; i++) {
148         fprintf(fp, "%d. %s\n", i + 1, candidates[winners[i]]);
149     }
150     
151     fclose(fp);
152     
153     printf("\n保存成功!");
154     
155     return 0;
156 }

aa09f58478c8c6a626878862e85ee7e9

 

posted @ 2025-12-30 19:49  陆绎  阅读(3)  评论(0)    收藏  举报