第十三章 输入输出流
- 1、下载文档前请自行甄别文档内容的完整性,平台不提供额外的编辑、内容补充、找答案等附加服务。
- 2、"仅部分预览"的文档,不可在线预览部分如存在完整性等问题,可反馈申请退款(可完整预览的文档不适用该条件!)。
- 3、如文档侵犯您的权益,请联系客服反馈,我们会尽快为您处理(人工客服工作时间:9:00-18:30)。
第十三章 输入输出流
1. 输入三角形的三边a,b,c,计算三角形的面积公式是
构成三角形的条件是:a+b>c,b+c>a,c+a>b
编写程序,输入a,b,c是否满足以上条件,如不满足,由cerr输出有关出错信息。
#include <iostream>
#include <cmath>
using namespace std;
int main()
{
double a,b,c,s,area;
cout<<"Please input the length of 3 sides:"<<endl;
cout<<"a = ";
cin>>a;
cout<<"b = ";
cin>>b;
cout<<"c = ";
cin>>c;
if(a+b>c && b+c>a && a+c>b)
s = (a+b+c)/2.0;
else
{
cerr<<"The 3 sides can not form a triangle!"<<endl;
exit(1);
}
area = sqrt(s*(s-a)*(s-b)*(s-c));
cout<<"The area of the triangle is: "<<area<<endl;
return 0;
}
/* 运行结果
* chinsung@gentoo ~ $ g++ test.cpp
* chinsung@gentoo ~ $ ./a.out
* Please input the length of 3 sides:
* a = 3
* b = 1
* c = 1
* The 3 sides can not form a triangle!
* chinsung@gentoo ~ $ ./a.out
* Please input the length of 3 sides:
* a = 3
* b = 4
* c = 5
* The area of the triangle is: 6
*/
143
area= s s?a s?b s?c
s=abc
2
第十三章 输入输出流
2. 从键盘输入一批数值,要求保留3位小数,在输出时上下行小数点对齐。
#include <iostream>
#include <iomanip>
using namespace std;
struct Double_number
{
double d;
Double_number* next;
};
void input_node(Double_number* p)
{
cout<<"Please input a number(0 for end): ";
cin>>p->d;
}
int main()
{
Double_number *head,*p1,*p2;
head=p1=p2=new Double_number;
input_node(head);
while(p2->d!=0)
{
p1=new Double_number;
input_node(p1);
p2->next=p1;
p2=p1;
}
p2->next=NULL;
p1=head;
while(p1->d!=0)
{
cout<<setw(10)<<setiosflags(ios::fixed)<<setprecision(3)<<p1->d<<endl;
p1=p1->next;
}
return 0;
}
/* 本题用链表完成,以下为运行结果:
* Please input a number(0 for end): 12.5
* Please input a number(0 for end): 126.3
* Please input a number(0 for end): 1.023
* Please input a number(0 for end): 0
* 12.500
* 126.300
* 1.023
*/
3. 编程序,在显示屏上显示一个由字母B组成的三角形。
B
BBB
BBBBB
BBBBBBB
BBBBBBBBB
BBBBBBBBBBB
BBBBBBBBBBBBB
BBBBBBBBBBBBBBB
#include <iostream>
144
第十三章 输入输出流
using namespace std;
int main()
{
int i,j,k;//will be used all over the function, so declared here
for(i=0;i<8;i++)//number of rows
{
for(j=0;j<7-i;j++)//number of whitespaces
cout<<' ';
for(k=0;k<2*(8-j)-1;k++)//number of Bs
cout<<'B';
cout<<endl;
}
return 0;
}
4. 建立两个磁盘文件f1.dat和f2.dat,编程序实现以下工作:
1. 从键盘输入20个整
数,分别存放在两个磁盘文件中(每个文件中放10个整数);
2. 从f1.dat读入10个数,然后存放到f2.dat文件原有数据的后面;
3. 从f2.dat中读入20个整数,对它们按从小到大的顺序存放到f2.dat中,不保留原来的数据。
//Question No.1
#include <iostream>
#include <fstream>
using namespace std;
int main
()
{
const int Len=20;
int a[Len],i,j,temp;//to store Len integers
fstream file1("f1.dat",ios::out),file2("f2.dat",ios::out);
if(file1==0 || file2==0)//if fail to open, the object==0
{
cout<<"Error while opening f1.dat or f2.dat!";
exit(1);
}
cout<<"Please input Len integers: "<<endl;
for(i=0;i<Len;i++)
{
cout<<"No."<<i+1<<": ";
cin>>a[i];
if(i<10)
file1<<a[i]<<' ';
else
file2<<a[i]<<' ';
}
file1.close();
file2.close();
//Question No.2
file1.open("f1.dat",ios::in);
file2.open("f2.dat",ios::out|ios::app);
if(file1==0 || file2==0)//if fail to open, the object==0
{
cout<<"Error while opening f1.dat or f2.dat!";
exit(1);
}
for(i=0;i<10;i++)
{
file1>>a[i];
file2<<a[i]<<' ';
}
145
第十三章 输入输出流
file1.close();
file2.close();
//Question No.3
file2.open("f2.dat",ios::in);
if(file2==0)
{
cout<<"Error while opening f2.dat!";
exit(1);
}
for(i=0;i<Len;i++)
file2>>a[i];
file2.close();
//sort numbers
for(i=0;i<Len-1;i++)
for(j=0;j<Len-i-1;j++)
if(a[j]>a[j+1])
{
temp=a[j];
a[j]=a[j+1];
a[j+1]=temp;
}
file2.open("f2.dat",ios::out);
for(i=0;i<Len;i++)
file2<<a[i]<<' ';
return 0;
}
/* 运行结果
* chinsung@gentoo ~ $ g++ test.cpp
* chinsung@gentoo ~ $ ./a.out
* Please input Len integers:
* No.1: 9
* No.2: 8
* No.3: 7
* No.4: 6
* No.5: 5
* No.6: 4
* No.7: 3
* No.8: 2
* No.9: 1
* No.10: 10
* No.11: 13
* No.12: 15
* No.13: 16
* No.14: 17
* No.15: 19
* No.16: 20
* No.17: 29
* No.18: 27
* No.19: 26
146
第十三章 输入输出流
* No.20: 11
* chinsung@gentoo ~ $ more f1.dat
* 9 8 7 6 5 4 3 2 1 10
* chinsung@gentoo ~ $ more f2.dat
* 1 2 3 4 5 6 7 8 9 10 11 13 15 16 17 19 20 26 27 29
*/
5. 编程实现以下功能:
1. 按职工号由小到大的顺序将5个员工的数据(包括号码、姓名、年龄、工资)输出到磁盘文件中保存
2. 从键盘输入两个员工的数据(职工号大于已有的职工号),增加到文件的末尾
3. 输出文件中全部职工的数据
4. 从键盘输入一个号码,从文件中查找有无此职工号,如有则显示从职工是第几个职工,以及此职工的全部数据。如
没有,就输出“无此人”。可以反复多次查询,如果查找的职工号为0,就结束
查询。
头痛,此题尚未完成...
147
第十四章 C++工具
第十四章 C++工具
待续...
148