01.1.#include <iostream>
02.#include <cmath>
03.using namespace std;
04.void area(float a,float b,float c)
05.{
06. double s,area;
07. if(a+b<=c)
08. cerr<<"a+b<=c,error!"<<endl;
09. else if(b+c<=a)
10. cerr<<"b+c<=a,error!"<<endl;
11. else if(a+c<=b)
12. cerr<<"a+c<=b,error!"<<endl;
13. else
14. {
15. s=(a+b+c)/2;
16. area=sqrt(s*(s-a)*(s-b)*(s-c));
17. cout<<"area="<<area<<endl;
18. }
19.}
20.int main()
21.{
22. float a=2,b=3,c=4;
23. area(a,b,c);
24. return 0;
25.}
01.2. #include <iostream>
02.using namespace std;
03.int main()
04.{
05. float a[5];
06. int i;
07. cout<<"input number:";
08. for(i=0;i<5;i++)
09. cin>>a[i];
10. cout.setf(ios::fixed);
11. cout.precision(3);
12. for(i=0;i<5;i++)
13. {
14. //cout.width(10);
15. cout<<a[i]<<endl;
16. }
17. return 0;
18.}
01.3. #include <iostream>
02.#include <iomanip>
03.using namespace std;
04.int main()
05.{
06. for(int i=1;i<8;i++)
07. cout<<setw(20-i)<<setfill(' ')<<" "<<setw(2*i-1)<<setfill('B')<<"B"<<endl; //设置填充字符
08. return 0;
09.}
01.4. #include <iostream>
02.#include <fstream>
03.using namespace std;
04.void fun1()
05.{
06. int a[10];
07. ofstream outfile1("f1.data",ios::out),outfile2("f2.data",ios::out);
08. if(!outfile1)
09. {
10. cerr<<"open error!"<<endl;
11. exit(1);
12. }
13. if(!outfile2)
14. {
15. cerr<<"open error!"<<endl;
16. exit(1);
17. }
18. cout<<"input 10 integer numbers:"<<endl;
19. for(int i=0;i<10;i++)
20. {
21. cin>>a[i];
22. outfile1<<a[i]<<" ";
23. }
24. cout<<"input 10 integer numbers:"<<endl;
25. for(i=0;i<10;i++)
26. {
27. cin>>a[i];
28. outfile2<<a[i]<<" ";
29. }
30. outfile1.close();
31. outfile2.close();
32.}
33.void fun2()
34.{
35. ifstream infile("f1.data");
36. if(!infile)
37. {
38. cerr<<"open error!"<<endl;
39. exit(1);
40. }
41. ofstream outfile("f2.data",ios::app);
42. if(!outfile)
43. {
44. cerr<<"open error!"<<endl;
45. exit(1);
46. }
47. int a;
48. for(int i=0;i<10;i++)
49. {
50. infile>>a;
51. outfile<<a<<" ";
52. }
53. infile.close();
54. outfile.close();
55.}
56.void fun3()
57.{
58. ifstream infile("f2.data");
59. if(!infile)
60. {
61. cerr<<"open error!"<<endl;
62. exit(1);
63. }
64. int a[20];
65. int i,j,t;
66. for(i=0;i<20;i++)
67. infile>>a[i];
68. for(i=0;i<20;i++)
69. for(j=i+1;j<20;j++)
70. if(a[i]>a[j])
71. {t=a[i];a[i]=a[j];a[j]=t;}
72. infile.close();
73. ofstream outfile("f2.data",ios::out);
74. if(!outfile)
75. {
76. cerr<<"open error!"<<endl;
77. exit(1);
78. }
79. cout<<"data in f2.data:"<<endl;
80. for(i=0;i<20;i++)
81. {
82. outfile<<a[i]<<" ";
83. cout<<a[i]<<" ";
84. }
85. cout<<endl;
86. outfile.close();
87.}
88.int main()
89.{
90. fun1();
91. fun2();
92. fun3();
93. return 0;
94.}
01.5. #include <iostream>
02.#include <fstream>
03.using namespace std;
04.struct staff
05.{
06. int num;
07. char name[10];
08. int age;
09. double pay;
10.};
11.int main()
12.{
13. staff s[7]={1,"xiong",18,10000,3,"xian",18,20000,2,"piao",26,20000,4,"xin",26,10000,5,"xun",20,15000},s1;
14. fstream iofile("staff.dat",ios::in|ios::out|ios::binary);
15. if(!iofile)
16. {
17. cerr<<"open error!"<<endl;
18. abort();
19. }
20. int i,m,num;
21. cout<<"Five staff:"<<endl;
22. for(i=0;i<5;i++)
23. {
24. cout<<s[i].num<<" "<<s[i].name<<" "<<s[i].age<<" "<<s[i].pay<<endl;
25. iofile.write((char *)&s[i],sizeof(s[i]));
26. }
27. cout<<"Please insert two staff's message:"<<endl;
28. for(i=0;i<2;i++)
29. {cin>>s1.num>>s1.name>>s1.age>>s1.pay;
30. iofile.seekp(0,ios::end);
31. iofile.write((char *)&s1,sizeof(s1));}
32. iofile.seekg(0,ios::beg);
33. for(i=0;i<7;i++)
34. {iofile.read((char *)&s[i],sizeof(s[i]));
35. cout<<s[i].num<<" "<<s[i].name<<" "<<s[i].age<<" "<<s[i].pay<<endl;}
36. bool find;
37. cout<<"enter number you want to search,enter 0 to stop";
38. cin>>num;
39. while(num)
40. {
41. find=false;
42. iofile.seekg(0,ios::beg);
43. for(i=0;i<7;i++)
44. {
45. iofile.read((char *)&s[i],sizeof(s[i]));
46. if(num==s[i].num)
47. {
48. m=iofile.tellg();
49. cout<<num<<"is NO."<<m/sizeof(s1)<<endl;
50. cout<<s[i].num<<" "<<s[i].name<<" "<<s[i].age<<" "<<s[i].pay<<endl;
51. find=true;
52. break;
53. }
54. }
55. if(!find)
56. cout<<"can't find!"<<num<<endl;
57. cout<<"please input again!";
58. cin>>num;
59. }
60. iofile.close();
61. return 0;
62.}
01.6. #include <iostream>
02.#include <strstream>
03.using namespace std;
04.//struct staff
05.struct student
06.{
07. int num;
08. char name[10];
09. //int age;
10. //double pay;
11. double score;
12.};
13.int main()
14.{
15. student stud[3]={1001,"Li",78,1002,"Wang",89.5,1004,"Fun",90},stud1[3];
16. char c[50];
17. int i;
18. ostrstream strout(c,50);
19. for(i=0;i<3;i++)
20. strout<<stud[i].num<<" "<<stud[i].name<<" "<<stud[i].score<<" ";
21. strout<<ends;
22. cout<<"array c:"<<endl<<c<<endl<<endl;
23. istrstream strin(c,50);
24. for(i=0;i<3;i++)
25. strin>>stud1[i].num>>stud1[i].name>>stud1[i].score;
26. cout<<"data from array c to arry s1:"<<endl;
27. for(i=0;i<3;i++)
28. cout<<stud[i].num<<" "<<stud[i].name<<" "<<stud[i].score<<endl;
29. cout<<endl;
30. return 0;
31.}