C++課程設計之學生選題系統

請爲高校設計一個課程設計選題系統,系統主要處理學生的課程設計選題信息。其中,學生信息主要包括:學號、姓名、年級、專業,登錄密碼等,課程設計信息主要包括:課程編號、課程名稱、選題題目、指導教師等,請按照以下的功能要求設計系統。
【功能要求】
(1)錄入功能:可輸入或從文件讀入學生信息、課程設計信息。
(2)打印功能:可輸出學生信息、課程設計信息、選題情況(最好用文件保存信息數據);
(3)選題功能:學生按學號和密碼登錄後可進行選題操作,學生可選擇查看課程設計信息,選題要求每個學生只能選一題,每個題目限定不超過3個學生;
(4)編輯功能:可對學生信息、課程設計信息及選題情況執行查詢、修改、添加、刪除等操作;
(5)菜單功能:每種功能的操作都是在菜單中進行相應選擇。
在這裏插入圖片描述

#include<iostream>
#include<cstring>
#include<fstream>
#include<cstdlib> 
using namespace std;
char A; //題目A 
int a = 0; //題目A的計數器 
char B;
int b = 0;
char C;
int c = 0;
class Student {//定義一個學生類

    friend class course;
public:

    int stuid; // 學號
    char stuname[10]; // 姓名  
    char clas[10]; // 年級
    char specialty[10]; // 專業
    string StuID;//登陸密碼
public:
    void add(); // 添加學生信息
    void total(); // 統計信息
    void display(); // 顯示信息
    void write(int n); // 向文件中寫入數據
    int read(); // 從文件中讀數據
}stu[40];

class Course { //定義一個課程設計類
    friend class Student;
private:

    string couId; // 課程編號
    char couName[10]; // 課程名稱
    char couTitle; // 選題題目
    char couTeacher[30]; // 指導教師
public:
    void search(); // 查詢信息
    void searchsid(); // 按學號查詢信息
    void alter(); // 修改信息
    void del(); // 刪除信息
}Cou[40];

//1. 向文件中寫入數據
void Student::write(int n)
{
    fstream myfile;//1.創建流對象
    myfile.open("student.txt", ios::out | ios::binary);
    if (!myfile)
    {
        cout << "該文件不能打開 !" << endl;
        abort();//功能: 終止程序的執行。返回值依賴於執行,可以通過返回值顯示錯誤。
    }
    int count = n;
    myfile << count << endl << endl;
    for (int i = 0; i <= count; i++)
    {
        myfile << stu[i].stuid << " " << stu[i].stuname << " " << stu[i].clas << " " << stu[i].specialty << " " << Cou[i].couId << " " << Cou[i].couName << " " << Cou[i].couTitle << " " << Cou[i].couTeacher << " " << endl;
    }
    myfile.close();

}
//2.從文件中讀數據
int Student::read()
{

    fstream myfile;//定義流對象
    myfile.open("student.txt", ios::in | ios::binary);
    if (!myfile)
    {
        cout << "該文件不能打開 !" << endl;
        abort();
    }
    int count;
    myfile.seekg(0);
    myfile >> count;
    for (int i = 0; i <= count; i++)
    {
        myfile >> stu[i].stuid >> stu[i].stuname >> stu[i].clas >> stu[i].specialty >> Cou[i].couId >> Cou[i].couName >> Cou[i].couTitle >> Cou[i].couTeacher;
        cout << endl;
    }
    myfile.close();
    return count;
}

//錄入功能
void Student::add()
{

    int n = read();;

    int i = 0;
    char sign;
    cout << endl << "是否輸入增加的學生的相關信息 :(Y or N) " << endl;
    cin >> sign;
    while (sign != 'N')
    {
    loop:
        cout << "學號 :";
        cin >> stu[i].stuid;
        cout << endl;
        cout << "學生登陸密碼 :";
        cin >> stu[i].StuID;
        cout << endl;
        int c = 0;
        while (c < i)
        {
            c++;
            if (stu[i].stuid == stu[i - c].stuid)
            {
                cout << "你輸入的學號已經存在 ! 請重新輸入 " << endl;
                goto loop;
            }
        }
        cout << "請錄入學生基本信息:" << endl;
        cout << "姓名 :";
        cin >> stu[i].stuname;
        cout << endl;
        cout << "年級 :";
        cin >> stu[i].clas;
        cout << endl;
        cout << "專業 :";
        cin >> stu[i].specialty;
        cout << endl;
        cout << "請錄入課程設計信息:" << endl;

        cout << "請輸入......" << endl;
        cout << "課程編號 :" << endl;

        cin >> Cou[i].couId;
        cout << endl;
        cout << "課程名稱 :";
        cin >> Cou[i].couName;
        cout << endl;
        cout << "進入選題程序......" << endl;
        int id;//學號
        string password;//登錄密碼

        cout << "選題題目 :本系統每個課程有3道題目[A,B,C],每道題目限定3名學生" << endl;
        for (int j = 0; j < 3; j++)
        {
            cout << "用戶名:[學號]" << endl;
            cin >> id;
            cout << "登錄密碼:" << endl;
            cin >> password;
            if (id == stu[i].stuid && password == stu[i].StuID) {
                cout << "登錄成功,請選題:" << endl;
                cin >> Cou[i].couTitle;
                if (Cou[i].couTitle == 'A') {
                    a++;
                    if (a > 3)
                        cout << "選擇A題目人數已滿" << endl;
                    break;
                }
                if (Cou[i].couTitle == 'B') {
                    b++;
                    if (b > 3)
                        cout << "選擇B題目人數已滿" << endl;
                    break;
                }
                if (Cou[i].couTitle == 'C') {
                    c++;
                    if (c > 3)
                        cout << "選擇C題目人數已滿" << endl;
                    break;
                }
                break;
            }
            else {
                if (2 - j == 0) {
                    cout << "3次登錄失敗,賬號被鎖定,請與指導老師聯繫!" << endl;
                }
                else {
                    cout << "選題程序登錄失敗,你還有" << 2 - j << "次機會登錄!" << endl;
                }
            }
        }


        cout << endl;
        cout << "指導教師 :";
        cin >> Cou[i].couTeacher;
        cout << endl;
        cout << "提示:是否繼續寫入學生信息 ?(Y/N)";
        cin >> sign; // 輸入判斷  
        i++;
    }
    write(i);
}

//查詢學生信息、課程設計信息
void Course::search()
{
    Student stu;
    int n = stu.read();
    cout << "查詢學生信息、課程設計信息:[確認查詢:1]" << endl;
    cout << endl;
    int c;
    Course g;
    cout << "請輸入選擇 :";
    cin >> c;
    switch (c)
    {
    case 1:
        g.searchsid();
    default:
        cout << "輸入錯誤 , 請重新輸入 !" << endl;
    }
    stu.write(n);
}
//登錄學號,密碼,查詢學生信息、課程設計信息
void Course::searchsid()
{
    Student st;
    int n = st.read();
    int s;
    int i = 0;
    cout << endl << "查找學生信息 :" << endl;
    cout << "請輸入需要查找學生的學號 :" << endl;
    cin >> s;
    string id;
    cout << "請輸入學生登陸密碼:";
    cin >> id;
    while ((stu[i].stuid - s) != 0 && i < n && (id == stu[i].StuID)) i++; // 查找判斷  
    if (i == n)
    {
        cout << "登陸失敗" << endl;
        cout << "提示:對不起,無法找到該學生的信息! " << endl;
    }
    else
    {
        cout << "查詢中,請等待......" << endl;
        cout << "---------------查詢結果如下----------------" << endl;
        cout << "學號 : " << stu[i].stuid << endl;
        cout << "姓名 : " << stu[i].stuname << endl;
        cout << "年級 : " << stu[i].clas << endl;
        cout << "專業 : " << stu[i].specialty << endl;

        cout << "課程編號 : " << Cou[i].couId << endl;
        cout << "課程名稱 : " << Cou[i].couName << endl;
        cout << "選題題目 : " << Cou[i].couTitle << endl;
        cout << "指導教師 : " << Cou[i].couTeacher << endl;
    }
}

//修改學生信息、課程設計信息
void Course::alter()
{
    Student st;
    int n = st.read();
    int s;
    int i = 0;
    cout << endl << "修改學生信息 :" << endl;
    cout << "請輸入需要修改學生的學號 :" << endl;
    cin >> s;
    while ((stu[i].stuid - s) != 0 && i < n) i++; // 查找判斷  
    if (i == n)
    {
        cout << "提示:對不起,無該學生的信息 !!!" << endl; // 輸入失敗信息
    }
    else
    {
        cout << "該學生的信息 :" << endl;
        cout << "學號: " << stu[i].stuid << '\n' << "姓名: " << stu[i].stuname << "年級: " << stu[i].clas << '\n' << "專業: " << '\n' << stu[i].specialty << '\n'
            << "課程編號 : " << Cou[i].couId << '\n' << "課程名稱 : " << Cou[i].couName << '\n' << "選題題目 : " << Cou[i].couTitle << '\n' << "指導教師 : " << Cou[i].couTitle << '\n' << endl;
        cout << "請重新輸入該學生的信息 " << endl;
        cout << "學號 :";
        cin >> stu[i].stuid;
        cout << endl;
        cout << "姓名 :";
        cin >> stu[i].stuname;
        cout << endl;
        cout << "年級 :";
        cin >> stu[i].clas;
        cout << endl;
        cout << "專業 :";
        cin >> stu[i].specialty;
        cout << endl;

        cout << "請重新輸入課程設計信息:" << endl;
        cout << "課程編號 :";
        cin >> Cou[i].couId;
        cout << endl;
        cout << "課程名稱 :";
        cin >> Cou[i].couName;
        cout << endl;
        cout << "進入選題程序......" << endl;
        int id;//學號
        string password;//登錄密碼

        cout << "選題題目 :本系統每個課程有3道題目[A,B,C],每道題目限定3名學生" << endl;
        for (int j = 0; j < 3; j++)
        {
            cout << "用戶名:[學號]" << endl;
            cin >> id;
            cout << "登錄密碼:" << endl;
            cin >> password;
            if (id == stu[i].stuid && password == stu[i].StuID) {
                cout << "登錄成功,請選題:" << endl;
                cin >> Cou[i].couTitle;
                if (Cou[i].couTitle == 'A') {
                    a++;
                    if (a > 3)
                        cout << "選擇A題目人數已滿" << endl;
                    break;
                }
                if (Cou[i].couTitle == 'B') {
                    b++;
                    if (b > 3)
                        cout << "選擇B題目人數已滿" << endl;
                    break;
                }
                if (Cou[i].couTitle == 'C') {
                    c++;
                    if (c > 3)
                        cout << "選擇C題目人數已滿" << endl;
                    break;
                }
                break;
            }
            else {
                if (2 - j == 0) {
                    cout << "3次登錄失敗,賬號被鎖定,請與指導老師聯繫!" << endl;
                }
                else {
                    cout << "選題程序登錄失敗,你還有" << 2 - j << "次機會登錄!" << endl;
                }
            }
        }
        cout << endl;
        cout << "指導老師 :";
        cin >> Cou[i].couTitle;
        cout << endl;
        char c;
        cout << "是否保存數據 ?(y/n)" << endl;
        cin >> c;
        if (c == 'y')
            cout << "修改成功 !" << endl;
        st.write(n);
    }
}
//刪除學生信息、課程設計信息
void  Course::del()
{
    Student st;
    int n = st.read();
    int s;
    int i = 0, j;
    cout << endl << "刪除學生信息 :" << endl;
    cout << "請輸入需要刪除學生的學號 :" << endl;
    cin >> s;
    while ((stu[i].stuid - s) != 0 && i < n) i++; // 查找判斷  
    if (i == n)
    {
        cout << "提示:記錄爲空 !!!" << endl; // 返回失敗信息
    }
    else
    {
        for (j = i; j < n - 1; j++) // 刪除操作  
        {
            stu[j].stuid = stu[j + 1].stuid;
            strcpy(stu[j].stuname, stu[j + 1].stuname);
            strcpy(stu[j].clas, stu[j + 1].clas);
            strcpy(stu[j].specialty, stu[j + 1].specialty);
            // Cou[i].couId >> Cou[i].couName >> Cou[i].couTitle >> Cou[i].couTeacher;
            Cou[j].couId = Cou[j + 1].couId;
            strcpy(Cou[j].couName, Cou[j].couName);
            //strcpy(Cou[j].couTitle, Cou[j].couTitle);
            Cou[j].couTitle = Cou[j].couTitle;
            strcpy(Cou[j].couTeacher, Cou[j].couTeacher);
        }
        cout << "提示:已成功刪除 !" << endl; // 返回成功信息
    }
    cout << "你要刪除的信息如下 :" << endl;
    cout << "姓名 :" << stu[i].stuname << endl;
    cout << "學號 :" << stu[i].stuid << endl;
    cout << "年級班級 :" << stu[i].clas << endl;
    cout << "專業 :" << stu[i].specialty << endl;

    cout << "課程編號 :" << Cou[i].couId << endl;
    cout << "課程名稱 :" << Cou[i].couName << endl;
    cout << "選題題目 :" << Cou[i].couTitle << endl;
    cout << "指導老師 :" << Cou[i].couTeacher << endl;
    st.write(n - 1);
}
//統計學生信息、課程設計信息
void Student::total()
{
    {
        int n = read();
        char c[10];
        cout << "請輸入需要查找的課程名稱 :" << endl;
        cin >> c;
        for (int i = 0; i < n; i++)
            if (strcmp(Cou[i].couName, c) == 0)
            {
                cout << "你要統計的信息如下 :" << endl;
                cout << "姓名 :" << stu[i].stuname << endl;
                cout << "學號 :" << stu[i].stuid << endl;
                cout << "年級班級 :" << stu[i].clas << endl;
                cout << "專業 :" << stu[i].specialty << endl;

                cout << "課程編號 :" << Cou[i].couId << endl;
                cout << "課程名稱 :" << Cou[i].couName << endl;
                cout << "選題題目 :" << Cou[i].couTitle << endl;
                cout << "指導老師 :" << Cou[i].couTeacher << endl;
            }
            else
            {
                cout << "沒有找到該課程的記錄! " << endl;
            }
    }
}
//顯示學生信息、課程設計信息
void Student::display()
{
    int n = read();
    cout << endl << "顯示全部學生信息: " << endl;
    if (!stu)
        cout << "沒有記錄" << endl;
    else
    {
        for (int i = 0; i < n; i++) // 循環輸入  

            cout << "學號: " << stu[i].stuid << '\n' << "姓名: " << stu[i].stuname << "年級: " << stu[i].clas << '\n' << "專業: " << '\n' << stu[i].specialty << '\n'
            << "課程編號 : " << Cou[i].couId << '\n' << "課程名稱 : " << Cou[i].couName << '\n' << "選題題目 : " << Cou[i].couTitle << '\n' << "指導教師 : " << Cou[i].couTeacher << '\n' << endl;

    }
}

int main()
{
    char choice;
    cout << "\n\n\ ********------歡迎使用課程設計選題管理系統------******** \n\n";
    cout << "|----------------- 1.[錄入]+[選題]功能   --------------|\n";
    cout << "|----------------- 2.查詢功能            --------------|\n";
    cout << "|----------------- 3.修改功能            --------------|\n";
    cout << "|----------------- 4.刪除功能            --------------|\n";
    cout << "|----------------- 5.統計功能            --------------|\n";
    cout << "|----------------- 6.打印功能[學生+課程] --------------|\n";
    cout << "|----------------- 0.退出選題系統功能    --------------|\n";
    cout << endl;
    cout << "請輸入對應功能的序號: ";
    cin >> choice;
    Course g;
    Student st;
    if (choice == '0')
    {
        cout << "感謝您使用課程設計選題管理系統!" << endl;
        exit(0);
    }
    else if (choice == '1')
    {
        st.add();
        system("pause");
        main();
    }
    else if (choice == '2')
    {
        g.search();
        system("pause");
        main();
    }
    else if (choice == '3')
    {
        g.alter();
        system("pause");
        main();
    }
    else if (choice == '4')
    {
        g.del();
        system("pause");
        main();
    }
    else if (choice == '5')
    {
        st.total();
        system("pause");
        main();
    }
    else if (choice == '6')
    {
        st.display();
        system("pause");
        main();
    }
    else
    {
        cout << "輸入錯誤,請重新輸入您的選擇: ";
        main();
    }
    return 0;
}
發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章