设计一个学生类,其中包含学号、姓名、成绩等数据成员,创建学生对象并且倒入到文件file.txt,然后由文件读取到另一个学生对象并输出,试编程实现。
来源:互联网 发布:工作流程图软件 编辑:程序博客网 时间:2024/05/23 13:03
《转自论坛》
#include <iostream>
#include <fstream>
#include <stdexcept>
#include <cassert>
#include <cstdlib>
using namespace std;
class Student {
string number;
string name;
int score;
public:
Student(const char *number0, const char *name0, int score0);
bool writeToFile(const char *path) const ;
const Student readFromFile(const char *path) const;
void display() const;
};
Student::Student(const char *number0, const char *name0, int score0): number(number0), name(name0), score(score0)
{
}
bool Student::writeToFile(const char *path) const
{
assert(path);
ofstream file(path);
if(!file)
return false;
file << "number:" << number << endl;
file << "name:" << name << endl;
file << "score:" << score<< endl;
file.close();
return true;
}
const Student Student::readFromFile(const char *path) const
{
assert(path);
string number;
string name;
int score;
ifstream file(path);
if(!path)
throw runtime_error("open file to read failed");
string s;
while(file >> s) {
int pos = s.find(":");
if(s.substr(0, pos).compare("number") == 0)
number = s.substr(pos + 1);
if(s.substr(0, pos).compare("name") == 0)
name = s.substr(pos + 1);
if(s.substr(0, pos).compare("score") == 0)
score = atoi(s.substr(pos + 1).c_str());
}
return Student(number.c_str(), name.c_str(), score);
}
void Student::display() const
{
cout << "number: " << number << endl;
cout << "name: " << name << endl;
cout << "score: " << score << endl;
}
int main()
{
const char *filePath = "./file.txt";
Student stu("037165", "张三", 50);
stu.writeToFile(filePath);
Student stuCopy = stu.readFromFile(filePath);
stuCopy.display();
}
#include <iostream>
#include <fstream>
#include <stdexcept>
#include <cassert>
#include <cstdlib>
using namespace std;
class Student {
string number;
string name;
int score;
public:
Student(const char *number0, const char *name0, int score0);
bool writeToFile(const char *path) const ;
const Student readFromFile(const char *path) const;
void display() const;
};
Student::Student(const char *number0, const char *name0, int score0): number(number0), name(name0), score(score0)
{
}
bool Student::writeToFile(const char *path) const
{
assert(path);
ofstream file(path);
if(!file)
return false;
file << "number:" << number << endl;
file << "name:" << name << endl;
file << "score:" << score<< endl;
file.close();
return true;
}
const Student Student::readFromFile(const char *path) const
{
assert(path);
string number;
string name;
int score;
ifstream file(path);
if(!path)
throw runtime_error("open file to read failed");
string s;
while(file >> s) {
int pos = s.find(":");
if(s.substr(0, pos).compare("number") == 0)
number = s.substr(pos + 1);
if(s.substr(0, pos).compare("name") == 0)
name = s.substr(pos + 1);
if(s.substr(0, pos).compare("score") == 0)
score = atoi(s.substr(pos + 1).c_str());
}
return Student(number.c_str(), name.c_str(), score);
}
void Student::display() const
{
cout << "number: " << number << endl;
cout << "name: " << name << endl;
cout << "score: " << score << endl;
}
int main()
{
const char *filePath = "./file.txt";
Student stu("037165", "张三", 50);
stu.writeToFile(filePath);
Student stuCopy = stu.readFromFile(filePath);
stuCopy.display();
}
- 设计一个学生类,其中包含学号、姓名、成绩等数据成员,创建学生对象并且倒入到文件file.txt,然后由文件读取到另一个学生对象并输出,试编程实现。
- 编写一个程序,输出学生已有的学号姓名成绩等数据并计算总人数和平均成绩
- 3.1定义学生类:成员变量有学号、姓名、出生日期(字符串)、成绩;成员方法包括构造方法、计算学生年龄的方法、输出学生信息的方法;在主方法中创造一个学生对象,然后输出他的信息(包括他的年龄)
- 创建一个学生类(Student),包括学号成绩,编程输入和显示学生的信息。建立一个人类(Person),包含姓名、性别和年龄,并作为学生类的基类
- 创建一个学生类(Student),包括学号和成绩,编程输入和显示学生的信息,建立一个人类(Person),包含姓名、性别和年龄,并作为学生类的基类
- 完成一个学生管理程序,使用学号作为键添加5个学生对象,并可以将全部信息保存在文件中,可以实现对学生信息的学号查找,输出全部学生信息的功能。
- [4.21]编写一个程序,已有若干个学生的数据,包括学号、姓名、成绩、要求输出这些学生的数据并计算出学生人数和平均成绩(要求将学生人数和总成绩用静态数据成员表示)。
- 【C++】定义一个学生通讯录类,数据成员包括:学号、姓名、院系等【原创技术】
- 编写一个程序,已有若干学生的数据,包括学号,姓名,成绩,要求输出这些学生的数据并计算出学生人数和平均成绩
- 笔试题:创建一个单链表,结点包含学生的学号,姓名,性别,年龄信息.写几个程序,实现按学生学号插入,查询,删除等操作.
- 设计一个学生类,包含学号、姓名、课程、成绩,计算学生们的各科平均成绩,学生人数依输入而定
- 建立一个对象数组,内放5个学生的数据(学号,成绩),用指向对象的指针做函数参数,在max函数中找出5个学生中成绩最高者,并输出其学号。
- 2.6使用二维数组存储学生的数据,包括学号、姓名、操作系统成绩、Java成绩、高数成绩、总分;一行存储一个学生的数据;要求输入若干学生的数据,求出总分;然后按照总分由高到低重新排列;输出排序后的结果
- 要求从用户输入的多行文本中提取学生的姓名、学号及登录日期,并封装到 Student 类中作为类的私有属性。创建一个Student类型的对象数组,对学号进行 升序排序并输出。
- 要求从用户输入的多行文本中提取学生的姓名、学号及登录日期,并封装到 Student 类中作为类的私有属性。创建一个 Student 类型的对象数组,对学号进行 升序排序并输出
- Student类,包括学生姓名、性别、年龄、Java成绩。要求创建五个该类对象,输出每个学生信息,计算并输出这五个学生Java成绩的平均值, 以及计算并输出他们Ja
- 键盘录入多名学生的信息: 格式:姓名,数学成绩,语文成绩,英文成绩,按总分由高到低 将学生的信息进行排列到文件中
- 输入10个学生的姓名、学号和成绩,将其中不及格者输出
- nagios 配置详解
- 将js发送的"*,*,*,。。。"格式字符串转化为list
- DevExpress Chart,将数据库的数据绑定到Chart
- JavaScript技巧大全--经典收藏
- 行转列、列转行题目(收集中...)
- 设计一个学生类,其中包含学号、姓名、成绩等数据成员,创建学生对象并且倒入到文件file.txt,然后由文件读取到另一个学生对象并输出,试编程实现。
- 摄像头编程WindowsAPI - avicap32.dll 参数详解
- Ubuntu 11.10/12.04 下 Code::Blocks 手动修改设置程序命令行参数
- jquery过滤特殊字符',防sql注入
- 上传图片前预览效果 html css
- linux命令
- 介绍 Android DropBoxManager Service
- 动态查找---->二叉查找树(Binary Search Tree)
- cocos2d-x