bsqy 2022-03-13 18:51 采纳率: 66.7%
浏览 19
已结题

为什么我的排序功能px()超过5个人就会排序错误少于5人却没问题


#include<iostream>
#include<fstream>
#include <string.h>
/*zjm  主界面
xz   选择
cw  错误
sr  输入
tj   添加功能
xs  显示功能
cz 查找功能
xg  修改功能
sc  删除功能
cc 存储功能
xh  学号
mz  名字
xb  性别
nl  年龄
cj  成绩
zh 账号
mm 密码
px 排序 
*/
using namespace std;
void zjm();
void cw();
void xz(); 
void sr();
void tj();
void xs();
void cz();
void xg();
void sc();
void cc();
void px();
int N;

struct student
{
    char xh[20];
    char mz[20];
    char xb[20];
    char nl[20];
    int  cj;
}stu[100];


int main()
{
    zjm(); 
    return 0;
}


void zjm() 
{
    system("color F0");
    cout<<endl; 
    cout<<"              ╔════════════════════════╗"<<endl;
    cout<<"              ║                                                ║"<<endl;
    cout<<"              ║                 欢迎登陆本系统                 ║"<<endl;
    cout<<"              ║                                                ║"<<endl; 
    cout<<"              ║                                                ║"<<endl;
    cout<<"              ╚════════════════════════╝"<<endl; 
    cout<<"               ◤━━━━━━━━━━━━━━━━━━━━━━━◥"<<endl;
    cout<<"                                                                 "<<endl;
    cout<<"                          ≡≡≡学生成绩管理系统 ≡≡≡          "<<endl;
    cout<<"               ◣━━━━━━━━━━━━━━━━━━━━━━━◢"<<endl;
    cout<<"              ╔════════════════════════╗"<<endl;  
    cout<<"              ║              组长:                            ║"<<endl; 
    cout<<"              ║              组员:                               ║"<<endl;
    cout<<"              ║              组员:                               ║"<<endl; 
    cout<<"              ║              指导教授:                        ║"<<endl;
    cout<<"              ╚════════════════════════╝"<<endl;
    char zh[10];
    char mm[10];
    cout<<"                              请输入用户密码"<<endl;    
    cout<<"                              用户:";
    cin>>zh;
    cout<<"                              密码:";
    cin>>mm;
    if(0== strcmp( "yx21",zh) && 0== strcmp( "123",mm))
    {
        xz();
    }
    else
    {
        cw();
    }
} 


void cw()
{
    system("cls");
    cout<<"              ╔════════════════════════╗"<<endl; 
    cout<<"              ║          ≡≡≡学生成绩管理系统 ≡≡≡         ║"<<endl; 
    cout<<"              ╚════════════════════════╝"<<endl;
    cout<<"                                用户名或密码错误!                 "<<endl; 
    cout<<"                                请重新输入用户密码"<<endl;
    cout<<"                                   用户:";
    char zh[10];
    char mm[10];
    cin>>zh;
    cout<<"                                   密码:";
    cin>>mm;
    if( 0== strcmp( "yx21",zh) && 0== strcmp( "123",mm))
    {
        xz();
    }    
    else
    {
        cw();
    }    
} 


void xz()
{
    system("cls"); 
    cout<<"              ╔════════════════════════╗"<<endl;
    cout<<"              ║          欢迎进入学生信息管理系统              ║"<<endl;
    cout<<"              ╚════════════════════════╝"<<endl; 
    cout<<"               ◤━━━━━━━━━━━━━━━━━━━━━━━◥"<<endl;
    cout<<"               ┃           <1>.   输入学生信息                ┃"<<endl;                        
    cout<<"               ┃           <2>.   添加学生信息                ┃"<<endl; 
    cout<<"               ┃           <3>.   显示学生信息                ┃"<<endl; 
    cout<<"               ┃           <4>.   查找学生信息                ┃"<<endl; 
    cout<<"               ┃           <5>.   修改学生信息                ┃"<<endl;                        
    cout<<"               ┃           <6>.   删除学生信息                ┃"<<endl;
    cout<<"               ┃           <7>.   保存学生信息                ┃"<<endl;
    cout<<"               ┃           <8>.   排序                        ┃"<<endl; 
    cout<<"               ┃           <0>.     退出系统                  ┃"<<endl; 
    cout<<"               ◣━━━━━━━━━━━━━━━━━━━━━━━◢"<<endl;
    char a;
    cout<<"                         请输入您要进行的操作选项:";
    cin>>a;
    switch(a)
    {
        case '1': sr();xz();break;
        case '2': tj();xz();break;
        case '3': xs();xz();break;
        case '4': cz();xz();break;
        case '5': xg();xz();break;
        case '6': sc();xz();break;
        case '7': cc();xz();break;
        case '8': px();xz();break;
        case '0': break;
    }
}


void sr()
{
    system("cls");
    cout<<"请输入学生的人数:";
    cin>>N;
    cout<<"现在开始输入: "<<endl;
    cout<<"━━━━━━━━━━━━━━━━"<<endl;
    cout<<"学号   姓名   性别   年龄  成绩"<<endl;
    cout<<"━━━━━━━━━━━━━━━━"<<endl;
    for(int i=0;i<N;i++)
    {
        cin>>stu[i].xh>>stu[i].mz>>stu[i].xb>>stu[i].nl>>stu[i].cj;
    }
}


void tj()
{
    system("cls");
    cout<<"请输入您要添加的学生的信息:"<<endl;
    cout<<"━━━━━━━━━━━━━━"<<endl;
    cout<<"学号  姓名  性别  年龄  成绩"<<endl;
    cout<<"━━━━━━━━━━━━━━"<<endl;
    cin>>stu[N].xh>>stu[N].mz>>stu[N].xb>>stu[N].nl>>stu[N].cj;
    N++;   //学生人数N+1
    system("pause");
}


void xs()
{
    system("cls");
    cout<<"━━━━━━━━━━━━━━"<<endl;
    cout<<"学号  姓名  性别  年龄  成绩"<<endl;
    cout<<"━━━━━━━━━━━━━━"<<endl;
    for(int i=0;i<N;i++)
    {
        cout<<stu[i].xh<<"   "<<stu[i].mz<<"   "<<stu[i].xb<<"   "<<stu[i].nl<<"   "<<stu[i].cj;
        cout<<endl;
    }
    system("pause");
}


void cz()
{
    system("cls");
    char number[20];
    cout<<"请输入您要查询的学生的学号:";
    cin>>number;    
    cout<<"━━━━━━━━━━━━━━"<<endl;
    cout<<"学号  姓名  性别  年龄  成绩"<<endl;
    cout<<"━━━━━━━━━━━━━━"<<endl;
    for(int i=0;i<N;i++)
    {
        if(strcmp(stu[i].xh,number)==0)
        {
            cout<<stu[i].xh<<"   "<<stu[i].mz<<"   "<<stu[i].xb<<"   "<<stu[i].nl<<"   "<<stu[i].cj;
        }
    }
    cout<<endl;
    system("pause");
}


void xg()
{
    system("cls");  
    char number[20]; 
    cout<<"请输入您要修改的学生的学号:";
    cin>>number; 
    cout<<"请输入新的信息:"<<endl;
    cout<<"━━━━━━━━━━━━━━"<<endl;
    cout<<"学号  姓名  性别  年龄  成绩"<<endl;
    cout<<"━━━━━━━━━━━━━━"<<endl;
    for(int i=0;i<N;i++) 
    {
        if(strcmp(stu[i].xh,number)==0)  
            cin>>stu[i].xh>>stu[i].mz>>stu[i].xb>>stu[i].nl>>stu[i].cj;
    }
    system("pause");
}

void sc()
{
    system("cls");
    char number[20];
    cout<<"请输入要删除的学生的学号:";
    cin>>number;
    for(int i=0;i<N;i++)
    {
        if(strcmp(stu[i].xh,number)==0)
        {
            for(i;i<N;i++)
                {
                    stu[i]=stu[i+1];
                }
        }    
    }
    cout<<"删除学生信息执行完毕,已删除该学生信息"<<endl;
    N--;
    system("pause");
}


void cc()
{
    system("cls");
    ofstream outfile("student.txt",ios::out);
    for(int i=0;i<N;i++)
    {
        outfile<<stu[i].xh<<"   "<<stu[i].mz<<"   "<<stu[i].xb<<"   "<<stu[i].nl<<"   "<<stu[i].cj<<endl;
    }
    outfile.close();
    cout<<"已成功保存信息。"<<endl;
    system("pause");
}


void px()
{
    system("cls");
    cout<<"━━━━━━━━━━━━━━"<<endl;
    cout<<"学号  姓名  性别  年龄  成绩"<<endl;
    cout<<"━━━━━━━━━━━━━━"<<endl;
    int temp;
    for(int i=0;i<N;i++)
    {
            if(stu[i].cj<stu[i+1].cj)
            {
                 temp=stu[i].cj;
                stu[i].cj=stu[i+1].cj;
                stu[i+1].cj=temp;
            }
    }
    for(int i=0;i<N;i++)
    {
        cout<<stu[i].cj<<"   ";
    }
    system("pause");
}
  • 写回答

2条回答 默认 最新

  • fuill 2022-03-13 19:20
    关注

    排序改这样,交换的是结构体

    struct student temp;
    for(int j=0; j<N-1; j++)
        for(int i=0; i<N-1-j; i++)
        {
            if(stu[i].cj<stu[i+1].cj)
            {
                temp=stu[i];
                stu[i]=stu[i+1];
                stu[i+1]=temp;
            }
        }
    
    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论 编辑记录
查看更多回答(1条)

报告相同问题?

问题事件

  • 系统已结题 3月21日
  • 已采纳回答 3月13日
  • 创建了问题 3月13日

悬赏问题

  • ¥15 phython如何实现以下功能?查找同一用户名的消费金额合并—
  • ¥15 孟德尔随机化怎样画共定位分析图
  • ¥18 模拟电路问题解答有偿速度
  • ¥15 CST仿真别人的模型结果仿真结果S参数完全不对
  • ¥15 误删注册表文件致win10无法开启
  • ¥15 请问在阿里云服务器中怎么利用数据库制作网站
  • ¥60 ESP32怎么烧录自启动程序
  • ¥50 html2canvas超出滚动条不显示
  • ¥15 java业务性能问题求解(sql,业务设计相关)
  • ¥15 52810 尾椎c三个a 写蓝牙地址