Experiment 9 C ++ input and output streams

The use of development tools and environment : PC machine set of Visual Studio 2010

Experimental requirements :
1. Basic Hardware Configuration: Intel PentiumIII above the level of a CPU, a greater than 64MB of memory.
2. Software Requirements: Window 2000 operating system, Visual Studio 6.0 or later development environment.
3. Experiment Duration: 2 hours
4. The realization of the subject of experimental content.
5. Write lab reports

Purpose :
1, in-depth understanding of the meaning of the input and output of its C ++ implementation.
2, master using standard input and output streams.
3, grasp the input and output operations on files.
Experiment:
(1) Enter the three sides of the triangle a, b, c, calculate the area of a triangle formula is
, // 20 minutes,
forming a triangle with the proviso that: a + b> c, b + c> a, a + c> b
programming, inputs a, b, c, check a, b, c satisfies the above conditions, if not satisfied, the output cerr information regarding the error.

#include<iostream>
#include<cmath>
using namespace std;
int main(){
	double a,b,c,s,area;
	cin>>a>>b>>c;
	if(a+b<=c)
		cerr<<"a+b<=c,error!"<<endl;
	else if(b+c<=a)
		cerr<<"b+c<=a,error!"<<endl;
	else if(c+a<=b)
		cerr<<"c+a<=b,error!"<<endl;
	else 
	{
		s=(a+b+c)/2;
		area=sqrt(s*(s-a)*(s-b)*(s-c));
		cout<<"area="<<area<<endl;
	}
	return 0;
} 

(2) (read and write text files) and create two disk files f1.dat f2.dat, programmed to achieve the following: 30 minutes @
① integer input from the keyboard 20, are stored in two disk files (each a document 10 placed integer);
② the number 10 is read from f1.dat, and then stored back into the original data file f2.dat;
③ from f2.dat 20 reads integers, they ascending stored in the order of large f2.dat (not retain the original data).

#include <iostream>
#include <fstream>
#include <algorithm>
using namespace std;
int a[100]={0};
const int Count=20;
void createDat(char *s){
	fstream f1,f2;
	f1.open("f1.dat",ios::out);
	f2.open("f2.dat",ios::out);
}
void writeDat(int i){
	fstream f1;
	f1.open("f1.dat",ios::app);
	fstream f2;
	f2.open("f2.dat",ios::app);
	if(i%2==0)
	f1<<i<<'\t';
	else
	f2<<i<<'\t';
	f1.close();
	f2.close();
}
void readDat(){
	fstream f1,f2;
	f1.open("f1.dat",ios::in);
	f2.open("f2.dat",ios::in);
	int i=0;
	while(!f1.eof()){
		f1>>a[i++];
	}
	i--;
	while(!f2.eof()){
		f2>>a[i++];
	}
}

int main(){
	createDat("f1.dat");
	createDat("f2.dat");
	int i,j;
	for(j=0;j!=Count;j++){
	cin>>i;
	writeDat(i);
}
	readDat();
	sort(a,a+Count);
	for(i=0;i<Count;i++)
	cout<<a[i]<<endl;
	return 0;
}

(3) (reading and writing binary files) programmed to achieve the following functions: (50 points)
A) in ascending order of number of workers will be five employees of the data (including the number, name, age, salary) output to a disk file save.
b) two staff from the keyboard input data (employee number is greater than the existing number of workers), added to the end of the file.
c) data for all workers in the output file.
d) a number input from the keyboard, from a lookup file has no such number of workers, this is displayed if workers are several workers, and all employees of this data. If not, then output "no such person." Queries can be repeated many times, if you enter to find employee number is 0, the query is finished.
Class reference to the following structure:

class staff
{
public:void input();
	   void display();
private:
	int id;//职工号
	char name[20];
	int age;
	double pay;//工资

};
class FileHandle
{
public:FileHandle(char *pn);
	   void writeBinaryFile();
//在此函数中输入5个职工信息,再把5人信息写到二进制文件staff.dat中
	   void writeBinaryFile(staff *pstaff,n);
//在此函数中直接写pstaff所指数组中的n个职工信息到二进制文件staff.dat中,其中n个职工信息在主函数中通过键盘输入
	   void readBinaryFile(staff *pstaff,int &n);
//从staff.dat文件中读员工信息出来存入pstaff所指数组中,所读到的职工数存入n中
private:
	char *filename;
};
int searchStaff(satff *pstaff,int n,int id);//实现在pstaff所指数组n个元素中查找员工号为id的员工。找到返回其在数组中的下标,未找到返回-1


#include<iostream> //通用输入流和其他输入流的基类
#include<fstream> //输入输出文件流类
#include<stdlib.h> //标准库头文件
using namespace std;
//录入员工信息
class staff
{
public:
staff(){}
//构造函数
staff(int num, char n[20], int ages, double wages)
{
id = num; 
strcpy_s(name,n);
age = ages;
pay = wages;
}
//输入员工信息
void input()
{
cout << " 职工号 : ";
cin >> id;
cout << " 姓名 : ";
cin >> name;
cout << " 年龄 : ";
cin >> age;
cout << " 工资 : ";
cin >> pay;
}
//输出员工信息
void display()
{cout << " 职工号 : " << id << " 姓名 : " << name << " 年龄 : " << age << " 工资 : " << pay << endl;}
int getid()
{
return id;
}
private:
int id; //职工号
char name[20]; //姓名
int age; //年龄
double pay; //工资
};
class FileHandle
{
public:
FileHandle(char *pn):filename(pn){}

//在此函数中输入5个职工信息,再把5人信息写到二进制文件staff.dat中
void writeBinaryFile(staff *pstaff, int n)
{
ofstream iofile(filename, ios::in|ios::out|ios::binary);
if(!iofile)
{
cerr << "open error!" << endl; //打开错误
abort(); //异常退出
}
for(int i = 0; i < n; i++) 
{
iofile.write((char *)&pstaff[i],sizeof(pstaff[i])); //写入文件
}
iofile.close();
}

//在此函数中直接写pstaff所指数组中的n个职工信息到二进制文件
//staff.dat中,其中n个职工信息在主函数中通过键盘输入
void writeBinaryFile(staff *pstaff)
{
ofstream iofile(filename, ios::out|ios::app);
if(!iofile)
{
cerr << "open error!" << endl;
abort(); //异常退出
}
iofile.seekp(0, ios::end);
//定位 直接跳到当前流的结尾,略过流内的所有数据。
iofile.write((char *)&pstaff, sizeof(staff));
iofile.close();
}

//从staff.dat文件中读员工信息出来存入pstaff所指数组中,所读到的职工数存入n中
void readBinaryFile(staff *pstaff, int n)
{
ifstream ifile(filename, ios::in|ios::out|ios::binary);
//创建输入文件,以输入文件打开,并以二进制方式输入
if(!ifile)
{
cerr << "open error!" << endl;
abort();
}
for(int i=0;i<n;i++)
ifile.read((char *)&pstaff[i], sizeof(pstaff[i]));
//读取文件内容以及文件长度
ifile.close();
}

//实现在pstaff所指数组n个元素中查找员工号为id的员工。
//找到返回其在数组中的下标,未找到返回-1
void searchStaff(int num)
{
bool find = false;//将find定义为错
ifstream ifile(filename, ios::in|ios::binary);
if(!ifile)
{
cerr<< " open error! " <<endl;
abort();
}
staff s;//定义成员名
while(!ifile.eof())//遍历文件内数据
{
ifile.read((char *)&s, sizeof(s));
if(s.getid() == num)
{
find = true;
break;
}
}
if(find)
{
int m = ifile.tellg(); //得到输入文件位置标记的当前位置
cout << num << " is NO. "<< m/sizeof(staff) << endl;
s.display();
}
else
{
cout << " -1 " << endl;
}
ifile.close();
}
private:
char *filename;
};

//测试
int main(){
staff staf[7] = {2001, "zhao", 34, 1203, 2002, "qian", 23, 674, 2003, "sun", 54, 778, 
2004, "li", 45, 476,2005, "zhou", 39, 656};
FileHandle file("staff.dat");
file.writeBinaryFile(s1, 5);
cout<< "please input data you want insert:" << endl;
staff s;
for(int i = 0; i < 1; i++){
s.input();
file.writeBinaryFile(&s);
}
staff s2[6];
file.readBinaryFile(s2, 6);
for(int i = 0; i < 6; i++){
s2[i].display();
}
cout << " enter number you want search,enter 0 to stop. ";
int num;
cin >> num;
while(num){
file.searchStaff(num);
cout << " enter number you want search,enter 0 to stop. ";
cin >> num;
}
return 0;
}

Results and Analysis (harvest problems)
1, in-depth understanding of the meaning of the input and output of its C ++ implementation.
2, master using standard input and output streams.
3, grasp the input and output operations on files.

Guess you like

Origin blog.csdn.net/qq_44621510/article/details/92368709