鱼C论坛

 找回密码
 立即注册
查看: 764|回复: 5

问题在最下面

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

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

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

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)
{
        for (int i = 0;i < 3;i++)
        {
                cout << abs.personArray[i].m_Name << " ";
                cout << abs.personArray[i].m_scores << endl;
        }
       

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

void pai(Addbooks& abs)
{
        for (int i = 0; i < abs.m_Size; i++)
        {
                for (int j = 0; j < i - abs.m_Size - 1; j++)
                {
                        if (abs.personArray[j].m_scores > abs.personArray[j + 1].m_scores)
                        {
                                Person temp = abs.personArray[j];
                                abs.personArray[j] = abs.personArray[j + 1];
                                abs.personArray[j + 1] = temp;
                        }
                }
        }
}
//3、进行排序
void Sort( ifstream &ifs, Addbooks& abs)
{
        ifs.open("text.txt", ios::in);

        string name;
        int studentid;
        int scores;
       
       
        while (ifs >> name && ifs >> studentid && ifs >> scores)
        {
                abs.personArray[abs.m_Size].m_Name = name;
                abs.personArray[abs.m_Size].m_scores = scores;
                abs.personArray[abs.m_Size].m_studentid = studentid;
        }



        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(ifs,abs);
                        break;
                case 4:
                        cout << "欢迎下次使用" << endl;
                        system("pause");
                        return 0;
                        break;
                case 5:pai(abs);
                        break;
                default:
                        cout << "请输入有效的数字。" << endl;
                        system("pause");
                        system("cls");
                }
        }

       
}

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

        SelMode(ofs, abs, ifs,Menu);
               

        system("pause");

        return 0;
}


要在文本中进行排序,但我这里面利用冒泡排序后文本中依然没有改变,不知道错在哪里,请各位大神帮帮忙最好把代码发出来,球球了
想知道小甲鱼最近在做啥?请访问 -> ilovefishc.com
回复

使用道具 举报

发表于 2022-3-20 10:17:13 | 显示全部楼层
本帖最后由 傻眼貓咪 于 2022-3-20 11:13 编辑

兄弟,你的代码太长了,我眼睛不好使。你的问题是说进行排序,但排序没有效果,但我看你的代码中,我相信你写的 pai() 函数应该就是你所谓的排序函数吧(毕竟除了这里,我是没有看到其它排序代码),而在 "选择模式 - SelMode" 里只有 1~4 选项,并没有第 5 选项,而你的 pai() 在第 5 不是吗?你的 Sort() 函数写的不是排序。
想知道小甲鱼最近在做啥?请访问 -> ilovefishc.com
回复 支持 反对

使用道具 举报

 楼主| 发表于 2022-3-20 11:36:07 | 显示全部楼层
傻眼貓咪 发表于 2022-3-20 10:17
兄弟,你的代码太长了,我眼睛不好使。你的问题是说进行排序,但排序没有效果,但我看你的代码中,我相信你 ...

sry说错了 是第五选项也没有排序
想知道小甲鱼最近在做啥?请访问 -> ilovefishc.com
回复 支持 反对

使用道具 举报

发表于 2022-3-20 11:44:06 | 显示全部楼层
tucc 发表于 2022-3-20 11:36
sry说错了 是第五选项也没有排序

这就要看你的代码了,因为你的代码中,数据储存在 2 个地方,内存 和 文件流(text.txt),而你的排序好象只针对内存作排序是吗?排序后并没有更新到 text.txt 里,然后选择选项 2 读取文件流(更本毫无意义)
想知道小甲鱼最近在做啥?请访问 -> ilovefishc.com
回复 支持 反对

使用道具 举报

发表于 2022-3-20 11:45:58 | 显示全部楼层
tucc 发表于 2022-3-20 11:36
sry说错了 是第五选项也没有排序

更新文件流好象是 void Sort() 函数,但因为你分开了,选项 5 只执行 void pai() 函数
想知道小甲鱼最近在做啥?请访问 -> ilovefishc.com
回复 支持 反对

使用道具 举报

发表于 2022-3-20 11:55:50 | 显示全部楼层
兄弟,如果有帮助到你请设最佳答案
想知道小甲鱼最近在做啥?请访问 -> ilovefishc.com
回复 支持 反对

使用道具 举报

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

本版积分规则

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

GMT+8, 2024-5-22 06:43

Powered by Discuz! X3.4

© 2001-2023 Discuz! Team.

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