2-28 实现一个简单的菜单程序,运行时显示“Menu:A(dd) D(elete) S(ort) Q(uit),Select one:” 提示用户输入。 A表示增加,D表示删除,S表示排序,Q表示退出。输入为A、D、S时分别提示“数据已经增加、删除、排序。”,输入为Q时程序结束。

[1] if-else

#include <iostream>
using namespace std;
int main(){
    char z;
    cout<< " Menu: A(dd) D(elete) S(ort) Q(uit),Select one; "<<endl;
    cin>>z;
    while(z!='Q'){ 
    if (z=='A'){
        cout<<" 数据已经增加 "<<endl;
        cin>>z;continue;
    }
    else if (z=='D'){
        cout<<" 数据已经删除 "<<endl;
        cin>>z;continue;
    }
    else if (z=='S'){
        cout<<" 数据已经排序 "<<endl;
        cin>>z;continue;
    }
    else {
        cout<<" 数据不是选择项 "<<endl;
        cin>>z;
    }
    }
    if (z=='Q'){
        cout<<" 数据错误!!!"<<endl; 
    }
    return 0;
}

[2] switch

#include <iostream>
using namespace std;
int main(){
    char z;
    cout<<" Menu: A(dd) D(elete) S(ort) Q(uit),Selete one; "<<endl;
    cin>>z; 
    while (z!='Q'){
        switch(z){
            case'A':
                {
                    cout<<" 数据已经增加 "<<endl; 
                }break;
            case'D':
                {
                    cout<<" 数据已经删除 "<<endl; 
                }break;
            case'S':
                {
                    cout<<" 数据已经排序 "<<endl; 
                }break;
            default:
                {
                    cout<<" 数据不是选择项 "<<endl; 
                }
        }
        cin>>z;
    }
    return 0;    
}

2-29 用穷举法找出1~100间的质数并显示出来。

[1] while

#include <iostream>
#include <cmath>
#include <iomanip>
using namespace std;
int main()
 { int i=2,j,f,k=0;
   while(i<=100)   
    {f=0;
     for(j=2;j<=sqrt(i);j++)
       if(i%j==0)
        {f=1;
         break;
        }
     if(f==0)
       {cout<<setw(4)<<i;
        k++;
        if(k%4==0)
         cout<<endl;
       }
     i++;
  }
}

[2] do-while

#include <iostream>
#include <cmath>
#include <iomanip>
using namespace std;
int main()
 { int i=2,j,f,k=0;
   do
    {f=0;
     for(j=2;j<=sqrt(i);j++)
       if(i%j==0)
        {f=1;
         break;
        }
     if(f==0)
       {cout<<setw(4)<<i;
        k++;
        if(k%4==0)
         cout<<endl;
       }
     i++;
  }while(i<=100);
}

[3]  for

#include <iostream>
#include <cmath>
#include <iomanip>
using namespace std;
int main()
 { int i=2,j,f,k=0;
   for(;i<=100;)   
    {f=0;
     for(j=2;j<=sqrt(i);j++)
       if(i%j==0)
        {f=1;
         break;
        }
     if(f==0)
       {cout<<setw(4)<<i;
        k++;
        if(k%4==0)
         cout<<endl;
       }
     i++;
  }
}

2-32 在程序中定义一个整形变量,赋予1~100的值。要求用户猜这个数,比较两个数的大小,把结果提示给用户,直到猜对为止。

[1] while

#include<iostream>
#include <ctime>
#include <cstdlib>
using namespace std;
int main()
{ int z,c;
  srand(time (NULL));
  z=rand()%100;
  cout<< " Hello ! Welcome to the game "<<endl;
  cout<< " please guess a number that ranges from 1 to 100 "<<endl;
  cin>>c; 
  while(z!=c)
  {    if (z>c) 
           cout<< " Sorry , That's a small number "<<endl;
       else  if (z<c) 
           cout<< " Sorry , That's a big number "<<endl;
       cin>>c;
  }
  cout<< " Cool ! You get it! "<<endl;
  return 0;
}

[2]  do-while

#include <iostream>
#include <ctime>
#include <cstdlib>
using namespace std;
int main()
{ int z,c;
  srand(time (NULL));
  z=rand()%100;
  cout<< " Hello ! Welcome to the game "<<endl;
  cout<< " please guess a number that ranges from 1 to 100 "<<endl;
  cin>>c;
  do
  {    if (z>c) 
          cout<< " Sorry , That's a small number "<<endl;
       else if (z<c) 
          cout<< " Sorry , That's a big number "<<endl;
       cin>>c;
  }while(c!=z);
  cout<< " Cool ! You get it! "<<endl;
  return 0;
}

2-34 口袋中有红、黄、蓝、白、黑 5 种颜色的球若干个。每次从口袋中取出3个不同颜色的球,问有多少种取法?

#include <iostream>
using namespace std;
int main()
{ int i,j,k,n=0;
  char c[5][7]={"R","Y","B1","W","B2"};
  cout<<" 注:'R'---红色,'Y'---黄色,'B1'---蓝色,'W'---白色,'B2'---黑色 "<<endl; 
  for(i=0;i<5;i++)
     for(j=i+1;j<5;j++)
        for(k=j+1;k<5;k++)
           {  if(i!=j && j!=k && k!=i)
              cout<<c[i]<<"     "<<c[j]<<"     "<<c[k]<<endl;
              n++;
            }
    cout<<"答:一共有"<<n<<"种取法";
}

总结:第一次使用C++编程,和C的感受不一样,比C更加简明,但编程思考时仍存在很多不足包括打代码的时候,希望继续努力,学好C++

评论链接:https://www.cnblogs.com/Yyaoyyy/p/10547742.html

                  https://www.cnblogs.com/csc13813017371/p/10541783.html

                  https://www.cnblogs.com/laboratory-X/p/10546983.html