这是我用c++写的一个100以内四则运算一分钟竟猜!大家看看有什么地方要改的!!
#include <iostream>#include "xsh.h"
#include <string>
#include <cstdlib>//求随机数
#include <ctime>//保证随机数的随机性,以及统计时间
int plus(int one,int two)//如果把函数名起plus再using namespace std:会有冲突
{
return one+two;
}
int subtract(int one,int two)
{
return one-two;
}
int product(int one,int two)
{
return one*two;
}
int multiply(int one,int two)
{
return one/two;
}
int main()
{
using std::cout;
using std::endl;
using std::cin;
using std::string;
string title;
string name;
string jxxhm;
int one,two;
int ending;//用来存放正确结果的
int result;//用来存放用户输入的结果的
int sum_diff=0;//用来存放做的加减题的正确的条数
int pro_quo=0;//用来存放做的乘除题的正确的条数
int x=0;//用来存放switch的控制变量,做那条运算
clock_t delay = 10*CLOCKS_PER_SEC;
title="欢迎来到老邢设计的100以内的四则运算竟猜游戏";
show_title(title);
cout<<"游戏规则:"<<endl;
cout<<" "<<"时间为一分钟"<<endl;
cout<<" "<<"答对一题加减题得10分,答对一题乘除题得20分"<<endl;
cout<<" "<<"答错一题加减题减20分,答错一题乘除题减10分"<<endl;
cout<<"大侠请您输入您的姓名!:";
cin>>name;
cout<<"输入play开始游戏:";
srand((unsigned)time(NULL));
while(name!="play")
{ cin>>name;
}
clock_t start_time = clock();
while(1)
{//刚刚竟然因为这个花括号没写纠结了半天
while(1)
{
one=rand()%100+1;
two=rand()%100+1;
x=rand()%4+1;
switch(x)
{
case 1 : cout<<one<<"+"<<two<<"=?:";
ending=plus(one,two);
break;
case 2 : cout<<one<<"-"<<two<<"=?:";
ending=subtract(one,two);
break;
case 3 : cout<<one<<"*"<<two<<"=?:";
ending=product(one,two);
break;
case 4 : cout<<one<<"/"<<two<<"=?:";
ending=multiply(one,two);
break;
}
cin>>result;
if(result==ending)
{
if(x==1||x==2)
{
cout<<"恭喜你!答对了!加10分!!!!!"<<endl;
++sum_diff;
}
else
{
cout<<"恭喜你!答对了!加20分!!!!!"<<endl;
pro_quo+=2;
}
}
else
{
if(x==1||x==2)
{
cout<<"你ma坑爹啊!100以内的加减题都做错了!减你20分!!!!!"<<endl;
sum_diff-=2;
}
else
{
cout<<"你小学没毕业吧!100以内的乘除题都做错了!减你10分!!!!!"<<endl;
--pro_quo;
}
}
if(clock()-start_time>delay) break;
}
cout<<"还要再继续吗?(输入yes继续非yes退出):";
cin>>jxxhm;
if(jxxhm!="yes") break;
}
cout<<"战绩统计:"<<endl;
cout<<" "<<"加减题得分:"<<sum_diff*10<<endl;
cout<<" "<<"乘除题得分:"<<pro_quo*10<<endl;
cout<<" "<<"总分:"<<sum_diff*10+pro_quo*10<<endl;
system("pause");
return 0;
}