面向对象编程的C++,和平时做题用的C++还是有差距的。实验的题目都是小题目,就都做一下吧。

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

1、  猜价格游戏

编写C++程序完成以下功能:

(1)      假定有一件商品,程序用随机数指定该商品的价格(1-1000的整数);

(2)      提示用户猜价格,并输入:若用户猜的价格比商品价格高或低,对用户作出相应的提示;

(3)      直到猜对为止,并给出提示。

 

1 #include <iostream> 2 #include <ctime> 3 #include <cstdlib> 4 using namespace std; 5 int main() { 6 srand((int)time(0)); 7 int price=rand()%1000+1;//产生1到1000的随机数 8 int l=1,r=1000; 9 while(1) { 10 cout<<"您可以猜一个价格,当前范围["<<l<<","<<r<<"]的整数。"<<endl; 11 int guess; 12 cin>>guess; 13 if(guess>1000||guess<1||cin.fail()) { 14 cout<<"输入不合法,请重新输入"<<endl; 15 cin.clear(); 16 cin.ignore(10000,'\n'); 17 continue; 18 } 19 if(guess>price) { 20 cout<<"猜大了"<<endl; 21 if(r>guess) 22 r=guess;//缩小范围 23 } else if(guess<price) { 24 cout<<"猜小了"<<endl; 25 if(l<guess) 26 l=guess;//缩小范围 27 } else { 28 cout<<"您猜对了,价格就是"<<price<<endl; 29 break; 30 } 31 } 32 return 0; 33 }

View Code

实验问题

    • 如何产生随机数 
      通过srand((int)time(0));生成随机数种子 
      然后用rand()%1000+1来产生1到1000的随机数
    • 是否要提示当前范围 
      为了人性化,每次猜了一个价格,就会根据猜大了或者小了来缩小下一次猜的范围。但是如果用户输入的数字不在当前范围内,则不改变猜的范围。
    • 如果输入的不是数字怎么办 
      cin.fail()为真则代表输入失败然后就 
      cout<<"输入不合法,请重新输入"< cin.clear(); 
      cin.ignore(10000,'\n'); 
      continue; 
      (可是这样真麻烦,后面的程序就不写了)

2、  计算 N 以内的所有素数

编写C++程序完成以下功能:

(1)      提示用户输入N;

(2)      计算出从2到N之间的所有素数;

(3)      将结果保存在一个文本文件中。

#include <iostream>
#include <cstring>
#include <fstream>
#define N 1000000
using namespace std;
int prime[N],cnt,n;
void getPrime(){
    for(int i=2;i<=n;i++){
        if(!prime[i])prime[++cnt]=i;
        for(int j=1;j<=cnt&&prime[j]<=N/i;j++){
            prime[prime[j]*i]=1;
            if(i%prime[j]==0)break;
        }
    }
}
int main(){
    ofstream f("prime.txt");
    cout<<"请输入n"<<endl;
    cin>>n;
    getPrime();
    for(int i=1;i<=cnt;i++)
        f<<prime[i]<<" ";
    f.close();
    return 0;
}

 

3、  袋中取球

编写C++程序完成以下功能(使用 enum):

(1)      袋子中有 red, yellow, blue, white, black 五种颜色的球多个;

(2)      一次从袋子里取出3个颜色不同的球,有几种取法;

(3)      将每种方法的所有取法输出到屏幕上。

 

#include <iostream>
using namespace std;
enum ball{
    red,yellow,blue,white,black
};
void output(int i){
    switch(i){
        case red:cout<<"red ";break;
        case yellow:cout<<"yellow ";break;
        case blue:cout<<"blue ";break;
        case white:cout<<"white ";break;
        case black:cout<<"black ";break;
    }
}

int main(){
    for(int i=red;i<=black;i++) 
        for(int j=i+1;j<=black;j++)
            for(int k=j+1;k<=black;k++){
                output(i);
                output(j);
                output(k);
                cout<<endl;
            }    
    return 0;
}

View Code

 

 

4、  乘法口诀表

编写C++程序完成以下功能:

(1)      输出乘法口诀表;

(2)      显示格式如下所示。

 

1*1=1  1*2=2  1*3=3  1*4=4  1*5=5  1*6=6  1*7=7  1*8=8  1*9=9  
       2*2=4  2*3=6  2*4=8  2*5=10 2*6=12 2*7=14 2*8=16 2*9=18 
              3*3=9  3*4=12 3*5=15 3*6=18 3*7=21 3*8=24 3*9=27 
                     4*4=16 4*5=20 4*6=24 4*7=28 4*8=32 4*9=36 
                            5*5=25 5*6=30 5*7=35 5*8=40 5*9=45 
                                   6*6=36 6*7=42 6*8=48 6*9=54 
                                          7*7=49 7*8=56 7*9=63 
                                                 8*8=64 8*9=72 
                                                        9*9=81

 

#include <iostream>
#include <iomanip>
using namespace std;
int main(){
    for(int i=1;i<=9;i++){
        for(int j=1;j<=i*7;j++)
            cout<<" ";
        for(int j=i;j<=9;j++)
            cout<<i<<"*"<<j<<"="<<setw(3)<<left<<i*j;
        cout<<endl;
    }
}

 

 

5、  最大公约数和最小公倍数

编写C++程序完成以下功能:

(1)      提示用户输入两个无符号整数;

(2)      计算两者的最大公约数和最小公倍数,并输出。

 

#include <iostream>
#define uint unsigned int
using namespace std;

uint gcd(uint a,uint b){
    return b?gcd(b,a%b):a;
}
int main(){
    uint a,b;
    cout<<"请输入两个无符号整数"<<endl;
    cin>>a>>b;
    cout<<a<<""<<b<<"的最大公约数是"<<gcd(a,b);
    cout<<",最小公倍数是"<<b/gcd(a,b)*a<<endl;
    return 0;
}
View Code

相关文章:

  • 2021-05-16
  • 2022-12-23
  • 2021-08-12
  • 2021-10-05
  • 2021-09-29
  • 2021-10-05
  • 2022-01-24
  • 2021-08-17
猜你喜欢
  • 2021-09-20
  • 2021-07-11
  • 2023-04-04
  • 2021-10-24
  • 2022-12-23
  • 2022-12-23
  • 2021-12-24
相关资源
相似解决方案