C++程序设计(第3版)谭浩强 第13章 习题

1.输入三角形的三边a,b,c,计算三角形的面积的公式是area=sqrt(s(s-a)(s-b)(s-c)),s=a+b+c2

形成三角形的条件是: a+b>c, b+c>a, c+a>b

编写程序,输入a,b,c,检查a,b,c是否满足以上条件,如不满足,由cerr输出有关出错信息。

【解】

//13.1
//1.输入三角形的三边a,b,c,计算三角形的面积的公式是area=s(s-a)(s-b)(s-c),
//  s=a+b+c2形成三角形的条件是: 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 input a,b,c:";
	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;
}

//① please input a, b, c:2 3 5↙
//a + b <= c, error!
//② please input a, b, c : 2 3 4↙
//area = 2.90474

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

【解】

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

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

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

//input data : 12.3 345.678 3.14159 –45.321 56
//12.30
//345.68
//3.14
//–45.32
//56.00

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

                                        B
                                       BBB
                                      BBBBB
                                     BBBBBBB
                                    BBBBBBBBB
                                   BBBBBBBBBBB
                                  BBBBBBBBBBBBB

【解】

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

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

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

(1)从键盘输入20个整数,分别存放在两个磁盘文件中(每个文件中放10个整数);

(2)从f1.dat读入10个数,然后存放到f2.dat文件原有数据的后面;

(3)从f2.dat中读入20个整数,将它们按从小到大的顺序存放到f2.dat (不保留原来的数据)。

【解】

//13.4
//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 integer 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);
	// 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;
}

//enter 10 integer numbers :
//2 4 6 8 1 3 5 7 –3 0↙
//enter 10 integer numbers :
//–23 34 56 –25 22 67 20 –45 52 123↙
//data in f2.dat :
//–45 –25 –23 –3 0 1 2 3 4 5 6 7 8 20 22 34 52 56 67 123

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

(1)按职工号由小到大的顺序将5个员工的数据(包括号码、姓名、年龄、工资) 输出到磁盘文件中保存。

(2)从键盘输入两个员工的数据(职工号大于已有的职工号),增加到文件的末尾。

(3)输出文件中全部职工的数据。

(4)从键盘输入一个号码,在文件中查找有无此职工号, 如有则显示此职工是第几个职工,以及此职工的全部数据。如没有,就输出“无此人”。可以反复多次查询,如果输入查找的职工号为0,就结束查询。

【解】

//13.5
//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] = { 2101,"Li",34,1203,2104,"Wang",23,674.5,2108,"Fun",54,778,
					 3006,"Xue",45,476.5,5101,"Ling",39,656.6 }, staf1;
	//职工数组, 含7个元素。先给出5个元素的值
	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:" << end1.pay;
	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 = 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 << "can't find " << num << endl;
		cout << "enter number you want search,enter 0 to stop.";
		cin >> num;							//再查下一个
	}
	iofile.close();
	return 0;
}

//Five staff : (显示5个职工数据)
//2101 Li 34 1203
//2104 Wang 23 674.5
//2108 Fun 54 778
//3006 Xue 45 476.5
//5101 Ling 39 656.6
//please input data you want insert : (插入两个职工数据)
//6001 Tan 45 1234↙
//6800 Yi 53 1345↙
//Seven staff : (显示7个职工数据)
//2101 Li 34 1203
//2104 Wang 23 674.5
//2108 Fun 54 778
//3006 Xue 45 476.5
//5101 Ling 39 656.6
//6001 Tan 45 1234
//6800 Yi 53 1345
//enter number you want search, enter 0 to stop.3100↙		(查找3100)
//can't find 3100								(找不到)
//enter number you want search, enter 0 to stop.6001↙		(查找6001)
//6001 is No. 6                       			(找到了)
//6001 Tan 45 1234
//enter number you want search, enter 0 to stop.0↙  		(不找了, 结束)

6.在例13.17的基础上,修改程序,将存放在c数组中的数据读入并显示出来。

【解】

//13.6
//6. 在例13.17的基础上,修改程序,将存放在c数组中的数据读入并显示出来。

//13.6 方法一
#include <iostream>
#include <strstream>
using namespace std;
struct student
{
	int num;
	char name[20];
	double score;
};
int main()
{
	student stud[3] = { 1001,"Li",78,1002,"Wang",89.5,1004,"Fun",90 }, stud1[3];
	char c[50];
	int i;
	ostrstream strout(c, 50);				//建立输出串流strout, 与字符数组c关联
	for (i = 0; i < 3; i++)						//向c写入3个学生的数据
		strout << " " << stud[i].num << " " << stud[i].name << " " << stud[i].score;
	strout << ends;
	cout << "array c:" << endl << c << endl << endl;	//显示数组c的内容
	istrstream strin(c, 50);					//建立输入串流strin, 与字符数组c关联
	for (i = 0; i < 3; i++)						//从c读入3个学生的数据, 赋给stud1数组
		strin >> stud1[i].num >> stud1[i].name >> stud1[i].score;
	cout << "data from array c to array stud1:" << endl;
	for (i = 0; i < 3; i++)						//显示stud1数组各元素
		cout << stud1[i].num << " " << stud1[i].name << " " << stud1[i].score << endl;
	cout << endl;
	return 0;
}

//13.6 方法二
#include <iostream>
#include <strstream>
using namespace std;
struct student
{
	int num;
	char name[20];
	double score;
};
int main()
{
	int i;
	student stud[3] = { 1001,"Li",78,1002,"Wang",89.5,1004,"Fun",90 }, stud1[3];
	char c[50];
	strstream strio(c, 50, ios::in | ios::out);
	//建立输入输出串流strio,与字符数组c关联
	for (i = 0; i < 3; i++)							//向c写入3个学生的数据
		strio << stud[i].num << " " << stud[i].name << " " << stud[i].score << " ";
	strio << ends;
	cout << "array c:" << endl << c << endl << endl;		//显示数组c的内容
	for (i = 0; i < 3; i++)							//从c读入3个学生的数据, 赋给stud1数组
		strio >> stud1[i].num >> stud1[i].name >> stud1[i].score;
	cout << "data from array c to array stud1:" << endl;
	for (i = 0; i < 3; i++)							//显示stud1数组各元素
		cout << stud1[i].num << " " << stud1[i].name << " " << stud1[i].score << endl;
	cout << endl;
	return 0;
}

//array c:
//1001 Li 78 1002 Wang 89.5 1004 Fun 90
//data from array c to array stud1 :
//1001 Li 78
//1002 Wang 89.5
//1004 Fun 90

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值