C++第七章

  1. include <iostream>  
  2. #include <cmath>  
  3. using namespace std;  
  4. int main()  
  5. {  
  6. double a,b,c,s,area;  
  7.  cout<<"please input a,b,c:";  
  8.  cin>>a>>b>>c;  
  9.  if (a+b<=c)  
  10.   cerr<<"a+b<=c,error!"<<endl;  
  11.  else if(b+c<=a)  
  12.   cerr<<"b+c<=a,error!"<<endl;  
  13.  else if (c+a<=b)  
  14.   cerr<<"c+a<=b,error!"<<endl;  
  15.  else  
  16.   {  
  17.       s=(a+b+c)/2;  
  18.       area=sqrt(s*(s-a)*(s-b)*(s-c));  
  19.         cout<<"area="<<area<<endl;  
  20.    }  
  21.  return 0;  
  22. }  


第2题

[cpp]  view plain copy 在CODE上查看代码片 派生到我的代码片
  1. #include <iostream>  
  2. #include <iomanip>  
  3. using namespace std;  
  4.   
  5. int main()  
  6. {  
  7. float a[5];  
  8.  cout<<"input data:";  
  9.  int i=0;  
  10.  for( i=0;i<5;i++)  
  11.   cin>>a[i];  
  12.  cout<<setiosflags(ios::fixed)<<setprecision(2);  
  13.  for(i=0;i<5;i++)  
  14.   cout<<setw(10)<<a[i]<<endl;  
  15.  return 0;  
  16. }  



第3题

[cpp]  view plain copy 在CODE上查看代码片 派生到我的代码片
  1. #include <iostream>  
  2. #include <iomanip>  
  3. using namespace std;  
  4. int main()  
  5. {  
  6.  for(int n=1;n<8;n++)  
  7.  cout<<setw(20-n)<<setfill(' ')<<" "         
  8.  <<setw(2*n-1)<<setfill('B')<<"B"<<endl;  
  9.  return 0;  
  10. }  



第4题

[cpp]  view plain copy
  1. #include <iostream>  
  2. #include <fstream>  
  3. using namespace std;                  
  4. //fun1函数从键盘输入20个整数,分别存放在两个磁盘文件中  
  5.   
  6. void fun1()  
  7. {int a[10];  
  8.  ofstream outfile1("f1.dat"),outfile2("f2.dat");  //分别定义两个文件流对象  
  9.  if(!outfile1)                        //检查打开f1.dat是否成功  
  10.   {cerr<<"open f1.dat error!"<<endl;  
  11.    exit(1);  
  12.   }  
  13.  if(!outfile2)                        //检查打开f2.dat是否成功  
  14.   {cerr<<"open f2.dat error!"<<endl;  
  15.    exit(1);  
  16.   }   
  17.  cout<<"enter 10 integer numbers:"<<endl;  
  18.  for(int i=0;i<10;i++)          //输入10个数存放到f1.dat文件中  
  19.   {cin>>a[i];  
  20.    outfile1<<a[i]<<" ";}  
  21.   cout<<"enter 10 integer numbers:"<<endl;  
  22.  for(int i=0;i<10;i++)           //输入10个数存放到f2.dat文件中  
  23.   {cin>>a[i];  
  24.    outfile2<<a[i]<<" ";}  
  25.  outfile1.close();               //关闭f1.dat文件  
  26.  outfile2.close();               //关闭f2.dat文件  
  27. }  
  28.   
  29. //从f1,dat读入10个数,然后存放到f2.dat文件原有数据的后面  
  30. void fun2()  
  31. {ifstream infile("f1.dat");       //f1.dat作为输入文件  
  32.  if(!infile)  
  33.   {cerr<<"open f1.dat error!"<<endl;  
  34.    exit(1);  
  35.   }  
  36.   ofstream outfile("f2.dat",ios::app);   
  37.  //f2.dat作为输出文件,文件指针指向文件尾,向它写入的数据放在原来数据的后面  
  38.   if(!outfile)  
  39.    {cerr<<"open f2.dat error!"<<endl;  
  40.    exit(1);  
  41.   }  
  42.   int a;  
  43.   for(int i=0;i<10;i++)  
  44.    {infile>>a;           //磁盘文件f2.dat读入一个整数  
  45.     outfile<<a<<" ";     //将该数存放到f2.dat中  
  46.    }  
  47.   infile.close();  
  48.   outfile.close();  
  49.  }  
  50.   
  51. //从f2.dat中读入20个整数,将它们按从小到大的顺序存放到f2.dat   
  52. void fun3()  
  53. {ifstream infile("f2.dat"); //定义输入文件流infile,以输入方式打开f2.dat   
  54.  if(!infile)  
  55.   {cerr<<"open f2.dat error!"<<endl;  
  56.    exit(1);  
  57.   }  
  58.  int a[20];  
  59.  int i,j,t;  
  60. for(i=0;i<20;i++)        
  61.   infile>>a[i];        //从磁盘文件f2.dat读入20个数放在数组a中  
  62.  for(i=0;i<19;i++)     //用起泡法对20个数排序  
  63.    for(j=0;j<19-i;j++)  
  64.       if(a[j]>a[j+1])  
  65.         {t=a[j];a[j]=a[j+1];a[j+1]=t;}  
  66.   infile.close();                //关闭输入文件f2.dat  
  67.   ofstream outfile("f2.dat",ios::out);  
  68. // f2.dat作为输出文件,文件中原有内容删除  
  69.   if(!outfile)  
  70.    {cerr<<"open f2.dat error!"<<endl;  
  71.     exit(1);}  
  72. cout<<"data in f2.dat:"<<endl;  
  73.   for( i=0;i<20;i++)  
  74.     {outfile<<a[i]<<" ";      //向f2.dat输出已排序的20个数  
  75.      cout<<a[i]<<" ";}        //同时输出到显示器  
  76.   cout<<endl;  
  77.   outfile.close();  
  78. }  
  79.   
  80. int main()  
  81. {fun1();                     //分别调用3个函数  
  82.  fun2();  
  83.  fun3();  
  84.  return 0;  
  85. }  




第5题

[cpp]  view plain copy
  1. #include <iostream>  
  2. #include <fstream>  
  3. using namespace std;  
  4. struct staff  
  5. {int num;  
  6.  char name[20];  
  7.  int age;  
  8.  double  pay;  
  9. };  
  10. int main()  
  11. {staff staf[7]={3898,"Fan",34,1200,3899,"Wang",23,6740,2448,"Chen",54,778,  
  12.                   3000,"Du",45,4763,3301,"Ling",39,6555},staf1;  
  13.  fstream iofile("staff.dat",ios::in|ios::out|ios::binary);  
  14.  if(!iofile)  
  15.   {cerr<<"open error!"<<endl;  
  16.    abort();  
  17.   }  
  18.  int i,m,num;  
  19.  cout<<"Five staff :"<<endl;  
  20.  for(i=0;i<5;i++)  
  21.    {cout<<staf[i].num<<" "<<staf[i].name<<" "<<staf[i].age<<" "<<staf[i].pay<<endl;  
  22.     iofile.write((char *)&staf[i],sizeof(staf[i]));}  
  23.  cout<<"please input data you want insert:"<<endl;  
  24.  for(i=0;i<2;i++)  
  25.    {cin>>staf1.num>>staf1.name>>staf1.age>>staf1.pay;  
  26.     iofile.seekp(0,ios::end);  
  27.     iofile.write((char *)&staf1,sizeof(staf1));}  
  28.   iofile.seekg(0,ios::beg);  
  29.   for(i=0;i<7;i++)  
  30. {iofile.read((char *)&staf[i],sizeof(staf[i]));  
  31.     cout<<staf[i].num<<" "<<staf[i].name<<" "<<staf[i].age<<" "<<staf[i].pay<<endl;  
  32.    }  
  33.  bool find;  
  34.  cout<<"enter number you want search,enter 0 to stop.";  
  35.  cin>>num;  
  36.  while(num)  
  37.  {find=false;  
  38.   iofile.seekg(0,ios::beg);  
  39.   for(i=0;i<7;i++)  
  40.    {iofile.read((char *)&staf[i],sizeof(staf[i]));  
  41.     if(num==staf[i].num)  
  42.      {m=iofile.tellg();  
  43.       cout<<num<<" is No."<<m/sizeof(staf1)<<endl;  
  44.       cout<<staf[i].num<<" "<<staf[i].name<<" "<<staf[i].age<<" "<<staf[i].pay<<endl;  
  45.       find=true;  
  46.       break;  
  47.      }  
  48.    }  
  49.   if(!find)  
  50.     cout<<"can't find "<<num<<endl;  
  51.   cout<<"enter number you want search,enter 0 to stop.";  
  52.   cin>>num;  
  53.  }  
  54.   iofile.close();  
  55.   return 0;  
  56. }  



第6题

[cpp]  view plain copy
  1. #include <iostream>  
  2. #include <strstream>  
  3. using namespace std;  
  4.   
  5. struct student  
  6. {int num;  
  7.  char name[20];  
  8.  double score;  
  9. };  
  10. int main()  
  11. {  
  12. student stud[3]={1001,"Li",78,1002,"Wang",89.5,1004,"Fun",90},stud1[3];  
  13.  char c[50];  
  14.  int i;  
  15.  ostrstream strout(c,50);  
  16.  for(i=0;i<3;i++)  
  17.   strout<<stud[i].num<<" "<<stud[i].name<<" "<<stud[i].score<<" ";  
  18.  strout<<ends;  
  19.  cout<<"array c:"<<endl<<c<<endl<<endl;  
  20.  istrstream strin(c,50);  
  21.  for(i=0;i<3;i++)  
  22.   strin>>stud1[i].num>>stud1[i].name>>stud1[i].score;  
  23.  cout<<"data from array c to array stud1:"<<endl;  
  24.  for(i=0;i<3;i++)  
  25.   cout<<stud1[i].num<<" "<<stud1[i].name<<" "<<stud1[i].score<<endl;  
  26.  cout<<endl;  
  27.  return 0;  
  28. }  
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值