作業2--實訓

還是找的最簡單的做,但是最簡單的都沒有做好!
調試沒有成功,第一步打開都出錯了。但是不想做了,做了許久。理論上可以這樣的啊!!!!!!

/*課題名稱:學生成績管理系統的設計
說明:成績記錄以下信息:班級,學號,姓名,課程名,成績(百分制)。可以用能表示學生成績的結構體數組存儲數據。
要求:
1)  具備對成績的管理功能(添加、刪除、排序)
2)  具備對成績的統計功能(最高分,最低分,平均分,及格率等)
3)  具備按學號、姓名查詢成績的功能。
*/
#include<iostream>
#include<cmath>
#include<string>
#include<fstream>
using namespace std;
struct position {//與存儲有關
    int  dong = 0;//一個規定的值,用來判斷輸入的,動的是那個值
    int all = 0;//所有的值
};
struct student
{
    bool flag=0;//存的值,判斷這個成員的存在。若爲0則不存在
    char classroom[10];//班級
    char xuehao[13];//學號
    char name[6];//名字
    char classA[10];//課程A
    float gradeA;//成績A
    char classB[10];
    float gradeB;//B

    float Allgrade;//總分
};
student shuruxinxi(student A,position add) {
    cout << "請按照順序輸入。班級,學號,名字,課程A,課程A成績,課程B,課程B成績,···" << endl;
    cin >> A.classroom;
    cin >> A.xuehao;
    cin >> A.name;
    cin >> A.classA;
    cin >> A.gradeA;
    cin >> A.classA;
    cin >> A.gradeA;
    A.flag = 1;
    A.Allgrade = A.gradeA + A.gradeB;
    add.dong++;
    add.all++;
    system("cls");
    return A;
}
void sort(student *stu,position add,char C[10]=0) 
{
    if (stu[0].classA == C) {
        student temp;
        for (int i = 0; i < add.all; i++)
        {
            for (int j = i+1; j < add.all; j++)
            {
                if (stu[i].gradeA <stu[j].gradeA) {
                    temp=stu[i];
                    stu[i]= stu[j];
                    stu[j] = stu[i];
                }
            }
        }
    }
    else if(stu[0].classB==C)
    {
        student temp;
        for (int i = 0; i < add.all; i++)
        {
            for (int j = i + 1; j < add.all; j++)
            {
                if (stu[i].gradeB <stu[j].gradeB) {
                    temp = stu[i];
                    stu[i] = stu[j];
                    stu[j] = stu[i];
                }
            }
        }
    }
    else {
        student temp;
        for (int i = 0; i < add.all; i++)
        {
            for (int j = i + 1; j < add.all; j++)
            {
                if (stu[i].Allgrade <stu[j].Allgrade) {
                    temp = stu[i];
                    stu[i] = stu[j];
                    stu[j] = stu[i];
                }
            }
        }
    }
}
void display(student A,int i)
{
    cout << i << "\t\t";
    cout << A.classroom << "\t\t";
    cout << A.xuehao << "\t\t";
    cout << A.name<< "\t\t";
    cout << A.classA<< "\t\t";
    cout << A.gradeA << "\t\t";
    cout << A.classB << "\t\t";
    cout << A.gradeB << "\t\t";
}
void display_demo()
{
    cout << "序號\t\t"<<"班級\t\t" << "學號\t\t" << "名字\t\t" << "課程A\t\t" << "A成績\t\t"<<"課程B\t\t"<<"B成績\t\t"<<"總分" << endl;
}
void display_operate()
{
    cout << "0.退出操作" << endl;
    cout << "1.添加學生" << endl;
    cout << "2.刪除學生" << endl;
    cout << "3.學生排序" << endl;
    cout << "4.統計學生" << endl;
    cout << "5.查詢學生" << endl;
}
void display_tongji()
{
    cout << "1.最高分" << endl;
    cout << "2.最低分" << endl;
    cout << "3.平均分" << endl;
    cout << "4.及格率" << endl;
}
void display_denghao()
{
    cout << "=================================================================================================================================" << endl;
}
int  query(student A,char xuehao[13] = 0, char name[6] = 0) {
    if (strcmp(A.xuehao,xuehao)==0){
        display_denghao();
        display_demo();
        display(A, 0);
        return 1;
    }
    if (strcmp(A.name, name) == 0) {
        display_denghao();
        display_demo();
        display(A, 0);
        return 1;
    }
    return 0;
}
int main()
{
    position add;
    int xuhao;//////////////////////del 
    char tempclassname[10];
    student studA;
    student studB;
    int choose=0;
    student *studpoint=&studB;
    fstream iofile ("stud.dat",ios::in | ios::out | ios::binary);
    if(!iofile)
    {
        cerr <<"open error!" << endl;
        system("pause");
        abort();
    }
    int i=1;
    char panduan[4];
    cout << "Follow the prompts to enter the information below" << endl;
add:while (i)
    {
        shuruxinxi(studA,add);
        iofile.write((char *)&studA, sizeof(studA));
        cout << "是否繼續輸入Y/N" << endl;
        cin >> panduan;
        if (panduan[0]== 'N' || panduan[0] == 'n')i = 0;
    }
    studB = studA;
    cout << "輸出完成,下表是你的輸入的信息" << endl;
    display_demo();
    for (int j = 0; j < add.all; j++)
    {
        iofile.seekg(j * sizeof(studB), ios::beg);
        iofile.read((char *)&studB, sizeof(studB));
        display(studB,j);
    }
choose: cout << "請選擇你需要的操作" << endl;
    display_operate();
    cin >> choose;
    if (choose == 0)goto end;
    if (choose == 1)goto add;
    if (choose == 2)goto del;
    if (choose == 3)goto sort;
    if (choose == 4)goto tongji;
    if (choose == 5)goto query;
    goto end;
del: {
    cout << "下表學生信息,你刪除那個學生數據。輸入序號" << endl;
    display_denghao();
    display_demo();
    for (int j = 0; j < add.all; j++)
    {
        iofile.read((char *)&studA, sizeof(studA));
        display(studA, j);
    }
    display_denghao();
    cin >> xuhao;
    while (studB.flag) {
        iofile.seekg((xuhao) * sizeof(studA), ios::beg);
        iofile.read((char*)&studA, sizeof(studA));
        iofile.seekg((xuhao + 1) * sizeof(studB), ios::beg);
        iofile.read((char*)&studB, sizeof(studB));
        if (studB.flag) {

            iofile.seekp(xuhao * sizeof(studA), ios::beg);
            iofile.write((char*)&studB, sizeof(studB));
        }
        else {
            studA.flag = 0;
            iofile.seekp(xuhao * sizeof(studA), ios::beg);
            iofile.write((char*)&studA, sizeof(studB));
        }
        //studB.flag = 0;
        //iofile.seekp((xuhao + 1) * sizeof(studA), ios::beg);
        //iofile.write((char*)&studB, sizeof(studB));

        xuhao++;
    }
    add.all--;
    cout << "刪除後的數據" << endl;
    display_denghao();
    display_demo();
    for (int j = 0; j < add.all; j++)
    {
        iofile.read((char *)&studB, sizeof(studB));
        display(studB, j);
    }
    display_denghao();
    goto choose; 
  }
sort: {
     cout << "1.課程排序" << endl;
     cout << "2.總分排序" << endl;
     cin >> choose;
     if (choose == 1)goto kechengpaixu;
     if (choose == 2)goto  zongfenpaixu;
 kechengpaixu:cout << "請輸入以那一門課程排序" << endl;
     cin >> tempclassname;
     student *stu = new student[add.all];
     for (int j = 0; j < add.all; j++) {
         iofile.seekg(j * sizeof(studA), ios::beg);
         iofile.read((char*)&stu[j], sizeof(stu[j]));
     }
     sort(stu, add, tempclassname);
     display_denghao();
     display_demo();
     for (int j = 0; j < add.all; j++)
     {
         display(stu[j], j);
     }
     display_denghao();
     delete[] stu;
     goto choose;
 zongfenpaixu:
     student *stu1 = new student[add.all];
     for (int j = 0; j < add.all; j++) {
         iofile.seekg(j * sizeof(studA), ios::beg);
         iofile.read((char*)&stu1[j], sizeof(stu[j]));
     }
     sort(stu, add);
     display_denghao();
     display_demo();
     for (int j = 0; j < add.all; j++)
     {
         display(stu1[j], j);
     }
     display_denghao();
     delete[] stu;
     goto choose;
}
  tongji: {display_tongji();
      cin >> choose;
      if(choose==1){
          cout << "1.課程最高分" << endl;
          cout << "2.總分最高分" << endl;
          cin >> choose;
          if (choose == 1)goto kechenzuigao;
          if (choose == 2)goto  zongfenzuigaofen;
    kechenzuigao:  cout << "請輸入以那一門課程名字" << endl;
          cin >> tempclassname;
          student *stu1 = new student[add.all];
          for (int j = 0; j < add.all; j++) {
              iofile.seekg(j * sizeof(studA), ios::beg);
              iofile.read((char*)&stu1[j], sizeof(stu1[j]));
          }
          sort(stu1, add, tempclassname);
          cout << tempclassname << "的最高分是" << endl;
          display_denghao();
          display_demo();
          display(stu1[1],1);
          display_denghao();
          delete[] stu1;
          goto choose;
      zongfenzuigaofen:
          student *stu2 = new student[add.all];
          for (int j = 0; j < add.all; j++) {
              iofile.seekg(j * sizeof(studA), ios::beg);
              iofile.read((char*)&stu2[j], sizeof(stu2[j]));
          }
          sort(stu2, add);
          cout << tempclassname << "的最高分是" << endl;
          display_denghao();
          display_demo();
          display(stu2[1], 1);
          display_denghao();
          delete[] stu2;
          goto choose;
      }//最高分
      if(choose==2){
          cout << "1.課程最低分" << endl;
          cout << "2.總分最低分" << endl;
          cin >> choose;
          if (choose == 1)goto kechenzuidi;
          if (choose == 2)goto  zongfenzuidifen;
      kechenzuidi:  cout << "請輸入以那一門課程名字" << endl;
          cin >> tempclassname;
          student *stu = new student[add.all];
          for (int j = 0; j < add.all; j++) {
              iofile.seekg(j * sizeof(studA), ios::beg);
              iofile.read((char*)&stu[j], sizeof(stu[j]));
          }
          sort(stu, add, tempclassname);
          cout << tempclassname << "的最低分是" << endl;
          display_denghao();
          display_demo();
          display(stu[add.all], add.all);
          display_denghao();
          delete[] stu;
          goto choose;
      zongfenzuidifen:
          student *stu3= new student[add.all];
          for (int j = 0; j < add.all; j++) {
              iofile.seekg(j * sizeof(studA), ios::beg);
              iofile.read((char*)&stu3[j], sizeof(stu3[j]));
          }
          sort(stu3, add);
          cout <<"總分的最高分是" << endl;
          display_denghao();
          display_demo();
          display(stu3[add.all], add.all);
          display_denghao();
          delete[] stu3;
          goto choose;
      }//最低分
      if(choose==3){
         float all=0.0;
          float allclassa = 0.0;
          float allclassb = 0.0;
          for (int i = 0; i < add.all; i++) {
              iofile.seekg(i * sizeof(studA), ios::beg);
              iofile.read((char*)&studB, sizeof(studB));
              all = all + studB.Allgrade;
              allclassa = allclassa + studB.gradeA;
              allclassb = allclassb + studB.gradeB;
          }
          cout << studA.classA << "的平均分是" << allclassa / add.all << endl;
          cout << studA.classB << "的平均分是" << allclassb/ add.all << endl;
          cout << "總分平均分是:" << all / add.all << endl;
      }//平均分
      if(choose==4){
          float ajige=0.0;
          float bjige=0.0;
          for (int i = 0; i < add.all; i++) {
              iofile.seekg(i * sizeof(studA), ios::beg);
              iofile.read((char*)&studB, sizeof(studB));
              if (studB.gradeA > 60)ajige++;
              if (studB.gradeB > 60)bjige++;
          }
          cout << studA.classA << "的及格率是" << ajige / add.all << endl;
          cout << studA.classB << "的及格率是" << bjige / add.all << endl;
      }//及格率
}
query:
cout << "1.通過學號查詢" << endl;
cout << "2.通過姓名查詢" << endl;
cin >> choose;
if (choose == 1) {
    cout << "輸入查詢的學號" << endl;
    char t[13];
    int a,j = 0;
    cin >> t;
    for ( j; j < add.all; j++) {
        iofile.seekg(j * sizeof(studA), ios::beg);
        iofile.read((char*)&studB, sizeof(studB));
        a=query(studB, t);
        if (a == 1)break;
    }if (j == add.all)cout << "沒找到" << endl;
}
if (choose == 2) {
    cout << "輸入查詢的姓名" << endl;
    char t[6];
    char t1[13];
    int a;
    int j = 0;
    cin >> t;
    for ( j; j < add.all; j++) {
        iofile.seekg(j * sizeof(studA), ios::beg);
        iofile.read((char*)&studB, sizeof(studB));
        a=query(studB, t1,t);
        if (a == 1)break;
    }
    if (j == add.all)cout << "沒找到" << endl;

}
goto choose;
end:

    return 0;
}
發佈了42 篇原創文章 · 獲贊 5 · 訪問量 2萬+
發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章