学习c++半年的试水作品

博主分享了自己学习C++半年后的初步作品,包括猜数字游戏、抽奖程序、加法计算器、数字雨及体质指数计算等多个项目。这些项目在Dev-c++环境下运行,诚邀经验丰富的开发者给予指导。
摘要由CSDN通过智能技术生成

本人学习c++半年的试水作品,做的不好,请见谅(不喜勿喷)(全部用Dev-c++运行)

1、猜数字

#include<iostream>
#include<cstdlib>
#include<ctime>
using namespace std;
int main(){
	int x,shu,cishu;
	srand(time(0));
	shu=rand()%101;
	while(1){
		cout<<"请输入0-100的数:";
		cin>>x;
		cishu++;
		if (x==shu){
		cout<<"猜对了!"<<endl;
		cout<<"你用了"<<cishu<<"次"<<endl;
		break; 
		}
		else if (x>shu){
			cout<<"大了"<<endl;
		}
		else if (x<shu){
			cout<<"小了"<<endl;
		}
		else{
			cout<<"你输入的信息错误,次数依然会加1"<<endl;
		}
	}		
}

2、抽奖游戏

#include<iostream>
#include<cstdlib>
#include<ctime>
#include<conio.h>
#include<windows.h> 
using namespace std;
int main(){
	int a,n=100,c2,r=0;
	int i;
	srand(time(0));
	while(1){
		cout<<"抽一次10元"<<endl;
		cout<<"2元:59%"<<endl;
		cout<<"5元:25%"<<endl;
		cout<<"10元:10%"<<endl;
		cout<<"20元:5%"<<endl;
		cout<<"500元:1%"<<endl;
		cout<<"你有"<<n<<"元"<<endl;
		cout<<"1、抽一次"<<" "<<"2、不抽了"<<endl;
		int c;
		c=getch();
		if(c=='1'){
			if (n<10||n==10){
				system("cls");
				cout<<"你的余额不足,抽奖结束"<<endl;
				cout<<"你抽了"<<r<<"次"<<endl; 
				break;
			}
			else{
				for(i=0;i<=3;i++){
					cout<<"抽奖中.";
					Sleep(1000);
					system("cls");
					cout<<"抽奖中..";
					Sleep(1000);
					system("cls");
					cout<<"抽奖中...";
					Sleep(1000);
					system("cls");
				}
				while(1){
					r=r+1;
					n=n-10;
					a=rand()%100;
					if(a==1||a<1){
						system("cls");
						cout<<"你抽中了500元!!!!"<<endl;
						n=n+500;
						cout<<"知道了(按任意键退出)"<<endl;
						c2=getch();
						system("cls");
						break;
					}
					if(a==5||a<5){
						system("cls");
						cout<<"你抽中了20元"<<endl;
						n=n+20;
						cout<<"知道了(按任意键退出)"<<endl;
						c2=getch();
						system("cls");
						break;
					}
					if(a==10||a<10){
						system("cls");
						cout<<"你抽中了10元"<<endl;
						n=n+10;
						cout<<"知道了(按任意键退出)"<<endl;
						c2=getch();
						system("cls");
						break;
					} 
					if(a==25||a<25){
						system("cls");
						cout<<"你抽中了25元"<<endl;
						n=n+25;
						cout<<"知道了(按任意键退出)"<<endl;
						c2=getch();
						system("cls");
						break;
					}
					if(a==59||a<59){
						system("cls");
						cout<<"你抽中了2元"<<endl;
						n=n+2;
						cout<<"知道了(按任意键退出)"<<endl;
						c2=getch();
						system("cls");
						break;
					}	
				}
			}	
		}
		if(c=='2'){
			system("cls");
			cout<<"你的抽完奖后的金额是"<<n<<endl;
			cout<<"你抽了"<<r<<"次"<<endl; 
			break;
		} 
	}
	return 0;
} 

3、加法计算机

#include<iostream>
using namespace std;
int main(){
	float a,s,d,f,g;
	cout<<"欢迎来到加法计算机"<<endl;
	cout<<"注意:一次计算只能输入5个数字!!!"<<endl;
	cout<<"请输入五个数字"<<endl;
	cin>>a>>s>>d>>f>>g;
	cout<<"答案是:"<<a+s+d+f+g;
	return 0; 
}

4、

#include<iostream>
#include<cstdlib>
#include<ctime>
using namespace std;
int main(){
	int playerscore,computerscore,i,playerschoice,choiceofcomputer,itendsinadraw;
	playerscore=0;
	choiceofcomputer=0;
	
	for(i=1;i<=10;i++){
		cout<<"请输入你要出什么:"<<endl; 
		cout<<"1、石头 2、剪刀 3、布"<<endl; 
		cin>>playerschoice;
		choiceofcomputer=rand()%3+1;
		if(playerschoice==1&&choiceofcomputer==2 ||playerschoice==2&&choiceofcomputer==3 ||playerschoice==3&&choiceofcomputer==1){
			cout<<"你赢了"<<endl;
			playerscore=playerscore+1;
		}	
		if(playerschoice==1&&choiceofcomputer==3 || playerschoice==2&&choiceofcomputer==1 ||playerschoice==3&&choiceofcomputer==2){
			cout<<"你输了"<<endl;
			choiceofcomputer=choiceofcomputer+1;
		}	
		if	(playerschoice==1&&choiceofcomputer==1 || playerschoice==2&&choiceofcomputer==2||playerschoice==3&&choiceofcomputer==2){
			 cout<<"平局"<<endl; 
			 itendsinadraw=itendsinadraw+1;
		}
	}	
	cout<<"你赢了"<<playerscore<<"场"<<endl;
	cout<<"电脑赢了"<<choiceofcomputer<<"场"<<endl;
	cout<<"平局"<<itendsinadraw<<"场"<<endl;		
	
}

5、数字雨

#include<iostream>
#include<cstdlib>
#include<ctime>
using namespace std;
int main(){
	int i;
 	i=1;
	while(1){
		i=rand()%101;
		cout<<i;
	}
	return 0;	
}

8、体质指数

#include<iostream>
using namespace std;
int main(){
	float kg,m,bmi;
	cout<<"高度:"; 
	cin>>m;
	cout<<"体重:";
	cin>>kg;
	bmi=kg/(m*m);
	if (m>0&&m<=3&&kg>0&&kg<=300){
		if (bmi<18.5)
			cout<<"偏瘦"<<endl; 
		else if (bmi<24)
			cout<<"正常"<<endl; 
		else if (bmi<28)
			cout<<"偏胖"<<endl;
		else if (bmi<40)
			cout<<"肥胖"<<endl;
		else 
			cout<<"极重度肥胖"<<endl;
	}
	else 
		cout<<"你输入的数据错误"<<endl; 
	return 0;
}

请大佬指点

评论 2
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值