用C语言编一个简单的学生管理系统

2024-11-24 15:23:00
推荐回答(1个)
回答1:

#include
#include

typedef struct student
{
char name[20];
char sex;
int num;
int score;
int age;
}STUDENT;

struct student st_stu[29];
int i = 0;
int num;

void Create()
{
int j;
//clrscr();
do
{
printf("\n\n\t\tenter his\hers name:");
scanf("%s", &st_stu[i].name);
getchar();
printf("\n\n\t\tenter his\hers sex:");
scanf("%c", &st_stu[i].sex);
printf("\n\n\t\tenter his\hers num:");
scanf("%d", &st_stu[i].num);
printf("\n\n\t\tenter his\hers score:");
scanf("%d", &st_stu[i].score);
printf("\n\n\t\tenter his\hers age:");
scanf("%d", &st_stu[i].age);
//fwrite(&st_stu[i], sizeof(st_stu), 1, fp);
printf("\n\n\n\t\tcontinue?\n\n\t\tyes:1\n\n\t\tno:2\n\n\n\t\tchoice:");
scanf("%d", &j);
//clrscr();
i++;
//}
}
while (j == 1);
}

void Search()
{
int b, j;
//FILE* fp;
//clrscr();
printf("There are %d student", i);
printf("\n\n\t\tinput his/hers num:");
scanf("%d", &b);
if (b<1 || b>28)
printf("input wrong");
else if (b<1 && b>i)
printf("the student's data isn't in");
else
{
for (j = 0; j < i; j++)
{
if (st_stu[j].num == b)
{
printf("\t%s\t\t%s\t\t%d\t\t%d\t\t%d\n", st_stu[j].name,
st_stu[j].sex, st_stu[j].num, st_stu[j].score,
st_stu[j].age);
break;
}
}
}

}

void Read_show()
{
struct student r_ststu;

FILE* fp;
int j = 0;
size_t readdata = 0;
//clrscr();
if ((fp = fopen("pupeng.txt", "r")) == NULL)
{
printf("error!\n");
//exit(0);
}
else
{
printf("the file is ready\n");
printf("\tname\t\tsex\t\tnum\t\tscore\t\tage\n");
//for (j = 0; j < 3; j++)
//do
while (!feof(fp)) //看是否是到了文件尾,没到,继续读
{
readdata = fread(&r_ststu, sizeof(r_ststu), 1, fp);
//printf("%d\n", readdata);
if (readdata == 0)
break; //如果没有数据了,返回为0
printf("\t%s\t\t%c\t\t%d\t\t%d\t\t%d\n", r_ststu.name,
r_ststu.sex, r_ststu.num, r_ststu.score, r_ststu.age);
//j++;
}//;

fclose(fp);
}
}

void Save()
{
FILE* fp;
int number = 0;
size_t SaveNum = 0;
//clrscr();
if ((fp = fopen("pupeng.txt", "w")) == NULL)
{
printf("error!\n");
//exit(0);
}
else
{
printf("Save to file pupeng.txt\n");
for (number = 0; number < i; number++)
{
///SaveNum =
fwrite(&st_stu[number], sizeof(st_stu[number]), 1, fp);
}
fclose(fp);

//printf("%d\n", SaveNum);
printf("Save OK!Please any key.....\n");
}
}
void LOVE()
{
int j;
//clrscr();
do
{
//if ((fp = fopen("pupeng.txt", "wb")) == NULL)
//{
// printf("error!\n");
//exit(0);
//}
//else
//{
printf("\n\n\t\tenter his\hers name:");
scanf("%s", &st_stu[i].name);
printf("\n\n\t\tenter his\hers sex:");
scanf("%s", &st_stu[i].sex);
printf("\n\n\t\tenter his\hers num:");
scanf("%d", &st_stu[i].num);
printf("\n\n\t\tenter his\hers score:");
scanf("%d", &st_stu[i].score);
printf("\n\n\t\tenter his\hers age:");
scanf("%d", &st_stu[i].age);
//rite(&st_stu[i], sizeof(st_stu), 1, fp);
printf("\n\n\n\t\tcontinue?\n\n\t\tyes:1\n\n\t\tno:2\n\n\n\t\tchoice:");
scanf("%d", &j);
//clrscr();
i++;
//}
}
while (j == 1);
}

main()
{
int a, b, j;
//clrscr();
printf("\n\n\n\n\t\t\tmake sure the way of the file is right\n");
printf("\n\n\t\t\t0------->yes\n");
printf("\n\n\t\t\t1------->no\n");
printf("\t\t\tchoice:");
scanf("%d", &j);
printf("choice:");
if (j == 0)
//clrscr();
do
{
printf("\n\n\n\n\n\t\tstudent score management system\n\n\n\n");
printf("\t\t^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^\n");
printf("\t\t\t0--------->Create\n");
printf("\t\t\t1--------->Search\n");
printf("\t\t\t2--------->Tappend\n");
printf("\t\t\t3--------->Printf\n");
printf("\t\t\t4--------->Save\n");
printf("\t\t\t5--------->Exit\n");
printf("\t\t^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^\n\n\n");
printf("Choice:");
scanf("%d", &a);
//clrscr();
switch (a)
{
case 0:
{
Create();
break;
}
case 1:
{
Search();
break;
}
case 2:
{
//LOVE();
break;
}
case 3:
{
Read_show();
break;
}
case 4:
{
Save();
getchar();
break;
}
case 5:
{
//exit(0);
}
default:
{
printf("input wrong!");
break;
}
}
}
while (a != 5);
if (j == 1)
//exit(0);
if (j != 0 && j != 1)
printf("wrong choice");
}