PTA 7-1 磁盘文件的读写

编程构建一个Student类,并在main函数中创建5个对象(对象信息如输入样例所示),其中每个对象的数据包括学号num、姓名name、年龄age以及数学maths、英语english、语文chinese三门功课的分数,然后求出每个人的平均分数ave,将学号、姓名和平均分数输出到磁盘文件STUD.DAT中,最后从STUD.DAT文件中读出这些数据,并显示在屏幕上。

输入格式:

5个学生的数据(学号、姓名、年龄以及数学、英语、语文三门功课的分数)。

输出格式:

从STUD.DAT文件中读出学号、姓名和平均分数。

输入样例:

在这里给出一组输入。例如:

Student stu1(1,'A',19,80,79,67);
Student stu2(2,'B',20,90,68,43);
Student stu3(3,'C',19,56,48,29);
Student stu4(4,'D',20,93,44,57);
Student stu5(5,'E',19,33,55,74);

输出样例:

在这里给出相应的输出。例如:

1 A 75.3333
2 B 67
3 C 44.3333
4 D 64.6667
5 E 54

先上自己写的满足题意的写法,但是显示段错误,在编译器上是能通过的

#include <iostream>
#include <fstream>
#include <sstream>
#include <vector>	//std::vector
#include <string> 	//std::string

std::ofstream outputFile("STUD.DAT", std::ios::out);
std::ifstream readFile("STUD.DAT", std::ios::in);

class Student {
private:
	int sno;
	char sname;
	int sage;
	int math;
	int english;
	int chinese;
	double avg;
public:
	Student() {}
	Student(int sno, char sname, int sage, int math, int english, int chinese, double avg) {
		this->sno = sno;
		this->sname = sname;
		this->sage = sage;
		this->math = math;
		this->english = english;
		this->chinese = chinese;
		this->avg = avg;
	}
	~Student() {}
	void display() {
		outputFile << sno << ' ' << sname << ' ' << avg << '\n';
	}
	void setStudent(int sno_, char sname_, int sage_, int math_, int english_, int chinese_, double avg_) {
		sno = sno_;
		sname = sname_;
		sage = sage_;
		math = math_;
		chinese = chinese_;
		avg = avg_;
	}
};
//C++ split
std::vector<std::string> Split(const std::string& s, const char& seperator) {
	std::vector<std::string> ans;
	std::istringstream stream(s);
	std::string word;
	while (std::getline(stream, word, seperator)) {
		ans.push_back(word);
	}
	return ans;
}

int main() {
	Student obj[5];
	std::string str;
	for (int i = 0; i < 5; i++) {
		getline(std::cin, str);
		std::string tmp = str.substr(str.find('(') + 1, str.size() - str.find('(') - 2);
		std::vector<std::string> ans = Split(tmp, ',');
		for (auto& x : ans) {
			std::cout << x << ' ';
		}
		std::cout << '\n';
		double avg = 1.0 * (stoi(ans[3]) + stoi(ans[4]) + stoi(ans[5])) / 3.0;
		obj[i].setStudent(stoi(ans[0]), ans[1][1], stoi(ans[2]), stoi(ans[3]), stoi(ans[4]), stoi(ans[5]), avg);
	}
	for (auto& x : obj) {
		x.display();
	}
	outputFile.close();
	for (int i = 0; i < 5; i++) {
		getline(readFile, str);
		std::cout << str << (i == 4 ? "" : "\n");
	}
	readFile.close();
	return 0;
}

 不得不说这题出的真是一言难尽....  后来再仔细读题发现 本题的信息

 编程构建一个Student类,并在main函数中创建5个对象(对象信息如输入样例所示)

意思就是不用再读入数据了 直接在主函数创建所给出的对象即可

这时我继续用字符串读写 编译器仍然能通过 但pta上过不了

/*
	Author: Spare Lin
	Date: 30/05/22 19:03
	Description: 7-1 磁盘文件的读写
*/

#include <iostream>
#include <fstream>
#include <sstream>
#include <vector>	//std::vector
#include <string> 	//std::string

std::ofstream outputFile("STUD.txt", std::ios::out);
std::ifstream readFile("STUD.txt", std::ios::in);

class Student {
private:
	int sno;
	char sname;
	int sage;
	int math;
	int english;
	int chinese;
	double avg;
public:
	Student() {}
	Student(int sno, char sname, int sage, int math, int english, int chinese) {
		this->sno = sno;
		this->sname = sname;
		this->sage = sage;
		this->math = math;
		this->english = english;
		this->chinese = chinese;
	}
	~Student() {}
	void display() {
		outputFile << sno << ' ' << sname << ' ' << 1.0 * (math + english + chinese) / 3 << '\n';
	}
};

int main() {
	Student stu1(1,'A',19,80,79,67);
	Student stu2(2,'B',20,90,68,43);
	Student stu3(3,'C',19,56,48,29);
	Student stu4(4,'D',20,93,44,57);
	Student stu5(5,'E',19,33,55,74);
	
	std::vector<Student> ans;
	
	ans.push_back(stu1);
	ans.push_back(stu2);
	ans.push_back(stu3);
	ans.push_back(stu4);
	ans.push_back(stu5);
	
	for (auto &obj : ans) {
		obj.display();
	}
	
	outputFile.close();
	
	std::string str;
	for (int i = 0; i < 5; i++) {
		getline(readFile, str);
		std::cout << str << (i == 4 ? "": "\n");
	}
	
	readFile.close();
	
	return 0;
}

 最后又用二进制读写文件的方法 顺利通过

 AC代码

/*
    Author: Spare Lin
    Date: 30/05/22 19:03
    Description: 7-1 磁盘文件的读写
*/
#include <iostream>
#include <fstream>
#include <vector>    //std::vector
#include <string>    //std::string

class Student {
private:
    int sno;
    char sname;
    int sage;
    int math;
    int english;
    int chinese;
    double avg;
public:
    Student(int sno, char sname, int sage, int math, int english, int chinese) {
        this->sno = sno;
        this->sname = sname;
        this->sage = sage;
        this->math = math;
        this->english = english;
        this->chinese = chinese;
        //this->avg = 1.0 * (math + english + chinese) / 3;
    }

    const int getSno() {
        return sno;
    }

    const char getSname() {
        return sname;
    }

    const double getAvg() {
        return 1.0 * (math + english + chinese) / 3;
    }

    ~Student() {}
};

struct Person {
    int sno;
    char sname;
    double avg;
};

int main() {
    Student stu1(1, 'A', 19, 80, 79, 67);
    Student stu2(2, 'B', 20, 90, 68, 43);
    Student stu3(3, 'C', 19, 56, 48, 29);
    Student stu4(4, 'D', 20, 93, 44, 57);
    Student stu5(5, 'E', 19, 33, 55, 74);

    std::vector<Student> ans;
    ans.push_back(stu1);
    ans.push_back(stu2);
    ans.push_back(stu3);
    ans.push_back(stu4);
    ans.push_back(stu5);

    Person per[5];
    for (int i = 0; i < 5; i++) {
        per[i].sno = ans[i].getSno();
        per[i].sname = ans[i].getSname();
        per[i].avg = ans[i].getAvg();
    }

    std::ofstream outputFile;
    outputFile.open("STUD.DAT", std::ios::out | std::ios::binary);
    for (int i = 0; i < 5; i++) {
        outputFile.write((char*)&per[i], sizeof(per[i]));
    }
    outputFile.close();

    std::ifstream readFile;
    readFile.open("STUD.DAT", std::ios::in | std::ios::binary);
    for (int i = 0; i < 5; i++) {
        readFile.read((char*)&per[i], sizeof(per[i]));
        std::cout << per[i].sno << ' ' << per[i].sname << ' ' << per[i].avg << '\n';
    }
    readFile.close();

    return 0;
}

参考:7-1 磁盘文件的读写_xp_xht123的博客-CSDN博客_磁盘文件的读写

  • 4
    点赞
  • 13
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值