question1
- 建立两个磁盘文件f1.dat和f2.dat,编程序实现以下工作:
- 从键盘输入20个整数,分别存放在两个磁盘文件中(每个文件中放10个整数);
- 从f1.dat中读10个数,然后存放在f2.dat原有数据的后面;
- 从f2.dat中读20个数
#include<iostream>
#include<fstream>
using namespace std;
int main()
{ int a;
fstream ofs1,ofs2;
ofs1.open("f1.dat",ios::out);
ofs2.open("f2.dat",ios::out);
cout<<"请输入10个int型数据到文件f1.dat中:"<<endl;
for(int i=0;i<10;i++){
if(i==0) cout<<"前五个数字:";
if(i==5) cout<<"后五个数字: ";
cin>>a;
ofs1<<a<<" ";
}
ofs1<<endl;
cout<<"请输入10个int型数据到文件f2.dat中:"<<endl;
for(int i=0;i<10;i++){
if(i==0) cout<<"前五个数字:";
if(i==5) cout<<"后五个数字: ";
cin>>a;
ofs2<<a<<" ";
}
ofs2<<endl;
//ofs1.seekg(0,ios::beg);
/*cout<<"ofs1当前指针位置:";cout<<ofs1.tellg()<<endl;
cout<<"ofs2当前指针位置:";cout<<ofs2.tellp()<<endl;*/
ofs1.close();
ofs2.close();
ofs1.open("f1.dat",ios::in);
ofs2.open("f2.dat",ios::app);
cout<<"从f1文件读10个数字,并储存到f2文件"<<endl;
while(!ofs1.eof()){
ofs1>>a;
ofs2<<a<<" ";
}
ofs2.close();
ofs2.open("f2.dat",ios::in);
//ofs2.seekg(0,ios::beg);
//cout<<"ofs2当前指针位置:";cout<<ofs2.tellg()<<endl;
for(int i=0;i<20;i++){
ofs2>>a;
cout<<a<<" ";
if(i%5==0&&i!=0) cout<<endl;
}
cout<<endl;
ofs1.close();
ofs2.close();
return 0;
}
question2
二、求一元二次方程的实根,如果没有实根,输出警告信息。如有实根,则将实根写到一个文本文件root1.txt,然后再写到一个二进制文件roots.bin文件中。
#include <iostream>
#include <fstream>
#include <math.h>
using namespace std;
class Equation
{private:
float a,b,c,delta,x1,x2;
public:
Equation()
{ cout<<"请输入a,b,c:";
cin>>a>>b>>c;
}
Equation(float aa=0 ,float bb=0 ,float cc=0 ):a(aa),b(bb),c(cc) {}
int rootExist()
{ delta=b*b-4*a*c;
if(delta<0){
cerr<<"erro! root not found!"<<endl;
return 0;
}
else{
x1 = (-b+sqrt(delta))/2/a;
x2 = (-b-sqrt(delta))/2/a;
return 1;
}}
void rootWrite()
{ ofstream ofs1,ofs2;
ofs1.open("root1.txt",ios::app|ios::in);
ofs2.open("root2.bin",ios::app|ios::binary);
if(rootExist()){
ofs1<<x1<<" "<<x2<<endl;
ofs2<<x2<<" "<<x2<<endl;
}
ofs1.close();
ofs2.close();
}
void disp()
{ if(rootExist()){
cout<<"方程 "<<a<<"x^2"<<(b>=0?"+":" ")<<b<<"x"<<(c>=0?"+":" ")<<c<<"=0 的根为:"<<endl;
cout<<" x1="<<x1<<", x2="<<x2<<endl;}
else
cout<<"方程 "<<a<<"x^2"<<(b>=0?"+":" ")<<b<<"x"<<(c>=0?"+":" ")<<c<<"=0 无根"<<endl;
}
};
int main()
{ Equation e1(4,4,1),e2(1,9,1),e3(3,1,2);
e1.rootWrite();
e1.disp();
e2.rootWrite();
e2.disp();
e3.rootWrite();
e3.disp();
return 0;
}
question3
三、编写一个简单的学生成绩管理,数据要存入文件。有增删改查询的功能。要考虑异常处理。(数组实现)
#include <iostream>
#include <fstream>
#include <iomanip>
#include <string.h>
#define Max 50
using namespace std;
class Student
{
private:
long no;
char name[20];
int score;
static int count;
public:
void add();//增加一条信息
void del();//删除 个人信息
void modify();//修个某个人信息
void disp_search();//查找某个人信息
void output();//输出所有学生信息
void save();//保存一条信息
void infoget();
};
int Student::count = 0;
int main()
{
char name[20];
int choice;
Student st;
do {
cout << " 请选择:" << endl;
cout << "1 添加学生 2 删除 3 更改信息 4 查询 5 显示所有学生信息 0 退出" << endl;
cin >> choice;
switch (choice) {
case 1:st.add(); break;
case 2:st.del(); break;
case 3:st.modify(); break;
case 4:st.disp_search(); break;
case 5:st.output(); break;
}
} while (choice != 0);
return 0;
}
void Student::add()
{
cout << "学号:"; cin >> no;
cout << "姓名:"; cin >> name;
cout << "成绩:"; cin >> score;
save();
}
void Student::del()
{
infoget();
int exit, index;
cout << "是否退出此操作? YES 1 NO 其他数字 :";
cin >> exit;
if (exit != 1) {
fstream fs;
fs.open("stu1.txt", ios::in);
Student stu[Max];
cout << "要删除的学生学号:"; cin >> index;
for (int i = 0; i < count; i++)
{
fs >> stu[i].no >> stu[i].name >> stu[i].score;
if (stu[i].no == index) {
cout << "查找到该学生,删除成功!" << endl;
index = i;
}
}
fs.close();
for (int i = index; i < count; i++)
{
stu[i].no = stu[i + 1].no;
strcpy(stu[i].name, stu[i + 1].name);
stu[i].score = stu[i + 1].score;
}
fstream fs0;
fs0.open("stu1.txt", ios::out);
for (int i = 0; i < count - 1; i++)
fs0 << setiosflags(ios::left) << setw(10) << stu[i].no << setw(10) << stu[i].name << setw(5) << stu[i].score << endl;
fs0.close();
}
}
void Student::modify()
{
int exit;
cout << "是否退出此操作? YES 1 NO 其他数字 :";
cin >> exit;
if (exit != 1) {
fstream fs;
fs.open("stu1.txt", ios::in);
Student stu[Max], st;
int i = 0, index;
while (!fs.eof())
{
fs >> stu[i].no >> stu[i].name >> stu[i].score;
i++;
}
infoget();
cout << "要修改的学生学号:"; cin >> index;
for (int i = 0; i < Student::count; i++)
if (stu[i].no == index) {
cout << "查找到该学生" << endl;
cout << "请输入新的姓名和分数(用空格隔开):";
cin >> stu[i].name >> stu[i].score;
}
fs.close();
fstream fs0;
fs0.open("stu1.txt", ios::out);
for (int i = 0; i < Student::count; i++)
fs0 << setiosflags(ios::left) << setw(10) << stu[i].no << setw(10) << stu[i].name << setw(5) << stu[i].score << endl;
fs0.close();
}
}
void Student::disp_search()
{
infoget();
int choice, find = 0;
fstream fs;
fs.open("stu1.txt", ios::in);
cout << "请输入要索引的学号:"; cin >> choice;
for (int i = 0; i < Student::count; i++) {
fs >> no >> name >> score; //逐个读取学生信息并判断是否存在
if (choice == no) {
cout << "查找成功,该同学信息为:" << setiosflags(ios::left) << setw(10) << no << setw(10) << name << setw(5) << score << endl;
find = 1;
}
}
if (find == 0)
cout << "查找失败,不存在该学生信息" << endl;
fs.close();
}
void Student::output()
{
infoget();
if (count == 0)
cout << "当前文件无学生信息" << endl;
else {
fstream fs;
fs.open("stu1.txt", ios::in);
for (int i = 0; i < Student::count; i++) {
fs >>no>> name >> score;
cout << setiosflags(ios::left)<<setw(10)<<no << setw(10) << name << setw(5) << score << endl;
}
fs.close();
}
}
void Student::save()
{
fstream fs;
Student st;
int i;
fs.open("stu1.txt", ios::app|ios::in);
infoget();
try {
for ( i = 0; i < count; i++) {
fs >> st.no >> st.name >> st.score;
if (st.score == score) {
throw i;
}
}
}
catch (int i) {
cout << "Exception: 该学生与文件中第" << i + 1 << "位学生的学号重复!" << endl;
}
fs << setiosflags(ios::left) << setw(10) << no << setw(10) << name << setw(5) << score << endl;
fs.close();
}
void Student::infoget()
{
int i = 0;
fstream fs;
fs.open("stu1.txt", ios::in);
while (!fs.eof()) {
i++;
fs >> no >> name >> score;
}
Student::count = i - 1;
fs.close();
}