c与c++实现学生管理系统

陈旧的代码,今天竟然翻出来了QAQ,渣渣学校只会让写学生管理系统,贴出来希望对大家有用。

#include <bits/stdc++.h>

using namespace std;

struct node {
    char stuNum[10]; //学号
    char stuName[10];//姓名
    char sex[10];//性别
    char domNum[10];//宿舍号码
    char tel[10];//电话号码
    int score;//成绩
    /*
    node(string stuNum[], string stuName, string sex, string domNum, string tel, int score) {
        this->domNum = domNum;
        this->score = score;
        this->sex = sex;
        this->stuName = stuName;
        this->tel = tel;
        this->stuNum = stuNum;
    }
     */
};

//上面一些数据使用String类型而非int 或者 long long是考虑到溢出问题
vector<node> info;
int countNum;//全局变量初始化为0,记录输入人数。
int cmp1(node n1, node n2) {//构造升序比较器
    return n1.score < n2.score;
}

int cmp2(node n1, node n2) {//构造降序比较器
    return n1.score > n2.score;
}

void showMenu() {
    cout << "********Welcome To Students' Information System********" << endl;
    cout << "*****************请输入数字以进行使用******************" << endl;
    cout << "*******************1:学生信息手动录入********************" << endl;
    cout << "*******************2:学生信息自动导入********************" << endl;
    cout << "*******************3:学生信息排序**********************" << endl;
    cout << "*******************4:学生信息总览**********************" << endl;
    cout << "*******************5:学生信息查询**********************" << endl;
    cout << "*******************6:  退出程序   **********************" << endl;
}

void infoInput() {//学生信息录入
    char stuNum[10]; //学号
    char stuName[10];//姓名
    string sex;//性别
    char domNum[10];//宿舍号码
    char tel[10];//电话号码
    int score;//成绩
    cout << "请输入学生学号:";
    cin >> stuNum;
    cout << "请输入学生姓名:";
    cin >> stuName;
    cout << "请输入学生性别:";
    cin >> sex;
    cout << "请输入学生宿舍号:";
    cin >> domNum;
    cout << "请输入学生电话:";
    cin >> tel;
    cout << "请输入学生分数(0-100):";
    cin >> score;
    if (score >= 0 && score <= 100 && (sex == "男" || sex == "女")) {
        node n;
        strcpy(n.stuName, stuName);
        strcpy(n.sex,sex.c_str());
        strcpy(n.stuNum, stuNum);
        strcpy(n.domNum, domNum);
        strcpy(n.tel, tel);
        n.score = score;
        info.push_back(n);
        countNum++;
        printf("信息插入成功\n");
    } else {
        cout << "插入非法数据" << endl;
    }
}

void infoSort() {//学生信息排序
    int s;
    cout << "请选择排序类型:" << endl;
    cout << "              1.按分数降序" << endl;
    cout << "              2.按分数升序" << endl;
    cin >> s;
    if (s == 1) sort(info.begin(), info.end(), cmp2);
    else if(s == 2) sort(info.begin(), info.end(), cmp1);
    return;
}

void infoQuery() { //学生信息查询
    cout << "请选择查询类型:" << endl;
    cout << "              1.按学号查询" << endl;
    cout << "              2.按姓名查询" << endl;

    int s;
    scanf("%d", &s);
    if (s == 1) {
        string sno;
        cout << "请输入学号:" << endl;
        cin >> sno;
        int i;
        for (i = 0; i < info.size(); i++) {
            if (info[i].stuNum == sno) {
                cout << "学号     姓名      性别        宿舍号码         电话号码       成绩" << endl;
                cout << info[i].stuNum << " " << info[i].stuName << " " << info[i].sex << " " << info[i].domNum << " "
                     << info[i].tel << " " << info[i].score << endl;
                cout << "查询成功";
                break;
            }
        }
        if (i == info.size())
            cout << "未查询到此学生" << endl;
    } else if(s == 2) {
        string sName;
        cout << "请输入学号:" << endl;
        cin >> sName;
        int i;
        for (i = 0; i < info.size(); i++) {
            if (info[i].stuName == sName) {
                cout << "学号   姓名   性别   宿舍号码     电话号码   成绩" << endl;
                cout << info[i].stuNum << " " << info[i].stuName << " " << info[i].sex << " " << info[i].domNum << " "
                     << info[i].tel << " " << info[i].score << endl;
                cout << "查询成功";
                break;
            }
        }
        if (i == info.size())
            cout << "未查询到此学生" << endl;
    }
    return;
}
void fileInput(){
    char stuNum[10]; //学号
    char stuName[10];//姓名
    char sex[10];//性别
    char domNum[10];//宿舍号码
    char tel[10];//电话号码
    int score;//成绩
    node n;
    FILE * fp;
    char file[100] ;
    cout<<"请输入数据路径:"<<endl;
    scanf("%s",file);
    if((fp = fopen(file,"r"))==NULL)
        cout<<"输入路径有误或数据非法"<<endl;
    else
        {while (!feof(fp)){
            fscanf(fp,"%s %s %s %s %s %d",stuNum, stuName,sex,domNum ,tel,&score);
            strcpy(n.stuName, stuName);
            strcpy(n.sex, sex);
            strcpy(n.stuNum, stuNum);
            strcpy(n.domNum, domNum);
            strcpy(n.tel, tel);
            n.score = score;
            info.push_back(n);
            countNum++;
        }
        cout<<"导入成功"<<endl;
        }
}
void infoOutput() {//学生信息总览
    cout << "序号   学号      姓名         性别      宿舍号码           电话号码            成绩" << endl;
    for (int i = 0; i < info.size(); i++) {
        cout << i + 1 << "       " << info[i].stuNum << "       " << info[i].stuName << "       " << info[i].sex << "       " << info[i].domNum
             << "             " << info[i].tel << "             " << info[i].score << endl;
    }
    cout << "共计" << countNum << "人" << endl;
}

int main() {
    showMenu();
    int a;
    while (scanf("%d", &a)) {
        switch (a) {
            case 1:
                infoInput();
                break;
            case 2:
                fileInput();
                break;
            case 3:
                infoSort();
                break;
            case 4:
                infoOutput();
                break;
            case 5:
                infoQuery();
                break;
            case 6:
                return 0;
            default:
                break;
        }
        showMenu();
    }
    return 0;
}


posted @ 2021-04-13 14:19  沃特艾文儿  阅读(62)  评论(0)    收藏  举报  来源