haha

#include<iostream>
#include<fstream>
#include<cmath>
using namespace std;


ifstream fin("record.txt");
ofstream fout("record.txt",ios::app);//在文件末尾写入 


int i;
int Time_1,Time_2;                   //时间 
int Num_1[11],Num_2[11],M[11];       //分别记录生成的两个数字及符号 


char m[4]={'+','-','*','/'};         //记录四则运算的字符数组 


double cAns[11],Ans[11];             //双精度型数据记录结果 


string ID;


bool CInit(char c,int index)         //判断输入ID是否合法 
{
     switch( index )
     {
             case 0:
                  if( (c>='A' && c<='Z') || ( c>='a' && c<='z' ) ) return 1;
                      else return 0;
             default:
                   if( c>='1' && c<='9' ) return 1; 
                      else return 0;
     } 
}


int Init()                          //输入 
{
    int a;
    flag: 
    cout<<"Please input your ID no:";
    cin>>ID;
    for( int i=0 ;i<6 ;i++ )
         if( !CInit(ID[i],i/2) )  
         {
             cout<<"wrong ID, please input again\n";
             goto flag; 
         } 
    cout<<endl;
    cout<<"(1)	Start a test\n(2)	Check scores\n(3)	Exit\n\n";
    cin>>a;
    cout<<"\n";
    return a;


}


double Cal(int a,int b,int c)     //计算 
{
    switch(c)
    {
             case 0:return a+b;
             case 1:return a-b;
             case 2:return a*b;
             case 3:return double(a)/double(b);
    }
}


bool Test()                        //生成测试题 
{
    int a,b,c;
    double ans,tmp1,tmp2;
    
    srand((unsigned)time(NULL)); 
    Num_1[i]=a=rand()%100+1;
    Num_2[i]=b=rand()%100+1;
    M[i]=c=rand()%4;
    cout<<a<<m[c]<<b<<'=';
    cin>>ans;
    Ans[i]=ans;
    cAns[i]=Cal(a,b,c);
    if( abs(Ans[i]-cAns[i])<=0.01 ) return 1;   //设置为保留两位小数 
        else
            return 0;


}


void Do_1()                                     //测试部分主函数 
{
     Time_1=time(NULL);
     int Mark=0;
     for( i=1 ;i<=10 ;i++ )
          if( Test() ) Mark+=10;
     Time_2=time(NULL);
     cout<<"ID:"<<ID<<" Mark:"<<Mark<<" Time:"<<Time_2-Time_1<<"seconds\n";
     cout<<"Prob. | CorrectAnsw. | UrAnsw\n";
     for( int j=1 ;j<=10 ;j++ )
          cout<<Num_1[j]<<m[M[j]]<<Num_2[j]<<"=?\t| "<<cAns[j]<<"\t| "<<Ans[j]<<endl;
     fout<<ID<<" "<<Mark<<" "<<Time_2-Time_1<<"seconds\n";
}


void Check()                                     //读入record,输出历史成绩 
{
     string cID,tmp;
     cout<<"Your previous records are:\n";
     while( fin>>cID )
            if( cID==ID )
            {
                cout<<ID<<" ";
                while( fin>>tmp )
                {
                       cout<<tmp<<" ";
                       if( tmp[tmp.length()-1]=='s' ) break;
                }
                cout<<endl;
            }
}


int main()
{
    switch(Init())
    {
                  case 1:Do_1();break;
                  case 2:Check();break;
                  case 3:cout<<"欢迎再次使用\n";
    }


    system("pause");
    return 0;
}


评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值