问题再最下面
#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;
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.m_Name = name;
ofs << name << " ";
cout << "请输入学生的学号:" << endl;
cin >> studentid;
abs.personArray.m_studentid = studentid;
ofs << studentid << " ";
cout << "请输入学生的总成绩:" << endl;
cin >> scores;
abs.personArray.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.m_Name << " ";
cout << abs.personArray.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.m_scores > abs.personArray.m_scores)
{
Person temp = abs.personArray;
abs.personArray = abs.personArray;
abs.personArray = 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.m_Name = name;
abs.personArray.m_scores = scores;
abs.personArray.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;
}
在按排序的时候没有进行排序,在文本中的顺序也没有发生变化,不知道怎么改好,求解。希望有代码帮忙,谢谢了 试试用 fstream 该数据类型通常表示文件流,且同时具有 ofstream 和 ifstream 两种功能,这意味着它可以创建文件,向文件写入信息,从文件读取信息。
你用的是 ifstream 该数据类型表示输入文件流,用于从文件读取信息。
模式试试附加 ios::out 打开文件用于写入。 本帖最后由 傻眼貓咪 于 2022-3-20 12:03 编辑
兄弟,你学习很快,几个月前看你发问的问题阶段和你现在的问题,明显增进不少啊,从 C 变 C++,很厉害。加油。
**如果有帮助到你请设最佳答案 傻眼貓咪 发表于 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;
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.m_Name = name;
ofs << name << " ";
cout << "请输入学生的学号:" << endl;
cin >> studentid;
abs.personArray.m_studentid = studentid;
ofs << studentid << " ";
cout << "请输入学生的总成绩:" << endl;
cin >> scores;
abs.personArray.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.m_scores > abs.personArray.m_scores)
{
Person temp = abs.personArray;
abs.personArray = abs.personArray;
abs.personArray = 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.m_Name = name;
abs.personArray.m_scores = scores;
abs.personArray.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;
}
还是排不了呀,求解{:5_99:} #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;
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.m_id << " "
<< student.list.m_name << " "
<< student.list.m_score << " "
<< endl;
}
file.close();
}
// 写入文件:ofstream
void writeFile(Student student, ContactBooks **Book) {
std::ofstream file;
ContactBooks* p = *Book;
p->list = 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.m_score > Book.list.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);
} 傻眼貓咪 发表于 2022-3-20 15:52
谢谢大佬
页:
[1]