实验1-C++简单程序设计


2-28

#include<iostream>

using namespace std;

int main(){

char choice;

while(1)

{

cout<<"Menu:A(dd) D(elete) S(ort) Q(uit),Select one:";

cin>>choice;

if(choice=='A') cout<<"Dates has been added"<<endl;

else if(choice=='D') cout<<"Dates has been deleted"<<endl;

else if(choice=='S') cout<<"Dates has been sorted"<<endl;

else if(choice=='Q') break;

}

return 0;

}

#include<iostream>

#include<cstdlib>

using namespace std;

int main(){

char choice;

while(1)

{

cout<<"Menu:A(dd) D(elete) S(ort) Q(uit),Select one:";

cin>>choice;

switch(choice)

{

case 'A': cout<<"Dates has been added"<<endl;break;

case 'D': cout<<"Dates has been deleted"<<endl;break;

case 'S': cout<<"Dates has been sorted"<<endl;break;

case 'Q': exit(0); break;

default : cout<<"no such choice"<<endl;

}

}

return 0;

}

2-29

#include<iostream>

#include<cmath>

using namespace std;

int main(){

int i,m,n,a;

for(i=2;i<=100;i++)

{

a=1;

m=sqrt(i);

for(n=2;n<=m;n++)

{

if(i%n==0)

{

a=0;

break;

}

}

if(a==1) cout<<i<<" ";

}

return 0;

}

#include<iostream>

#include<cmath>

using namespace std;

int main()

{

int a,i,m=1,n;

do{

a=2;

n=0;

i=sqrt(m);

if(m==1) n=1;

else

{

while(a<=i)

{

if(m%a==0)

{

n=1;

break;

}

else a++;

}

}

if(n==0)

cout<<m<<" ";

m++;

}while(m<=100);

return 0;

}

2-32

#include<iostream>

using namespace std;

int main(){

int n=66,i,m=1;

while(m)

{

cout<<"Please guess the number(1-100):";

cin>>i;

if(i>n)

cout<<"bigger than number"<<endl;

else if(i<n)

cout<<"smaller than number"<<endl;

else if(i==n)

{

cout<<"you are right"<<endl;

m=0;

}

}

return 0;

}

#include<iostream>

using namespace std;

int main()

{

int n=66,i,t=1;

do

{

cout<<"Please guess the number(1-100):";

cin>>i;

if(i>n)

cout<<"bigger than number"<<endl;

else if(i<n)

cout<<"smaller than number"<<endl;

else if(i==n)

{

cout<<"you are right"<<endl;

break;

}

}while(t=1);

return 0;

}

2-34

#include<iostream>

using namespace std;

int main()

{

int m,n,i,a=0;

for(m=1;m<=5;m++)

{

for(n=m+1;n<=4;n++)

{

for(i=n+1;i<=5;i++)

{

cout<<m<<n<<i<<endl;

a++;

}

}

}

cout<<a<<endl;

return 0;

}

1、上学期因为经常使用for循环导致对do--while和while形式使用十分生硬,甚至是想不起来使用。

2、经常出现可以用很简单方式解决的问题却要用很多不必要的代码,简单来说就是废话太多。

3、明显感觉C++的输出和输入要比c更简洁明了,更为方便。

优质内容筛选与推荐>>
1、授权码模式
2、Spring Boot 之 https
3、【Unity】3.6 导入图片资源
4、LINQ查询XML
5、Entity Framework Linq 动态组合where条件


长按二维码向我转账

受苹果公司新规定影响,微信 iOS 版的赞赏功能被关闭,可通过二维码转账支持公众号。

    阅读
    好看
    已推荐到看一看
    你的朋友可以在“发现”-“看一看”看到你认为好看的文章。
    已取消,“好看”想法已同步删除
    已推荐到看一看 和朋友分享想法
    最多200字,当前共 发送

    已发送

    朋友将在看一看看到

    确定
    分享你的想法...
    取消

    分享想法到看一看

    确定
    最多200字,当前共

    发送中

    网络异常,请稍后重试

    微信扫一扫
    关注该公众号