C++第二十课:快递运费计算器 / 黑白配+石头剪刀布小游戏

快递运费计算器

帮一家快递站点开发一个快递运费计算器,快递站点人员只需要输入包裹重量和地点编号即可计算出对应的运费。

假设快递费计算规则如下:

首重:3公斤 3公斤以内:1.东三省/宁夏/青海/海南:12元,2.新疆/西藏:20元,3.港澳台/国外:不接受寄件,4.其他地区:10元。

超过3公斤部分:1.东三省/宁夏/青海/海南:8元/公斤,2.新疆/西藏:10元/公斤,3.港澳台/国外:联系公司总部,4.其他地区:5元/公斤

如果要求加急,需额外收加急费

3公斤以内: 1.东三省/宁夏/青海/海南:12元,2.新疆/西藏:20元,3.港澳台/国外:不接受件,其他地区:5元 超过3公斤: 1.东三省/宁夏/青海/海南:20元,2.新疆/西藏:30元,3.港澳台/国外:联系公司总部,4.其他地区:10元

cpp 复制代码
#include<iostream>
using namespace std;
int main() {
    int height, num, money, urgent;
    cout << "请输入包裹的重量(单位:kg):";
    cin >> height;
    cout << "请输入地区编号(1.东三省/宁夏/青海/海南,2.新疆/西藏,3.港澳台/国外,4.其他地区):";
    cin >> num;
    cout << "是否加急(1.加急 0.不加急):";
    cin >> urgent;
    if (height <= 0) {
        cout << "你输入的包裹重量有误。" << endl;
    } else if (height <= 3) {
        cout << "首重。" << endl;
        switch (num) {
            case 1:
                money = 12;
                if (urgent == 1) money += 12;
                break;
            case 2:
                money = 20;
                if (urgent == 1) money += 20;
                break;
            case 3:
                money = 9999;
                cout << "不接受寄件。" << endl;
                break;
            case 4:
                money = 10;
                if (urgent == 1) money += 5;
                break;
            default:
                cout << "输入的地区编号有误" << endl;
        }
    } else {
        cout << "超过3公斤。" << endl;
        switch (num) {
            case 1:
                money = 12 + (height - 3) * 8;
                if (urgent == 1) money += 20;
                break;
            case 2:
                money = 20 + (height - 3) * 10;
                if (urgent == 1) money += 30;
                break;
            case 3:
                money = 99999;
                cout << "请联系公司总部。" << endl;
                break;
            case 4:
                money = 10 + (height - 3) * 5;
                if (urgent == 1) money += 10;
                break;
            default:
                cout << "输入的地区编号有误" << endl;
        }
    }
    cout << "本次快递费用:" << money << endl;
    return 0;
}

黑白配+石头剪刀布小游戏

请设计一个四人参与的游戏程序,规则如下:

  1. 游戏参与者包括3个电脑玩家和1个人类玩家

  2. 游戏分为三个阶段:

  • 第一回合:所有玩家从"黑"和"白"中选择一个

  • 第二回合:剩余玩家继续从"黑"和"白"中选择一个

  • 第三回合:最后两位玩家进行石头剪刀布对决

  1. 具体规则:
  • 电脑玩家的选择通过随机数生成

  • 人类玩家通过输入进行选择

  • 前两回合采用"少数者淘汰制":选择人数较少的颜色对应的玩家被淘汰

  • 如果两种颜色选择人数相同,则判定为平局,游戏结束

  • 每回合淘汰1名玩家,直到剩余2名玩家

  • 最后两位玩家通过石头剪刀布决定最终胜负(石头胜剪刀,剪刀胜布,布胜石头)

  1. 程序需要:
  • 清晰展示每回合的选择结果

  • 正确判断淘汰情况并显示

  • 当人类玩家被淘汰时,游戏结束并提示

  • 处理可能的非法输入 请根据以上需求实现这个游戏程序。

cpp 复制代码
#include<iostream>
#include<ctime> 
#include<cstdlib>

using namespace std;

int main() {
	srand(time(0)); 
    int num_1 = rand()%2+1;
    int num_2 = rand()%2+1;
    int num_3 = rand()%2+1;
    
    string str_1; 
    string str_2; 
    string str_3; 
    string str_me;
    
    // 数量
	int sum_h = 0;
	int sum_b = 0;
	
	// 游戏人数
	int sum_rs = 4; 
    
    // 随机数与变量进行对应
	switch(num_1){
		case 1:str_1 = "黑";sum_h++;break;
		case 2:str_1 = "白";sum_b++;break;
	} 
	switch(num_2){
		case 1:str_2 = "黑";sum_h++;break;
		case 2:str_2 = "白";sum_b++;break;
	} 
	switch(num_3){
		case 1:str_3 = "黑";sum_h++;break;
		case 2:str_3 = "白";sum_b++;break;
	} 
	
    // 第一回合 
	cout<<"游戏开始,请出(黑/白):";
	cin>>str_me;
	if(str_me == "黑"){
		sum_h++;
	}else if(str_me=="白"){
		sum_b++; 
	}else{
		cout<<"输入错误!";
		return 0;
	}
	cout<<"结果为:"<<endl;
	cout<<"我:"<<str_me<<"  其他人:"<<str_1<<" "<<str_2<<" "<<str_3<<endl;
	// 淘汰
	if(sum_h>sum_b){
		cout<<"白色淘汰"<<endl;
		if(str_me=="白"){
			cout<<"我是白色,我被淘汰,游戏结束";
			return 0; 
		} 
	} else if(sum_h<sum_b){
		cout<<"黑色淘汰"<<endl;
		if(str_me=="黑"){
			cout<<"我是黑色,我被淘汰,游戏结束";
			return 0;
		}
	}else{
		cout<<"平局,游戏结束";
		return 0;
	} 
	cout<<"我没有被淘汰,游戏继续"<<endl;
	sum_rs -= 1;
	cout<<"游戏剩余人数:"<<sum_rs<<endl; // 剩余:3个 
	// 第二回合
	sum_h = 0; // 重置计数 
	sum_b = 0; // 重置计数 
	int num_11 = rand()%2+1;
    int num_22 = rand()%2+1;
    
     // 随机数与变量进行对应
	switch(num_11){
		case 1:str_1 = "黑";sum_h++;break;
		case 2:str_1 = "白";sum_b++;break;
	} 
	switch(num_22){
		case 1:str_2 = "黑";sum_h++;break;
		case 2:str_2 = "白";sum_b++;break;
	} 
 
	
	cout<<"游戏开始,请出(黑/白):";
	cin>>str_me;
	if(str_me == "黑"){
		sum_h++;
	}else if(str_me=="白"){
		sum_b++; 
	}else{
		cout<<"输入错误!";
		return 0;
	}
	cout<<"结果为:"<<endl;
	cout<<"我:"<<str_me<<"  其他人:"<<str_1<<" "<<str_2<<endl;
	// 淘汰
	if(sum_h>sum_b){
		cout<<"白色淘汰"<<endl;
		if(str_me=="白"){
			cout<<"我是白色,我被淘汰,游戏结束";
			return 0; 
		} 
	} else if(sum_h<sum_b){
		cout<<"黑色淘汰"<<endl;
		if(str_me=="黑"){
			cout<<"我是黑色,我被淘汰,游戏结束";
			return 0;  
	    }
	}else{
		cout<<"平局,游戏结束";
		return 0;
	} 
	cout<<"我没有被淘汰,游戏继续"<<endl;
	sum_rs -= 1;
	cout<<"游戏剩余人数:"<<sum_rs<<endl; // 剩余:2个
	
	// 第三回合
    // 使用石头剪刀布
    cout<<"请输入你的选择(石头/剪刀/布):";
	string str_my;
	cin>>str_my;
	// 随机生成对手结果
	int num_ds = rand()%3+1;
	string str_ds;
	// 随机数与变量进行对应
	switch(num_ds){
		case 1:str_ds = "石头";break;
		case 2:str_ds = "剪刀";break;
		case 3: str_ds = "布";break; 
	} 
	//对手出的结果
	cout << "对手出:" << str_ds << endl; 
	// 判断
	if(str_my=="石头"){
		if(str_ds=="石头"){
			cout<<"平局"; 
		}else if(str_ds=="剪刀"){
			cout<<"你赢了!";
		}else if(str_ds=="布"){
			cout<<"你输了!";
		}
	}else if(str_my=="剪刀"){
		    if(str_ds=="石头"){
			    cout<<"你输了!"; 
	    	}else if(str_ds=="剪刀"){
			    cout<<"平局!";
		    }else if(str_ds=="布"){
			    cout<<"你赢了!";
		    }
	}else if(str_my=="布"){
		    if(str_ds=="石头"){
			    cout<<"你赢了!"; 
	    	}else if(str_ds=="剪刀"){
			    cout<<"你输了!";
		    }else if(str_ds=="布"){
			    cout<<"平局!";
		    }
		}else{
            cout << "输入错误!";  // 新增:处理非法输入
        }
			 

    return 0;
}