C++学生成绩管理系统,用结构体做,有没有人会?

img

参考:

#define  _CRT_SECURE_NO_WARNINGS
 
#include<iostream>
using namespace std;
#include<string.h>
#include<fstream>
 
class student
{
private:
    student* next;
public:
    char stu_num[15];                  //学号
    char stu_name[30];                //姓名
    float stu_score;                      //成绩
 
    void afterInsert(student *p);//在该节点后插入一个节点
    void afterDelete();//在该节点后删除一个节点
    
    student *getNext()//获得下一个节点的指针
    { 
        return next; 
    }
 
    /***********查询学生信息************/
    void getMage();
 
    /******学生信息修改******/
    void changeMage(int n, char *ptr);
    void changegrade(float p);
 
    /******构造*****/
    student(char *num, char *name, float score);
    student();
};
 
void student::changegrade(float p)
{
    stu_score = p;
}
 
student::student()           //构造
{
    strcpy(stu_num, "\0");
    strcpy(stu_name, "\0");
    stu_score = 0;
    next = '\0';
}
 
student::student(char *num, char *name, float score)
{
    strcpy(stu_num, num);
    strcpy(stu_name, name);
    stu_score = score;
    next = '\0';
}
 
void student::afterInsert(student *p)//插入节点
{
    p->next = next;
    next = p;
}
 
void student::afterDelete()        //删除节点
{
    student *p = next;
    next = p->next;
    delete p;
}
 
void student::getMage()             //获得信息
{
    cout << "学号:" << stu_num << "      姓名:" << stu_name;
    cout << "      c++成绩:" << stu_score << endl;
}
 
void student::changeMage(int n, char *ptr)
{
    switch (n)
    {
    case 1: strcpy(stu_num, ptr); 
        break;
    case 2: strcpy(stu_name, ptr);
    }
}
 
//建立链表函数
void  construct_list(student *tail)
{
    student *p = new student;
    char very[20];
    float achieve;
    cout << "请输入学号:" << endl;
    cin >> very;
    p->changeMage(1, very);
    cout << "请输入姓名:" << endl;
    cin >> very;
    p->changeMage(2, very);
    cout << "请输入c++成绩:" << endl;
    cin >> achieve;
    p->changegrade(achieve);
    system("cls");
    cout << "信息输入完毕" << endl;
 
    for (; tail->getNext() != '\0';)
    {
        tail = tail->getNext();
    }
 
    tail->afterInsert(p);
}
 
/*********查询信息*********/
student *findmege(student *head)
{
loop:
    cout << "1--按姓名查询           2--按学号查询              q--返回上一级菜单" << endl;
    char p[5], ptr[20];
    student *mid = head;
    cin >> p;
 
    if (p[0] != '1'&&p[0] != '2'&&p[0] != 'q' || strlen(p)>1)
    {
        system("cls");
        cout << "对不起,你的输入有误,请重新输入!" << endl;
        goto loop;
    }
 
    switch (p[0])
    {
    case '1':
    {
        system("cls");
        cout << "请输入要查找的姓名:" << endl;
        cin >> ptr;
 
        for (; strcmp(ptr, mid->stu_name) != 0; mid = mid->getNext())
        {
            if (mid->getNext() == '\0')
            {
                cout << "对不起,你要查找的人不存在,请确认你的输入是否正确!" << endl;
                goto loop;
            }
        }
        return mid;
    }
    case '2':
    {
        system("cls");
        cout << "请输入您要查找的学号:" << endl;
        cin >> ptr;
        for (; strcmp(ptr, mid->stu_num) != 0; mid = mid->getNext())
        {
            if (mid->getNext() == '\0')
            {
                cout << "对不起,您要查找的内容不存在,请确认您的输入是否正确!" << endl;
                goto loop;
            }
        }
        return mid;
    }
    case 'q': 
    {
        return '\0';
    }
    default:
    {
        system("cls");
        cout << "对不起,您的输入有误,请重新输入!" << endl;
        goto loop;
    }
    }
}
 
/******************删除链表 节点***********************/
void delete_list(student *head)
{
    student *p = '\0';
    char selet[4];
    system("cls");
    cout << "在删除前,系统会根据您的提示找到您要删除的学生信息!" << endl;
    p = findmege(head);
    if (p != '\0')
    {
        cout << "确认要删除吗(yes/任意键返回)" << endl;
        cin >> selet;
 
        if (strcmp(selet, "yes") == 0)
        {
            for (; head->getNext() != p; head = head->getNext());
            head->afterDelete();
            system("cls");
            cout << "该信息删除成功!" << endl;
        }
    }
}
 
/*******************修改节点信息********************/
void change_info(student *head)
{
    system("cls");
    cout << "在您修改前,系统会根据您提供的信息找的您要修改的信息:" << endl;
    student *p = '\0';
 
    float achieve;
    p = findmege(head);
    if (p != '\0')
    {
        cout << "请输入c++成绩:" << endl;
        cin >> achieve;
        p->changegrade(achieve);
        system("cls");
        cout << "修改成功!" << endl;
    }
 
}
 
/**************输出学生成绩信息**************/
void output(student *head)
{
    system("cls");
    cout << "1-查看指定学生信息;2-查看所有学生信息;3-分段输出学生信息" << endl;
    char ch;
    int n = 0;
    head = head->getNext();
    cin >> ch;
    switch (ch)
    {
    case '1': 
        head = findmege(head);
        if (head == '\0')
        {
            break;
        }
        head->getMage();
        break;
    case '2': 
    while (head)
    {
        head->getMage();
        head = head->getNext();
    }
    break;
    case '3': 
        cout << "a-60分以下;b-60~70分之间;c-70~80分之间;d-80~90分之间;e-90~100分之间:" << endl;
        cin >> ch;
        switch (ch)
        {
        case 'a':
        while (head)
        {
            if (head->stu_score <= 60)
            {
                head->getMage();
                n++;
            }
            head = head->getNext();
        }
         break;
        case 'b': 
        while (head)
        {
            if (head->stu_score>60 && head->stu_score <= 70) 
            { 
                head->getMage();
                n++; 
            }
            head = head->getNext();
        }
        break;
        case 'c': 
        while (head)
        {
            if (head->stu_score>70 && head->stu_score <= 80)
            { 
                head->getMage(); 
                n++; 
            }
            head = head->getNext();
        }
        break;
        case 'd': 
        while (head)
        {
            if (head->stu_score>80 && head->stu_score <= 90)
            {
                head->getMage();
                n++;
            }
            head = head->getNext();
        }
        break;
        case 'e': 
        while (head)
        {
            if (head->stu_score>90 && head->stu_score <= 100)
            { 
                head->getMage();
                n++;
            }
            head = head->getNext();
        }
        }
        if (n == 0)
        {
            cout << "该分段内没有您要找的学生信息" << endl;
        }
    }
}
 
/*****************主菜单************************/
void mainmenu(student *head)
{
    char selet[10];
    int n = 1;
    ofstream outfile;
    ifstream infile;
    student *p, *ptr;
    student *test = head, *mid;
    cout << "*************************欢迎进入学生信息管理系统*************************" << endl;
    do {
        cout << "**************************************************************************" << endl;
        cout << "1.插入信息;   2.删除信息;  3.修改信息; 4.查看信息; 5.保存  " << endl;
        cout << "按'q'键退出      " << endl;
        cout << "**************************************************************************" << endl;
        cin >> selet;
        if (((selet[0]<'1' || selet[0]>'6') && selet[0] != 'q') || strlen(selet)>1)
        {
            system("cls");
            cout << "您的输入有误,请重新输入!" << endl;
            break;
        }
        switch (selet[0])
        {
 
        case '1':
            construct_list(head);
            break; 
        case '2': 
            delete_list(head); 
            break;
        case '3': 
            change_info(head);
            break;
        case '4': 
            output(head);
            break;
        case '5':  
            outfile.open("students.txt", ios::out | ios::app);
            for (p = head->getNext(); p != '\0'; p = p->getNext())
            {
                outfile << p->stu_name << ' ';
                outfile << p->stu_num << ' ';
                outfile << p->stu_score << ' ';
                outfile << endl;
            }
            outfile.close();
            system("cls");
            cout << "保存成功!" << endl;
            break;
        case 'q': 
            break;
        }
    } while (selet[0] != 'q');
}
 
void main()
{
    student head;
    mainmenu(&head);
}

可以参考下这篇文章,希望对你有帮助:

你题目的解答代码如下:(如有帮助,望采纳!谢谢! 点击我这个回答右上方的【采纳】按钮)

#include"stdio.h"
#include"malloc.h"
#define format "%d %s %s %d"
 
struct student{
    int code;
    char name[10];
    char sex[3];
    int age;
    struct student *next;
};
//初始化头节点
struct student *init(){
    struct student *head = NULL;
    head = (struct student *)malloc(sizeof(struct student));
    if(head){
        printf("内存初始化成功!!\n");
        head->next=NULL;
        return head;
    }else{
        printf("内存初始化失败!!\n");
        return NULL;
    }
}
//插入
int insert(struct student  *head){
    struct student *p=head;
    struct student *p1;
    while(p->next!=NULL){//所指的地址不为ull,就向后移动
        p=p->next;
    }
    p1=(struct student *)malloc(sizeof(struct student ));
    if(p1){
        printf("新节点开辟成功!\n");
        printf("请输入学号、姓名、性别、年龄:");
        scanf(format,&(p1->code),p1->name,p1->sex,&(p1->age));
        p1->next=NULL;//最后一个节点地址域设为ull
        p->next=p1;//接上
        return 1;
 
    }else{
        printf("新节点开辟 失败!\n");
        return 0;
    }
}
//查询
void  print(struct student *head){
    struct student *p=head;
    while(p->next!=NULL){
            p=p->next;
            printf(format,p->code,p->name,p->sex,p->age);
            printf("\n");
 
    }
}
//修改
int update(struct student *head,int code){
    struct student *p=head;
    while(p->next!=NULL){//遍历,匹配出对应的code,则输出
        p=p->next;
        if(p->code==code){
            printf("请输入修改后的值:");
            scanf("%s %s %d",p->name,p->sex,&(p->age));
            return 1;
        }
        if(p->next==NULL){//到了最后一个节点,看他的指针域为NULl,表示没有该人
            printf("没有该人!");
            return 0;
        }
    }
}
//删除
int del(struct student *head,int code){
    struct student *p1=head;
    struct student *p2 = head;
    while(p1->next!=NULL){
        p2=p1->next;
        if(p2->code==code){
                if(p2->next==NULL){//最后一个
                    p1->next=NULL;
                    free(p2);
                }else{
                    p1->next=p1->next->next;
                    free(p2);
                }
                return 1;
        }
        if(p2->next==NULL){//
            printf("没有该人!");
            return 0;
        }
        p1=p1->next;
    }
}
void destroy(struct student *head){
    struct student *p=head;
    struct student *q=p->next;
    while(q->next!=NULL){
            free(p);
            printf("已释放!");
            p=q;
            q=q->next;
    }
    free(p);
    printf("已释放!");
}
main(){
    int flag = 1;
    struct student *head=init();
    while(flag){
        printf("1---学生信息添加\n");
        printf("2---学生信息查询\n");
        printf("3---学生信息修改\n");
        printf("4---学生信息删除\n");
        printf("5---学生信息退出\n");
        int sel;
        printf("请输入你的选择:");
        scanf("%d",&sel);
 
        switch(sel){
        case 1:
        {
            int f= insert(head);
            if(f){
                printf("添加成功!\n");
            }else{
                printf("添加失败!\n");
            }
        }
            break;
        case 2:
            {
                print(head);
            }
            break;
        case 3:
            {
                printf("请输入code:");
                int code ,f1;
                scanf("%d",&code);
                f1= update(head,code);
                if(f1){
                    printf("修改成功!\n");
                }else{
                    printf("修改失败!\n");
                }
            }
 
            break;
        case 4:
            {
                printf("请输入删除的code:");
                int code ,f2;
                scanf("%d",&code);
                f2= del(head,code);
                if(f2){
                    printf("删除成功!\n");
                }else{
                    printf("删除失败!\n");
                }
            }
            break;
        case 5:
            destroy(head);
            flag=0;
            break;
        default:
            printf("输入有误!");
        }
    }
 
}