代码改变世界

实验一

2019-03-17 17:20  Chirly  阅读(200)  评论(1编辑  收藏  举报

 

 

 

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

①if...else语句

#include <iostream>
using namespace std;
int main() {                                                                                                                               
char Select;
cout<<"Menu:A(dd) D(elete) S(ort) Q(uit),Select one:";endl;
cin>>Select;
while (Select!='Q')
   {switch (select)
   {case 'A':cout<<"Data has added."<<endl;break;
    case 'D':cout<<"Data has deleted."<<endl;break;
    case 'S':cout<<"Data has sorted."<<endl;break;
    default:cout<<"Menu:A(dd) D(elete) S(ort) Q(uit),Select one:";endl;}
    cout<<"Menu:A(dd) D(elete) S(ort) Q(uit),Select one:";
  cin>>Select;
   }
return 0;
}
2-28-1

 

运行结果:

②Switch语句

#include <iostream>
using namespace std;
int main() {                                                                                                                               
char Select;
cout<<"Menu:A(dd) D(elete) S(ort) Q(uit),Select one:";
cin>>Select;
while (Select!='Q')
   {
   switch (Select)
   {
    case 'A':cout<<"Data has added."<<endl;break;
    case 'D':cout<<"Data has deleted."<<endl;break;
    case 'S':cout<<"Data has sorted."<<endl;break;
    default:cout<<"Input Error!"<<endl;break;}
    cout<<"Menu:A(dd) D(elete) S(ort) Q(uit):";
  cin>>Select;
   }
return 0;
}
2-28-2

运行结果:

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

①while语句

2-29-1
#include<iostream>#include<cmath>usingnamespacestd;intmain(){intn=2,t=0,i,f;while(n<=100){f=1;for(i=2;i<=sqrt(n);i++)if(n%i==0){f==0;break;}if(f==1){cout<<""<<n;t++;cout<<endl;}n++;}}

运行结果:

②do...while语句

#include <iostream>
#include <cmath>
using namespace std;
int main()
{int n=2,t=0,i,f;
do
{f=1;
for (i=2;i<=sqrt(n);i++)
if (n%i==0)
{f==0;
break;
}
if (f==1)
{cout<<""<<n;
t++;
cout<<endl;
}
n++;
}
while (n<=100);
}
2-29-2

运行结果:

③for语句

#include <iostream>
#include <cmath>
using namespace std;
int main()
{int n=2,t=0,i,f;
for (n=2;n<=100;n++) 
{f=1;
for (i=2;i<=sqrt(n);i++)
if (n%i==0)
{f==0;
break;
}
if (f==1)
{cout<<""<<n;
t++;
cout<<endl;
}
n++;
}
}
2-29-3
 

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

 

①while语句

#include <iostream>
using namespace std;
int main()
{int n=88,m;
cout<<"Please guess a number(1-100):";
cin>>m;
while (n!=m)
{if (m<n)
{cout<<"Bigger than the number:";
cin>>m;
 } 
 if (m>n)
{cout<<"Smaller than the number:";
cin>>m;
}
}
cout<<"You are right!"<<endl;
return 0;
}
2-32-1

②do...while语句

#include <iostream>
using namespace std;
int main()
{int n=88,m;
cout<<"Please guess a number(1-100):";
cin>>m;
do
{if (m<n)
{cout<<"Bigger than the number:";
cin>>m;
 } 
 if (m>n)
{cout<<"Smaller than the number:";
cin>>m;
}
}
while (n!=m);
cout<<"You are right!"<<endl;
return 0;
}
2-32-2

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

#include <iostream>
using namespace std;
int main()
{int a,b,c;
 for (a=0;a<=4;a++)
 for (b=a+1;b<=4;b++)
 for (c=b+1;c<=4;c++)
 cout<<a<<b<<c<<endl;
return 0;    
}
3-34

 互评:

https://www.cnblogs.com/mxueyyqx/p/10544369.html

https://www.cnblogs.com/Yyaoyyy/p/10547742.html

https://www.cnblogs.com/fearless04/p/10543040.html