julia_faneast 2013-10-31 10:36
浏览 1193

VC6.0 小学生四则运算

程序调试没有错误,就是运行不了
#include
#include
#include
#include
#include
#include
#include
using namespace std;

#define MaxSize 100
struct stud
{
string usename;//文件记录的用户名
string code;//文件记录的密码
int sumnum;//总题数
double percent;//正确率
string end;
}student[100];//stud student[100];
string rightusename,rightcode;
int rightsumnum;
double rightpercent;
int n2;//文件中记录的学生所在第几个位置
int init1()
{
cout<<"小学生四则运算考试模拟系统"<<endl;
cout<<" | "<<endl;
cout<<" | "<<endl;
cout<<" 1.登陆 | "<<endl;
cout<<" | "<<endl;
cout<<"------------+-------------"<<endl;
cout<<" | "<<endl;
cout<<" 2.注册 | "<<endl;
cout<<" | "<<endl;
cout<<" | "<<endl;
cout<<"请选择(输入数字):"<<endl;
return 1;
}
int calculate()
{

        string usename;
        string code;
      ifstream fp2;
      fp2.open ("data.txt");  
         fp2.seekg (0, ios::end);  
        int last = fp2.tellg();  
     //  cout<<"个数"<<last;
     // cout<<"模版"<<sizeof(student)/100;
       int n=last/(sizeof(student)/100-7);
    // cout<<"对象个数:"<<n<<"\n"; //查询文件中对象的个数
       ifstream fp3("data.txt");
       int  i=0;
       int sumnum;
       double percent;
       string end;
       while(i<n)
       {
        fp3>>usename>>code>>sumnum>>percent>>end;
        student[i].usename=usename;
        student[i].code=code;   
        i++;
       }
return n;

}
bool checkout()//登陆验证
{

string usename;
string code;
cout<<"please input your usename:";
cin>>usename;
int count=calculate();
bool findname=false;
for (int i=0;i<count;i++)
{
if (student[i].usename==usename)
{
findname=true;
rightusename=usename;
cout<<"Name OK!\n";
int j=0;
char str[10]="\0";
cout<<"please input your password:";
while (1)
{
str[j]= getch();
if (str[j++]=='\r')break;
cout<<"*";
}
for(int s = 0;s<j-1;s++) code += str[s];
bool findcode=false;
if (student[i].code==code)
{
cout<<"\nLogin successfully!"<<endl;
findcode=true;
n2=i;
rightcode=code;
system("pause");
return true;
}
if (!findcode)
{
cout<<"\nPassword is not correct!"<<endl;
system("pause");
return false;

        }
    }
}
if (!findname)
{
    cout<<"Usename is not existed!"<<endl;
    system("pause");
    return false;
} 
 getch();

}
void Register()//注册
{
string code,usename;
double percent=0;
int sumnum=0;
cout<<"请输入你所确定用户名:";
cin>>usename;
bool findname = true;

while(findname){
findname = false;

int count = calculate();
for (int i=0;i < count;i++)
{
if (!student[i].usename.compare(usename) )
{findname=true;}
}

                        if (findname)
                        {
                            cout<<"******************************"<<endl;
                            cout<<"*        用户名已存在!      *"<<endl;
                            cout<<"*    请重新输入你的用户名!  *"<<endl;
                            cout<<"******************************"<<endl;
                            system("pause");
                            system("cls");
                            cout<<"你所确定的用户名:";
                            cin>>usename;
                        }
                 }  
             int  i=0;
            char str[10]="\0";
            cout<<"请输入你所确定的密码:";
            while (1)
            {
                str[i]= getch();
                if (str[i++]=='\r')break;
                cout<<"*";
            }

               for(int s = 0;s<i-1;s++)
                 code += str[s];
            // cout<<"\n请分别输入你所确定密码:"<<code<<endl;
                    string   end="end";
                    int number=0;
            ofstream fp("data.txt",ios::app);
                fp<<setw(16)<<usename<<"\n"<<setw(16)<<code<<"\n"
                   <<setw(4)<<sumnum<<"\n"<<setw(8)<<percent<<"\n"<<end<<endl;
               cout<<"\n"<<"\t"<<"  注册成功!"<<endl;
                cout<<"*************************************"<<endl;
                    printf("请重新登陆,退出请按E,继续请按其他键!");
                        char E;
                scanf("%s",&E);
               if(E=='E')exit(0);

}

struct

{
char ch;

int pri;

} //设定运算符优先级,ch运算符,pri优先级
lpri[]={{'=',0},{'(',1},{'*',5},{'/',5},{'+',3}, {'-',3},{')',6}},
rpri[]={{'=',0},{'(',6},{'*',4},{'/',4},{'+',2}, {'-',2},{')',1}};
int leftpri(char op) //求左运算符op的优先级
{ int i;
for (i=0;i if (lpri[i].ch==op) return lpri[i].pri;
}
int rightpri(char op) //求右运算符op的优先级
{ int i;
for (i=0;i if (rpri[i].ch==op) return rpri[i].pri;
}
bool InOp(char ch) //判断ch是否为运算符
{ if (ch=='(' || ch==')' || ch=='+' || ch=='-'
|| ch=='*' || ch=='/')
return true;
else
return false;
}
int Precede(char op1,char op2) //op1和op2运算符优先级的比较结果
{ if (leftpri(op1)==rightpri(op2))
return 0;
else if (leftpri(op1) return -1;
else
return 1;
}
void trans(char *exp,char postexp[])
//将算术表达式exp转换成后缀表达式postexp
{ struct
{ char data[MaxSize]; //存放运算符
int top; //栈指针
} op; //定义运算符栈
int i=0; //i作为postexp的下标
op.top=-1;
op.top++; //将'='进栈
op.data[op.top]='=';
while (*exp!='\0') //exp表达式未扫描完时循环
{ if (!InOp(*exp)) //为数字字符的情况
{ while (*exp>='0' && *exp<='9') //判定为数字
{ postexp[i++]=*exp;
exp++;
}
postexp[i++]='#'; //用#标识一个数值串结束
}
else //为运算符的情况
switch(Precede(op.data[op.top],*exp))
{

case -1: //栈顶运算符的优先级低:进栈
op.top++;op.data[op.top]=*exp;
exp++; //继续扫描其他字符
break;
case 0: //只有括号满足这种情况
op.top--; //将(退栈
exp++; //继续扫描其他字符
break;
case 1: //退栈并输出到postexp中
postexp[i++]=op.data[op.top];
op.top--;
break;
}
} //while (*exp!='\0')
while (op.data[op.top]!='=')
//此时exp扫描完毕,退栈到'='为止
{ postexp[i++]=op.data[op.top];
op.top--;

}
postexp[i]='\0'; //给postexp表达式添加结束标识
}

float compvalue(char exp[]) //计算后缀表达式的值
{ struct
{ float data[MaxSize]; //存放数值
int top; //栈指针
} st; //定义数值栈
float d; char ch; int t=0; //t作为exp的下标
st.top=-1; ch=exp[t];t++;
while (ch!='\0') //exp字符串未扫描完时循环
{ switch (ch)
{
case'+':st.data[st.top-1]=
st.data[st.top-1]+st.data[st.top];
st.top--;break;
case '-':st.data[st.top-1]=
st.data[st.top-1]-st.data[st.top];
st.top--;break;
case '*':st.data[st.top-1]=
st.data[st.top-1]*st.data[st.top];
st.top--;break;
case '/':
if (st.data[st.top]!=0)
st.data[st.top-1]=
st.data[st.top-1]/st.data[st.top];
else
{ cout<<"\n"<<"\t"<<"除零错误!"< exit(0); //异常退出
}
st.top--;break;
default:
d=0; //将数字字符转换成数值存放到d中
while (ch>='0' && ch<='9') //为数字字符
{ d=10*d+ch-'0';

ch=exp[t];t++;
}
st.top++; st.data[st.top]=d;
}
ch=exp[t];t++;
}
return st.data[st.top];
}

void test()

{

char algtype[10] ;//计算类型
int i,j,t,//for的循环控制变量
points = 0;//成绩

string usename,code,end;
srand((unsigned )time(NULL));
int n=rand()%6+5,//题目的个数,为5到10个
num[MaxSize][MaxSize];//操作数

char op[]={'+','-','*','/'};
cout<<"---------------------------------小学生算法运算测评-----------------------------";
cout<<"现在开始计算:"<<endl<<endl;
for(i=0;i<n;++i)
{

cout<<"第"<<i+1<<"道题:"<<endl;
srand((unsigned )time(NULL));
int m=rand()%4+2;//每道题中数字的个数,为2到5个
for(j=0;j<m;++j)
{
num[i][j] = rand() % 9+1; //产生的随机整数

algtype[j] = rand() % 4;

}
char string[MaxSize];//把符号数组和数字数组组合在一个字符串里
for(t=0,j=0;t<2*m-2;++t)
{
string[t]=num[i][j]+'0';
t++;
string[t]=op[algtype[j]];
j++;
}
string[2*m-2]=num[i][m-1]+'0';
string[2*m-1]='\0';

     //插入括号
     int t=rand() % 5,
         r=rand() % 5,//r,t都是小于m的随机数
         min,max,a;//a为字符串的长度
     while(t==r||t>=m||r>= m)
         {
             r=rand()%5;
             t=rand() % 5;
         }
         min=t>r?r:t;//min为r,t较小的数
         max=t>r?t:r;//max为t,r较大的数
     if(min==0&&max==m-1);
     else
         {
              for(a=2*m-1;a>2*min;a--)
              string[a]=string[a-1];
              string[2*min]='(';

              for(a=2*m;a>2*max+2;a--)
              string[a]=string[a-1];
              string[2*max+2]=')';
               string[2*m+1]='\0';
         }
     //输出字符串

        int d=0;
        while(string[d]!='\0')
            cout<<string[d++];
        cout<<"= ?"<<"\n"<<"\t"<<"请输入答案:";  
        char answer[4]; //学生回答 
        cin>>answer;
        char postexp[MaxSize];
        trans(string,postexp);//求得正确的答案
         int value= compvalue(postexp);//value放的是正确的结果程序的计算结果 
         int i1= 0;
         int totle=0;
         bool fuhao = false;
         if(answer[0] == '-')
            {fuhao = true;i1++;}
         for(;answer[i1]!='\0';i1++)
         {
             totle=totle*10+answer[i1]-'0';
         }
         if(fuhao)
            {totle = -totle;}
          if(totle == value)
         {
               cout<<"\t"<<"恭喜你,答案是对的!"<<endl;
               points++;
         }
         else
         {
             cout<<"\t"<<"抱歉,你做错了!";
        cout<<"\t"<<"正确的答案是:"<<value<<endl;
         }

       int sumnum;
       double percent;
        char yesORno ; //回答是否继续 
        if(i<n-1)
        {
            cout<<"\t"<<"是否继续?y/n"<<"\t";
            cin>>yesORno;
            if(yesORno =='y')
                cout<<"~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~"<<endl;
            else 
            {
                cout<<"~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~"<<endl;
                cout<<"你已终止答题!"<<endl;
                cout<<"你所答的题目总数为:"<<i+1<<"\t";
                cout<<"你目前答对的题目数:"<<points<<"\t";
                cout<<"\n"<<"你答题的正确率是:"<<setprecision(3)<<float(points)/float(i+1)*100<<"%"<<endl;
                rightsumnum=i+1;
                rightpercent=float(points)/float(i+1)*100;
                i = n;
            }
        }
        if(i==n-1)
        {
            cout<<"~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~"<<endl;
                cout<<"你已完成所有的题目!"<<endl;
                cout<<"你所答的题目总数为:"<<i+1<<"\t";
                cout<<"你目前答对的题目数:"<<points<<"\t";
                cout<<"你答题的正确率是:"<<setprecision(3)<<float(points)/float(i+1)*100<<"%"<<endl;
                    rightsumnum=i+1;
                rightpercent=float(points)/float(i+1)*100;

        }
         int location=n2*(sizeof(student)/100-7);
        fstream inoutfile("data.txt");
        inoutfile.seekp(location);
        inoutfile<<setw(16)<<rightusename<<"\n"<<setw(16)<<rightcode<<"\n"
                <<setw(4)<<rightsumnum<<"\n"<<setw(8)<<rightpercent<<"\n"<<"end"<<endl;
        inoutfile.close ();
 }

}
int seegrade()
{
string end;
system("cls");
int location=n2*(sizeof(student)/100-7);
ifstream infile("data.txt");
infile.seekg(location);
infile>>rightusename>>rightcode>>rightsumnum>>rightpercent>>end;
int yes=rightpercent/100*rightsumnum;
cout<<" \1\1\1\1 尊敬的"<<rightusename<<" \1\1\1\1"<<endl;
cout<<"****************************************************"<<endl;
cout<<"*您的上一次答题情况 "<<endl;
cout<<"
你所答题的个数为:"<<rightsumnum<<" "<<endl;
cout<<"
你答对的题目数为:"<<yes<<" "<<endl;
cout<<"
你答题的正确率为:"<<rightpercent<<"%"<<" "<<endl;
cout<<"
***************************************************"<<endl;
infile.close ();
return 1;
}

int init2()
{
system("cls");
cout<<"小学生四则运算考试模拟系统"<<endl;
cout<<" | "<<endl;
cout<<" | "<<endl;
cout<<" 1.开始答题| "<<endl;
cout<<" | "<<endl;
cout<<"------------+-------------"<<endl;
cout<<" | "<<endl;
cout<<" 2.查看分数| "<<endl;
cout<<" | "<<endl;
cout<<"------------+-------------"<<endl;
cout<<" | "<<endl;
cout<<" 3.退出 | "<<endl;
cout<<" | "<<endl;
cout<<" | "<<endl;
cout<<"请选择(输入数字):"<<endl;
return 1;
}

int main()

{
while(1)
{
int i;
init1();
i = getch();
if(i=='1') system("cls");
else if(i='2'){system("cls");Register();system("cls");cout<<"请重新登录!"<<endl; }
if(checkout())
{
while(1)
{
init2();
int i = getch();
fflush(stdin);//清除缓冲区
if(i == '1')
{
system("cls");
test();
system("pause");
}
else if(i == '2') {seegrade();system("pause");}
else if(i == '3') break;
}
system("cls");
cout<<"感谢您的使用!"<<endl;
system("pause");
}
system("cls");
}
return(0);

}
请各位大神帮一下忙,不胜感激!

  • 写回答

0条回答 默认 最新

    报告相同问题?

    悬赏问题

    • ¥30 这是哪个作者做的宝宝起名网站
    • ¥60 版本过低apk如何修改可以兼容新的安卓系统
    • ¥25 由IPR导致的DRIVER_POWER_STATE_FAILURE蓝屏
    • ¥50 有数据,怎么建立模型求影响全要素生产率的因素
    • ¥50 有数据,怎么用matlab求全要素生产率
    • ¥15 TI的insta-spin例程
    • ¥15 完成下列问题完成下列问题
    • ¥15 C#算法问题, 不知道怎么处理这个数据的转换
    • ¥15 YoloV5 第三方库的版本对照问题
    • ¥15 请完成下列相关问题!