鱼C论坛

 找回密码
 立即注册
查看: 746|回复: 2

在c++中要将文本中的东西按成绩排名,并可以在模式1中查询到,求救

[复制链接]
发表于 2022-3-17 23:49:00 | 显示全部楼层 |阅读模式

马上注册,结交更多好友,享用更多功能^_^

您需要 登录 才可以下载或查看,没有账号?立即注册

x
#include<iostream>
using namespace std;
#include<fstream>
#include<string>
#define MAX 100

class Person
{
public:
        string m_Name;
        int m_studentid = 0;
        int m_scores = 0;
};

class Addbooks
{
public:
        Person personArray[MAX];

        int m_Size = 0;
};


//1、输入模式
void Intput(Addbooks &abs, ofstream & ofs)
{

        cout << "模式一:输入数据" << endl;

        ofs.open("text.txt", ios::out|ios::app);

        string name;
        int studentid;
        int scores;

        cout << "请输入学生的姓名: " << endl;
        cin >> name;
        abs.personArray[abs.m_Size].m_Name = name;
        ofs << name << " ";

        cout << "请输入学生的学号:  " << endl;
        cin >> studentid;
        abs.personArray[abs.m_Size].m_studentid = studentid;
        ofs << studentid << " ";

        cout << "请输入学生的总成绩:  " << endl;
        cin >> scores;
        abs.personArray[abs.m_Size].m_scores = scores;
        ofs << scores <<endl;

        abs.m_Size++;

        ofs.close();

        cout << "导入成功!!!" << endl;
       
        system("pause");
        system("cls");

}

//2、输出模式
void Output(Addbooks& abs)
{
        ifstream ifs;

        ifs.open("text.txt", ios::in);
       
        string buf;
        while (getline(ifs, buf))
        {
                cout << buf << endl;
        }

        ifs.close();

        system("pause");
        system("cls");
}

//3、进行排序
void Sort(Addbooks& abs, ifstream &ifs)
{
        ifs.open("text.txt", ios::in);

        for (int i = 0; i < abs.m_Size; i++)
        {
                for (int j = 0; j < abs.m_Size - i - 1; j++)
                {
                        if (abs.personArray[j].m_scores > abs.personArray[j + 1].m_scores)
                        {
                                string temp1 = abs.personArray[j].m_Name;
                                abs.personArray[j].m_Name = abs.personArray[j + 1].m_Name;
                                abs.personArray[j + 1].m_Name= temp1;

                                int temp2 = abs.personArray[j].m_scores;
                                abs.personArray[j].m_scores = abs.personArray[j + 1].m_scores;
                                abs.personArray[j + 1].m_scores = temp2;

                                int temp3 = abs.personArray[j].m_studentid;
                                abs.personArray[j].m_studentid = abs.personArray[j + 1].m_studentid;
                                abs.personArray[j + 1].m_studentid = temp3;
                        }
                }
        }

        ifs.close();

        system("pause");

        system("cls");
}

void Menu()
{
        cout << "*************************" << endl;
        cout << " |  请选择你的模式   |" << endl;
        cout << " |  1、输入数据      |" << endl;
        cout << " |  2、输出数据      |" << endl;
        cout << " |  3、进行排序      |" << endl;
        cout << " |  4、退出程序      |" << endl;
        cout << "*************************" << endl;

        cout << "请输入你的模式(1 - 4)" << endl;
}

//选择模式
int SelMode(ofstream & ofs, Addbooks &abs, ifstream &ifs, void Menu())
{

        while (true)
        {
                Menu();

                int choose = 0;
                cin >> choose;

                switch (choose)
                {
                case 1:Intput(abs, ofs);
                        break;
                case 2:Output(abs);
                        break;
                case 3:Sort(abs, ifs);
                        break;
                case 4:
                        cout << "欢迎下次使用" << endl;
                        system("pause");
                        return 0;
                        break;
                default:
                        cout << "请输入有效的数字。" << endl;
                        system("pause");
                        system("cls");
                        break;
                }
        }

       
}

int main()
{       
        ofstream ofs;
        Addbooks abs;
        ifstream ifs;

        SelMode(ofs, abs, ifs,Menu);
               

        system("pause");

        return 0;
}
想知道小甲鱼最近在做啥?请访问 -> ilovefishc.com
回复

使用道具 举报

发表于 2022-3-18 06:42:49 | 显示全部楼层
掉那条河里啦?,出什么错误了?
想知道小甲鱼最近在做啥?请访问 -> ilovefishc.com
回复 支持 反对

使用道具 举报

 楼主| 发表于 2022-3-18 09:44:38 | 显示全部楼层
jhq999 发表于 2022-3-18 06:42
掉那条河里啦?,出什么错误了?


排序后在输出程序中没有排序
想知道小甲鱼最近在做啥?请访问 -> ilovefishc.com
回复 支持 反对

使用道具 举报

您需要登录后才可以回帖 登录 | 立即注册

本版积分规则

小黑屋|手机版|Archiver|鱼C工作室 ( 粤ICP备18085999号-1 | 粤公网安备 44051102000585号)

GMT+8, 2024-5-21 17:37

Powered by Discuz! X3.4

© 2001-2023 Discuz! Team.

快速回复 返回顶部 返回列表