#include <iostream>
#include <cmath>
using namespace std;
void area(float a,float b,float c)
{
double s,area;
if(a+b<=c)
cerr<<"a+b<=c,error!"<<endl;
else if(b+c<=a)
cerr<<"b+c<=a,error!"<<endl;
else if(a+c<=b)
cerr<<"a+c<=b,error!"<<endl;
else
{
s=(a+b+c)/2;
area=sqrt(s*(s-a)*(s-b)*(s-c));
cout<<"area="<<area<<endl;
}
}
int main()
{
float a=1,b=2,c=3;
area(a,b,c);
return 0;
}
2.
#include <iostream>
using namespace std;
int main()
{
float a[5];
int i;
cout<<"input number:";
for(i=0;i<5;i++)
cin>>a[i];
cout.setf(ios::fixed);
cout.precision(3);
for(i=0;i<5;i++)
{
cout<<a[i]<<endl;
}
return 0;
}
3.
#include <iostream>
#include <iomanip>
using namespace std;
int main()
{
for(int i=1;i<8;i++)
cout<<setw(20-i)<<setfill(' ')<<" "<<setw(2*i-1)<<setfill('B')<<"B"<<endl;
return 0;
}
4.
#include <iostream>
#include <fstream>
using namespace std;
void fun1()
{
int a[10];
ofstream outfile1("f1.data",ios::out),outfile2("f2.data",ios::out);
if(!outfile1)
{
cerr<<"open error!"<<endl;
exit(1);
}
if(!outfile2)
{
cerr<<"open error!"<<endl;
exit(1);
}
cout<<"input 10 integer numbers:"<<endl;
for(int i=0;i<10;i++)
{
cin>>a[i];
outfile1<<a[i]<<" ";
}
cout<<"input 10 integer numbers:"<<endl;
for(i=0;i<10;i++)
{
cin>>a[i];
outfile2<<a[i]<<" ";
}
outfile1.close();
outfile2.close();
}
void fun2()
{
ifstream infile("f1.data");
if(!infile)
{
cerr<<"open error!"<<endl;
exit(1);
}
ofstream outfile("f2.data",ios::app);
if(!outfile)
{
cerr<<"open error!"<<endl;
exit(1);
}
int a;
for(int i=0;i<10;i++)
{
infile>>a;
outfile<<a<<" ";
}
infile.close();
outfile.close();
}
void fun3()
{
ifstream infile("f2.data");
if(!infile)
{
cerr<<"open error!"<<endl;
exit(1);
}
int a[20];
int i,j,t;
for(i=0;i<20;i++)
infile>>a[i];
for(i=0;i<20;i++)
for(j=i+1;j<20;j++)
if(a[i]>a[j])
{t=a[i];a[i]=a[j];a[j]=t;}
infile.close();
ofstream outfile("f2.data",ios::out);
if(!outfile)
{
cerr<<"open error!"<<endl;
exit(1);
}
cout<<"data in f2.data:"<<endl;
for(i=0;i<20;i++)
{
outfile<<a[i]<<" ";
cout<<a[i]<<" ";
}
cout<<endl;
outfile.close();
}
int main()
{
fun1();
fun2();
fun3();
return 0;
}
5.
#include <iostream>
#include <fstream>
using namespace std;
struct staff
{
int num;
char name[10];
int age;
double pay;
};
int main()
{
staff s[7]={1,"xiong",18,10000,3,"xian",18,20000,2,"piao",26,20000,4,"xin",26,10000,5,"xun",20,15000},s1;
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<<s[i].num<<" "<<s[i].name<<" "<<s[i].age<<" "<<s[i].pay<<endl;
iofile.write((char *)&s[i],sizeof(s[i]));
}
cout<<"Please insert two staff's message:"<<endl;
for(i=0;i<2;i++)
{cin>>s1.num>>s1.name>>s1.age>>s1.pay;
iofile.seekp(0,ios::end);
iofile.write((char *)&s1,sizeof(s1));}
iofile.seekg(0,ios::beg);
for(i=0;i<7;i++)
{iofile.read((char *)&s[i],sizeof(s[i]));
cout<<s[i].num<<" "<<s[i].name<<" "<<s[i].age<<" "<<s[i].pay<<endl;}
bool find;
cout<<"enter number you want to 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 *)&s[i],sizeof(s[i]));
if(num==s[i].num)
{
m=iofile.tellg();
cout<<num<<"is NO."<<m/sizeof(s1)<<endl;
cout<<s[i].num<<" "<<s[i].name<<" "<<s[i].age<<" "<<s[i].pay<<endl;
find=true;
break;
}
}
if(!find)
cout<<"can't find!"<<num<<endl;
cout<<"please input again!";
cin>>num;
}
iofile.close();
return 0;
}
6.
#include <iostream>
#include <strstream>
using namespace std;
struct student
{
int num;
char name[10];
double score;
};
int main()
{
student stud[3]={1001,"Yi",78,1002,"Ya",89.5,1004,"Fun",88},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 arry s1:"<<endl;
for(i=0;i<3;i++)
cout<<stud[i].num<<" "<<stud[i].name<<" "<<stud[i].score<<endl;
cout<<endl;
return 0;
}