C语言千人长跑活动应用程序,C语言文件操作,结构体
大学学完人生第一个编程课《C语言》时,写的课终作品,当时不知道重构、冗余、时间空间复杂度,所以代码有点low。
如果复制粘贴后可以运行,希望老板点一个赞,如果不能运行欢迎评论。
#include<stdio.h>
#include<stdlib.h>
#include<string.h>
#include<conio.h>
/***************************全局变量***************************/
struct student /*定义结构体*/
{
char num[10];
char name[10];
char college[10];
char major[10];
char time[300][20];
int days;
int mileage;
int score;
}stu[100001];
/*//函数声明//*/
int main_menu(); /*主菜单*/
void print_ten(); /*输入前十的成绩和信息*/
void student_new(); /*录入学生信息*/
void score_search(); /*根据学号查询信息*/
void F5(); /*刷新成绩*/
void days_mileage(); /*打卡并输入里程数*/
void Score_student(); /*计算每位同学的成绩*/
void score_print(); /*输出全部同学成绩*/
void into_menu(); /*从功能模块进入主菜单*/
int number_people(); /*输出人数*/
void input_strect(); /*将文件内容输入结构体*/
void delete_one(); /*删除一个学生信息*/
/*//主函数//*/
void main()
{
int x,y,z;
FILE*fp = fopen("参赛学生资料.txt", "r");
if (fp == NULL)
{
z=0;
FILE *op = fopen("人数.txt", "w+");
if (op == NULL)
{
printf("文件读取失败!\n");
system("pause");
exit(0);
}
fprintf(op, "%d", z);
fclose(op);
}
fclose(fp);
FILE *ap= fopen("人数.txt", "r");
if(ap==NULL)
{
printf("文件《人数》读取失败!\n");
system("pause");
exit(0);
}
fscanf(ap,"%d",&x);
if(x==0)
{
FILE *bp= fopen("参赛学生资料.txt", "w+");
if(bp==NULL)
{
printf("文件《参赛学生资料》读取失败!\n");
system("pause");
exit(0);
}
fclose(bp);
}
fclose(ap);
x = 0;
while (1) /*永远不退出主菜单*/
{
x = main_menu();
if (x == 1)
break;
}
printf("\n\n已退出!\n\n");
}
/*/主菜单///*/
int main_menu()
{
char c;
system("CLS"); /*清屏*/
printf("*************************************************************\n");
printf("/*唐小尊 千人长跑活动应用程序 2018/6/28*/\n");
int number = number_people(); /*number_people是单独输出一共的参赛人员的函数*/
int n = number;
if (number>0)
{
printf("\n");
}
if (number>0) /*当人数大于0时,主页面就开始显示前十名的成绩*/
{
print_ten();
}
printf("\n>>>>>>>>>>>>>>>千人长跑活动程序<<<<<<<<<<<<<<<\n\n");
printf("\t1 录入学生信息\n\n");
printf("\t2 打卡/里程数\n\n");
printf("\t3 按学号查询\n\n");
printf("\t4 刷新成绩\n\n");
printf("\t5 输出全部成绩\n\n");
printf("\t6 删除一个学生信息\n\n");
printf("\t7 退出\n");
if (n >= 0)
printf(" 现已有%d人参加!\n", n);
else
{
number = 0;
FILE *op = fopen("人数.txt", "a+");
if (op == NULL)
{
printf("文件读取失败!\n");
system("pause");
exit(0);
}
fprintf(op, "%d", number);
fclose(op);
}
printf("**********************************************\n");
printf("请用数字键1—6选择操作\n\n");
scanf("%c", &c);
system("CLS");
switch (c)
{
case '1': student_new(); break; /*录入学生基本信息*/
case '2': days_mileage(); break; /*打卡+里程*/
case '3': score_search(); break; /*根据学号查询*/
case '4': F5(); break; /*刷新成绩*/
case '5': score_print(); break; /*输出全部成绩*/
case '6':delete_one(); break; /*删除一个学生信息*/
case '7':return 1; break; /*退出*/
}
}
/*//录入学生基本信息*/
void student_new()
{
int i, j, z, k, q = 0, number, days1, mileage1, score1, n;
char num1[10], name1[10], college1[10], major1[10];
printf("此次录入几位同学?\n");
scanf("%d", &n);
printf("\n学号\t姓名\t学院\t专业\n\n");
for (k = 1; k <= n; k++) //录入n次
{
scanf("%s %s %s %s", num1, name1, college1, major1);
number = number_people();
void input_strect(); //从文件中输入数据到结构体
for (i = 1; i <= number; i++) //判断同一学号是否重复录入
{
if (strcmp(num1, stu[i].num) == 0)
{
q = 1;
break;
}
}
if (q == 0) //录入文件
{
FILE *fp = fopen("参赛学生资料.txt", "a+");
if (fp == NULL)
{
printf("文件打开失败!\n");
system("pause");
exit(0);
}
days1 = 0;
mileage1 = 0;
score1 = 0;
fprintf(fp, "%s\t%s\t%s\t%s\t%d\t%d\t%d\n", num1, name1, college1, major1, days1, mileage1, score1);
fflush(stdin);
fclose(fp);
number = number + 1;
FILE *op = fopen("人数.txt", "w+"); //录入人数
char num1[10], name1[10], college1[10], major1[10];
if (op == NULL)
{
printf("文件打开失败!\n");
system("pause");
exit(0);
}
fprintf(op, "%d", number);
fclose(op);
}
if (q == 1) //因学号重复而重新输入
{
printf("\n此学号重复录入!请重新输出此次数据!\n\n");
k = k - 1;
q = 0;
}
}
}
/*//删除一个学生信息*/
void delete_one()
{
char number1[10];
int i, j, z, x, r, k, number;
number = number_people();
printf("请输入学号:\n\n");
scanf("%s", number1); getchar();
input_strect(); //从文件里面取出数据到结构体
for (i = 1; i <= number; i++)
{
k = i;
if (strcmp(number1, stu[i].num) == 0)
break;
}
FILE *ap = fopen("参赛学生资料.txt", "w+"); /*录成绩入文件*/
if (ap == NULL)
{
printf("刷新成绩时读取文件失败!");
exit(0);
}
for (j = 1; j <= number; j++)
{
if (j != k)
{
fprintf(ap, "%s %s %s %s %d %d %d ", stu[j].num, stu[j].name, stu[j].college, stu[j].major, stu[j].days, stu[j].mileage, stu[j].score);
x = stu[j].days;
for (r = 1; r <= x; r++)
{
fprintf(ap, "%s ", stu[j].time[r]);
}
fprintf(ap, "\n");
}
}
fclose(ap);
number = number - 1;
FILE *op = fopen("人数.txt", "w+"); //录入人数
if (op == NULL)
{
printf("文件打开失败!\n");
system("pause");
exit(0);
}
fprintf(op, "%d", number);
fclose(op);
}
/*打卡+里程/*/
void days_mileage()
{
char a[10];
char b;
int i, j, l, r, x, n, z;
n = number_people();
input_strect(); //文件内容输入到结构体
while (1)
{
printf("输入学号:");
scanf("%s", a); getchar();
for (i = 1; i <= n; i++)
{
if (strcmp(a, stu[i].num) == 0)
break;
}
printf("请输入你今日的里程数:\n");
scanf("%d", &l);
stu[i].mileage += l;
stu[i].days += 1;
x = stu[i].days;
printf("请输入签到时间:\n");
scanf("%s", stu[i].time[x]);
getchar();
printf("继续录入?(Y/N)");
scanf("%c", &b);
if (b == 'N')
break;
}
FILE *op = fopen("参赛学生资料.txt", "w+");
if (op == NULL)
{
printf("文件读取失败!\n");
system("pause");
exit(0);
}
for (j = 1; j <= n; j++)
{
fprintf(op, "%s %s %s %s %d %d %d \n", stu[j].num, stu[j].name, stu[j].college, stu[j].major, stu[j].days, stu[j].mileage, stu[j].score);
x = stu[j].days;
for (r = 1; r <= x; r++)
{
fprintf(op, "%s ", stu[j].time[r]);
}
fprintf(op, "\n");
}
fclose(op);
}
/*//输入学号查成绩//*/
void score_search()
{
printf("请输入学号: ");
char a[10];
char x;
scanf("%s", a); getchar();
int i, j, z, n, k;
n = number_people();
input_strect(); //文件内容输入结构体
for (i = 1; i <= n; i++)
{
k = i;
if (strcmp(a, stu[i].num) == 0)
break;
}
printf("学号——姓名——学院——专业——打卡天数——总里程——成绩——打卡日期\n");
printf("%s\t%s\t%s\t%s\t%d\t%d\t%d\t", stu[k].num, stu[k].name, stu[k].college, stu[k].major, stu[k].days, stu[k].mileage, stu[k].score);
z = stu[i].days;
for (j = 1; j<z; j++)
{
printf("%s\t", stu[k].time[j]);
}
printf("\n");
while (1)
{
printf("是否退出Y/N?\n");
scanf("%c", &x);
if (x == 'Y')
break;
}
}
/*///刷新成绩///*/
void F5()
{
int j, x, r, n;
Score_student(); /*计算成绩并排序*/
n = number_people();
FILE *ap = fopen("参赛学生资料.txt", "w+"); /*录成绩入文件*/
if (ap == NULL)
{
printf("刷新成绩时读取文件失败!");
exit(0);
}
for (j = 1; j <= n; j++)
{
fprintf(ap, "%s %s %s %s %d %d %d ", stu[j].num, stu[j].name, stu[j].college, stu[j].major, stu[j].days, stu[j].mileage, stu[j].score);
x = stu[j].days;
for (r = 1; r <= x; r++)
{
fprintf(ap, "%s ", stu[j].time[r]);
}
fprintf(ap, "\n");
}
fclose(ap);
}
/*///输出全部信息///*/
void score_print()
{
int i, j, z, o, n;
char x;
n = number_people();
input_strect(); //文件内容输入到结构体
printf("学号—姓名—学院—专业—打卡天数—总里程—打卡日期\n\n");
for (i = 1; i <= n; i++)
{
printf("%s %s %s %s %d %d ", stu[i].num, stu[i].name, stu[i].college, stu[i].major, stu[i].days, stu[i].mileage);
o = stu[i].days;
for (j = 1; j <= o; j++)
{
printf("第%d次打卡:%s ", j, stu[i].time[j]);
}
printf("\n");
}
while (1)
{
printf("是否退出Y/N?\n");
scanf("%c", &x);
if (x == 'Y')
break;
}
}
/*///输出前十///*/
void print_ten()
{
int i,n, g;
n = number_people();
input_strect(); //文件内容输入到结构体
if (n >= 10)
{
g = 10;
}
if (n<10)
{
g = n;
}
printf(" 名次 学号 姓名 学院 专业 打卡天数 路程\n");
for (i = 1; i <= g; i++)
{
printf("第%2d名\t%9s %6s %5s %3s %3d %d\n", i, stu[i].num, stu[i].name, stu[i].college, stu[i].major, stu[i].days, stu[i].mileage);
}
}
/*输出人数//*/
int number_people()
{
int n, number, x = 0;
FILE *op = fopen("人数.txt", "r+");//输出人数
if (op == NULL)
{
printf("文件读取失败!\n");
system("pause");
exit(0);
}
fscanf(op, "%d", &number);
n = number;
fclose(op);
return n;
}
/*计算成绩并排序*/
void Score_student()
{
int i, j, z, n, h;
char f;
n = number_people();
input_strect(); //文件内容输入到结构体
int x, a5, a6, y;
char a1[10], a2[10], a3[10], a4[10], a8[20];
long int a7;
for (i = 1; i <= n; i++)
{
stu[i].score = stu[i].days * 1000 + stu[i].mileage;
}
for (i = 1; i <= n; i++)
{
x = i;
for (j = i + 1; j <= n; j++)
{
if (stu[i].score<stu[j].score)
{
x = j;
}
strcpy(a1, stu[i].num); strcpy(stu[i].num, stu[x].num); strcpy(stu[x].num, a1);
strcpy(a2, stu[i].name); strcpy(stu[i].name, stu[x].name); strcpy(stu[x].name, a2);
strcpy(a3, stu[i].college); strcpy(stu[i].college, stu[x].college); strcpy(stu[x].college, a3);
strcpy(a4, stu[i].major); strcpy(stu[i].major, stu[x].major); strcpy(stu[x].major, a4);
a5 = stu[i].days; stu[i].days = stu[x].days; stu[x].days = a5;
a6 = stu[i].mileage; stu[i].mileage = stu[x].mileage; stu[x].mileage = a6;
a7 = stu[i].score; stu[i].score = stu[x].score; stu[x].score = a7;
y = stu[x].days;
for (z = 1; z <= x; z++)
{
strcpy(a8, stu[i].time[z]); strcpy(stu[i].time[z], stu[x].time[z]); strcpy(stu[x].time[z], a8);
}
}
}
FILE*op = fopen("参赛学生资料.txt", "w+");
if (op == NULL)
{
printf("文件读取失败!");
system("pause");
exit(0);
}
for (i = 1; i < n; i++)
{
fprintf(op, "%s %s %s %s %d %d %d ", stu[i].num, stu[i].name, stu[i].college, stu[i].major, stu[i].days, stu[i].mileage, stu[i].score);
for (j = 1; j <= stu[i].days; j++)
{
fprintf(op, "%s ", stu[i].time[j]);
}
fprintf(op, "\n");
}
while (1)
{
printf("退出(Y/N)?");
scanf("%c", &f);
if (f == 'N');
break;
}
}
/*将文件内容输入到结构体*/
void input_strect()
{
int i,z,j,number;
number=number_people();
FILE*fp = fopen("参赛学生资料.txt", "r"); //从文件里面取出数据到结构体
if (fp == NULL)
{
printf("文件读取失败!");
system("pause");
exit(0);
}
for (i = 1; i <= number; i++)
{
fscanf(fp, "%s %s %s %s %d %d %d ", stu[i].num, stu[i].name, stu[i].college, stu[i].major, &stu[i].days, &stu[i].mileage, &stu[i].score);
z = stu[i].days;
for (j = 1; j <= z; j++)
{
fscanf(fp, "%s ", stu[i].time[j]);
}
}
fclose(fp);
}