解决的计算器问题

<pre name="code" class="cpp">#include<iostream>
#include <cstdio>
#include<ctime>
#include<cstdlib>
#include <cmath>
using namespace std;

void add_fun();
void sub_fun();
void mul_fun();
void div_fun();

int sum=0;

int main()
{
	int i,m;
	char xx[4]={'+','-','*','/'};
	m=xx[4];
	for(i=0;i<10;i++)
	{
	  srand((unsigned)time(0));
	  m=rand()%4;
	  switch(m)
	  {
	  	case 0:add_fun();break;
	  	case 1:sub_fun();break;
	  	case 2:mul_fun();break;
		case 3:div_fun();break;
	  }
    }

    cout<<"你一共做对了"<<sum<<"道题!"<<endl;
    return 0;
}

void add_fun()
{
	int x,y,z,w;
 	char yy;
	srand((unsigned)time(NULL));
	x=rand()%100;
	y=rand()%100;
	z=x+y;//得到正确答案
	cout << x <<"+"<< y << "=";
	cin >> w ;
    if(w==z)
    {
    	cout << "right!" << endl;
    	sum++;
	}
	else
    {
        cout << "wrong!点击 y 重新此题" << endl;
        cin >> yy;
        if(yy=='y')
        {
            cout<<x<<"+"<<y<<"=";
            cin>>w ;
            if(w==z)
            {
                cout << "right!" << endl;
            }
        }
    }
}

void sub_fun()
{
	int x,y,z,w;
	char yy;
    srand((unsigned)time(NULL));
    do
    {
        x=rand()%100;
        y=rand()%100;
    }while(x<=y);
        z=x-y;
        cout << x <<"-"<< y << "=";
        cin >> w;
        if(w==z)
        {
            cout << "right!" << endl;
            sum++;
        }
        else
        {
            cout << "wrong!点击 y 重新此题" << endl;
            cin >> yy;
            if(yy=='y')
            {
                cout<<x<<"-"<<y<<"=";
                cin>>w ;
                if(w==z)
       			{
            		cout << "right!" << endl;
        		}
            }
        }
}

void mul_fun()
{
 	int x,y,z,w;
 	char yy;
    srand((unsigned)time(0));
    do
    {
        x=rand()%10;
        y=rand()%10;
    }while(x<=y);
    z=x*y;
		cout<<x<<"*"<<y<<"=";
    	cin>>w ;
        if(w==z)
        {
            cout<<"right!" <<endl;
            sum++;
        }
        else
        {
            cout << "wrong!点击 y 重新此题" << endl;
            cin >> yy;
            if(yy=='y')
            {
                cout<<x<<"*"<<y<<"=";
                cin>>w ;
                if(w==z)
       			{
            		cout << "right!" << endl;
        		}
            }
        }
}

void div_fun()
{
    int x,y,z,w;
    char yy;
	srand((unsigned)time(0));
	do
    {
        x=rand()%10;
        y=rand()%10;
    }while(x%y!=0);

 		z=x/y;
		cout<<x<<"/"<<y<<"=";
    	cin >> w ;
       	if(w==z)
       	{
    		cout << "right!" << endl;
    		sum++;
	   	}
	   	else
        {
            cout << "wrong!点击 y 重新此题" << endl;
            cin >> yy;
            if(yy=='y')
            {
                cout<<x<<"*"<<y<<"=";
                cin>>w ;
                if(w==z)
       			{
            		cout << "right!" << endl;
        		}
            }
        }
}


以上是改正过后的程序,改之前的程序,用到了递归调用,导致程序不能运行,因为对于随机生成的x,y,不满足条件的一直在缓存区中,没有释放!

#include<iostream>
#include <cstdio>
#include<ctime>
#include<cstdlib>
#include <cmath>
using namespace std;


void add_fun();
void sub_fun();
void mul_fun();
void div_fun();




int main()
{
<span style="white-space:pre">	</span>int i,m;
<span style="white-space:pre">	</span>char xx[4]={'+','-','*','/'};
<span style="white-space:pre">	</span>m=xx[4];
<span style="white-space:pre">	</span>for(i=0;i<10;i++)
<span style="white-space:pre">	</span>{
<span style="white-space:pre">	</span>  srand((unsigned)time(0));
<span style="white-space:pre">	</span>  m=rand()%4;
<span style="white-space:pre">	</span>  switch(m)
<span style="white-space:pre">	</span>  {
<span style="white-space:pre">	</span>  <span style="white-space:pre">	</span>case 0:add_fun();break;
<span style="white-space:pre">	</span>  <span style="white-space:pre">	</span>case 1:sub_fun();break;
<span style="white-space:pre">	</span>  <span style="white-space:pre">	</span>case 2:mul_fun();break;
<span style="white-space:pre">		</span>case 3:div_fun();break;
<span style="white-space:pre">	</span>  }
    }


    //cout<<"你一共做对了"<<sum1<<"道题!"<<endl;
    return 0;
}




void add_fun()
{
<span style="white-space:pre">	</span>int x,y,z,w;
 <span style="white-space:pre">	</span>char yy;
<span style="white-space:pre">	</span>srand((unsigned)time(NULL));
<span style="white-space:pre">	</span>x=rand()%100;
<span style="white-space:pre">	</span>y=rand()%100;
<span style="white-space:pre">	</span>z=x+y;//得到正确答案
<span style="white-space:pre">	</span>cout << x <<"+"<< y << "=";
<span style="white-space:pre">	</span>cin >> w ;
    if(w==z)
    {
    <span style="white-space:pre">	</span>cout << "right!" << endl;
<span style="white-space:pre">	</span>}
<span style="white-space:pre">	</span>else
    {
        cout << "wrong!点击 y 重新此题" << endl;
        cin >> yy;
        if(yy=='y')
        {
            cout<<x<<"+"<<y<<"=";
            cin>>w ;
            if(w==z)
            {
                cout << "right!" << endl;


            }
        }
    }
}




void sub_fun()
{
<span style="white-space:pre">	</span>int x,y,z,w;
<span style="white-space:pre">	</span>char yy;
    srand((unsigned)time(NULL));
    x=rand()%100;
    y=rand()%100;
    if(x>y)
    {
        z=x-y;
        cout << x <<"-"<< y << "=";
        cin >> w;
        if(w==z)
        {
            cout << "right!" << endl;


        }
        else
        {
            cout << "wrong!点击 y 重新此题" << endl;
            cin >> yy;
            if(yy=='y')
            {
                cout<<x<<"-"<<y<<"=";
                cin>>w ;
                if(w==z)
       <span style="white-space:pre">			</span>{
            <span style="white-space:pre">		</span>cout << "right!" << endl;


        <span style="white-space:pre">		</span>}
            }
        }
    }
    else
    {
        sub_fun();
    }
}




void mul_fun()
{
 <span style="white-space:pre">	</span>int x,y,z,w;
 <span style="white-space:pre">	</span>char yy;
    srand((unsigned)time(0));
    do
    {
        x=rand()%10;
        y=rand()%10;
    }while(x<=y);
    z=x*y;
<span style="white-space:pre">		</span>cout<<x<<"*"<<y<<"=";
    <span style="white-space:pre">	</span>cin>>w ;
        if(w==z)
        {
            cout<<"right!" <<endl;
        }
        else
        {
            cout << "wrong!点击 y 重新此题" << endl;
            cin >> yy;
            if(yy=='y')
            {
                cout<<x<<"*"<<y<<"=";
                cin>>w ;
                if(w==z)
       <span style="white-space:pre">			</span>{
            <span style="white-space:pre">		</span>cout << "right!" << endl;


        <span style="white-space:pre">		</span>}
            }
        }
}








void div_fun()
{
    int x,y,z,w;
    char yy;
<span style="white-space:pre">	</span>srand((unsigned)time(0));
<span style="white-space:pre">	</span>do
    {
        x=rand()%10;
        y=rand()%10;
    }while(x%y!=0);


 <span style="white-space:pre">		</span>z=x/y;
<span style="white-space:pre">		</span>cout<<x<<"/"<<y<<"=";
    <span style="white-space:pre">	</span>cin >> w ;
       <span style="white-space:pre">	</span>if(w==z)
       <span style="white-space:pre">	</span>{
    <span style="white-space:pre">		</span>cout << "right!" << endl;
<span style="white-space:pre">	</span>   <span style="white-space:pre">	</span>}
}


所以在我们写程序的时候,要考虑多种因素!争取到最简洁,最少耗时的程序!






评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值