zoukankan      html  css  js  c++  java
  • 第六章 编程练习

    第一题

     1 #include<iostream>
     2 #include<cctype>
     3 using namespace std;
     4 int main()
     5 {
     6     char arr[80];
     7     char ch;
     8     cin.getline(arr,80);
     9     int i=0;
    10     ch=arr[0];
    11     while((ch!='@')&&(i<=80))
    12     {
    13         if((ch>='a')&&(ch<='z'))cout<<char(toupper(ch));
    14         else if((ch>='A')&&(ch<='Z'))cout<<char(tolower(ch));
    15         else cout<<ch;
    16         i++;
    17         ch=arr[i];
    18     }
    19     return 0;
    20 }

    第二题

     1 #include<iostream>
     2 #include<cctype>
     3 using namespace std;
     4 int main()
     5 {
     6     double temp;
     7     double donation[10];
     8     double sum=0;
     9     int count=0;
    10     double ave=0;
    11     int i;
    12     for(i=0;i<10&&cin>>temp;i++)
    13     {
    14         donation[i]=temp;
    15         sum+=donation[i];
    16     }
    17     ave=sum/i;
    18     for(int j=0;j<i;j++)
    19     {
    20         if(donation[j]>ave)count++;
    21     }
    22     if(i!=10)cout<<"input error"<<endl;
    23     cout<<"the number of digit you have input "<<i<<endl;
    24     cout<<"ave="<<ave<<endl;
    25     cout<<"count="<<count<<endl;
    26     return 0;
    27 }

     第三题

     1 #include<iostream>
     2 #include<string>
     3 using namespace std;
     4 int main()
     5 {
     6     cout<<"please enter one of the following choices"<<endl;
     7     cout<<"c)carnivore        p)pianist"<<endl;
     8     cout<<"t)tree             g)game"<<endl;
     9     char ch;
    10     cout<<"intput your choice ,q to quit";
    11     cin>>ch;
    12     while(ch!='q')
    13     {
    14         switch(ch)
    15         {
    16         case 'c':cout<<"carnivore"<<endl;break;
    17         case 'p':cout<<"pianist"<<endl;break;
    18         case 't':cout<<"tree"<<endl;break;
    19         case 'g':cout<<"game"<<endl;break;
    20         default:cout<<"please enter a c,p,t,g";
    21         }
    22         cin>>ch;
    23     }
    24     return 0;
    25 }

    网上下载版:

     1 #include <iostream>  
     2 using namespace std;  
     3 void main()  
     4 {  
     5     cout<<"Please enter one of the following choices :"<<endl;  
     6     cout<<"c) carnivore              p) pianist"<<endl;  
     7     cout<<"t) tree                  g) game"<<endl;  
     8     char ch;  
     9     //boolean  
    10     bool flag=true; //用于跳出循环  
    11     while(flag)  
    12     {  
    13         cout<<"
    Please enter a a c,p,t,or g:";  
    14         cin>>ch;  
    15         switch(ch)  
    16         {  
    17         case 'c':cout<<"A mape is a carnivore",flag=false;break;  
    18         case 'p':cout<<"A mape is a pianist",flag=false;break;  
    19         case 't':cout<<"A mape is a tree",flag=false;break;  
    20         case 'g':cout<<"A mape is a game",flag=false;break;  
    21         default:;  
    22         }     
    23       
    24 }  
    25   
    26         system("pause");  
    27   
    28 }  

    第四题

     1 #include<iostream>
     2 using namespace std;
     3 const int strsize=20;
     4 
     5 struct bop
     6 {
     7     char fullname[strsize];
     8     char title[strsize];
     9     char bopname[strsize];
    10     int preference;
    11 };
    12 void displayfullname(bop *);
    13 void displaytitle(bop *);
    14 void displaybopname(bop *);
    15 void displaypreference(bop *);
    16 int main()
    17 {
    18     bop arr[5]={{"fullname1","title1","bopname1",0},{"fullname2","title2","bopname2",1},{"fullname3","title3","bopname3",2},{"fullname4","title4","bopname4",1},{"fullname5","title5","bopname5",2}};
    19     cout<<"benevolent order if progreammers report"<<endl;
    20     cout<<"a.display by name        b.display by title"<<endl;
    21     cout<<"c.display by bopname     d.display by preference"<<endl;
    22     cout<<"q.quit"<<endl;
    23     char ch;
    24     cout<<"enter your choice";
    25     cin>>ch;
    26     while(ch!='q')
    27     {
    28         switch(ch)
    29             {case 'a':displayfullname(arr);break;
    30             case 'b':displaytitle(arr);break;
    31             case 'c':displaybopname(arr);break;
    32             case 'd':displaypreference(arr);break;
    33             default:cout<<"enter wrong";}
    34         cout<<"next choice";
    35         cin>>ch;
    36     }
    37 }
    38 
    39 void displayfullname(bop * a)
    40 {
    41     for(int i=0;i<5;i++)
    42         cout<<a[i].fullname<<endl;
    43 }
    44 
    45 void displaytitle(bop *a)
    46 {
    47     for(int i=0;i<5;i++)
    48         cout<<a[i].title<<endl;
    49 }
    50 void displaybopname(bop *a)
    51 {
    52     for(int i=0;i<5;i++)
    53         cout<<a[i].bopname<<endl;
    54 }
    55 void displaypreference(bop *a)
    56 {
    57     int s;
    58     for(int i=0;i<5;i++)
    59     {
    60         s=a[i].preference;
    61         switch(s)
    62             {case 0:cout<<a[i].fullname;break;
    63             case 1:cout<<a[i].title;break;
    64             case 2:cout<<a[i].bopname;break;
    65             default:cout<<"enter wrong";}
    66         cout<<endl;
    67     }
    68 }

    网上下载

     1 #include <iostream>  
     2 using namespace std;  
     3 const int strsize=20;  
     4   
     5 struct bop{  
     6     char fullname[strsize]; //real name  
     7     char title[strsize];    //job title  
     8     char bopname[strsize];  //secret BOP name  
     9     int preference; //0=fullname 1=title 2=bopname  
    10 };  
    11 void getCout(char c,bop *b);  
    12 void main()  
    13 {  
    14     bop b[4]={{"Chen shiguang","Coder","guang",1}, //初始化数组  
    15                 {"Lijing","Student","Li",2},  
    16                 {"Lijing2","Student2","Li2",2},  
    17                 {"Chenguang","dent","ci",0}  
    18     };  
    19   
    20     cout<<"Benevolent order of Programmers Report"<<endl;  
    21     cout<<"a.display by name              b.display by title"<<endl;  
    22     cout<<"c.display by bopname           d.display by priference"<<endl;  
    23     cout<<"q.quit"<<endl;  
    24     cout<<"Enter your choice:";  
    25     char ch;  
    26     cin>>ch;  
    27     while(ch!='q')  
    28     {  
    29   
    30     switch(ch)  
    31     {  
    32     case 'a':for(int i=0;i<4;i++){  
    33         cout<<b[i].fullname<<endl;  
    34              };break;  
    35     case 'b':for(int i=0;i<4;i++){  
    36         cout<<b[i].title<<endl;  
    37              };break;  
    38     case 'c':for(int i=0;i<4;i++){  
    39         cout<<b[i].bopname<<endl;  
    40              };break;  
    41     case 'd':getCout(ch,b);break;  
    42         //<span style="white-space:pre">  </span>default:cout<<"wrong input"<<endl;  
    43     //case 'q':cout<<"bye"<<endl,cin.get(),exit(1);  
    44       
    45     }  
    46     cout<<"Next Choice:";  
    47     cin>>ch;  
    48     }  
    49     cout<<"bye"<<endl;  
    50     cin.get();  
    51 }  
    52   
    53 void getCout(char c,bop *b)//<span style="white-space:pre">   </span>//选择为preference时调用的函数  
    54 {  
    55     int s;  
    56     for(int i=0;i<4;i++,b++)  
    57     {  
    58         s=b->preference;  
    59         switch(s){  
    60         case 0:cout<<b->fullname<<endl;break;  
    61         case 1:cout<<b->title<<endl;break;  
    62         case 2:cout<<b->bopname<<endl;break;  
    63         default:cout<<"wrong request!";  
    64         }  
    65     }  
    66 }  

    第五题

     1 #include<iostream>
     2 using namespace std;
     3 int main()
     4 {
     5     int income;
     6     cout<<"intput your income: ";
     7     cin>>income;
     8     double tax;
     9     while(income>=0)
    10     {
    11         if(income<=5000)tax=0;
    12         else if(income>5000&&income<=15000)tax=(income-5000)*0.1;
    13         else if(income>15000&&income<=35000)tax=1000+(income-15000)*0.15;
    14         else tax=4000+(income-35000)*0.2;
    15         cout<<"your tax="<<tax<<endl;
    16         cout<<"intput your income: ";
    17         cin>>income;
    18     }
    19     return 0;
    20 }

    第六题

     1 #include<iostream>
     2 #include<vector>
     3 #include<string>
     4 using namespace std;
     5 struct a
     6 {
     7     string name;
     8     double num;
     9 };
    10 
    11 int main()
    12 {
    13     int s;
    14     cout<<"the number of the people:";
    15     cin>>s;
    16     vector<a>ps(s);
    17     for(int i=0;i<s;i++)
    18     {
    19         cout<<"the name and the number of no."<<i+1<<" people"<<endl;
    20         cin>>ps[i].name>>ps[i].num;
    21     }
    22     cout<<"grand patrins:"<<endl;
    23     int count=0;
    24     for(int i=0;i<s;i++)
    25     {
    26         
    27         if(ps[i].num>1000)
    28         {
    29             cout<<ps[i].name<<",  "<<ps[i].num<<endl;
    30             count++;
    31         }
    32         
    33     }
    34     if(count==0)cout<<"none";
    35 
    36     cout<<endl<<endl<<"patrons"<<endl;
    37     count=0;
    38     for(int i=0;i<s;i++)
    39     {
    40         
    41         if(ps[i].num<=1000)
    42         {
    43             cout<<ps[i].name<<",  "<<ps[i].num<<endl;
    44             count++;
    45         }
    46         
    47     }
    48     if(count==0)cout<<"none";
    49     return 0;
    50 }

    第七题

     1 #include<iostream>
     2 #include<string>
     3 #include<cctype>
     4 using namespace std;
     5 const int size =10;
     6 int main()
     7 {
     8     char temp[size];
     9     cout<<"enter the world:";
    10     cin>>temp;
    11     int yuanyin=0;
    12     int fuyin=0;
    13     int other =0;
    14     while(temp[0]!='q')
    15     {
    16         cout<<temp<<" ";
    17         if(!isalpha(temp[0]))other++;
    18         else {
    19             if(temp[0]=='a'||temp[0]=='e'||temp[0]=='i'||temp[0]=='o'||temp[0]=='u')yuanyin++;
    20             else fuyin++;
    21                 }
    22         cin>>temp;
    23     }
    24     cout<<"yuanyin="<<yuanyin<<endl;
    25     cout<<"fuyin="<<fuyin<<endl;
    26     cout<<"other="<<other<<endl;
    27     return 0;
    28 }

    第八题

     1 #include<iostream>
     2 #include<fstream>
     3 #include<string>
     4 using namespace std;
     5 int main()
     6 {
     7     string name;
     8     char ch;
     9     int count=0;
    10     ifstream infile;
    11     cout<<"input the title of the file:";
    12     cin>>name;
    13     infile.open(name);
    14     if(!infile.is_open())cout<<"open failed"<<endl;
    15     else cout<<"open success"<<endl;
    16     infile>>ch;
    17     while(infile.good())
    18     {
    19         cout<<ch;
    20         if(ch!=' ')count++;
    21         infile>>ch;
    22     }
    23     cout<<endl<<"munber of the char ="<<count<<endl;
    24     infile.close();
    25     return 0;
    26 }

    空格和换行符没有输出。

    第九题

     1 #include<fstream>
     2 #include<stdlib.h>
     3 #include<iostream>
     4 #include<string>
     5 #include<vector>
     6 using namespace std;
     7 struct a
     8 {
     9     string name;
    10     double num;
    11 };
    12 int main()
    13 {
    14     string filename;
    15     cout<<"enter the filename:";
    16     cin>>filename;
    17     ifstream infile;
    18     infile.open(filename);
    19     if(!infile.is_open())cout<<"open failed"<<endl;
    20     else cout<<"open success"<<endl;
    21 
    22     int n;
    23     string temp;
    24     getline(infile,temp);
    25     n=atoi(temp.c_str());//atoi头文件#include<stdlib.h>
    26     vector<a>ps(n);
    27 
    28     for(int i=0;i<n;i++)
    29     {
    30         getline(infile,ps[i].name);
    31         getline(infile,temp);
    32         ps[i].num=atoi(temp.c_str());
    33     }
    34     cout<<endl<<endl<<" grandpatrons"<<endl;
    35     int count=0;
    36     for(int i=0;i<n;i++)
    37     {
    38        if(ps[i].num>10000)
    39         {
    40            cout<<ps[i].name<<",  "<<ps[i].num<<endl;
    41            count++;
    42         }
    43          
    44     }
    45     if(count==0)cout<<"none";
    46  
    47     cout<<endl<<endl<<"patrons"<<endl;
    48     count=0;
    49     for(int i=0;i<n;i++)
    50      {
    51          
    52        if(ps[i].num<=10000)
    53        {
    54            cout<<ps[i].name<<",  "<<ps[i].num<<endl;
    55            count++;
    56        }
    57          
    58     }
    59     if(count==0)cout<<"none";
    60     return 0;
    61 }
  • 相关阅读:
    MetaWeblog API
    序列化Hashtable
    贴几个CodeDOM的链接
    Summing it all up: 35 blog entries in 2 days from the BCL Team [Kit George] --from BCLTeam's Weblog
    上海在下尘土
    PSP升级速度令然惊喜!(update:2007.10.13)
    Linkin Park林肯公园上海演唱会(2007年11月18日)
    STARCRAFT2 / 星际争霸2
    Enterprise Library 3.0 April 2007
    《The Devil Wears Prada / 穿布拉达的女王》很有意思.
  • 原文地址:https://www.cnblogs.com/taoxiuxia/p/4149971.html
Copyright © 2011-2022 走看看