项目作业:学生成绩管理系统
实验目的:熟悉一维数组作函数参数,排序、查找、统计分析等常用算法,模块化程序设计方法。
实验题目:某班有最多不超过30人(具体人数由键盘输入)参加某门课程的考试,用一维数组作函数参数编程实现如下学生成绩管理:
(1)录入每个学生的学号和考试成绩;
(2)计算课程的总分和平均分;
(3)按成绩由高到低排出名次表;
(4)按学号由小到大排出成绩表;
(5)按学号查询学生排名及其考试成绩;
(6)按优秀(90~ 100)、良好(8089)、中等(7079)、及格(60~69)、不及格5个类别,统计每个类别的人数以及所占的百分比;
(7)输出每个学生的学号、考试成绩,课程总分和平均分。
[思考题]如果要求程序运行后先显示如下菜单,并提示用户输入选项:
建议可以自己先动手试试,遇到问题解决问题,遇到困难,解决困难!
你要相信自己可以的。
这样对后续的自己,本身是一种提高的过程!你觉得呢
自己动手写一遍,不会的知识点还可以及时补漏,以后再遇到相类似的就会知识怎么解决了
时间比较急,主要的业务逻辑就在下面,你看看!
int ReadScore(long num[], float score[],int n)
{
printf("正在录入学号和成绩...\n");
printf("同时输入两个-1结束录入\n");
int i = n-1;
do
{
i++;
printf("正在录入第%d个学生的学号和成绩\n", i + 1);
scanf_s("%ld%*c%f", &num[i], &score[i]);
} while (score[i] > 0 && i <= 30);
printf("录入完成总共录入%d人\n", i);
return i;
}
int AverSumofScore(float score[], int n)
{
float sum = 0;
for (int i = 0; i < n; i++)
{
sum += score[i];
}
printf("课程总分为%f,平均分为%f\n", sum, sum / n);
return sum;
}
void DeSortbyScore(long num[], float score[], int n)//选择排序
{
long nTemp;
float sTemp;
//选择排序,第一层循环整个数组,每次归一位
for (int i = 0; i < n-1; i++)
{
int max = i;//最大位索引值,每次初始化为乱序区第一位
//第二层循环乱序位,每次从乱序列中选择最大一位
for (int j = i+1; j < n; j++)
{
if (score[max] < score[j])
{
max = j;
}
}
//发生改变,交换
if (i != max)
{
nTemp = num[i];
num[i] = num[max];
num[max] = nTemp;
sTemp = score[i];
score[i] = score[max];
score[max] = sTemp;
}
}
}
void AsSortbyNum(long num[], float score[], int n)
{
long nTemp;
float sTemp;
//选择排序,第一层循环整个数组,每次归一位
for (int i = 0; i < n - 1; i++)
{
int min = i;//最小位索引值,每次初始化为乱序区第一位
//第二层循环乱序位,每次从乱序列中选择学号最小的一位
for (int j = i+1; j < n; j++)
{
if (num[min] > num[j])
{
min = j;
}
}
//发生改变,交换
if (i != min)
{
nTemp = num[i];
num[i] = num[min];
num[min] = nTemp;
sTemp = score[i];
score[i] = score[min];
score[min] = sTemp;
}
}
}
int SearchbyNum(long num[], long x, int n)
{
//顺序查找
for (int i = 0; i < n; i++)
{
if (x == num[i])
{
return i;
}
}
return -1;
}
void StatistAnalysis(float score[], int n)
{
int ranks[5] = {0};// 各种档次的人数,首位赋值为0,后续位补充为0
for (int i = 0; i < n; i++)//遍历整个成绩,统计数据
{
if (score[i] < 60)
{
ranks[0]++;
}
else if (score[i] < 70 && score[i] >= 60)
{
ranks[1]++;
}
else if (score[i] < 80 && score[i] >= 70)
{
ranks[2]++;
}
else if (score[i] < 90 && score[i] >= 80)
{
ranks[3]++;
}
else//90到100档
{
ranks[4]++;
}
}
//输出统计数据
printf("rank amount percent\n");
printf("0-59 %d %.0f%%\n", ranks[0], ranks[0] / (float)n * 100);
printf("60-69 %d %.0f%%\n", ranks[1], ranks[1] / (float)n * 100);
printf("70-79 %d %.0f%%\n", ranks[2], ranks[2] / (float)n * 100);
printf("80-89 %d %.0f%%\n", ranks[3], ranks[3] / (float)n * 100);
printf("90-100 %d %.0f%%\n", ranks[4], ranks[4] / (float)n * 100);
}
void PrintScore(long num[], float score[], int n)
{
printf("学号 成绩 \n");
for (int i = 0; i < n; i++)
{
printf("%d %f \n", num[i], score[i]);
}
}
可以找相关代码看看
int main()
{
int order = -1;//用户指令
/*int n;
long num[STU_NUM];
float score[STU_NUM];*/
//初始数据
int n = 5;
long num[STU_NUM] = { 10001,10002,10003,10004,10005 };
float score[STU_NUM] = { 20.0,80.0,50.0,66.0,95.0 };
Mean();//显示菜单
//使用系统
while (1)
{
scanf_s("%d", &order);
switch (order)
{
case 1://录入成绩
n = ReadScore(num, score, n);
break;
case 2://求平均分和总分
AverSumofScore(score, n);
break;
case 3://按成绩降序排名
DeSortbyScore(num, score, n);
printf("Scorted scores:\n");
PrintScore(num, score, n);
break;
case 4://按学号升序排名
AsSortbyNum(num, score, n);
printf("Scorted scores:\n");
PrintScore(num, score, n);
break;
case 5://按学号查询学生排名及其考试成绩
{//如果想在case标签初始化变量,必须加大括号{}
int rank = -1;//学生排名
int x;//被查询学号
printf("Please enter the student ID queried\n");
scanf_s("%d", &x);
DeSortbyScore(num, score, n);//先排序
rank = SearchbyNum(num, x, n);//即使排名也是所在数组的位置
printf("The student's rank is %d and his grade is %f\n", rank+1, score[rank]);//索引+1为排名
break;
}
case 6://按优秀(90- 100)、良好(80- 89)、中等(70-79)、及格(60- 69)、不及格(0-59)5个类别,
//统计每个类别的人数以及所占的百分比;
StatistAnalysis( score, n);
break;
case 7://输出每个学生的学号、考试成绩,课程总分和平均分
PrintScore(num, score, n);
AverSumofScore(score, n);
break;
case 0://退出系统
printf("Good Bye!\n");
return 0;
break;
default:
break;
}
printf("Please enter your choice : \n");
}
return 0;
}
int Mean(void)
{
printf("1.Input record\n");
printf("2.Caculate total and average score of course\n");
printf("3.Sort in descending order by score\n");
printf("4.Sort in ascending order by number\n");
printf("5.Search by number\n");
printf("6.Statistic analysis\n");
printf("7.List record\n");
printf("0.Exit\n");
printf("Please enter your choice : \n");
return 0;
}
项目头文件
/程序功能: 学生成绩管理
编码者: JackieFeng
日期: 04/11/2021
版本号:1.1
备注:
某班有最多不超过30人(具体人数由键盘输入)国实现如下学生成绩管理:用一维数组作区函数参数编程实现如下学生成绩管理:
(1)录入每个学生的学号和考试成绩;
(2)计算课程的总分和平均分;
(3)按成绩由高到低排出名次表;
(4)按学号由小到大排出成绩表;
(5)按学号查询学生排名及其考试成绩;
(6)按优秀(90- 100)、良好(80- 89)、中等(70-79)、及格(60- 69)、不及格(0-59)5个类别,统计每个类别的人数以及所占的百分比;
(7)输出每个学生的学号、考试成绩,课程总分和平均分。/
#pragma once
#define STU_NUM 30
/*
函数名称: ReadScore
功能描述: 录入每个学生的学号和成绩,并返回总人数
参数:
num[]:学号
score[]:分数
返回:总人数
备注:*/
int ReadScore(long num[], float score[], int n);
/*
函数名称: AverSumofScore
功能描述: 计算总分和平均值并输出
参数:
score[]:分数
n:总分
返回:
备注:*/
int AverSumofScore(float score[], int n);
/*
函数名称: DeSortbyScore
功能描述: 按照成绩降序排列名次表
参数:
num[]:学号
score[]:分数
n:总人数
返回:
备注:*/
void DeSortbyScore(long num[], float score[], int n);
/*
函数名称: AsSortbyNum
功能描述: 按照学号升序排列
参数:
num[]:学号
score[]:分数
n:总人数
返回:
备注:*/
void AsSortbyNum(long num[], float score[], int n);
/*
函数名称: SearchbyNum
功能描述: 按学号查询学生排名及其考试成绩
参数:
num[]:学号
x:查询学号
n:总人数
返回:索引号,-1为没找到
备注:*/
int SearchbyNum(long num[], long x, int n);
/*
函数名称: StatistAnalysis
功能描述: 按优秀(90 - 100)、良好(80 - 89)、中等(70 - 79)、及格(60 - 69)、不及格(0 - 59)5个类别,
统计每个类别的人数以及所占的百分比;
参数:
num[]:学号
score[]:分数
n:总人数
返回:
备注:*/
void StatistAnalysis( float score[], int n);
/*
函数名称: PrintScore
功能描述: 打印每位学生的成绩
参数:
num[]:学号
score[]:分数
n:总人数
返回:
备注:*/
void PrintScore(long num[], float score[],int n);
/*
函数名称: Mean
功能描述: 显示菜单页面,提示用户如何使用
参数:
返回:
备注:*/
int Mean(void);
具体实现
int ReadScore(long num[], float score[],int n)
{
printf("正在录入学号和成绩...\n");
printf("同时输入两个-1结束录入\n");
int i = n-1;
do
{
i++;
printf("正在录入第%d个学生的学号和成绩\n", i + 1);
scanf_s("%ld%*c%f", &num[i], &score[i]);
} while (score[i] > 0 && i <= 30);
printf("录入完成总共录入%d人\n", i);
return i;
}
int AverSumofScore(float score[], int n)
{
float sum = 0;
for (int i = 0; i < n; i++)
{
sum += score[i];
}
printf("课程总分为%f,平均分为%f\n", sum, sum / n);
return sum;
}
void DeSortbyScore(long num[], float score[], int n)//选择排序
{
long nTemp;
float sTemp;
//选择排序,第一层循环整个数组,每次归一位
for (int i = 0; i < n-1; i++)
{
int max = i;//最大位索引值,每次初始化为乱序区第一位
//第二层循环乱序位,每次从乱序列中选择最大一位
for (int j = i+1; j < n; j++)
{
if (score[max] < score[j])
{
max = j;
}
}
//发生改变,交换
if (i != max)
{
nTemp = num[i];
num[i] = num[max];
num[max] = nTemp;
sTemp = score[i];
score[i] = score[max];
score[max] = sTemp;
}
}
}
void AsSortbyNum(long num[], float score[], int n)
{
long nTemp;
float sTemp;
//选择排序,第一层循环整个数组,每次归一位
for (int i = 0; i < n - 1; i++)
{
int min = i;//最小位索引值,每次初始化为乱序区第一位
//第二层循环乱序位,每次从乱序列中选择学号最小的一位
for (int j = i+1; j < n; j++)
{
if (num[min] > num[j])
{
min = j;
}
}
//发生改变,交换
if (i != min)
{
nTemp = num[i];
num[i] = num[min];
num[min] = nTemp;
sTemp = score[i];
score[i] = score[min];
score[min] = sTemp;
}
}
}
int SearchbyNum(long num[], long x, int n)
{
//顺序查找
for (int i = 0; i < n; i++)
{
if (x == num[i])
{
return i;
}
}
return -1;
}
void StatistAnalysis(float score[], int n)
{
int ranks[5] = {0};// 各种档次的人数,首位赋值为0,后续位补充为0
for (int i = 0; i < n; i++)//遍历整个成绩,统计数据
{
if (score[i] < 60)
{
ranks[0]++;
}
else if (score[i] < 70 && score[i] >= 60)
{
ranks[1]++;
}
else if (score[i] < 80 && score[i] >= 70)
{
ranks[2]++;
}
else if (score[i] < 90 && score[i] >= 80)
{
ranks[3]++;
}
else//90到100档
{
ranks[4]++;
}
}
//输出统计数据
printf("rank amount percent\n");
printf("0-59 %d %.0f%%\n", ranks[0], ranks[0] / (float)n * 100);
printf("60-69 %d %.0f%%\n", ranks[1], ranks[1] / (float)n * 100);
printf("70-79 %d %.0f%%\n", ranks[2], ranks[2] / (float)n * 100);
printf("80-89 %d %.0f%%\n", ranks[3], ranks[3] / (float)n * 100);
printf("90-100 %d %.0f%%\n", ranks[4], ranks[4] / (float)n * 100);
}
void PrintScore(long num[], float score[], int n)
{
printf("学号 成绩 \n");
for (int i = 0; i < n; i++)
{
printf("%d %f \n", num[i], score[i]);
}
}
可用结构体做下
9 #include <stdio.h>
10 #include<string.h>
11 #include<stdlib.h>
12 #define N 50
13 struct Student{
14 char name[20];
15 int sno;
16 int age;
17 char gender;
18 float EnglishScore;
19 float mathScore;
20 float chineseScore;
21 }stu[N]={{"zhao",1000,20,'M',98,99,97},
22 {"zhao",1001,21,'F',97,96,95},
23 {"qian",1002,23,'M',95,96,92},
24 {"hong",1003,22,'F',91,93,97},
25 {"zhou",1004,25,'M',90,90,90},
26 {"feng",1005,21,'F',96,93,96},
27 {"wang",1006,23,'M',97,91,90},
28 {"chen",1007,21,'F',94,92,91},
29 {"geng",1008,20,'F',90,99,91},
30 {"tang",1009,24,'M',99,99,99}};
31
32 //添加数据
33 void add(struct Student stu[],int pos,int nums);
34
35
36 //删除数据
37 void Delete_name(struct Student *stu,char Name[]);//(按姓名)
38 void Delete_sno(struct Student *stu,int Sno);//(按学号)
39
40
41 //修改数据
42 void update(struct Student stu[],int Sno,int len);
43
44
45 //查询子菜单
46 void menu(struct Student stu[],int m);
47 void search(struct Student stu[],int n,char Name[]);//按姓名查询
48 void search1(struct Student stu[],int n,int Num);//按学号查询
49 void sort(struct Student stu[],int n); //按总分高低排序
50 void print(struct Student stu[],int n);
51 void My_print_sum(struct Student stu[],int n);
52 void max_min(struct Student stu[],int n);
53
54
55 //主菜单
56 void PrintScreen()
57 {
58 printf("------------------\n");
59 printf("** 1.增加学生记录 **\n");
60 printf("** 2.删除学生记录 **\n");
61 printf("** 3.查找学生记录 **\n");
62 printf("** 4.修改学生记录 **\n");
63 printf("** 0.退出管理系统 **\n");
64 printf("------------------\n");
65 }
66
67 //删除子菜单
68 void deleteScreen()
69 {
70 printf("------------------\n");
71 printf("** 0.按姓名删除数据**\n");
72 printf("** 1.按学号删除数据**\n");
73 printf("** 2.返回主菜单 **\n");
74 printf("------------------\n");
75 printf("please select 0-1:");
76 }
77
78 //查找子菜单
79 void seekScreen()
80 {
81 printf("-------------------------\n");
82 printf("****** 1.按姓名查找信息 *\n");
83 printf("****** 2.按学号查找信息 *\n");
84 printf("****** 3.查看所有学生成绩 *\n");
85 printf("****** 4.成绩名次排序(总分)*\n");
86 printf("****** 5.查看成绩最优最差 *\n");
87 printf("****** 6.返回主菜单 *\n");
88 printf("-------------------------\n");
89 printf("please you select 1-6:");
90 }
91
92 //定义全局静态变量,统计目前结构体数组中的人数
93 static int count = 10;
94
95
96
97
98 //主函数
99 int main(int argc, const char * argv[])
100 {
101 int nums,temp;
102 int snos;
103 char names[20];
104 while(1)
105 {
106 PrintScreen();
107 printf("please press enter_key continue!\n");
108 getchar();
109 printf("please select 0-4:");
110 char c = getchar();
111 switch(c)
112 {
113 case '1':
114 printf("please student'numbers you want to add:");
115 scanf("%d",&nums);
116 add(stu,count,nums); //添加数据
117 getchar();
118 break;
119 case '2':
120 deleteScreen();
121 scanf("%d",&temp);
122 switch(temp)
123 {
124 case 0:
125 printf("please input delete name:");
126 scanf("%s",names);;
127 Delete_name(stu,names); //删除数据(按姓名删除)
128 break;
129 case 1:
130 printf("please input delete sno:");
131 scanf("%d",&snos);
132 Delete_sno(stu,snos); //删除数据(按学号删除)
133 break;
134 case 2:
135 break;
136 }
137 getchar();
138 break;
139 case '3':
140 menu(stu,count);//查找数据
141 getchar();
142 break;
143 case '4':
144 printf("please input update sno:");
145 scanf("%d",&snos);
146 update(stu,snos,count); //修改数据
147 break;
148 case '0':
149 exit(0); //退出系统
150 default:
151 printf("data is illeagel!\n");
152 getchar();
153 break; //输入非法
154 }
155 }
156 return 0;
157 }
158
159
160
161 //添加数据
162 void add(struct Student stu[],int pos,int nums)//开始位置、添加人数
163 {
164 for(int i=pos;i<pos+nums;i++)
165 {
166 printf("please input name:");
167 scanf("%s",stu[i].name);
168 int flag = 1,sno;
169 while(flag)
170 {
171 printf("please input sno:");
172 scanf("%d",&sno);
173 for(int j=0;j<pos;j++)
174 {
175 if(stu[j].sno==sno)
176 {
177 printf("sno is exist!\n");
178 break;
179 }
180 else
181 {
182 flag = 0;
183 }
184 }
185 stu[i].sno = sno;
186 }
187 printf("please input age:");
188 scanf("%d",&stu[i].age);
189 printf("please input gender:(f/m or F/M)");
190 getchar();
191 scanf("%c",&stu[i].gender);
192 printf("please input EnglishScore:");
193 scanf("%f",&stu[i].EnglishScore);
194 printf("please input mathScore:");
195 scanf("%f",&stu[i].mathScore);
196 printf("please input ChineseScore:");
197 scanf("%f",&stu[i].chineseScore);
198 printf("学生信息添加成功!\n");
199 }
200 count = count + nums;
201 }
202
203 //按学号删除
204 void Delete_sno(struct Student *stu,int Sno)
205 {
206 if(count==0)
207 {
208 printf("成员已为空!\n");
209 return;
210 }
211 int flag=0;
212 for(int i=0;i<count;i++)
213 {
214 if((stu+i)->sno==Sno)
215 {
216 for(int j=i;j<count;j++)
217 {
218 *(stu+j)=*(stu+j+1);
219 }
220 printf("\n");
221 printf("学号为%d的学生已被删除\n",Sno);
222 printf("\n");
223 flag=1;
224 count = count-1;
225 break;
226 }
227 }
228 if(flag==0)
229 printf("学号%d不存在.\n",Sno);
230 }
231
232 //按姓名删除
233 void Delete_name(struct Student *stu,char Name[])
234 {
235 if(count==0)
236 {
237 printf("成员已为空!\n");
238 return;
239 }
240 int flag=0;
241 int n=count;
242 for(int i=0;i<n;i++)
243 {
244 if(strcmp((stu+i-flag)->name,Name)==0)
245 {
246 for(int j=i-flag;j<count;j++)
247 {
248 *(stu+j)=*(stu+j+1);
249 }
250 printf("\n");
251 printf("学生:%s 已被删除.\n",Name);
252 printf("\n");
253 flag=flag+1;
254 count =count -1;
255 }
256 }
257 if(flag==0)
258 printf("学生:%s 不存在.\n",Name);
259 }
260
261 //修改数据
262 void update(struct Student stu[],int sno,int len)
263 {
264 char cs,cs1,cs2,cs3;
265 char p[20];
266 int grade,i;
267 for(i=0; i<len; i++)
268 {
269 if(sno==stu[i].sno)
270 {
271 printf("please ask update name?(y/n):");
272 getchar();
273 cs = getchar();
274 if(cs == 'y')
275 {
276 printf("please input a new name:");
277 scanf("%s",p);
278 getchar();
279 strcpy(stu[i].name,p);
280 printf("update is succeed!\n");
281 }
282
283 printf("please ask update EnglishScore?(y/n):");
284 cs1 = getchar();
285 if(cs1 == 'y')
286 {
287 printf("please input a new grade:");
288 scanf("%d",&grade);
289 stu[i].EnglishScore = grade;
290 printf("update is succeed!\n");
291 }
292
293 printf("please ask update mathScore?(y/n):");
294 getchar();
295 cs2 = getchar();
296 if(cs2 == 'y')
297 {
298 printf("please input a new grade:");
299 scanf("%d",&grade);
300 stu[i].mathScore = grade;
301 printf("update is succeed!\n");
302 }
303
304 printf("please ask update chineseScore?(y/n):");
305 getchar();
306 cs3 = getchar();
307 if(cs3 == 'y')
308 {
309 printf("please input a new grade:");
310 scanf("%d",&grade);
311 stu[i].chineseScore = grade;
312 printf("update is succeed!\n");
313 break;
314 }
315 else
316 {
317 getchar();
318 break;
319 }
320 }
321 else if(i==len-1)
322 {
323 printf("error,don't have the sno!");
324 break;
325 }
326 else
327 {
328 continue;
329 }
330 }
331 }
332
333 //查询数据
334 void menu(struct Student stu[],int m)
335 {
336 int logel = 1;
337 while(logel)
338 {
339 seekScreen();
340 int n;
341 scanf("%d",&n);
342 char name[20];
343 int num;
344 switch(n)
345 {
346 case 1 :
347 printf("请输入同学的姓名: ");
348 scanf("%s",name);
349 search(stu,count,name);
350 break;
351 case 2 :
352 printf("\n请输入同学的学号: ");
353 scanf("%d",&num);
354 search1(stu,count,num);
355 break;
356 case 3:
357 print(stu,count);
358 break;
359 case 4:
360 sort(stu,count);
361 print(stu,count);
362 break;
363 case 5:
364 max_min(stu,count);
365 break;
366 case 6:
367 logel = 0;
368 break;
369 default:
370 logel = 0;
371 printf("输入数字有误!\n");
372 break;
373 }
374 }
375 }
376 void search(struct Student stu[],int n,char Name[])
377 {
378 char* p= Name;
379 int flag = 0;
380 for(int i=0;i<n;i++)
381 {
382 if(strcmp(stu[i].name,p)==0)
383 {
384 flag = 1;
385 My_print_sum(stu,i);
386 }
387 }
388 if(flag==0)
389 {
390 printf("the name is not exist!\n");
391 }
392 }
393 void search1(struct Student stu[],int n,int Num)
394 {
395 int flag = 0;
396 for(int i=0;i<n;i++)
397 {
398 if(Num==stu[i].sno)
399 {
400 flag = 1;
401 My_print_sum(stu,i);
402 }
403 }
404 if(flag==0)
405 {
406 printf("the sno is not exist!\n");
407 }
408 }
409 void sort(struct Student stu[],int n)
410 {
411 float sum[N];
412 for(int i=0;i<n;i++)
413 {
414 sum[i]=stu[i].EnglishScore+stu[i].mathScore+stu[i].chineseScore;
415 }
416 for(int i=0;i<n-1;i++)
417 {
418 for(int j=0;j<n-1-i;j++)
419 {
420 if(sum[j]<sum[j+1])
421 {
422 float temp1 = sum[j];
423 sum[j] = sum[j+1];
424 sum[j+1] = temp1;
425
426 struct Student temp;
427 temp=stu[j];
428 stu[j]=stu[j+1];
429 stu[j+1]=temp;
430 }
431 }
432 }
433 }
434 void max_min(struct Student stu[],int n)
435 {
436 sort(stu,n);
437
438 printf("成绩最优的同学:");
439 My_print_sum(stu,0);
440
441 printf("成绩最差的同学:");
442 My_print_sum(stu,n-1);
443 }
444
445 void print(struct Student stu[],int n)
446 {
447 for(int i=0;i<n;i++)
448 {
449 My_print_sum(stu,i);
450 }
451 }
452
453 void My_print_sum(struct Student stu[],int n)
454 {
455 printf("\n姓名:%s,学号:%d,年龄:%d,性别:%c,英语:%.2f,数学:%.2f,语文: %.2f,总分:%.2f\n",
456 stu[n].name,stu[n].sno,stu[n].age,stu[n].gender,
457 stu[n].EnglishScore,stu[n].mathScore,stu[n].chineseScore,(stu[n].EnglishScore+stu[n].mathScore+stu[n].chineseScore));
458 }
这个我以前做过,有点复杂,不好说明,发给你看看
有例子,也没人联系。。。
采纳我
这个问答类似的看过不下十次
我有现成的
有现成的可惜不是C++语言的
找一个类似的案例自己动手改改
一个需求写一个函数,这个不难的
最简单的C预言,完全可以解决新手阶段的学生成绩管理系统问题 C语言必备项目,自带登陆系统界面的学生信息管理系统!_藏宝大厦-CSDN博客_c语言管理系统登录界面
#include
#include
#include
#include<stdlib.h>//用到system()
#include
using namespace std;
const short MAX_SIZE = 50;
class Student {
//学生类,完成个人信息的输出和返回
/*
SetMath();//输入数学成绩
SetEnglish();//输入英语成绩
SetComputer();//输入电脑成绩
SetName();//输入姓名
GetSum();//总分
Match();//输入字符串信息与名字或学号是否匹配
Print();//显示该学生信息
*/
public:
Student() {};//分号加与不加均可通过编译
Student(char *n) {
//待实参传入时,形参就会指向实参的地址,不再是野指针
strcpy_s(name, n);
}
Student(char *n, char *i) {
strcpy_s(name, n);
strcpy_s(id, i);
}
void SetMath(float m) {
math = m;
}
void SetEnglish(float e) {
english = e;
}
void SetComputer(float c) {
computer = c;
}
void SetName(char *n) {
strcpy_s(name, n);
}
string GetName() {
return name;
}
string GetId() {
return id;
}
float GetSum() {
return math + english + computer;
}
void SetAllScore(float m, float e, float c) {
math = m;
english = e;
computer = c;
}
bool Match(char *str, short flag) {
//flag为1,与姓名匹配;flag为2,与学号匹配
return flag == 1 ? strcmp(name, str) == 0 : strcmp(id, str) == 0;
}
float GetMath() {
return math;
}
float GetEnglish() {
return english;
}
float GetComputer() {
return computer;
}
void Print() {
cout << name << '\t' << id << endl;
cout << "MathScore:" << setprecision(1) << setiosflags(ios::fixed) << math << '\t' << "EnglishScore:" << english << '\t' << "ComputerScore:" << computer << endl;
}
private:
char name[12];
char id[10];
float math;
float english;
float computer;
};
bool Compare1(Student stu1, Student stu2) {
return stu1.GetId() < stu2.GetId();
}
bool Compare2(Student stu1, Student stu2) {
return stu1.GetSum() > stu2.GetSum();
}
class DataBase {
//数据采集类,将学生类的数组作为一个成员,封装对数组添加、插入、查找、删除元素等操作
/*
Push();//添加
Search();//查找
Alter();//修改
Delete();//删除
Display();//显示所有学生信息
ShowMenu();//显示功能菜单
*/
public:
DataBase() {
size = 0;
}
bool Push(char *n,char *i,float m,float e,float c) {
if (size == MAX_SIZE) {
return false;
}
Student st(n, i);
st.SetAllScore(m, e, c);
stu[size++] = st;
return true;
}
bool Push() {
if (size == MAX_SIZE) {
cout << "系统不能容纳更多学生。" << endl;
system("pause");
return false;
}
char n[12], i[10];
cout << "Please input name:";
cin >> n;
int idx;
do {
cout << "Please input id:";
cin >> i;
for (idx = 0; idx < size; idx++) {
if (stu[idx].Match(i, 2)) {
cout << "该学号已存在,不能重复输入。" << endl;
break;
}
}
} while (idx < size);
Student stu_tmp(n, i);
float m, e, c;
cout << "Please input math score:";
cin >> m;
cout << "Please input english score:";
cin >> e;
cout << "Please input computer score:";
cin >> c;
stu_tmp.SetAllScore(m, e, c);
stu[size] = stu_tmp;
size++;
cout << "添加成功!" << endl;
system("pause");
return true;
}
short AimedSearch(short start_id, char *str, short flag) {
//start_id为起始下标
for (short i = start_id; i < size; i++) {
if (stu[i].Match(str, flag)) {
stu[i].Print();
return i;
}
}
return -1;//未找到
}
short Search() {
//实现查找,提供输入接口
short choice;
do {
cout << "请问按什么条件搜索?1、姓名 2、学号" << endl;
cin >> choice;
} while (choice != 1 && choice != 2);
char match[12];
cout << "请输入你要找的" << (choice == 1 ? "姓名:" : "学号:");
cin >> match;
short result = 0;
char nod;
while (true) {
//若数组中有重复姓名,且先出现的不是自己想找的那个学生
result = AimedSearch(result, match, choice);
if (choice == -1) {
cout << "未找到匹配信息。" << endl;
system("pause");
return result;
}
cout << "这是你要找的人吗?(y/n)";
cin >> nod;
if (nod == 'y' || nod == 'Y')
return result;
else
result++;
}
}
bool Delete() {
short result = 0;
result = Search();
char nod;
short idx;
if (result == -1)
return false;
cout << "是否要进行删除?(y/n)" << endl;
cin >> nod;
if (nod == 'y' || nod == 'Y') {
for (idx = result; idx < size; idx++) {
stu[idx] = stu[idx + 1];
}
size--;
cout << "删除成功!" << endl;
system("pause");
return true;
}
else {
cout << "删除失败!" << endl;
system("pause");
return false;
}
}
bool Alter() {
short result = 0;
result = Search();
char nod;//选择是否修改
char choice;//选择要修改什么
char newname[12];
float newscore;
if (result == -1) {
cout << "未找到想要修改的元素。" << endl;
system("pause");
return false;
}
cout << "是否要进行修改?(y/n)" << endl;
cin >> nod;
if (nod == 'Y' || nod == 'y') {
cout << "修改什么?1、Name 2、MathScore 3、EnglishScore 4、ComputerScore" << endl;
cin >> choice;
switch (choice) {
case '1':
cout << "Please input a new name:";
cin >> newname;
stu[result].SetName(newname);
cout << "修改成功!" << endl;
system("pause");
break;
case '2':
cout << "Please input a new mathscore:";
cin >> newscore;
stu[result].SetMath(newscore);
cout << "修改成功!" << endl;
system("pause");
break;
case '3':
cout << "Please input a new englishscore:";
cin >> newscore;
stu[result].SetEnglish(newscore);
cout << "修改成功!" << endl;
system("pause");
break;
case '4':
cout << "Please input a new computerscore:";
cin >> newscore;
stu[result].SetComputer(newscore);
cout << "修改成功!" << endl;
system("pause");
break;
default:
break;
}
return true;
}
cout << "未修改!" << endl;
system("pause");
return false;
}
void Display() {
cout << endl << setw(12) << setiosflags(ios::left) << "姓名" << setw(12) << "学号" << setw(8) << "数学" << setw(8) << "英语" << setw(8) << "计算机" << endl;
cout << endl;
cout << setprecision(1) << setiosflags(ios::fixed);
for (int i = 0; i < size; i++) {
cout << setw(12) << stu[i].GetName() << setw(12) << stu[i].GetId() << setw(8) << stu[i].GetMath() << setw(8) << stu[i].GetEnglish() << setw(8) << stu[i].GetComputer() << endl;
}
cout << resetiosflags(ios::left);
system("pause");
}
void Sort() {
char choice;
do {
cout << "请选择排序方式:1、学号升序 2、总成绩降序";
cin >> choice;
} while (choice != '1'&&choice != '2');
if (choice == '1') {
sort(&stu[0], &stu[0] + size, Compare1);
}
else {
sort(&stu[0], &stu[0] + size, Compare2);
}
cout << "排序完成!" << endl;
system("pause");
}
char ShowMenu() {
char choice;
do {
system("cls");
cout << " --------欢迎使用学生成绩管理系统-------- " << endl;
cout << endl;
cout << " 1、添加学生" << endl;
cout << " 2、查找学生" << endl;
cout << " 3、删除学生" << endl;
cout << " 4、修改学生" << endl;
cout << " 5、重新排序" << endl;
cout << " 6、显示全部" << endl;
cout << " 7、退出" << endl;
cin >> choice;
} while (choice < '1' && choice>'7');
return choice;
}
private:
Student stu[MAX_SIZE];
short size;
};
int main() {
DataBase db;
bool quit = false;
char choice;
db.Push("张三", "00006", 70, 72, 76);
db.Push("李四", "00003", 62, 60, 70);
db.Push("王五", "00001", 61.5, 74, 68.5);
while (!quit) {
choice = db.ShowMenu();
switch (choice) {
case '1':
db.Push();
break;
case '2':
db.Search();
break;
case '3':
db.Delete();
break;
case '4':
db.Alter();
break;
case '5':
db.Sort();
break;
case '6':
db.Display();
break;
case '7':
quit = true;
break;
}
}
return 0;
}
管理系统Demo,我有现成的。
直接采纳,再联系