本人学习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;
}
请大佬指点