1 #include <iostream> 2 #include <cstdio> 3 #include <cstdlib> 4 #include <cstring> 5 #include <cctype> 6 #include <ctime> 7 8 using namespace std; 9 10 template <class T> 11 class stack 12 { 13 private: 14 int sum=0; 15 struct node{ 16 T data;struct node *link; 17 }; 18 public: 19 struct node *ttop=NULL; 20 stack(){ttop=NULL;sum=0;} 21 ~stack() 22 { 23 struct node *temp; 24 while (ttop) 25 { 26 temp=ttop;ttop=ttop->link; 27 delete temp; 28 } 29 } 30 inline bool empty() 31 { 32 if (ttop)return false; 33 else return true; 34 } 35 bool push(const T item) 36 { 37 struct node *temp=new node; 38 if (temp){sum++;temp->data=item;temp->link=ttop; 39 ttop=temp;return true;} 40 else return false; 41 } 42 bool pop(void) 43 { 44 if (empty())return false; 45 else { 46 sum--; 47 struct node *temp; 48 temp=ttop; 49 ttop=ttop->link; 50 delete temp;return true; 51 } 52 } 53 T top(void) 54 { 55 if (sum>0) 56 return ttop->data; 57 else return 0x7fffffff; 58 } 59 int size(void){return sum;} 60 }; 61 62 class expression{ 63 private:int Numlen(char *s) 64 { 65 int i=0;int f=0; 66 while (s[i]=='.'||isdigit(s[i])){ 67 if (s[i]=='.')f++; 68 i++;} 69 if (f>1)return -1; 70 return i; 71 } 72 double Read(char *s) 73 { 74 double num; 75 sscanf(s,"%lf",&num); 76 return num; 77 } 78 public:char s[1000]; 79 double Calc(void) 80 { 81 double a,b,sum; 82 int l; 83 stack <double> Num; 84 stack <char> Op; 85 while (!Num.empty())Num.pop(); 86 while (!Op.empty())Op.pop(); 87 if (s[0]=='+'||s[0]=='-')Num.push(0); 88 int flag=0; 89 while (s[flag]!='\0') 90 { 91 switch(char t=s[flag]) 92 { 93 case '(':Op.push('(');flag++;break; 94 case ')': 95 sum=0; 96 while (Op.top()!='('){ 97 a=Num.top();Num.pop(); 98 char c=Op.top();Op.pop(); 99 if (c=='+')sum+=a; 100 else if (c=='-')sum-=a; 101 } 102 Op.pop(); 103 b=Num.top()+sum;Num.pop();Num.push(b); 104 flag++; 105 if (Op.top()=='*'||Op.top()=='/') 106 { 107 a=Num.top();Num.pop(); 108 if (Op.top()=='*'){b=Num.top()*a;Num.pop();Num.push(b);} 109 else {b=Num.top()/a;Num.pop();Num.push(b);} 110 Op.pop(); 111 } 112 break; 113 case '+':case '-': 114 flag++;Op.push(t); 115 break; 116 case '*':case '/':flag++; 117 if (s[flag]!='('){ 118 a=Read(s+flag); 119 int l=Numlen(s+flag);flag+=l; 120 if (t=='*'){b=Num.top()*a;Num.pop();Num.push(b);} 121 else if (t=='/'){b=Num.top()/a;Num.pop();Num.push(b);} 122 } 123 else Op.push(t); 124 break; 125 default:{ 126 a=Read(s+flag);Num.push(a); 127 l=Numlen(s+flag);flag+=l; 128 }break; 129 } 130 } 131 sum=0; 132 while (!Op.empty()) 133 { 134 if (Op.top()=='+')sum+=Num.top(); 135 else if (Op.top()=='-')sum-=Num.top(); 136 Num.pop();Op.pop(); 137 } 138 sum+=Num.top(); 139 return sum; 140 } 141 }; 142 143 class Main_code{ 144 private: 145 class expression s[100]; 146 int sum=0,sumt=0,sumc=0; 147 void cl(void) 148 { 149 system("pause"); 150 system("cls"); 151 } 152 void save(void) 153 { 154 system("cls"); 155 puts("正在保存记录"); 156 freopen("记录.txt","w",stdout); 157 printf("%d\n%d\n",sum,sumc); 158 fclose(stdout);freopen("con","w",stdout); 159 puts("保存成功"); 160 system("pause"); 161 } 162 163 void ex(void) 164 { 165 sum++; 166 int num=rand()%sumt; 167 puts(s[num].s); 168 double Ca=s[num].Calc(); 169 double Ya;scanf("%lf",&Ya); 170 if (Ya==Ca){sumc++;puts("Good job!答对了");} 171 else {puts("孩子,这个题你也能错?");printf("正确答案应该是%g!\n",Ca);} 172 } 173 174 void bl(void) 175 { 176 if (sum==0)puts("你还没有做过题!"); 177 else { 178 puts(" ------------------ "); 179 printf("|你一共做过%4d道题|\n|正确的共有%4d道题|\n|正确率应为 %5.2lf%%|\n",sum,sumc,(double)sumc/(double)sum); 180 puts(" ------------------ "); 181 } 182 183 } 184 public:bool Initt(void) 185 { 186 srand((unsigned)time( NULL)); 187 if(freopen("题库.txt","r",stdin)==NULL){puts("未找到题库!");system("pause");return 0; 188 } 189 printf("正在载入题库"); 190 while (gets(s[sumt].s)){if(s[sumt].s[0]!='\0')sumt++;putchar('.');} 191 putchar('\n'); 192 puts("正在载入记录"); 193 fclose(stdin); 194 if(freopen("记录.txt","r",stdin)!=NULL){ 195 if(scanf("%d%d",&sum,&sumc)==EOF){sum=0,sumc=0;puts("记录为空!");} 196 } 197 else puts("未找到记录"); 198 fclose(stdin);freopen("CON","r",stdin); 199 puts("载入完毕"); 200 system("pause");system("cls");return 1; 201 } 202 bool Menu(void) 203 { 204 puts(" 小学算术系统 "); 205 puts("************************"); 206 puts(" (1).题目练习 "); 207 puts(" (2).历史回顾 "); 208 puts(" (3).退出 "); 209 puts("************************"); 210 printf("请输入你的选择:(1~3):"); 211 int C;scanf("%d",&C); 212 switch (C) 213 { 214 case 1:{printf("请完成下面5道计算题:\n");for (int c=0;c<5;c++){ 215 puts("-------------------"); 216 printf("(%d).",c+1);ex();} 217 puts("-------------------"); 218 break; 219 } 220 case 2:{bl();break;} 221 case 3:{save();return 1;} 222 default:{puts("选择无效!"); 223 break;} 224 }cl();return 0; 225 } 226 }; 227 228 int main(void) 229 { 230 class Main_code One; 231 if(One.Initt()==0)return 0; 232 while (true) 233 { 234 if(One.Menu())break; 235 } 236 return 0; 237 }
浙公网安备 33010602011771号