鱼C论坛

 找回密码
 立即注册
查看: 731|回复: 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
  1. #include <iostream>
  2. #include <fstream>
  3. #include <iomanip>
  4. #include <string>
  5. #define MAX 100 // 最大储存人数

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

  7. // 学生个资
  8. class Student {
  9. public:
  10.         std::string m_name;
  11.         int m_id, m_score;
  12.         Student();
  13.         Student(std::string, int, int);
  14.         ~Student();
  15. };

  16. Student::Student() {
  17.         m_name = "";
  18.         m_id = -1;
  19.         m_score = -1;
  20. }

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

  23. // 联络列表
  24. class ContactBooks {
  25. public:
  26.         Student list[MAX];
  27.         size_t m_size = 0;
  28. };

  29. // 读取文件:ifstream
  30. void readFile() {
  31.         std::string message;
  32.         std::ifstream file;
  33.         file.open("text.txt");
  34.         while (getline(file, message))
  35.         {
  36.                 cout << message << endl;
  37.         }
  38.         file.close();
  39. }

  40. // 更新文件:ofstream
  41. void updateFile(ContactBooks &student) {
  42.         std::ofstream file;
  43.         int N = student.m_size;
  44.         file.open("text.txt", std::ios::trunc);
  45.         for (int i = 0; i < N; i++) {
  46.                 file
  47.                         << student.list[i].m_id << " "
  48.                         << student.list[i].m_name << " "
  49.                         << student.list[i].m_score << " "
  50.                         << endl;
  51.         }
  52.         file.close();
  53. }

  54. // 写入文件:ofstream
  55. void writeFile(Student student, ContactBooks **Book) {
  56.         std::ofstream file;
  57.         ContactBooks* p = *Book;
  58.         p->list[p->m_size++] = student;
  59.         file.open("text.txt", std::ios::app);
  60.         file
  61.                 << student.m_id << " "
  62.                 << student.m_name << " "
  63.                 << student.m_score << " "
  64.                 << endl;
  65.         file.close();
  66. }

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

  70.         // 排序列表里的元素
  71.         int N = static_cast<int> (Book.m_size);
  72.         for (int i = 0; i < N - 1; i++) {
  73.                 for (int j = i + 1; j < N; j++) {
  74.                         if (Book.list[i].m_score > Book.list[j].m_score) {
  75.                                 temp = *(Book.list + i);
  76.                                 *(Book.list + i) = *(Book.list + j);
  77.                                 *(Book.list + j) = temp;
  78.                         }
  79.                 }
  80.         }

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

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

  88.         std::string name;
  89.         int id, score;

  90.         cout
  91.                 << "请输入学生的姓名: "
  92.                 << endl;
  93.         cin >> name;

  94.         cout
  95.                 << "请输入学生的学号:  "
  96.                 << endl;
  97.         cin >> id;

  98.         cout
  99.                 << "请输入学生的总成绩:  "
  100.                 << endl;
  101.         cin >> score;

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

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

  106.         system("pause");
  107.         system("cls");
  108. }

  109. // 菜单
  110. void menu() {
  111.         cout
  112.                 << std::setfill('*') << std::setw(25) << endl
  113.                 << std::setfill(' ') << std::setw(15)
  114.                 << " |  请选择你的模式   |" << endl
  115.                 << std::setfill(' ') << std::setw(15)
  116.                 << " |  1、输入数据      |" << endl
  117.                 << std::setfill(' ') << std::setw(15)
  118.                 << " |  2、输出数据      |" << endl
  119.                 << std::setfill(' ') << std::setw(15)
  120.                 << " |  3、进行排序      |" << endl
  121.                 << std::setfill(' ') << std::setw(15)
  122.                 << " |  4、退出程序      |" << endl
  123.                 << std::setfill('*') << std::setw(25) << endl;

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

  126. // 指令
  127. int command(ContactBooks Book) {
  128.         while (true) {
  129.                 menu();
  130.                 int cmd;
  131.                 cin >> cmd;

  132.                 switch (cmd)
  133.                 {
  134.                 case 1:
  135.                         insert(Book);
  136.                         break;
  137.                 case 2:
  138.                         readFile();
  139.                         break;
  140.                 case 3:
  141.                         sort(Book);
  142.                         break;
  143.                 case 4:
  144.                         cout << "欢迎下次使用" << endl;
  145.                         return 0;
  146.                 default:
  147.                         cout << "请输入有效的数字。" << endl;
  148.                         system("pause");
  149.                         system("cls");
  150.                         break;
  151.                 }
  152.         }
  153. }

  154. int main() {
  155.         ContactBooks Book; // 创建列表
  156.         return command(Book);
  157. }
复制代码
想知道小甲鱼最近在做啥?请访问 -> 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 | 显示全部楼层    本楼为最佳答案   
  1. #include <iostream>
  2. #include <fstream>
  3. #include <iomanip>
  4. #include <string>
  5. #define MAX 100 // 最大储存人数

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

  7. // 学生个资
  8. class Student {
  9. public:
  10.         std::string m_name;
  11.         int m_id, m_score;
  12.         Student();
  13.         Student(std::string, int, int);
  14.         ~Student();
  15. };

  16. Student::Student() {
  17.         m_name = "";
  18.         m_id = -1;
  19.         m_score = -1;
  20. }

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

  23. // 联络列表
  24. class ContactBooks {
  25. public:
  26.         Student list[MAX];
  27.         size_t m_size = 0;
  28. };

  29. // 读取文件:ifstream
  30. void readFile() {
  31.         std::string message;
  32.         std::ifstream file;
  33.         file.open("text.txt");
  34.         while (getline(file, message))
  35.         {
  36.                 cout << message << endl;
  37.         }
  38.         file.close();
  39. }

  40. // 更新文件:ofstream
  41. void updateFile(ContactBooks &student) {
  42.         std::ofstream file;
  43.         int N = student.m_size;
  44.         file.open("text.txt", std::ios::trunc);
  45.         for (int i = 0; i < N; i++) {
  46.                 file
  47.                         << student.list[i].m_id << " "
  48.                         << student.list[i].m_name << " "
  49.                         << student.list[i].m_score << " "
  50.                         << endl;
  51.         }
  52.         file.close();
  53. }

  54. // 写入文件:ofstream
  55. void writeFile(Student student, ContactBooks **Book) {
  56.         std::ofstream file;
  57.         ContactBooks* p = *Book;
  58.         p->list[p->m_size++] = student;
  59.         file.open("text.txt", std::ios::app);
  60.         file
  61.                 << student.m_id << " "
  62.                 << student.m_name << " "
  63.                 << student.m_score << " "
  64.                 << endl;
  65.         file.close();
  66. }

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

  70.         // 排序列表里的元素
  71.         int N = static_cast<int> (Book.m_size);
  72.         for (int i = 0; i < N - 1; i++) {
  73.                 for (int j = i + 1; j < N; j++) {
  74.                         if (Book.list[i].m_score > Book.list[j].m_score) {
  75.                                 temp = *(Book.list + i);
  76.                                 *(Book.list + i) = *(Book.list + j);
  77.                                 *(Book.list + j) = temp;
  78.                         }
  79.                 }
  80.         }

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

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

  88.         std::string name;
  89.         int id, score;

  90.         cout
  91.                 << "请输入学生的姓名: "
  92.                 << endl;
  93.         cin >> name;

  94.         cout
  95.                 << "请输入学生的学号:  "
  96.                 << endl;
  97.         cin >> id;

  98.         cout
  99.                 << "请输入学生的总成绩:  "
  100.                 << endl;
  101.         cin >> score;

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

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

  106.         system("pause");
  107.         system("cls");
  108. }

  109. // 菜单
  110. void menu() {
  111.         cout
  112.                 << std::setfill('*') << std::setw(25) << endl
  113.                 << std::setfill(' ') << std::setw(15)
  114.                 << " |  请选择你的模式   |" << endl
  115.                 << std::setfill(' ') << std::setw(15)
  116.                 << " |  1、输入数据      |" << endl
  117.                 << std::setfill(' ') << std::setw(15)
  118.                 << " |  2、输出数据      |" << endl
  119.                 << std::setfill(' ') << std::setw(15)
  120.                 << " |  3、进行排序      |" << endl
  121.                 << std::setfill(' ') << std::setw(15)
  122.                 << " |  4、退出程序      |" << endl
  123.                 << std::setfill('*') << std::setw(25) << endl;

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

  126. // 指令
  127. int command(ContactBooks Book) {
  128.         while (true) {
  129.                 menu();
  130.                 int cmd;
  131.                 cin >> cmd;

  132.                 switch (cmd)
  133.                 {
  134.                 case 1:
  135.                         insert(Book);
  136.                         break;
  137.                 case 2:
  138.                         readFile();
  139.                         break;
  140.                 case 3:
  141.                         sort(Book);
  142.                         break;
  143.                 case 4:
  144.                         cout << "欢迎下次使用" << endl;
  145.                         return 0;
  146.                 default:
  147.                         cout << "请输入有效的数字。" << endl;
  148.                         system("pause");
  149.                         system("cls");
  150.                         break;
  151.                 }
  152.         }
  153. }

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

使用道具 举报

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

使用道具 举报

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

本版积分规则

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

GMT+8, 2024-5-19 19:20

Powered by Discuz! X3.4

© 2001-2023 Discuz! Team.

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