SWQXDBA / my-ideas

1 stars 0 forks source link

链表-学籍管理系统 #3

Open SWQXDBA opened 3 years ago

SWQXDBA commented 3 years ago

include

include

using namespace std; bool whetherLinkMore = false; int classnumberis = 0; string classname[10]; int option; enum//增加 2删除 3查找 4修改 5按名字插入 6问询链表长度 { 增加 = 1, 删除 =2, 查找 = 3, 修改 =4, 按名字插入 =5, 问询链表长度 =6, 查询全部数据 =7, 退出

}; class othermarks { public: othermarks() { cout << "出现错误!!您不该调用此额外科目的默认构造函数!!!" << endl; } othermarks(int number) { object = new int[number]; //cout << "class新建完成" << number << "个" << endl << "-----------------" << endl; } int *object; }; class student {

public: string name; int englishmark; int mathmark; student Next; othermarks moremarks;

}; class Nodelist { public: Nodelist() :Start(NULL), End(NULL) { cout << "已初始化一个空链表" << endl; } int getlenth() { int num=1; if (!Start) return 0; student *temp = Start; while (temp!= End) { temp = temp->Next; num++; }

    return num;

}
student* Start;
student* End;

}; int getsallmarks(const student &stp); int getsallmarks(const student &stp) { int sum=0; if (!whetherLinkMore) { sum = stp.englishmark + stp.mathmark; return sum; } else { sum += stp.englishmark + stp.mathmark; for (int i = 0; i < classnumberis; i++) {

        sum+= stp.moremarks->object[i];

    }
    return sum;
}

} void Add(Nodelist& p, student stp)//在所输入的student信息之后插入 { student temp = new student; if (temp != NULL) { cout << "请输入姓名" << endl; cin >> temp->name; cout << "请输入英语分数" << endl; cin >> temp->englishmark; cout << "请输入数学分数" << endl; cin >> temp->mathmark; if (whetherLinkMore) { temp->moremarks = new othermarks(classnumberis); for (int i = 0; i < classnumberis; i++) { cout << "请输入额外科目:" << classname[i] << "的分数" << endl; cin >> temp->moremarks->object[i];

        }
    }
    cout << "录入完成,正在进行数据操作...!" << endl;
    if (NULL==p.Start)
    {
        cout << "检测到目前链表为空,新增第一个数据..." << endl;
        temp->Next = p.End;
        p.Start = temp;
        p.End = temp;
    }
    else if((stp==p.Start&&option==增加)||p.End==stp)//当要求添加在表头之后,认为添加到表尾。且操作为增加时,或者明确要求添加到表尾
    {
        cout << "添加到表尾" << endl;
        p.End->Next = temp;
        temp->Next = NULL;
        p.End = temp;
    }
    else
    {
        cout << "进行插入操作" << endl;
        temp->Next = stp->Next;
        stp->Next = temp;

    }

    cout << "添加完成 !" << endl;
}
else
{
    cout << "添加失败" << endl;
    temp = NULL;
    delete temp;
}

} void Addtofront(Nodelist& p, student stp)//在前面插入 { student temp=p.Start; if (stp != NULL) { if (stp == p.Start) { temp = new student; if (temp != NULL) { cout << "此数据将被添加在表头" << endl; cout << "请输入姓名" << endl; cin >> temp->name; cout << "请输入英语分数" << endl; cin >> temp->englishmark; cout << "请输入数学分数" << endl; cin >> temp->mathmark; if (whetherLinkMore) { temp->moremarks = new othermarks(classnumberis); for (int i = 0; i < classnumberis; i++) { cout << "请输入额外科目:" << classname[i] <<"的分数"<< endl; cin >> temp->moremarks->object[i];

                }
            }
            cout << "录入完成,正在进行数据操作...!" << endl;
            temp->Next = p.Start;//相当于stp
            p.Start = temp;
            return;
        }

    }

    while (temp)
    {
        if (temp->Next == stp)//找到他前面的一个
        {
            cout << "找到目标,请进行插入操作" << endl;

            Add(p, temp);
            return;
        }
        temp = temp->Next;
    }

}
else
    cout << "不可在NULL之前插入" << endl;

} student search(Nodelist& p, string name) { if (!p.getlenth()) { cout << "链表为空,查询失败!" << endl; return NULL; } student temp = p.Start; if (1 == p.getlenth()) { if (p.Start->name == name) { return temp; } else cout << "未找到目标信息" << endl; return NULL;

}

while (1)
{
    if (temp->name == name)
        return temp;
    else if(temp == p.End)
        return NULL;
    else
        temp = temp->Next;

}

} void searchone(Nodelist &p) { cout << "请输入要查找的姓名" << endl; string name; cin >> name; student *temp= search(p, name); if (temp == NULL) { cout << "未找到目标信息" << endl; return; }

student stp = *temp;
if ((&stp) != NULL)
{
    cout << "姓名\t英语\t数学\t";
    if (whetherLinkMore)
    {
        for (int i = 0; i < classnumberis; i++)
        {
            cout << classname[i] << "\t";
        }

    }
    cout << "总分" << endl;
    cout << stp.name << "\t" << stp.englishmark << "\t" << stp.mathmark << "\t";
    if (whetherLinkMore)
    {
        for (int i = 0; i < classnumberis; i++)
            cout << stp.moremarks->object[i] << "\t";
    }
    cout << getsallmarks(stp) << endl;

}

} void showstudent(const student &stp) {

if (&(stp) != NULL)
{
    cout << stp.name << "\t" << stp.englishmark << "\t" << stp.mathmark<<"\t";
    if (whetherLinkMore)
    {
        for (int i = 0; i < classnumberis; i++)
            cout << stp.moremarks->object[i] << "\t";
    }
    cout <<getsallmarks(stp) <<endl;

}

else
    cout << "输入的学生地址非法,或无数据可供查询,您不能打印一个空的数据表" << endl;

} void showstudents(Nodelist &p) { student *temp = p.Start; cout << "姓名\t英语\t数学\t"; if (whetherLinkMore) { for (int i = 0; i < classnumberis; i++) { cout << classname[i] << "\t"; }

}
cout << "总分"<<endl;
while (1)
{
    showstudent(*temp);
    if (temp == p.End)
        return;
    else
    temp = temp->Next;
}

} void Add_CanBaseOnName(Nodelist& p) { string name; cout << "请输入要查找的名字" << endl; cin >> name; student * temp; int flag=0; if (p.Start == NULL) { cout << "该链表为空,请直接输入要新增的数据" << endl; Add(p, p.Start); } else { temp = search(p, name); if (temp != NULL) { while (1) {

            cout << "请选择插入顺序:1 目标前,2 目标后" << endl;
            cin >> flag;
            if (flag == 1 ||flag == 2)
                break;
            else
                cout << "请输入提供的选项" << endl;
        }

        if (flag == 1)
            Addtofront(p, temp);
        else
            Add(p, temp);
    }
    else
        cout << "未找到提供的目标" << endl;
}

} void deletNodeList(Nodelist &p, student *stp) { if (0 == p.getlenth()) { cout << "数据为空!" << endl; return; } if (1 == p.getlenth() && stp == p.Start) {

        delete p.Start;
        p.Start = NULL;
        p.End = NULL;
        cout << "删除成功" << endl;
    return;

}

if (stp==p.Start)
{
    p.Start = stp->Next;
    delete stp;
    cout << "表头删除成功" << endl;
    return;
}
student *srch = p.Start;
while (srch)
{

    if (srch->Next == p.End)
    {
        if (srch->Next == stp)
        {
            delete stp;
            srch->Next = NULL;
            p.End = srch;
            cout << "删除成功" << endl;
        }
        else
            cout << "未找到要删除的目标信息" << endl;
        return;
    }
    if (srch->Next == stp)//找到他前面的一个
    {

        srch->Next = stp->Next;
        delete stp;
        cout << "删除成功" << endl;
        return;
    }
    srch = srch->Next;
}

} void deletNodeList(Nodelist &p) { string name; if (0 == p.getlenth()) { cout << "数据为空!" << endl; return; }

cout << "请输入要删除的姓名"<<endl;
cin >> name;
if (1 == p.getlenth())
{
    if (p.Start->name == name)
    {
        delete p.Start;
        p.Start = NULL;
        p.End = NULL;
        cout << "删除成功,现在链表为空" << endl;

    }
    else
        cout << "未找到要删除的目标信息" << endl;
        return;

}
student *srch = p.Start;
if (srch->name == name)
{
    p.Start = srch->Next;
    delete srch;
    cout << "表头删除成功" << endl;
    return;
}
while (srch)
{

    if (srch->Next == p.End)
    {
        if (srch->Next->name == name)
        {
            delete srch->Next;
            srch->Next = NULL;
            p.End = srch;
            cout << "删除成功" << endl;
        }
        else
            cout << "未找到要删除的目标信息" << endl;
        return;
    }
    if (srch->Next->name==name)//找到他前面的一个
    {
        student *temp = srch->Next;//temp就是要删除的信息
        srch->Next = temp->Next;

        delete temp;
        cout << "删除成功" << endl;
        return;
    }
    srch = srch->Next;
}

} void changestudent(Nodelist &p) { if (0 == p.getlenth()) { cout << "当前数据为空!" << endl; return; } string name; cout << "请输入要替换的学生名字" << endl; cin >> name; student *temp = search(p, name); if (temp == NULL) { cout << "未查询到要求改的目标" << endl; }

else 
{
    Add(p,temp);
    deletNodeList(p, temp);
    cout << "修改成功" << endl;
}

}

int main() { cout << "################################################" << endl; cout << "###############学生成绩管理查询系统#############" << endl; cout << "################################################" << endl<<endl; cout << " 支持自定义无限数量的额外科目,以供拓展,默认提供英语,数学的成绩输入 " << endl; cout << "您可以自定义科目的名字,但请注意不能超过10个字符" << endl; cout << "在【按名字插入】功能中,您可以把新的信息录入到任意位置" << endl; cout << "###############以下内容等待实现#############" << endl; cout << "按照指定的科目进行排名并打印。从文件读取、保存等操作" << endl; cout << "排名操作会生成一个新的链表根据需要是否进行替换,或者仅更改输出的顺序 " << endl<<endl<<endl;

    Nodelist newstudents;
    cout << "是否启用额外科目?    1是   0否" << endl;
    cin >> whetherLinkMore;
    if (whetherLinkMore)
    {
        cout << "请输入额外的科目数量" << endl;
        cin >> classnumberis;
        if (!classnumberis)
        {

            cout << "额外科目数量为0,强制关闭额外科目模式!!!" << endl;
            whetherLinkMore = false;
        }
    }
    if(whetherLinkMore)
    {
        for (int i = 0; i < classnumberis; i++)
        {
            cout << "请输入科目名字" << i + 1 << endl;
            cin >> classname[i];
        }

        cout << "额外科目录入完成!" << endl;

    }

    cout << "请输入操作:1增加 2删除 3查找 4修改 5按名字插入 6问询链表长度 7查询全部数据 8退出" << endl;
cin >> option;
while (1)
{

    switch (option)
    {
    case 增加:Add(newstudents, newstudents.Start); break;
    case 删除:deletNodeList(newstudents); break;
    case 查找:searchone(newstudents); break;
    case 修改:changestudent(newstudents); break;
    case 查询全部数据:showstudents(newstudents); break;
    case 按名字插入:Add_CanBaseOnName(newstudents); break;
    case 问询链表长度:cout<<"长度是"<<newstudents.getlenth()<<endl; break;
    case 退出:return 0; break;
    default:cout << "输入错误" << endl; break;
    }
    cout << "请输入操作:1增加 2删除 3查找 4修改 5按名字插入 6问询链表长度 7查询全部数据" << endl;

    cin >> option;
    getchar();

}
return 0;

}

SWQXDBA commented 3 years ago

基于链表的学生成绩管理系统,支持自定义科目数量、名称。实现了增删查改,以及按需插入新信息到任意位置。