解决的计算器问题

来源:互联网 发布:减速箱装配图 淘宝 编辑:程序博客网 时间:2024/06/05 20:22
<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>}}所以在我们写程序的时候,要考虑多种因素!争取到最简洁,最少耗时的程序!
0 0
原创粉丝点击