用C语言写个学生成绩管理系统程序

2024年11月28日 14:39
有2个网友回答
网友(1):

楼上的一看就是抄的,一点注释都没有。

你也真够辛苦的,加油,挣点分不容易。

网友(2):

#include /* TC mem.h */
#include
#include
#include
#include

#define FILE1 "1.txt"
#define FILE2 "2.txt"
#define FILE3 "3.txt"
#define FILE4 "4.txt"
struct STUDENT
{
char name[20];
int number;
int chinese;
int math;
int english;
int total;
};
typedef STUDENT student;
int count = 0;
student *ss = NULL;

void combine2files()
{
FILE *in, *out;
student s;

out = fopen(FILE3, "w");
in = fopen(FILE1, "r");

if (!in || !out)
{
printf("can't open file(s)\n");
exit(-1);
}

while (1)
{
s.name[0] = '\0';
fscanf(in, "%s %d %d %d %d", s.name, &s.number, &s.chinese, &s.math, &s.english);

if (s.name[0] == '\0')
break;

++count;
fprintf(out, "%s %d %d %d %d\n", s.name, s.number, s.chinese, s.math, s.english);
/* printf("%s %d %d %d %d\n", s.name, s.number, s.chinese, s.math, s.english); */
}

fclose(in);
fflush(in);
in = fopen(FILE2, "r");

if (!in)
{
fclose(out);
printf("can't open file\n");
exit(-1);
}

while (1)
{
s.name[0] = '\0';
fscanf(in, "%s %d %d %d %d", s.name, &s.number, &s.chinese, &s.math, &s.english);

if (s.name[0] == '\0')
break;

++count;
fprintf(out, "%s %d %d %d %d\n", s.name, s.number, s.chinese, s.math, s.english);
/* printf("%s %d %d %d %d\n", s.name, s.number, s.chinese, s.math, s.english); */
}

fclose(in);
fclose(out);
}

void check4makeup()
{
FILE *in, *out;
student s;

out = fopen(FILE4, "w");
in = fopen(FILE3, "r");

if (!in || !out)
{
printf("can't open file(s)\n");
exit(-1);
}

while (1)
{
s.name[0] = '\0';
fscanf(in, "%s %d %d %d %d", s.name, &s.number, &s.chinese, &s.math, &s.english);

if (s.name[0] == '\0')
break;

if (s.chinese < 60 || s.math < 60 || s.english < 60)
fprintf(out, "%s %d %d %d %d\n", s.name, s.number, s.chinese, s.math, s.english);
}

fclose(in);
fclose(out);
}

void selectsort(int type = 0)
{
int i, j, k;
student s;

for (i = 0; i < count - 1; ++i)
{
k = i;
for (j = i + 1; j < count; ++j)
{
if (type == 0)
{
if (ss[k].total < ss[j].total)
k = j;
}
else
{
if (strcmp(ss[k].name, ss[j].name) < 0)
k = j;
}
}

if (k != i)
{
memcpy(&s, &ss[i], sizeof(student));
memcpy(&ss[i], &ss[k], sizeof(student));
memcpy(&ss[k], &s, sizeof(student));
}
}
}

void bubblesort()
{
int i, j;
student s;

for (i = 0; i < count - 1; ++i)
{
for (j = count - 1; j > i; --j)
if (ss[j].total > ss[j-1].total)
{
memcpy(&s, &ss[j-1], sizeof(student));
memcpy(&ss[j-1], &ss[j], sizeof(student));
memcpy(&ss[j], &s, sizeof(student));
}
}
}

void outputrecords()
{
int i;
printf("No:\tName:\t\tTotal:\tChinese:\tMath:\tEnglish:\n");

for (i = 0; i < count; ++i)
{
printf("%d\t%s\t\t%d\t%d\t\t%d\t%d\n",
ss[i].number, ss[i].name, ss[i].total, ss[i].chinese, ss[i].math, ss[i].english);
}
}

void sortrecords()
{
int i = 0;
FILE *fp;
char choice;

fp = fopen(FILE3, "r+");

if (!fp)
{
printf("can't open file\n");
exit(-1);
}

while (1)
{
fscanf(fp, "%s %d %d %d %d", ss[i].name, &ss[i].number,
&ss[i].chinese, &ss[i].math, &ss[i].english);

if (i == count)
break;

ss[i].total = ss[i].chinese + ss[i].math + ss[i].english;
++i;
}

rewind(fp);
puts("Please select the sort method:");
puts("1. Bubble Sort");
puts("2. Selection Sort");
choice = getchar();

if (choice == '1')
bubblesort();
else
selectsort();

rewind(fp);
fclose(fp);
}
int binsearch(const char *name)
{
int low = 0;
int high = count - 1;

while (low <= high)
{
int mid = (low + high) / 2;

if (!strcmp(ss[mid].name, name))
{
return mid;
}
else
{
if (strcmp(ss[mid].name, name) > 0)
high = mid - 1;
else
low = mid + 1;
}
}

return -1;
}

int seqsearch(const char *name)
{
int i;

for (i = 0; i < count; ++i)
{
if (!strcmp(ss[i].name, name))
return i;
}
return -1;
}

void findrecord()
{
char nm[20], choice;
int i;

putchar('\n');
selectsort(1);
while (1)
{
printf("Please input the student name you want to find(!=end):\n");
scanf("%s", nm);

if (nm[0] == '!')
break;

puts("Please select the search method:");
puts("1. binary Search");
puts("2. Sequence Search");
choice = getchar();
getchar();

if (choice == '1')
i = binsearch(nm);
else
i = seqsearch(nm);

if (i != -1)
printf("No: %d Name: %s Total: %d Chinese: %d Math: %d English %d\n",
ss[i].number, ss[i].name, ss[i].total, ss[i].chinese, ss[i].math, ss[i].english);
else
printf("Not Found\n");
}
}

void main()
{
combine2files();
ss = (student *)malloc(sizeof(student) * count);
check4makeup();
sortrecords();
outputrecords();
findrecord();
free(ss);
}