首页 技术 正文
技术 2022年11月11日
0 收藏 467 点赞 3,087 浏览 2005 个字

实验结论

2-28 if…else

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

C++实验一

switch

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

C++实验一

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

C++实验一

do…while

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

C++实验一

for

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

C++实验一

2-32 while

#include <iostream>
using namespace std;
int main()
{ int n=,m;
cout<<"please guess the nunmber(1-100): ";cin>>m;
while(n!=m)
{if(m<n)
{cout<<"bigger than the number: ";cin>>m;}
else
{cout<<"smaller than the number: ";cin>>m;}
}
cout<<"You are right!"<<endl;
return ;
}

C++实验一

do…while

#include <iostream>
using namespace std;
int main()
{ int n=,m;
cout<<"please guess the nunmber(1-100): ";cin>>m;
do
{if(m<n)
{cout<<"bigger than the number: ";cin>>m;}
else
{cout<<"smaller than the number: ";cin>>m;}
}while(n!=m);
cout<<"You are right!"<<endl;
return ;
}

C++实验一

2-34

#include <iostream>
using namespace std;
int main()
{ int i,j,k,r=;
int c1,c2,c3;
cout<<"Red: 0,Yellow: 1,Blue: 2,White: 3,Black:4"<<endl;
int a[]={,,,,};
for(i=;i<=;i++)
{c1=a[i];
for(j=i+;j<=;j++)
{c2=a[j];
for(k=j+;k<=;k++)
{c3=a[k];
r++;
cout<<c1<<c2<<c3<<endl;}
c2=a[j];
}
c1=a[i];
}
cout<<"取法有:"<<r<<"种"<<endl;
return ;
}

C++实验一

实验总结与体会

格式与C语言有点区别,还是要学挺多东西的。穷举法不会。

  

相关推荐
python开发_常用的python模块及安装方法
adodb:我们领导推荐的数据库连接组件bsddb3:BerkeleyDB的连接组件Cheetah-1.0:我比较喜欢这个版本的cheeta…
日期:2022-11-24 点赞:878 阅读:9,117
Educational Codeforces Round 11 C. Hard Process 二分
C. Hard Process题目连接:http://www.codeforces.com/contest/660/problem/CDes…
日期:2022-11-24 点赞:807 阅读:5,589
下载Ubuntn 17.04 内核源代码
zengkefu@server1:/usr/src$ uname -aLinux server1 4.10.0-19-generic #21…
日期:2022-11-24 点赞:569 阅读:6,435
可用Active Desktop Calendar V7.86 注册码序列号
可用Active Desktop Calendar V7.86 注册码序列号Name: www.greendown.cn Code: &nb…
日期:2022-11-24 点赞:733 阅读:6,206
Android调用系统相机、自定义相机、处理大图片
Android调用系统相机和自定义相机实例本博文主要是介绍了android上使用相机进行拍照并显示的两种方式,并且由于涉及到要把拍到的照片显…
日期:2022-11-24 点赞:512 阅读:7,842
Struts的使用
一、Struts2的获取  Struts的官方网站为:http://struts.apache.org/  下载完Struts2的jar包,…
日期:2022-11-24 点赞:671 阅读:4,927