文件输入和输出练习题
来源:互联网 发布:qt4.8 json封装和解析 编辑:程序博客网 时间:2024/06/05 05:41
文件输入和输出练习题
有一个学生类Student,属性有学号、姓名、成绩。
1.需要从键盘上输入一系列学生类对象,并将这些学生信息写入的哦啊文件data.txt中。
2.显示文件data.txt中的学生数据和相应的成绩等级
#include <iostream>#include <iomanip>#include <fstream>#include <string>using namespace std;class Student{private: int number; string name; double score;public: void mark(); friend istream & operator >>(istream &,Student &); friend ostream & operator <<(ostream &,Student &);};void Student::mark(){ if(score>=90) cout<<setw(10)<<"优\n"; else if(score>=80) cout<<setw(10)<<"良\n"; else if(score>=70) cout<<setw(10)<<"中\n"; else if(score>=60) cout<<setw(10)<<"及格\n"; else cout<<setw(10)<<"不及格\n";}istream & operator>>(istream & in,Student & s){ cout<<"请依次输入学生的学号、姓名、成绩:\n"; in>>s.number>>s.name>>s.score; return in;}ostream & operator<<(ostream & out,Student & s){ out<<s.number<<setw(6)<<s.name<<setw(6)<<s.score; return out;}int main(){ int n; ofstream fout("/Users/用户名/Desktop/data.txt",ios::out);//Xcode要写明路径 if(!fout) { cerr<<"open file error!"<<endl; exit(1); } cout<<"请输入总人数:\n"; cin>>n; Student *pstu=new Student[n+1]; for(int i=0;i<n;i++) { cin>>pstu[i]; } cout<<"--------------------"<<endl; for(int i=0;i<n;i++) { cout<<pstu[i]; pstu[i].mark(); fout<<pstu[i]<<endl; } fout.close(); return 0;}
输出:
请输入总人数:3请依次输入学生的学号、姓名、成绩:171 Lily 100请依次输入学生的学号、姓名、成绩:172 Leo 80请依次输入学生的学号、姓名、成绩:173 Amy 70--------------------171 Lily 100 优172 Leo 80 良173 Amy 70 中Program ended with exit code: 0
txt文件:
//data.txt171 Lily 100172 Leo 80173 Amy 70
阅读全文
0 0
- 文件输入和输出练习题
- 文件输入 和 输出
- 文件的输入和输出
- java文件输入和输出
- 小结:文件输入和输出
- 体验文件输入和输出
- 文件格式化输入和输出
- 文件的输入和输出
- 文件的输入和输出
- 文件的输入和输出
- python文件输入和输出
- python 文件输入和输出
- 文件输入和输出流
- CRT输入和输出(文件)
- 文件的输入和输出
- 文件的输入和输出
- 【C】文件输入和输出!!!
- Python-文件输入和输出
- View事件分发源码分析(二)
- 高德地图api定位问题
- tensorflow基于softmax模型实现手写体MNIST数字集识别
- 学习shiro代码报错:Line argument must contain a key and a value. Only one string token was found.
- UVA 11300 Spreading the Wealth
- 文件输入和输出练习题
- html响应式与自适应
- keepalived的vip设置为公网IP
- Moco框架(mock-server)
- 数据挖掘系列(4)决策树分类算法
- 单身狗掐指一算:k邻近(KNN)算法举例
- 3年_面试
- python画简单图
- angularjs——service