实验一:C++简单程序设计

---恢复内容开始---

【实验结论】 #2-28 实现一个简单的菜单程序,运行时显示“Menu:A(dd) D(elete) S(ort) Q(uit),Selete one:”提示用户输入。A表示增加,D表示删除,S表示排序,Q表示退出。输入为A、D、S时分别提示“数据已经增加、删除、排序。”,输入Q时程序结束。 (1)if...else...语句,break,continue控制

#include <iostream>
using namespace std;
int main() {
    char a;
    while(true)
         {cout<<"Menu:A(dd)D(elete)S(ort)Q(uit),Select one:"<<endl;
          cin>>a;
          if (a=='A') 
             {cout<<"数据已经增加"<<endl;
              continue;}
            else if (a=='D') 
             {cout<<"数据已经删除"<<endl;
              continue;}
            else if (a=='S') 
             {cout<<"数据已经排序"<<endl;
              continue;}
             else if (a=='Q') break;
              else cout<<"输入无效"<<endl;
         }
    return 0;
}

(2)switch语句

#include <iostream>
using namespace std;
int main() {
    char a;
    while(true)
         {cout<<"Menu:A(dd)D(elete)S(ort)Q(uit),Select one:"<<endl;
          cin>>a;
          switch (a)
          {case 'A':{cout<<"数据已经增加"<<endl;continue;}
          case 'D':{cout<<"数据已经减少"<<endl;continue;}
          case 'S':{cout<<"数据已经排序"<<endl;continue;}
          case 'Q':break;
          default:{cout<<"输入无效"<<endl;continue;}
          }
          break; 
         }
    return 0;
}

[运行截图]

 

#2-29

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

(1)while语句

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

(2)do...while语句

#include<iostream>
#include<math.h>
using namespace std;
int main()
{int i=2,j,n=0;
 do{j=2;
    do{if(i%j==0)
       break;
       j++;
      }while(j<=sqrt(i));
    if(j>sqrt(i)) 
      {cout<<i<<"\t";
       n++;
      }
    if(n%5==0&&n!=0)
      {n=0;
       cout<<endl;
      }
    i++; 
   }while(i<=99);
 return 0;
}

(3)for语句

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

[运行截图]

 

 

#2-32

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

 (1)while语句

#include<iostream>
#include<cstdlib>
#include<ctime>
using namespace std;
int main()
{int a,b;
 srand(time(0));
 a=rand()%100;
 cin>>b;
 while (true)
 {if (a<b)
    {cout<<"你输入的数太大了"<<endl;
     cin>>b;    
    }
  else if (a>b)
    {cout<<"你输入的数太小了"<<endl;
     cin>>b;    
    }
  else 
    {cout<<"你猜对了"<<endl;
     break;    
    }
 }
return 0;    
}

(2)do...while语句

#include<iostream>
#include<cstdlib>
#include<ctime>
using namespace std;
int main()
{int a,b;
 srand(time(0));
 a=rand()%100;
 cin>>b;
 do
 {if (a<b)
    {cout<<"你输入的数太大了"<<endl;
     cin>>b;    
    }
  else if (a>b)
    {cout<<"你输入的数太小了"<<endl;
     cin>>b;    
    }
  else 
    {cout<<"你猜对了"<<endl;
     break;    
    }
 }while (true);
 return 0;
}

[运行截图]

#2-34

口袋中有红黄蓝白黑5种颜色的球若干个。没词葱口袋中取出3个颜色不同的球,问有多少种取法。

#include<iostream>
using namespace std;
void A(int a)
{switch(a)
 {case 1:cout<<"红\t";break;
  case 2:cout<<"黄\t";break;
  case 3:cout<<"蓝\t";break;
  case 4:cout<<"白\t";break;
  case 5:cout<<"黑\t";break;
 }
}
int main()
{int n=0,i,j,k; 
 for(i=1;i<=5;i++)
    {for(j=i+1;j<=5;j++)
        {for(k=j+1;k<=5;k++)
            {A(i);A(j);A(k);
             cout<<endl;
             n++;
            }
        }
    }
 cout<<"一共"<<n<<"" ;
return 0;    
}

[运行截图]

【实验总结与体会】

自己上网查随机数的方法,现在只会一种根据时间来随机的方法,但也至少学会了一种了,还有很多头文件包含的函数要学。

https://www.cnblogs.com/shenqidetao/p/10539780.html

https://www.cnblogs.com/aiwenzhuo/p/10547801.html

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

https://www.cnblogs.com/hongzai1206/p/10548435.html

转载于:https://www.cnblogs.com/xiaobailong123/p/10539836.html

  • 0
    点赞
  • 2
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值