学生成绩管理系统 c语言算法求助!
https://bbs.bccn.net/thread-472070-1-2.html可不可以请问一下这个帖子里的代码要怎么写算法啊 我们小组作业也是这个 但是我不会写算法QAQ
不知道能不能发帖子里的代码 我斗胆发在下面了。。。。。
程序代码:
#include<stdio.h>
#include<string.h>
#include<stdlib.h>
struct student1
{
int id;
char name[10];
int gender;
int College;
int data[10];
};
struct student2
{
int id;
char name[10];
char gender[4];
char College[30];
int data[10];
float average;
}student2[100];
struct Gender
{
int id;
char gender[4];
};
struct College
{
int id;
char College[30];
};
void getGender(char gender[], int genderId)
{
FILE *p;
struct Gender stGender;
p = fopen("d:\\S_Info.txt", "rb+");
if (p == NULL)
{
printf("文件打开失败!\n");
}
else
{
while (fscanf(p, "%d %s", &stGender.id, stGender.gender) != EOF)
{
if (stGender.id == genderId)
{
memcpy(gender, stGender.gender, 4);
break;
}
}
fclose(p);
}
}
void getCollege(char College[], int CollegeId)
{
FILE *p;
struct College stCollege;
p = fopen("d:\\C_Info.txt", "rb+");
if (p == NULL)
{
printf("文件打开失败!\n");
}
else
{
while (fscanf(p, "%d %s", &stCollege.id, stCollege.College) != EOF)
{
if (stCollege.id == (CollegeId + 1))
{
memcpy(College, stCollege.College, 30);
break;
}
}
fclose(p);
}
}
void read()
{
int i = 0,j = 0;
FILE *p;
struct student1 ststudent1;
//struct student2 ststudent2;
p = fopen("d:\\Student_Info.txt", "rb+");
if (p == NULL)
{
printf("文件打开失败!\n");
}
else
{
while (fscanf(p, "%d %s %d %d %d %d %d %d %d %d %d %d %d %d", &ststudent1.id, ststudent1.name,
&ststudent1.gender, &ststudent1.College, &ststudent1.data[0], &ststudent1.data[1]
, &ststudent1.data[2], &ststudent1.data[3], &ststudent1.data[4], &ststudent1.data[5]
, &ststudent1.data[6], &ststudent1.data[7], &ststudent1.data[8], &ststudent1.data[9]) != EOF)
{
student2[i].id = ststudent1.id;
memcpy(student2[i].name, ststudent1.name, 10);
memcpy(student2[i].data, ststudent1.data, 10 * sizeof(int));
getGender(student2[i].gender, ststudent1.gender);
getCollege(student2[i].College, ststudent1.College);
student2[i].average = 0;
for (j = 0; j < 10; j++)
{
student2[i].average += student2[i].data[j];
}
student2[i].average /= 10.0;
i++;
}
fclose(p);
printf("读取成功!\n");
}
}
void write()
{
int i = 0;
FILE *fp = fopen("d:\\result.txt", "w");
if (fp == NULL)
{
printf("文件打开失败!\n");
return;
}
else
{
fclose(fp);
for (i = 0; i < 100; i++)
{
fp = fopen("d:\\result.txt", "ab+");
if (fp == NULL)
{
printf("文件打开失败!\n");
return;
}
else
{
fprintf(fp, "%d %s %s %s %d %d %d %d %d %d %d %d %d %d\n", student2[i].id, student2[i].name,
student2[i].gender, student2[i].College, student2[i].data[0], student2[i].data[1]
, student2[i].data[2], student2[i].data[3], student2[i].data[4], student2[i].data[5]
, student2[i].data[6], student2[i].data[7], student2[i].data[8], student2[i].data[9]);
fclose(fp);
}
}
printf("写入成功!\n");
}
}
void sortWithName()
{
int i, j;
char name[100][10] = { 0 }, temp[10] = { 0 };
if (student2[0].id == 0)
{
printf("请先读取原文件!\n");
return;
}
for (i = 0; i < 100; i++)
{
strcpy(name[i], student2[i].name);
}
for (i = 0; i < 100; i++)
{
for (j = 0; j < 99; j++)
{
if (strcmp(name[j], name[j+1])>0)
{
strcpy(temp, name[j]);
strcpy(name[j], name[j+1]);
strcpy(name[j+1], temp);
}
}
}
for (i = 0; i < 100; i++)
{
for (j = 0; j < 100; j++)
{
if (strcmp(name[i], student2[j].name)==0)
{
printf("%d\t%s\t%s\t%s\t%d\t%d\t%d\t%d\t%d\t%d\t%d\t%d\t%d\t%d\t%f\n", student2[j].id, student2[j].name,
student2[j].gender, student2[j].College, student2[j].data[0], student2[j].data[1]
, student2[j].data[2], student2[j].data[3], student2[j].data[4], student2[j].data[5]
, student2[j].data[6], student2[j].data[7], student2[j].data[8], student2[j].data[9], student2[j].average);
break;
}
}
}
}
void sortWithScore()
{
int i, j;
float faverage[100] = {0},temp = 0;
if (student2[0].id == 0)
{
printf("请先读取原文件!\n");
return;
}
for (i = 0; i < 100; i++)
{
faverage[i] = student2[i].average;
}
for (i = 0; i < 100; i++)
{
for (j = 0; j < 99; j++)
{
if (faverage[j] < faverage[j + 1])
{
temp = faverage[j];
faverage[j] = faverage[j + 1];
faverage[j + 1] = temp;
}
}
}
for (i = 0; i < 100; i++)
{
for (j = 0; j < 100; j++)
{
if (faverage[i] == student2[j].average)
{
printf("%d\t%s\t%s\t%s\t%d\t%d\t%d\t%d\t%d\t%d\t%d\t%d\t%d\t%d\t%f\n", student2[j].id, student2[j].name,
student2[j].gender, student2[j].College, student2[j].data[0], student2[j].data[1]
, student2[j].data[2], student2[j].data[3], student2[j].data[4], student2[j].data[5]
, student2[j].data[6], student2[j].data[7], student2[j].data[8], student2[j].data[9], student2[j].average);
break;
}
}
}
}
void printfCollege()
{
int i, j, College;
char temp[30] = { 0 };
if (student2[0].id == 0)
{
printf("请先读取原文件!\n");
return;
}
puts("\t\t*********************************************");
puts("\t\t******* 0--信息学院 ****");
puts("\t\t******* 1--计算机学院 ****");
puts("\t\t******* 2--文法学院 ****");
puts("\t\t******* 3--外国语学院 ****");
puts("\t\t******* 4--数理学院 ****");
puts("\t\t******* 5--会金学院 ****");
puts("\t\t******* 6--化工学院 ****");
puts("\t\t******* 7--商学院 ****");
puts("\t\t******* 8--航空学院 ****");
puts("\t\t******* 9--艺术学院 ****");
puts("\t\t*********************************************");
printf("请选择学院名称(1~10): [ ]\b\b");
scanf("%d", &College);
switch (College)
{
case 1: sprintf(temp, "信息学院"); break;
case 2: sprintf(temp, "计算机学院"); break;
case 3: sprintf(temp, "文法学院"); break;
case 4: sprintf(temp, "外国语学院"); break;
case 5: sprintf(temp, "数理学院"); break;
case 6: sprintf(temp, "会金学院"); break;
case 7: sprintf(temp, "化工学院"); break;
case 8: sprintf(temp, "商学院"); break;
case 9: sprintf(temp, "航空学院"); break;
case 10: sprintf(temp, "艺术学院"); break;
default:printf("输入错误!"); return;
}
for (j = 0; j < 100; j++)
{
if (strcmp(temp, student2[j].College) == 0)
{
printf("%d\t%s\t%s\t%s\t%d\t%d\t%d\t%d\t%d\t%d\t%d\t%d\t%d\t%d\t%f\n", student2[j].id, student2[j].name,
student2[j].gender, student2[j].College, student2[j].data[0], student2[j].data[1]
, student2[j].data[2], student2[j].data[3], student2[j].data[4], student2[j].data[5]
, student2[j].data[6], student2[j].data[7], student2[j].data[8], student2[j].data[9], student2[j].average);
}
}
}
void modifyStudent()
{
int i, j;
char name[10] = { 0 };
if (student2[0].id == 0)
{
printf("请先读取原文件!\n");
return;
}
printf("请输入学生名字:");
scanf("%s", name);
for (j = 0; j < 100; j++)
{
if (strcmp(name, student2[j].name) == 0)
{
printf("请输入要修改的10门课的成绩,用空格隔开:");
scanf("%d%d%d%d%d%d%d%d%d%d", &student2[j].data[0], &student2[j].data[1]
, &student2[j].data[2], &student2[j].data[3], &student2[j].data[4], &student2[j].data[5]
, &student2[j].data[6], &student2[j].data[7], &student2[j].data[8], &student2[j].data[9]);
student2[j].average = 0;
for (i = 0; i < 10; i++)
{
student2[j].average += student2[j].data[i];
}
student2[j].average /= 10.0;
write();
break;
}
}
if (j == 100)
{
printf("找不到该学生!\n");
}
}
void printfName()
{
int i, j;
char name[10] = { 0 };
if (student2[0].id == 0)
{
printf("请先读取原文件!\n");
return;
}
printf("请输入学生名字:");
scanf("%s", name);
for (j = 0; j < 100; j++)
{
if (strcmp(name, student2[j].name) == 0)
{
printf("%d\t%s\t%s\t%s\t%d\t%d\t%d\t%d\t%d\t%d\t%d\t%d\t%d\t%d\t%f\n", student2[j].id, student2[j].name,
student2[j].gender, student2[j].College, student2[j].data[0], student2[j].data[1]
, student2[j].data[2], student2[j].data[3], student2[j].data[4], student2[j].data[5]
, student2[j].data[6], student2[j].data[7], student2[j].data[8], student2[j].data[9], student2[j].average);
break;
}
}
if (j == 100)
{
printf("找不到该学生!\n");
}
}
void modifyPassword()
{
;;//选做
}
void menu() //菜单函数
{
int n, m;
do
{
puts("\t\t*********************************************");
puts("\t\t*********************************************");
puts("\t\t**************学生成绩系统菜单***************");
puts("\t\t*********************************************");
puts("\t\t*********************************************");
puts("\t\t******* 1--原始文件读取 ****");
puts("\t\t******* 2--按姓名排序,输出 ****");
puts("\t\t******* 3--按平均成绩排序,输出 ****");
puts("\t\t******* 4--输出给定学院学生 ****");
puts("\t\t******* 5--修改给定学生成绩信息 ****");
puts("\t\t******* 6--按姓名查询学生,输出 ****");
puts("\t\t******* 7--修改系统密码 ****");
puts("\t\t******* 0--返回上一级菜单 ****");
puts("\t\t*********************************************");
printf("请选择服务种类(0~7): [ ]\b\b");
scanf("%d", &n);
if (n<1 || n>8)
{
system("cls");
printf("选择错误! 请重新选择!\n");
m = 1;
}
else
{
m = 0;
}
} while (m == 1);
switch (n)
{
case 1: read(); break;
case 2: sortWithName(); break;
case 3: sortWithScore(); break;
case 4: printfCollege(); break;
case 5: modifyStudent(); break;
case 6: printfName(); break;
case 7: modifyPassword(); break;
case 0: break;
default:printf("输入错误!"); break;
}
}
int main()
{
while (1)
{
menu();
}
return 0;
}