C++程序设计 上机实验(第13章——输入输出流)

1. 输入三角形的三边a,b,c,计算三角形的面积的公式是 a r e a = s ( s − a ) ( s − b ) ( s − c ) , s = a + b + c 2 area=\sqrt{s(s-a)(s-b)(s-c)},s=\frac{a+b+c}2 area=s(sa)(sb)(sc) s=2a+b+c。形成三角形的条件是:a+b>c,b+c>a,c+a>b。编写程序,输入a,b,c,检查a,b,c是否满足以上条件,如不满足,由cerr输出有关出错信息。

#include <iostream>
#include <cmath>
using namespace std;
int main()
{
	double a, b, c, s, area;
	cout << "Please enter a, b, c:";
	cin >> a >> b >> c;
	if (a + b <= c) cerr << "a+b<=c,Error!" << endl;
	if (b + c <= a) cerr << "b+c<= a,Error!" << endl;
	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;
}

为简化主函数,可以将具体操作由专门的函数实现,如:

#include <iostream>
#include <cmath>
using namespace std;
void input(double& a, double& b, double& c)
{
	cout << "Please enter a, b, c:";
	cin >> a >> b >> c;
}
void area(double a, double b, double c)
{
	double s, area;
	if (a + b <= c) cerr << "a+b<=c,Error!" << endl;
	if (b + c <= a) cerr << "b+c<= a,Error!" << endl;
	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;
	}
}
int main()
{
	double a = 2, b = 3, c = 5;
	input(a, b, c);
	area(a, b, c);
	return 0;
}

运行结果:

2. 从键盘输入一批数据,要求保留3位小数,在输出时上下行小数点对齐

方法一:用控制符控制输出格式

#include<iostream>
#include<iomanip>
using namespace std;
int main()
{
	float a[5];
	cout << "input data:";
	for (int i = 0; i < 5; i++) cin >> a[i]; //输入5个数给a[0]~a[4]
	cout << setiosflags(ios::fixed) << setprecision(2); //设置定点格式和精度
	for (int i = 0; i < 5; i++) cout << setw(10) << a[i] << endl; //设置域宽和精度
	return 0;
}

运行结果:

方法二:用流成员函数控制输出格式

#include<iostream>
#include<iomanip>
using namespace std;
int main()
{
	float a[5];
	cout << "input data:";
	cout.setf(ios::fixed); //设置定点格式
	cout.precision(2); //设置精度
	for (int i = 0; i < 5; i++)
	{
		cout.width(10); //设置域宽
		cout << a[i] << endl; //输出数据
	}
	return 0;
}

3. 编写程序,在显示屏上显示一个由字母B组成的三角形

#include<iostream>
#include<iomanip>
using namespace std;
int main()
{
	for (int n = 1; n < 8; n++)
		//指定输出空格时的域宽和填充字符以及输出'B'时的域宽和填充字符
		cout << setw(8 - n) << setfill(' ') << " " << setw(2 * n - 1) << setfill('B') << "B" << endl;
	return 0;
}

运行结果:

4. 建立两个磁盘文件f1.dat和f2.dat,编写程序实现以下工作:

(1)从键盘输入20个整数,分别存放在两个磁盘文件中(每个文件中放10个整数)
(2)从f1.dat读入10个数,然后存放到f2.dat文件原有数据的后面
(3)从f2.dat中读入20个整数,将它们按从小到大的顺序存放到f2.dat中(不保留原来的数据)

#include<iostream>
#include<fstream>
using namespace std;
//fun1函数从键盘输入20个整数,分别存放在两个磁盘文件中
void fun1()
{
	int a[10];
	ofstream outfile1("f1.dat"), outfile2("f2.dat"); //分别定义两个文件流对象
	if (!outfile1) //检查打开f1.dat是否成功
	{
		cerr << "open f1.dat error!" << endl;
		exit(1);
	}
	if (!outfile2) //检查打开f2.dat是否成功
	{
		cerr << "open f2.dat error!" << endl;
		exit(1);
	}
	cout << "enter 10 enteger numbers:" << endl;
	for (int i = 0; i < 10; i++) //输入10个数存放到f1.dat文件中
	{
		cin >> a[i];
		outfile1 << a[i] << " ";
	}
	cout << "enter 10 integer numbers:" << endl;
	for (int i = 0; i < 10; i++) //输入10个数存放到f2.dat文件中
	{
		cin >> a[i];
		outfile2 << a[i] << " ";
	}
	outfile1.close(); //关闭f1.dat文件
	outfile2.close(); //关闭f2.dat文件
}
//从f1.dat读入10个数,然后存放到f2.dat文件原有数据的后面
void fun2()
{
	ifstream infile("f1.dat"); //f1.dat作为输入文件
	if (!infile)
	{
		cerr << "open f1.dat error!" << endl;
		exit(1);
	}
	ofstream outfile("f2.dat", ios::app);
	//f2.dat作为输出文件,文件指针指向文件尾,向它写入的数据放在原来数据的后面
	if (!outfile)
	{
		cerr << "open f2.dat error!" << endl;
		exit(1);
	}
	int a;
	for (int i = 0; i < 10; i++)
	{
		infile >> a; //磁盘文件f2.dat读入一个整数
		outfile << a << " "; //将该数存放到f2.dat中
	}
	infile.close();
	outfile.close();
}
//从f2.dat中读入20个整数,将它们按从小到大的顺序存放到f2.dat
void fun3()
{
	ifstream infile("f2.dat"); //定义输入文件流infile,以输入方式打开f2.dat
	if (!infile)
	{
		cerr << "open f2.dat error!" << endl;
		exit(1);
	}
	int a[20];
	int i, j, t;
	for (i = 0; i < 20; i++) infile >> a[i]; //从磁盘文件f2.dat读入20个数放在数组a中
	for (i = 0; i < 19; i++) //用起泡法对20个数排序
		for (j = 0; j < 19 - i; j++)
			if (a[j] > a[j + 1])
			{
				t = a[j];
				a[j] = a[j + 1];
				a[j + 1] = t;
			}
	infile.close(); //关闭输入文件f2.dat
	ofstream outfile("f2.dat", ios::out); //或ofstream outfile("f2.dat");
	//f2.dat作为输出文件,文件中原有内容输出
	if (!outfile)
	{
		cerr << "open f2.dat error!" << endl;
		exit(1);
	}
	cout << "data in f2.dat:" << endl;
	for (i = 0; i < 20; i++)
	{
		outfile << a[i] << " "; //向f2.dat输出已排序的20个数
		cout << a[i] << " "; //同时输出到显示器
	}
	cout << endl;
	outfile.close();
}

int main()
{
	fun1(); //分别调用3个函数
	fun2();
	fun3();
	return 0;
}

5. 编写程序实现以下功能:

(1)按职工号由小到大的顺序将5个员工的数据(包括号码、姓名、年龄、工资)输出到磁盘文件中保存
(2)从键盘输入两个员工的数据(职工号大于已有的职工号),增加到文件的末尾
(3)输出文件中全部职工的数据
(4)从键盘输入一个号码,在文件中查找有无此职工号,如有则显示此职工是第几个职工,以及此职工的全部数据。如没有,就输出"无此人"。可以反复多次查询,如果输入查找的职工号为0,就结束查询。

#include<iostream>
#include<fstream>
using namespace std;
struct staff
{
	int num;
	char name[20];
	int age;
	double pay;
};

int main()
{
	staff staf[7] = //职工数组,含7个元素。先给出5个元素的值
	{
		2101,"Li",34,1203,
		2104,"Wang",23,674.5,
		2108,"Fun",54,778,
		3006,"Xue",45,476.5,
		5101,"Ling",39,656.6
	}, staf1;
	fstream iofile("staff.dat", ios::in | ios::out | ios::binary); //建立输入输出文件流
	if (!iofile)
	{
		cerr << "open error!" << endl;
		abort();
	}
	int i, m, num;
	cout << "Five staff:" << endl;
	for (i = 0; i < 5; i++)
	{
		cout << staf[i].num << " " << staf[i].name << " " << staf[i].age << " " << staf[i].pay << endl; //显示职工数据
		iofile.write((char*)&staf[i], sizeof(staf[i])); //写入文件
	}
	cout << "please input data you want insert:" << endl;
	iofile.seekp(0, ios::end); //定位在文件尾,此行也可不写
	for (i = 0; i < 2; i++) //增加两个职工的数据
	{
		cin >> staf1.num >> staf1.name >> staf1.age >> staf1.pay;
		iofile.write((char*)&staf1, sizeof(staf1)); //写到文件尾
	}
	cout << "Seven staff:" << endl;
	iofile.seekg(0, ios::beg); //定位于文件开头,此行不能省
	for (i = 0; i < 7; i++) //逐个读入并显示
	{
		iofile.read((char*)&staf[i], sizeof(staf[i])); //读入一个职工数据
		cout << staf[i].num << " " << staf[i].name << " " << staf[i].age << " " << staf[i].pay << endl; //显示一个职工数据
	}
	bool find; //用find来检测是否找到
	cout << "enter number you want search,enter 0 to stop.";
	cin >> num; //输入要查的职工号
	while (num) //num不为0时
	{
		find = false; //先设find为假,表示未找到
		iofile.seekg(0, ios::beg); //定位于文件开头
		for (i = 0; i < 7; i++)
		{
			iofile.read((char*)&staf[i], sizeof(staf[i])); //读入一个职工数据
			if (num == staf[i].num) //看职工号是否等于num
			{
				m = int(iofile.tellg()); //返回当前字节位置
				cout << num << " is No." << m / sizeof(staf1) << endl; //第几个职工
				cout << staf[i].num << " " << staf[i].name << " " << staf[i].age << " " << staf[i].pay << endl; //输出职工数据
				find = true; //表示"找到了"
				break;
			}
		}
		if (!find) //find为假表示找不到
			cout << "无此人" << endl;
		cout << "enter number you want search,enter 0 to stop.";
		cin >> num; //再查下一个
	}
	iofile.close();
	return 0;
}
  • 1
    点赞
  • 11
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值