班级程序管理系统 还是有很多问题 感谢指点
摸索着自学了一段时间写了个程序,遇到了很多问题,调试 输入 输出,再调试......总算体会到程序猿的痛苦。特别是cin的输入判断问题,我搜索了很久没有满意的答案,最后自己考虑用数组的形式来存储 判断。
程序还不完善,感谢各位大神查看,指点。
程序代码:
#include<iostream> using namespace std; struct zh { char name[20]; int yw; int sx; int yy; int sw; int hx; }xs[100], px[100], *pp; int I = 0; char caidan();//菜单模块 void cjlr();//成绩录入模块 void cjxs();//成绩显示模块 void cjbc();//成绩保存模块 void cjpx();//成绩排序模块 void cjxg();//成绩修改模块 void cjtj();;//成绩统计模块 int srpd();//输入判断模块 返回输入值 int main() { top:char xz; xz = caidan(); switch (xz) { case 'a':cjlr(); goto top; case 'b':cjxs(); goto top; case 'c':cjbc(); goto top; case 'd':cjpx(); goto top; case 'e':cjxg(); goto top; case 'f':cjtj(); goto top; case 'g':break; default: cout << "错误";; break; } } char caidan() { char a[50]; system("cls"); cout << endl; cout << "\t\t\t班级成绩管理系统" << endl; cout << "\t请选择系统功能项:(输入前面的字母)" << endl << endl; cout << "a.成绩录入" << endl; cout << "b.成绩显示" << endl; cout << "c.成绩保存" << endl; cout << "d.成绩排序" << endl; cout << "e.成绩修改" << endl; cout << "f.成绩统计" << endl; cout << "g.退出系统" << endl << endl; cout << "请输入您的选择:"; cin >> a; while (1) //采用字符组判断输入的是否正确 不重复 不带多个字符 { if (a[0] >= 'A'&& a[0] <= 'G') { a[0] += 32; } if (a[0] < 'a' || a[0]>'g' || a[1] != '\0') { cout << "输入错误,请重新输入:"; cin >> a; } else { break; } } return (a[0]); } void cjlr() { top2:char a[20]; system("cls"); cout << endl; cout << "\t\t\t班级成绩管理系统" << endl; cout << "\t成绩录入模块" << endl << endl; cout << "请输入姓名:"; cin >> xs[I].name; cout << "请输入语文成绩:"; xs[I].yw = srpd(); cout << "请输入数学成绩:"; xs[I].sx = srpd(); cout << "请输入英语成绩:"; xs[I].yy = srpd(); cout << "请输入生物成绩:"; xs[I].sw = srpd(); cout << "请输入化学成绩:"; xs[I].hx = srpd(); cout << "是否继续输入下个学生(Y/N):"; cin >> a; while (1) //采用字符组判断输入的是否正确 不重复 不带多个字符 { if (a[0] == 'Y' || a[0] == 'N') { a[0] += 32; } if (a[0] != 'y' && a[0] != 'n') { cout << "输入错误,请重新输入:"; cin >> a; continue; } if (a[1] != '\0') { cout << "输入错误,请重新输入:"; cin >> a; } else if (a[0] == 'y') { I++; goto top2; } else I++; break; } } void cjxs() { int i = 0; system("cls"); cout << endl; cout << "\t\t\t班级成绩管理系统" << endl; cout << "\t成绩显示模块" << endl << endl; cout << "序号\t" << "姓名\t" << "语文\t" << "数学\t" << "英语\t" << "生物\t" << "化学\t" << endl; //for (int i = 0; i < length; i++) while (xs[i].name[0] != '\0') { cout << i << "\t" << xs[i].name << "\t" << xs[i].yw << "\t" << xs[i].sx << "\t" << xs[i].yy << "\t" << xs[i].sw << "\t" << xs[i].hx << "\t" << endl; i++; } system("pause"); } void cjbc() { FILE *fp; errno_t err; int i = 0; pp = xs; system("cls"); cout << endl; cout << "\t\t\t班级成绩管理系统" << endl; cout << "\t成绩显示模块" << endl << endl; if ((err = fopen_s(&fp, "学生成绩.txt", "wt+")) != 0) cout << "无法打开此文件"; //如果打不开,就输出打不开 while (xs[i].name[i] != '\0') { fwrite(&xs[i], sizeof(struct zh), i, fp); i++; } cout << "保存成功" << endl; fclose(fp); //关闭文件 system("pause"); } void cjpx() { system("cls"); cout << endl; cout << "\t\t\t班级成绩管理系统" << endl; cout << "\t成绩排序模块" << endl << endl; int i = 0, a, b, c, j = 0; while (xs[i].name[0] != '\0') { px[i].yw = xs[i].yw; px[i].sx = xs[i].sx; px[i].yy = xs[i].yy; px[i].sw = xs[i].sw; px[i].hx = xs[i].hx; i++; } for (a = 0; a < i - 1; a++) { for (b = 0; b < i - 1 - a; b++) { if (px[b].yw > px[b + 1].yw) { c = px[b].yw; px[b].yw = px[b + 1].yw; px[b + 1].yw = c; } if (px[b].sx > px[b + 1].sx) { c = px[b].sx; px[b].sx = px[b + 1].sx; px[b + 1].sx = c; } if (px[b].yy > px[b + 1].yy) { c = px[b].yy; px[b].yy = px[b + 1].yy; px[b + 1].yy = c; } if (px[b].sw > px[b + 1].sw) { c = px[b].sw; px[b].sw = px[b + 1].sw; px[b + 1].sw = c; } if (px[b].hx > px[b + 1].hx) { c = px[b].hx; px[b].hx = px[b + 1].hx; px[b + 1].hx = c; } } } cout << "序号\t" << "语文\t" << "数学\t" << "英语\t" << "生物\t" << "化学\t" << endl; while (j < i) { cout << j << "\t" << px[j].yw << "\t" << px[j].sx << "\t" << px[j].yy << "\t" << px[j].sw << "\t" << px[j].hx << "\t" << endl; j++; } system("pause"); } void cjxg() { top3:char a[20], b[20]; int i, j = 0, k, pd; system("cls"); cout << endl; cout << "\t\t\t班级成绩管理系统" << endl; cout << "\t成绩修改模块" << endl << endl; cout << "请输入所需修改学生的名字:"; cin >> a; while (a[j] != '\0') { j++; } for (i = 0; i <= I; i++) { pd = 0; for (k = 0; k <= j; k++) { if (xs[i].name[k] == a[k]) pd = 1; else { pd = 0; break; } } if (pd) { break; } } if (pd) { cout << "请修改语文成绩:"; xs[i].yw = srpd(); cout << "请修改数学成绩:"; xs[i].sx = srpd(); cout << "请修改英语成绩:"; xs[i].yy = srpd(); cout << "请修改生物成绩:"; xs[i].sw = srpd(); cout << "请修改化学成绩:"; xs[i].hx = srpd(); } else { cout << "查无此人" << endl; } cout << "是否继续输入下个学生(Y/N):"; cin >> b; while (1) //采用字符组判断输入的是否正确 不重复 不带多个字符 { if (b[0] == 'Y' || b[0] == 'N') { b[0] += 32; } if (b[0] != 'y' && b[0] != 'n') { cout << "输入错误,请重新输入:"; cin >> b; continue; } if (b[1] != '\0') { cout << "输入错误,请重新输入:"; cin >> b; } else if (b[0] == 'y') { I++; goto top3; } else break; } } void cjtj() { double b[5] = {0}; int i; system("cls"); cout << endl; cout << "\t\t\t班级成绩管理系统" << endl; cout << "\t成绩统计模块" << endl << endl; cout << "\t" << " 语文\t" << " 数学\t" << " 英语\t" << " 生物\t" << " 化学\t" << endl; for ( i = 0; i <=I; i++) { b[0] += xs[i].yw; } for (i = 0; i <= I; i++) { b[1] += xs[i].sx; } for (i = 0; i <= I; i++) { b[2] += xs[i].yy; } for (i = 0; i <= I; i++) { b[3] += xs[i].sw; } for (i = 0; i <= I; i++) { b[4] += xs[i].hx; } b[0] /= I; b[1] /= I; b[2] /= I; b[3] /= I; b[4] /= I; cout <<"平均成绩:\t" << b[0] << "\t" << b[1] << "\t" << b[2] << "\t" << b[3] << "\t" << b[4] << "\t"<<endl; system("pause"); } int srpd() { char a[20]; int b = -1, i = 1; cin >> a; while (1) { if (a[1] != '\0' && a[2] != '\0'&&a[3] != '\0') { cout << "输入错误,请重新输入:"; cin >> a; continue; } while (a[i] != '\0') { i++; } switch (i) { case 1: if (a[0]<'0' || a[0]>'9') { break; } b = int(a[0]) - 48; break; case 2: if (a[0]<'0' || a[0]>'9') { break; } else if (a[1]<'0' || a[1]>'9') { break; } b = (int(a[0]) - 48) * 10; b = b + (int(a[1]) - 48); break; case 3: if (a[0]<'0' || a[0]>'9') { break; } else if (a[1]<'0' || a[1]>'9') { break; } else if (a[2]<'0' || a[2]>'9') { break; } b = (int(a[0]) - 48) * 100; b = b + (int(a[1]) - 48) * 10; b = b + (int(a[2]) - 48); break; default: cout << "致命错误:" << endl; break; } if (b >= 0 && b <= 100) break; else { cout << "输入错误,请重新输入:"; cin >> a; i = 1, b = -1; } } return b; }