您的位置:首页 > 编程语言 > C语言/C++

C++简单程序设计-2

2018-03-24 15:11 162 查看

(1)习题2-28

(1)

#include<iostream>
using namespace std;

int main(){
char x;
do{
cout<<"Menu: Add Delete Sort Quit, Select one:"<<endl;
cin>>x;
switch(x)
{
case 'A':   cout<<"数据已增加"<<endl;    break;
case 'D':   cout<<"数据已删除"<<endl;    break;
case 'S':   cout<<"数据已排序"<<endl;    break;
case 'Q':   break;
default:    continue;
}
}while(x!='Q');
return 0;
}


(2)

#include<iostream>
using namespace std;

int main(){
char x;
do{
cout<<"Menu: Add Delete Sort Quit, Select one:"<<endl;
cin>>x;
if(x=='A') cout<<"数据已增加"<<endl;
else if(x=='D') cout<<"数据已删除"<<endl;
else if(x=='S') cout<<"数据已排序"<<endl;
else if(x=='Q') break;
else continue;
}while(x!='Q');
return 0;
}




习题2-29

#include<iostream>
using namespace std;

bool check(int a){
int i=1;
while(i<a){
if(i!=1&&i!=a&&a%i==0)
return false;
i++;
}
return true;
}

int main(){
int a=1;
while(a<100){
if(check(a))    cout<<a<<" ";
a=a+1;
}
return 0;
}


(2)

#include<iostream>
using namespace std;

bool check(int a){
int i;
for(i=1;i<a;i++){
if(i!=1&&i!=a&&a%i==0)
return false;
}
return true;
}

int main(){
int a;
for(a=1;a<100;a++){
if(check(a))    cout<<a<<" ";
}
return 0;
}




习题2-32

(1)

#include<iostream>
#include<stdlib.h>
#include<ctime>
using namespace std;

int random(){
int a;
srand((unsigned)time(NULL));
a=rand()%100;
return a;
}

int main(){
int x=random(),y;
do{
cin>>y;
if(y>x)
{cout<<"输入值过大"<<endl;   continue;}
else if(y<x)
{cout<<"输入值过小"<<endl;   continue;}
else if(y=x)
{cout<<"输入正确"<<endl;    break;}
}while(y!=x);
return 0;
}


(2)

#include<iostream>
#include<stdlib.h>
#include<ctime>
using namespace std;

int random(){
int a;
srand((unsigned)time(NULL));
a=rand()%100;
return a;
}

int main(){
int x=random(),y;
bool z=true;
while(z==true){
cin>>y;
if(y>x)
{cout<<"输入值过大"<<endl;   continue;}
else if(y<x)
{cout<<"输入值过小"<<endl;   continue;}
else if(y=x)
{cout<<"输入正确"<<endl;    z=false;    break;}
}
return 0;




习题2-34

hljs cpp">#include<cmath>
using namespace std;
int getball(int ,int );
int main()
{
int m=5,n=3;
getball(m,n);
return 0;
}

int getball(int i,int j)
{
int result;
int a,b=0,c=1,A,B=0,C=1;
for(a=j;a>0;a--)
{
b=a;
c*=b;
}
for(A=i;A>i-j;A--)
{
B=A;
C*=B;
}
result=C/c;
cout<<"共有"<<result<<"种方法。"<<endl;
return result;
}


内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: