栖枝Fairy

导航

实验一

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

(1)要求使用if…else 语句进行判断,用break,continue控制程序流程。

#include <iostream>
using namespace std;
int main()
{
    char c;
    cout<<"Menu: A(dd) D(elete) S(ort) Q(uit),Select One:"<<endl;
    cin>>c;
    while(c!='Q')
    {
        if(c=='A')
        {
            cout<<"Data has Added"<<endl;
            cout<<"Menu: A(dd) D(elete) S(ort) Q(uit),Select One:"<<endl;
        }
        else if (c=='D')
        {
            cout<<"Data has Deleted"<<endl;
            cout<<"Menu: A(dd) D(elete) S(ort) Q(uit),Select One:"<<endl;
        }
        else if(c=='S')
        {
            cout<<"Data has Sorted"<<endl;
            cout<<"Menu: A(dd) D(elete) S(ort) Q(uit),Select One:"<<endl;
        }
        else
        {
            cout<<"no such choise,please select again."<<endl;
            cout<<"Menu: A(dd) D(elete) S(ort) Q(uit),Select One:"<<endl;
        }
        cin>>c;
    }
    return 0;
}

 

(2)要求使用switch语句。

#include <iostream>
using namespace std;
int main(){
    char c;
    cout<<"Menu: A(dd) D(elete) S(ort) Q(uit),Select One:"<<endl;
    cin>>c;
    while(c!='Q')
    {
        switch(c)
        {
            case 'A': {cout<<"Data has Added."<<endl;break;}
            case 'D': {cout<<"Data has Deleted."<<endl;break;}
            case 'S': {cout<<"Data has Sort."<<endl;break;}
            default : {cout<<"no choice, please select again."<<endl;break;}
        }
        cout<<"Menu: A(dd) D(elete) S(ort) Q(uit),Select One:"<<endl;
        cin>>c;
     } 
     return 0;
}

 

 

 2-29 用穷举法找出1~100间的质数并显示出来。分别使用 while,do-while,for循环语句实现。

     (1)while

#include <iostream>
#include <cmath>
using namespace std; 
int main( )
{
   int j,i=2,flag,t=0;
   while(i<=100)
   {flag=1;j=2;
   while(j<=sqrt(i))
    {
     if(i%j==0) 
        {flag=0;
         break;
     }
        j++;
    }
        if (flag==1)
        {t++;
         cout<<i<<"   ";
         if(t%5==0)
            cout<<"\n"<<endl;
        }
        i++;
   }
   return 0;
 } 

(2)do-while

#include <iostream>
#include <cmath>
using namespace std; 
int main( )
{
   int j,i=2,flag,t=0;
  do
   {flag=1;j=2;
   while(j<=sqrt(i))
    {
     if(i%j==0) 
        {flag=0;
         break;
     }
        j++;
    }
        if (flag==1)
        {t++;
         cout<<i<<"   ";
         if(t%5==0)
            cout<<"\n"<<endl;
        }
        i++;
   }
    while(i<100);
   return 0;
 } 

(3)for循环

#include <iostream>
#include <cmath>
using namespace std; 
int main( )
{
   int j,i=2,flag,t=0;
  while(i<=100)
  {flag=1;
    for(j=2;j<=sqrt(i);j++) 
    {
     if(i%j==0) 
        {flag=0;
         break;
     }
    }
        if (flag==1)
        {t++;
         cout<<i<<"   ";
         if(t%5==0)
            cout<<"\n"<<endl;
        }
        i++;
   }
   return 0;
 } 

 2-32 在程序中定义一个整形变量,赋予1~100的值。要求用户猜这个数,比较两个数的大小,把结果显示给用户,直到猜对为止。分别使用while,do-while语句实现循环。

 (1)while

#include <iostream>
using namespace std;
int main( )
{
    int a=17;
    int n;
    while(1)
    {cout<<"guess this number:"<<endl;
     cin>>n;
     if(a==n)
     {cout<<"Congratulations!"<<endl;
      break;
     }
     else if(a<n)
     {cout<<"bigger than the number."<<endl;}
     else if(a>n)
     {cout<<"smaller than the number."<<endl;}
    }
    return 0;
}

(2)do-while

#include <iostream>
using namespace std;
int main( )
{
    int a=17;
    int n;
    do
    {cout<<"guess this number:"<<endl;
     cin>>n;
     if(a==n)
     {cout<<"Congratulations!"<<endl;
      break;
     }
     else if(a<n)
     {cout<<"bigger than the number."<<endl;}
     else if(a>n)
     {cout<<"smaller than the number."<<endl;}
    }
    while(1);
    return 0;
}

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

 

#include <iostream>
using namespace std;
int main()
{int i,j,k,a=0;//1:红,2:黄,3:蓝,4:白,5:黑
 for(i=1;i<=5;i++)
     {for(j=i+1;j<=5;j++)
          {for(k=j+1;k<=5;k++)
               {cout<<i<<j<<k<<endl;
                 a++;
                } 
           }
    }
    cout<<"total:"<<a<<endl;
    return 0;
}

实验总结与体会:

2-28 第一个程序我用的很傻,大多都是复制粘贴,我尝试改了之后,发现运行有问题,最后放弃了。

2-29 质数一开始用的j<=i-1;后来发现有重复,所以加了个头文件从#include <cmath> 用sqrt函数做。

3-32 中的while(1)进入死循环我一开始没想到,然后参考了一下百度,通过 if 函数退出死循环。

3-34 这题的枚举法是真的不会,然后我去问了学长,还是没弄明白,我就用了最简单的数字代表球的颜色,列出取法。

我觉得我需要花更多的时间去学习C++,有一些细节需要去钻研。

posted on 2019-03-17 16:22  栖枝Fairy  阅读(220)  评论(2编辑  收藏  举报