1#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(1)#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];
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;
}
3#include<iostream>
#include<fstream>
using namespace std;
void fun1()
{int a[10];
ofstream outfile1("f1.dat"),outfile2("f2.dat");
if(!outfile1)
{cerr<<"open f1.dat error!"<<endl;
exit(1);
}
if(!outfile2)
{cerr<<"open f2.dat error!"<<endl;
exit(1);
}
cout<<"enter 10 integer numbers:"<<endl;
for (int i=0;i<10;i++)
{cin>>a[i];
outfile1<<a[i]<<" ";}
cout<<"enter 10 integer numbers:"<<endl;
for(int i=0;i<10;i++)
{cin>>a[i];
outfile2<<a[i]<<" ";}
outfile1.close();
outfile2.close();
}
void fun2()
{ifstream infile("f1.dat");
if(!infile)
{cerr<<"open f1.dat error!"<<endl;
exit(1);
}
ofstream outfile ("f2.dat",ios::app);
if(!outfile)
{cerr<<"open f2.dat 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.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];
for(i=0;i<19;i++)
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();
ofstream outfile("f2.dat",ios::out);
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]<<" ";
cout<<a[i]<<" ";}
cout<<endl;
outfile.close();
}
int main()
{fun1();
fun2();
fun3();
return 0;
}
4#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;
ofstream outfile1("staff.dat");
if (!outfile1)
cerr << "open staff.dat error!" << endl;
exit(1);
}
ofstream outfile("staff.dat", ios::out);
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 2 staff data you want to 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;
cout << "enter number you want search,enter 0 to stop.";
cin >> num;
while (num)
{
find = false;
iofile.seekg(0, ios::beg);//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 << "not found the stuff" << endl;
cout << "enter number you want search,enter 0 to stop.";
cin >> num;
}
iofile.close();
return 0;
}