鱼C论坛

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

[已解决]问题再最下面

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

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

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

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");
}


//3、进行排序
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;
                        }
                }
        }
}

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),pai(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;
}


在按排序的时候没有进行排序,在文本中的顺序也没有发生变化,不知道怎么改好,求解。希望有代码帮忙,谢谢了
最佳答案
2022-3-20 15:52:52
#include <iostream>
#include <fstream>
#include <iomanip>
#include <string>
#define MAX 100 // 最大储存人数

using std::cout, std::cin, std::endl;

// 学生个资
class Student {
public:
        std::string m_name;
        int m_id, m_score;
        Student();
        Student(std::string, int, int);
        ~Student();
};

Student::Student() {
        m_name = "";
        m_id = -1;
        m_score = -1;
}

Student::Student(std::string name, int id, int score) : m_name(name), m_id(id), m_score(score) {}
Student::~Student() {}

// 联络列表
class ContactBooks {
public:
        Student list[MAX];
        size_t m_size = 0;
};

// 读取文件:ifstream
void readFile() {
        std::string message;
        std::ifstream file;
        file.open("text.txt");
        while (getline(file, message))
        {
                cout << message << endl;
        }
        file.close();
}

// 更新文件:ofstream
void updateFile(ContactBooks &student) {
        std::ofstream file;
        int N = student.m_size;
        file.open("text.txt", std::ios::trunc);
        for (int i = 0; i < N; i++) {
                file
                        << student.list[i].m_id << " "
                        << student.list[i].m_name << " "
                        << student.list[i].m_score << " "
                        << endl;
        }
        file.close();
}

// 写入文件:ofstream
void writeFile(Student student, ContactBooks **Book) {
        std::ofstream file;
        ContactBooks* p = *Book;
        p->list[p->m_size++] = student;
        file.open("text.txt", std::ios::app);
        file
                << student.m_id << " "
                << student.m_name << " "
                << student.m_score << " "
                << endl;
        file.close();
}

// 冒泡排序 - 根据学生成绩 score 进行排序
void sort(ContactBooks& Book) {
        Student temp;

        // 排序列表里的元素
        int N = static_cast<int> (Book.m_size);
        for (int i = 0; i < N - 1; i++) {
                for (int j = i + 1; j < N; j++) {
                        if (Book.list[i].m_score > Book.list[j].m_score) {
                                temp = *(Book.list + i);
                                *(Book.list + i) = *(Book.list + j);
                                *(Book.list + j) = temp;
                        }
                }
        }

        updateFile(Book); // 更新文件内容
}

// 输入学生资料
void insert(ContactBooks &Book) {
        cout
                << "模式一: 输入数据"
                << endl << endl;

        std::string name;
        int id, score;

        cout
                << "请输入学生的姓名: "
                << endl;
        cin >> name;

        cout
                << "请输入学生的学号:  "
                << endl;
        cin >> id;

        cout
                << "请输入学生的总成绩:  "
                << endl;
        cin >> score;

        // 存入文件 text.txt 和列表 ContactBooks 里
        ContactBooks* p = &Book;
        writeFile({ name, id, score }, &p);

        cout << "导入成功!!!" << endl;

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

// 菜单
void menu() {
        cout
                << std::setfill('*') << std::setw(25) << endl
                << std::setfill(' ') << std::setw(15)
                << " |  请选择你的模式   |" << endl
                << std::setfill(' ') << std::setw(15)
                << " |  1、输入数据      |" << endl
                << std::setfill(' ') << std::setw(15)
                << " |  2、输出数据      |" << endl
                << std::setfill(' ') << std::setw(15)
                << " |  3、进行排序      |" << endl
                << std::setfill(' ') << std::setw(15)
                << " |  4、退出程序      |" << endl
                << std::setfill('*') << std::setw(25) << endl;

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

// 指令
int command(ContactBooks Book) {
        while (true) {
                menu();
                int cmd;
                cin >> cmd;

                switch (cmd)
                {
                case 1:
                        insert(Book);
                        break;
                case 2:
                        readFile();
                        break;
                case 3:
                        sort(Book);
                        break;
                case 4:
                        cout << "欢迎下次使用" << endl;
                        return 0;
                default:
                        cout << "请输入有效的数字。" << endl;
                        system("pause");
                        system("cls");
                        break;
                }
        }
}

int main() {
        ContactBooks Book; // 创建列表
        return command(Book);
}
想知道小甲鱼最近在做啥?请访问 -> ilovefishc.com
回复

使用道具 举报

发表于 2022-3-20 11:53:49 | 显示全部楼层
试试用 fstream 该数据类型通常表示文件流,且同时具有 ofstream 和 ifstream 两种功能,这意味着它可以创建文件,向文件写入信息,从文件读取信息。
你用的是 ifstream 该数据类型表示输入文件流,用于从文件读取信息

模式试试附加 ios::out 打开文件用于写入
想知道小甲鱼最近在做啥?请访问 -> ilovefishc.com
回复 支持 反对

使用道具 举报

发表于 2022-3-20 11:56:23 | 显示全部楼层
本帖最后由 傻眼貓咪 于 2022-3-20 12:03 编辑

兄弟,你学习很快,几个月前看你发问的问题阶段和你现在的问题,明显增进不少啊,从 C 变 C++,很厉害。加油。

**如果有帮助到你请设最佳答案
想知道小甲鱼最近在做啥?请访问 -> ilovefishc.com
回复 支持 反对

使用道具 举报

 楼主| 发表于 2022-3-20 15:50:23 | 显示全部楼层
傻眼貓咪 发表于 2022-3-20 11:56
兄弟,你学习很快,几个月前看你发问的问题阶段和你现在的问题,明显增进不少啊,从 C 变 C++,很厉害。加 ...

#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 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;
                        }
                }
        }
}

void Sort( fstream &fs, Addbooks& abs)
{
       
        void pai();

        fs.open("text.txt", ios::in|ios::out);

        string name;
        int studentid;
        int scores;
       
       
        while (fs >> name && fs >> studentid && fs >> 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;
        }



        fs.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, fstream& fs, void Menu())
{

        while (true)
        {
                Menu();

                int choose = 0;
                cin >> choose;

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

       
}

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

        SelMode(ofs, abs, ifs, fs,Menu);
               

        system("pause");

        return 0;
}

还是排不了呀,求解
想知道小甲鱼最近在做啥?请访问 -> ilovefishc.com
回复 支持 反对

使用道具 举报

发表于 2022-3-20 15:52:52 | 显示全部楼层    本楼为最佳答案   
#include <iostream>
#include <fstream>
#include <iomanip>
#include <string>
#define MAX 100 // 最大储存人数

using std::cout, std::cin, std::endl;

// 学生个资
class Student {
public:
        std::string m_name;
        int m_id, m_score;
        Student();
        Student(std::string, int, int);
        ~Student();
};

Student::Student() {
        m_name = "";
        m_id = -1;
        m_score = -1;
}

Student::Student(std::string name, int id, int score) : m_name(name), m_id(id), m_score(score) {}
Student::~Student() {}

// 联络列表
class ContactBooks {
public:
        Student list[MAX];
        size_t m_size = 0;
};

// 读取文件:ifstream
void readFile() {
        std::string message;
        std::ifstream file;
        file.open("text.txt");
        while (getline(file, message))
        {
                cout << message << endl;
        }
        file.close();
}

// 更新文件:ofstream
void updateFile(ContactBooks &student) {
        std::ofstream file;
        int N = student.m_size;
        file.open("text.txt", std::ios::trunc);
        for (int i = 0; i < N; i++) {
                file
                        << student.list[i].m_id << " "
                        << student.list[i].m_name << " "
                        << student.list[i].m_score << " "
                        << endl;
        }
        file.close();
}

// 写入文件:ofstream
void writeFile(Student student, ContactBooks **Book) {
        std::ofstream file;
        ContactBooks* p = *Book;
        p->list[p->m_size++] = student;
        file.open("text.txt", std::ios::app);
        file
                << student.m_id << " "
                << student.m_name << " "
                << student.m_score << " "
                << endl;
        file.close();
}

// 冒泡排序 - 根据学生成绩 score 进行排序
void sort(ContactBooks& Book) {
        Student temp;

        // 排序列表里的元素
        int N = static_cast<int> (Book.m_size);
        for (int i = 0; i < N - 1; i++) {
                for (int j = i + 1; j < N; j++) {
                        if (Book.list[i].m_score > Book.list[j].m_score) {
                                temp = *(Book.list + i);
                                *(Book.list + i) = *(Book.list + j);
                                *(Book.list + j) = temp;
                        }
                }
        }

        updateFile(Book); // 更新文件内容
}

// 输入学生资料
void insert(ContactBooks &Book) {
        cout
                << "模式一: 输入数据"
                << endl << endl;

        std::string name;
        int id, score;

        cout
                << "请输入学生的姓名: "
                << endl;
        cin >> name;

        cout
                << "请输入学生的学号:  "
                << endl;
        cin >> id;

        cout
                << "请输入学生的总成绩:  "
                << endl;
        cin >> score;

        // 存入文件 text.txt 和列表 ContactBooks 里
        ContactBooks* p = &Book;
        writeFile({ name, id, score }, &p);

        cout << "导入成功!!!" << endl;

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

// 菜单
void menu() {
        cout
                << std::setfill('*') << std::setw(25) << endl
                << std::setfill(' ') << std::setw(15)
                << " |  请选择你的模式   |" << endl
                << std::setfill(' ') << std::setw(15)
                << " |  1、输入数据      |" << endl
                << std::setfill(' ') << std::setw(15)
                << " |  2、输出数据      |" << endl
                << std::setfill(' ') << std::setw(15)
                << " |  3、进行排序      |" << endl
                << std::setfill(' ') << std::setw(15)
                << " |  4、退出程序      |" << endl
                << std::setfill('*') << std::setw(25) << endl;

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

// 指令
int command(ContactBooks Book) {
        while (true) {
                menu();
                int cmd;
                cin >> cmd;

                switch (cmd)
                {
                case 1:
                        insert(Book);
                        break;
                case 2:
                        readFile();
                        break;
                case 3:
                        sort(Book);
                        break;
                case 4:
                        cout << "欢迎下次使用" << endl;
                        return 0;
                default:
                        cout << "请输入有效的数字。" << endl;
                        system("pause");
                        system("cls");
                        break;
                }
        }
}

int main() {
        ContactBooks Book; // 创建列表
        return command(Book);
}
想知道小甲鱼最近在做啥?请访问 -> ilovefishc.com
回复 支持 反对

使用道具 举报

 楼主| 发表于 2022-3-20 17:07:38 | 显示全部楼层
想知道小甲鱼最近在做啥?请访问 -> ilovefishc.com
回复 支持 反对

使用道具 举报

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

本版积分规则

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

GMT+8, 2024-10-5 14:14

Powered by Discuz! X3.4

© 2001-2023 Discuz! Team.

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