实验7
任务四
- 源代码
#define _CRT_SECURE_NO_WARNINGS
#include <stdio.h>
#include<string.h>
#define N 50
struct DATE
{
char a[N];
int b;
};
void write();
int fun(int n, char* str[]);
int main()
{
int n, i;
struct DATE k[N];
char* str[] = { "0123456789-0123456789",
"nuist2026",
"cocmos galaxy" };
FILE* fp;
fp = fopen("data4.txt", "w");
if (fp == NULL)
{
printf("File open error!");
return;
}
n = sizeof(str) / sizeof(str[0]);
for (int i = 0; i < n; i++)
{
fprintf(fp, "%s\n", str[i]);
}
fclose(fp);
printf("date4.txt统计结果:\n");
strcpy(k[0].a, "行数:");
strcpy(k[1].a, "字符数(不计空白符):");
k[0].b = n;
k[1].b=fun(n, str);
for (int i = 0;i < 2;i++)
{
printf("%-25s %d\n", k[i].a, k[i].b);
}
return 0;
}
int fun(int n, char* str[])
{
int i, j,t=0;
for(i=0;i<n;i++)
{
for(j=0;str[i][j]!='\0';j++)
{
if(str[i][j] != ' ' && str[i][j] != '\t' && str[i][j] != '\n')
{
t++;
}
}
}
return t;
}
- 结果截图


任务五
- 源代码
#define _CRT_SECURE_NO_WARNINGS
#include <stdio.h>
#include<string.h>
#define N 50
struct STU
{
int number;
char name[N];
int s1;
int s2;
int s;
char grade[N];
};
void read(struct STU st[], int n);
int process(struct STU st[], int n, struct STU st_pass[]);
void write(struct STU st_pass[], int n);
void output(struct STU st_pass[], int n);
int main()
{
int n, i;
struct STU st[] = {
{1001, "桃乐丝", 36, 55},
{1002, "稻草人", 28, 40},
{1003, "千寻", 39, 55},
{1004, "白龙", 35, 60},
{1005, "汤婆婆", 20, 35},
{1006, "无脸男", 33, 50},
{1007, "希达", 40, 32},
{1008, "巴鲁", 28, 30},
{1009, "苏菲", 37, 60},
{1010, "哈尔", 35, 57}
};
struct STU st_pass[N];
n = sizeof(st) / sizeof(st[0]);
FILE* fp;
fp = fopen("examinee.txt", "w");
if (fp == NULL)
{
printf("File open error!");
return;
}
for (int i = 0; i < n; i++)
{
fprintf(fp, "%-10d %-10s %-10d %-10d\n", st[i].number, st[i].name, st[i].s1, st[i].s2);
}
fclose(fp);
read(st, n);
n = process(st, n, st_pass);
write(st_pass, n);
output(st_pass, n);
return 0;
}
void read(struct STU st[], int n)
{
FILE* fp;
fp = fopen("examinee.txt", "r");
if (fp == NULL)
{
printf("File open error!");
return;
}
for (int i = 0; i < n; i++)
{
fscanf(fp, "%d %s %d %d", &st[i].number, st[i].name, &st[i].s1, &st[i].s2);
}
fclose(fp);
}
int process(struct STU st[], int n, struct STU st_pass[])
{
int j = 0;
for (int i = 0; i < n; i++)
{
st[i].s = st[i].s1 + st[i].s2;
if (st[i].s>=60)
{
st_pass[j] = st[i];
strcpy(st_pass[j].grade, "合格");
j++;
}
}
return j;
}
void write(struct STU st_pass[], int n)
{
FILE* fp;
fp = fopen("list_pass.txt", "w");
if (fp == NULL)
{
printf("File open error!");
return;
}
fprintf(fp, "%-12s %-12s %-10s %-10s %-10s %-12s\n",
"准考证号", "姓名", "客观成绩", "主观成绩", "总分", "成绩等级");
for (int i = 0; i < n; i++)
{
fprintf(fp, "%-12d %-12s %-10d %-10d %-10d %-12s\n", st_pass[i].number, st_pass[i].name, st_pass[i].s1, st_pass[i].s2, st_pass[i].s, st_pass[i].grade);
}
fclose(fp);
}
void output(struct STU st_pass[], int n)
{
printf( "%-12s %-12s %-10s %-10s %-10s %-12s\n",
"准考证号", "姓名", "客观成绩", "主观成绩", "总分", "成绩等级");
for (int i = 0; i < n; i++)
{
printf("%-12d %-12s %-10d %-10d %-10d %-12s\n", st_pass[i].number, st_pass[i].name, st_pass[i].s1, st_pass[i].s2, st_pass[i].s, st_pass[i].grade);
}
}
- 结果截图


任务六
- 源代码
#define _CRT_SECURE_NO_WARNINGS
#include <stdio.h>
#include <stdlib.h>
#include <time.h>
#include <string.h>
#define N 100
#define TOTAL_STUDENTS 80
#define LUCKY_NUM 5
struct STU
{
int number;
char name[N];
char class[N];
};
void read(struct STU st[]);
void process(struct STU st[], struct STU lucky[]);
void write(struct STU lucky[]);
void output(struct STU lucky[]);
int stuCount = 0;
int main()
{
srand((unsigned int)time(NULL));
int i;
struct STU st[N];
struct STU lucky[5];
read(st);
process(st, lucky);
write(lucky);
output(lucky);
return 0;
}
void read(struct STU st[])
{
FILE* fp;
int count = 0;
fp = fopen("list.txt", "r");
if (fp == NULL) {
printf("文件打开失败!\n");
}
while (fscanf(fp, "%d %s %[^\n]\n", &st[count].number, st[count].name, st[count].class) == 3) {
count++;
if (count >= TOTAL_STUDENTS) break;
}
fclose(fp);
stuCount = count;
}
void process(struct STU st[], struct STU lucky[])
{
int chosen[LUCKY_NUM];
int count = 0;
while (count < LUCKY_NUM) {
int r = rand() % stuCount;
int duplicate = 0;
for (int i = 0; i < count; i++) {
if (chosen[i] == r) {
duplicate = 1;
break;
}
}
if (!duplicate) {
chosen[count] = r;
count++;
}
}
for (int i = 0; i < LUCKY_NUM; i++) {
lucky[i] = st[chosen[i]];
}
}
void write(struct STU lucky[])
{
FILE* fp;
fp = fopen("lucky.txt", "w");
if (fp == NULL)
{
printf("File open error!");
return;
}
for (int i = 0; i < LUCKY_NUM; i++)
{
fprintf(fp, "%d %s %s\n", lucky[i].number, lucky[i].name, lucky[i].class);
}
fclose(fp);
}
void output(struct STU lucky[])
{
struct STU temp;
int i, j;
for (int i = 0; i < LUCKY_NUM - 1; i++)
{
for (int j = 0; j < LUCKY_NUM - i - 1; j++)
{
if (lucky[j].number > lucky[j + 1].number)
{
temp = lucky[j];
lucky[j] = lucky[j + 1];
lucky[j + 1] = temp;
}
}
}
for (int i = 0; i < LUCKY_NUM; i++)
{
printf("%d %s %s\n", lucky[i].number, lucky[i].name, lucky[i].class);
}
}
- 结果截图



浙公网安备 33010602011771号