- 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;
- }
第2题
- #include <iostream>
- #include <iomanip>
- using namespace std;
- int main()
- {
- float a[5];
- cout<<"input data:";
- int i=0;
- for( i=0;i<5;i++)
- cin>>a[i];
- cout<<setiosflags(ios::fixed)<<setprecision(2);
- for(i=0;i<5;i++)
- cout<<setw(10)<<a[i]<<endl;
- return 0;
- }
第3题
- #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;
- return 0;
- }
第4题
- #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;
- }
第5题
- #include <iostream>
- #include <fstream>
- using namespace std;
- struct staff
- {int num;
- char name[20];
- int age;
- double pay;
- };
- int main()
- {staff staf[7]={3898,"Fan",34,1200,3899,"Wang",23,6740,2448,"Chen",54,778,
- 3000,"Du",45,4763,3301,"Ling",39,6555},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;
- for(i=0;i<2;i++)
- {cin>>staf1.num>>staf1.name>>staf1.age>>staf1.pay;
- iofile.seekp(0,ios::end);
- iofile.write((char *)&staf1,sizeof(staf1));}
- 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;
- cout<<"enter number you want search,enter 0 to stop.";
- cin>>num;
- while(num)
- {find=false;
- iofile.seekg(0,ios::beg);
- for(i=0;i<7;i++)
- {iofile.read((char *)&staf[i],sizeof(staf[i]));
- if(num==staf[i].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)
- cout<<"can't find "<<num<<endl;
- cout<<"enter number you want search,enter 0 to stop.";
- cin>>num;
- }
- iofile.close();
- return 0;
- }
第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);
- for(i=0;i<3;i++)
- strout<<stud[i].num<<" "<<stud[i].name<<" "<<stud[i].score<<" ";
- strout<<ends;
- cout<<"array c:"<<endl<<c<<endl<<endl;
- istrstream strin(c,50);
- for(i=0;i<3;i++)
- 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++)
- cout<<stud1[i].num<<" "<<stud1[i].name<<" "<<stud1[i].score<<endl;
- cout<<endl;
- return 0;
- }