#include<stdio.h>
#include<string.h>
#include<windows.h>
#include<stdlib.h>
#include<conio.h>
#pragma warning(disable:4996)
typedef struct s1
{
char id[50];
char name[50];
int cnt;
float pscj[10];
float examine;
float examine2;
float sum;
struct s1 *next;
int inputflag;
}Msg,*linklist;
void menu()
{
printf("\t|--------------------------------|\n");
printf("\t| 1.添加学生基本信息 |\n");
printf("\t| 2.输入学生成绩信息 |\n");
printf("\t| 3.按照学号查找学生 |\n");
printf("\t| 4.按照学号查找学生并修改其信息 |\n");
printf("\t| 5.打印输出学生信息 |\n");
printf("\t| 6.程序退出 |\n");
printf("\t| 请输入选择(1-6): |\n");
printf("\t|--------------------------------|\n\t");
}
int add_stu(const char *filename, const char *mode)
{
FILE *fp;
Msg stu;
int i;
char ch;
fp = fopen(filename, mode);
if (fp == NULL)
{
printf("fail to open file!\n");
return 0;
}
printf("Successfully opened file!\n");
while (1)
{
printf("输入学号:\n");
scanf("%s", &stu.id);
printf("输入姓名:\n");
scanf("%s", stu.name);
stu.cnt = 0;
for (i = 0; i < 3; i++)
stu.pscj[i] = 0;
stu.examine = 0;
stu.sum = 0;
stu.inputflag = 0;
fwrite(&stu, sizeof(Msg), 1, fp);
getchar();
printf("输入y或Y继续:\n");
ch = getchar();
if ((ch == 'Y') || (ch == 'y'))
continue;
else
break;
}
fclose(fp);
return 1;
}
int input_stu(const char *filename, const char *mode)
{
FILE *fp;
Msg stu;
int i, len;
float ave;
fp = fopen(filename, mode);
if (fp == NULL)
{
printf("fail to open file!\n");
return 0;
}
printf("Successfully opened file!\n");
while (fread(&stu, sizeof(Msg), 1, fp))
{
//fread(&stu, sizeof(Msg), 1, fp);
if (stu.inputflag == 1)
continue;
len = sizeof(Msg);
fseek(fp, -len, 1);//由于读过后,指针已经指向下一个学生,所以需要返回
printf("输入%s->%s的缺勤次数:", stu.id, stu.name);
scanf("%d", &stu.cnt);
printf("输入%s->%s的平时成绩:\n", stu.id, stu.name);
ave = 0;
for (i = 0; i < 9; i++)
{
printf("输入第%d次课后作业成绩:", i + 1);
scanf("%f", &stu.pscj[i]);
ave += stu.pscj[i];
}
stu.pscj[9] = ave / 9;
printf("输入%s->%s的大作业成绩:", stu.id, stu.name);
scanf("%f", &stu.examine2);
printf("输入%s->%s的考试成绩:", stu.id, stu.name);
scanf("%f", &stu.examine);
stu.sum = stu.pscj[9] * 0.15 + stu.examine*0.7+ stu.examine2*0.15;
stu.inputflag = 1;
fwrite(&stu, sizeof(Msg), 1, fp);
fseek(fp, -len, 1);//由于读过后,指针已经指向下一个学生,所以需要返回
}
fclose(fp);
}
int find_stu(const char *filename, const char *mode,linklist &H)
{
FILE *fp;
linklist p;
char ch;
Msg stu[50];
p = H;
char no[50];
char id[50];
fp = fopen(filename, mode);
if (fp == NULL)
{
printf("fail to open file!\n");
return 0;
}
printf("Successfully opened file!\n");
printf("要查询的学生学号:");
scanf("%s", id);
while (p != NULL)
{
if(strcmp(p->id,no)==0)
{
break;
}
else
{
p = p->next;
}
}
if (p != NULL)
{
printf("|-------------------------------------TUTE--------------------------------------|\n");
printf("| 学号 | 姓名 | 考勤 | 课后作业成绩 | 大作业成绩 | 期末考核成绩 | 总评成绩 |\n");
printf("| %s | %s | %d | %f | %f | %f | %f |\n", p->id, p->name, p->cnt, p->pscj, p->examine2, p->examine, p->sum);
printf("|-------------------------------------TUTE--------------------------------------|\n");
}
else
{
printf("没有此人");
}
ch = getchar();
fread(&stu, sizeof(Msg), 1, fp);
fclose(fp);
}
int modify_stu(const char *filename, const char *mode)
{
FILE *fp;
fp = fopen(filename, mode);
if (fp == NULL)
{
printf("fail to open file!\n");
return 0;
}
printf("Successfully opened file!\n");
fclose(fp);
}
int print_stu(const char *filename, const char *mode)
{
FILE *fp;
Msg stu;
int i;
fp = fopen(filename, mode);
if (fp == NULL)
{
printf("fail to open file!\n");
return 0;
}
printf("Successfully opened file!\n");
while (fread(&stu, sizeof(Msg), 1, fp))
{
printf("学号:");
printf("%s\n", stu.id);
printf("姓名:");
printf("%s\n", stu.name);
printf("考勤:");
printf("%d\n", stu.cnt);
printf("平时成绩:");
for (i = 0; i < 8; i++)
printf("%6.1f", stu.pscj[i]);
printf("\n");
printf("考试:");
printf(" %.1f\n", stu.examine);
printf("总评:");
printf(" %.1f\n", stu.sum);
}
fclose(fp);
}
int main()
{
linklist H=NULL;
int sel;
const char *filename = "classinf.txt";
const char *mode[3] = { "a","r","r+" };
while (1)
{
menu();
scanf("%d", &sel);
if (sel == 1)
add_stu(filename, mode[0]);
else if (sel == 2)
input_stu(filename, mode[2]);
else if (sel == 3)
find_stu(filename, mode[1],H);
else if (sel == 4)
modify_stu(filename, mode[2]);
else if (sel == 5)
print_stu(filename, mode[1]);
else if (sel == 6)
break;
else
break;
system("pause");
system("cls");
}
return 1;
}

