zoukankan      html  css  js  c++  java
  • 四则运算4 WEB(结对开发)

    在第三次实验的基础上,teacher又对此提出了新的要求,实现网页版或安卓的四则运算。

    结对开发的伙伴:

    博客名:Mr.缪

    姓名:缪金敏

    链接:http://www.cnblogs.com/miaojinmin799/

    详细网页四则运算要求:

    1、生成的题目中计算过程不能产生负数,也就是说算术表达式中如果存在e1-e2的子表达式,那么结果大于等于0;

    2、生成的题目中如果存在形式如e1/e2的子表达式,那么其结果应该是真分数。

    3、每道题目中出现的运算符个数不超过3个,括号不做详细要求。

    问题分析:

    在第三次实验的基础上,要求把C++语言转换成java语言,即把cpp文件程序转换为java,jsp文件。但由于C和java在全局变量和栈的应用上有一定的不同,在对栈的使用中由于出栈和读取栈顶元素都为Object对象转换起来比较麻烦,所以我们在原先cpp文件中栈的应用改成了数组来实现压栈入栈。因为是网页版的所以就会使用jsp,html与java文件之间的数据交换,我们通过session,request和调用返回函数等方法来实现数据交互。

    所遇困难:

    1、在使用java中的栈时,出栈元素难以转换为所要类型。

    2、jsp之间传递数组返回nill.

    3、jsp与java之间的数据交互。

    解决办法:

    1、因为是顺序栈,所以直接使用数组来实现栈的所有功能

    2、使用session来传递数组

    3、在class文件中书写一个返回函数,在new生成对象时自动传递数组

    前cpp文件:

    复制代码
      1 #include<iostream>
      2 #include<string>
      3 #include<sstream>
      4 #include<time.h>
      5 #include<iomanip>
      6 #include<fstream>
      7 #define MAX 100
      8 using namespace std;
      9 
     10 stringstream formula;   //当前算式
     11 string buffer[MAX];     //缓冲区数组
     12 int TopNumber;          //上限
     13 int BaseNumber;         //下限
     14 int IsMulDlvExist;      //是否有乘除
     15 int Amount;             //操作数的个数
     16 int BracketNum;         //括号个数
     17 int LBraket[2];         //左括号的位置
     18 int RBraket[2];         //右括号的位置
     19 int IsNeg;              //是否有负数
     20 int IsRem;              //是否有余数
     21 int IsBra;              //是否有括号
     22 int IsRep;              //是否重复
     23 float Result[MAX];      //正确结果数组
     24 char lastOp;            //记录上个运算符是否为除号
     25 
     26 //优先级数组
     27 char prior[7][7] = {
     28     { '>', '>', '<', '<', '<', '>', '>' },
     29     { '>', '>', '<', '<', '<', '>', '>' },
     30     { '>', '>', '>', '>', '<', '>', '>' },
     31     { '>', '>', '>', '>', '<', '>', '>' },
     32     { '<', '<', '<', '<', '<', '=', ' ' },
     33     { '>', '>', '>', '>', ' ', '>', '>' },
     34     { '<', '<', '<', '<', '<', ' ', '=' }
     35 };
     36 //操作符栈
     37 typedef struct {
     38     char *base;
     39     char *top;
     40 }OperChar;
     41 //操作数栈
     42 typedef struct{
     43     float *base;
     44     float *top;
     45 }NumberLink;
     46 //初始化栈
     47 void InitOperStack(OperChar &S)
     48 {
     49     S.base = new char[MAX];
     50     if (!S.base)
     51         exit(1);
     52     S.top = S.base;
     53 }
     54 void InitNumStack(NumberLink &S)
     55 {
     56     S.base = new float[MAX];
     57     if (!S.base)
     58         exit(1);
     59     S.top = S.base;
     60 }
     61 //进栈
     62 void PushOper(OperChar &S,char e){
     63     if (S.top - S.base == MAX)
     64         exit(1);
     65     *S.top++ = e;
     66 
     67 }
     68 void PushNum(NumberLink &S,float e){
     69     if (S.top - S.base == MAX)
     70         exit(1);
     71     *S.top++ = e;
     72 }
     73 //出栈
     74 void PopOper(OperChar &S, char &e)
     75 {
     76     if (S.top == S.base)
     77         exit(1);
     78     e = *--S.top;
     79 }
     80 void PopNum(NumberLink &S, float &e)
     81 {
     82     if (S.top == S.base)
     83         exit(1);
     84     e = *--S.top;
     85 }
     86 //取栈顶元素
     87 char GetTopOper(OperChar S)
     88 {
     89     if (S.top == S.base)
     90     {
     91         exit(1);
     92 
     93     }
     94     return *(S.top - 1);
     95 }
     96 float GetTopNum(NumberLink S)
     97 {
     98     if (S.top == S.base)
     99     {
    100         exit(1);
    101 
    102     }
    103     return *(S.top - 1);
    104 }
    105 //将操作符转化为优先级数组的下标
    106 int Change(char Oper)
    107 {
    108     switch (Oper)
    109     {
    110         case '+': return 0; break;
    111         case '-': return 1; break;
    112         case '*': return 2; break;
    113         case '/': return 3; break;
    114         case '(': return 4; break;
    115         case ')': return 5; break;
    116         case '=': return 6; break;
    117         default:  return 6; break;
    118 
    119     }    
    120 }
    121 //返回优先级的大小
    122 char Precede(char Oper, char ch)
    123 {    
    124     return prior[Change(Oper)][Change(ch)];
    125 }
    126 //计算两个数的结果
    127 float Operate(float first,char oper1, float second)
    128 {
    129     switch (oper1)
    130     {
    131         case '+':
    132         {
    133             return (first + second);
    134             break; 
    135         }
    136         case '-': 
    137         {
    138             return (first - second); 
    139             break;
    140         }
    141         case '*':
    142         {
    143             return (first * second);
    144             break;
    145         } 
    146         case '/':
    147         {
    148                if (second == 0)   
    149             {
    150                 IsRep = 1;
    151                 return 0;
    152             }
    153             return (first / second);
    154             break;
    155             
    156         } 
    157         default:  return 0; break;
    158     }
    159 }
    160 //数字的个数
    161 void NumberAmount()
    162 {
    163     Amount = 2 + rand() % 5;
    164 }
    165 //加左括号   随机选择在第几个数字前面加括号
    166 void AddLbracket(){
    167     for (int j = 0; j < 2; j++)
    168         LBraket[j] = 0;
    169     if (Amount == 2)
    170     {
    171         BracketNum = 0;
    172     }
    173     if (Amount == 3){
    174         BracketNum = rand() % 2;
    175     }
    176     if (Amount > 3)
    177     {
    178         BracketNum = rand() % 3;
    179     }
    180     for (int i = 0; i < BracketNum; i++){
    181         LBraket[i] = 1 + rand() % (Amount-2);
    182     }
    183 }
    184 //加右括号
    185 void AddRbracket(){
    186     for (int j = 0; j < 2; j++)
    187         RBraket[j] = 0;
    188     int choose;
    189     int trance;
    190     if (BracketNum == 1){
    191         RBraket[0] = LBraket[0] + 1 + rand() % (Amount - LBraket[0]);
    192     }
    193         if (BracketNum == 2)
    194         
    195             {
    196             //把最左边的左括号放在第一个数组中
    197                 if (LBraket[0] < LBraket[1])
    198                 {
    199                     trance = LBraket[0];
    200                     LBraket[0] = LBraket[1];
    201                     LBraket[1] = trance;
    202                 }
    203                 //当两个左括号之间相差有点远时有2中右括号添加方法
    204                 if (LBraket[0] - LBraket[1]>2){
    205                     choose = rand() % 2;
    206                     if (choose == 0){
    207                         RBraket[0] = LBraket[0] + 1 + rand() % (Amount - LBraket[0]);
    208                         RBraket[1] = LBraket[0] + 1 + rand() % (Amount - LBraket[0]);
    209                     }
    210                     if (choose == 1)
    211                     {
    212                         RBraket[0] = LBraket[0] + 1 + rand() % (Amount - LBraket[0]);
    213                         RBraket[1] = LBraket[1] + 1 + rand() % (LBraket[0] - 2);
    214                     }
    215                 }
    216                 else
    217                 {
    218                     RBraket[0] = LBraket[0] + 1 + rand() % (Amount - LBraket[0]);
    219                     RBraket[1] = LBraket[0] + 1 + rand() % (Amount - LBraket[0]);
    220                     if (LBraket[0] == LBraket[1] && RBraket[0] == RBraket[1]){
    221                         LBraket[0] = LBraket[1] = 0;
    222                         RBraket[0] = RBraket[1] = 0;
    223                         BracketNum = 0;
    224 
    225                     }
    226                     if (LBraket[1] == 1 && (RBraket[0] == Amount || RBraket[1] == Amount))
    227                     {
    228                         LBraket[0] = LBraket[1] = 0;
    229                         RBraket[0] = RBraket[1] = 0;
    230                         BracketNum = 0;
    231                     }
    232                         
    233                 }
    234         }
    235 }
    236 //随机产生最简真分数
    237 void Score(){
    238     int Left, Right;
    239     Left = BaseNumber + rand() % (TopNumber - BaseNumber + 1);
    240     Right = BaseNumber + rand() % (TopNumber - BaseNumber + 1);
    241     while (Left >= Right || Left==0)
    242     {
    243         Left = BaseNumber + rand() % (TopNumber - BaseNumber + 1);
    244         Right = BaseNumber + rand() % (TopNumber - BaseNumber + 1);
    245     }
    246     int max=1;
    247     //求最大公约数
    248     for (int i = 2; i <= Left; i++)
    249     {
    250         if (Left%i == 0 && Right%i == 0)
    251         {
    252             max = i;
    253         }
    254     }
    255     if (max > 1)
    256     {
    257         Left /= max;
    258         Right /= max;
    259     }
    260     formula << '('<<Left << '/' << Right<<')';
    261 }
    262 //随机生成操作符
    263 void Operater()
    264 {
    265     int choose;
    266     char op;
    267     if (IsMulDlvExist == 1)
    268         choose = 1 + rand() % 4;
    269     else
    270         choose = 1 + rand() % 2;
    271 
    272     switch (choose)
    273     {
    274     case 1:{op = '+'; lastOp = '+'; break; }
    275     case 2:{op = '-';  lastOp = '-'; break; }
    276     case 3:{op = '*';  lastOp = '*'; break; }
    277     case 4:
    278     {
    279               //防止连续除法产生运算误区
    280               op = '/';
    281               if (lastOp == '/')
    282                   IsRep = 1;
    283               else
    284                   lastOp = '/';
    285               break;
    286     }
    287     }
    288     formula << op;
    289 }
    290 //随机生成整数
    291 void Integer(){
    292     int num;
    293     num = BaseNumber + rand() % (TopNumber - BaseNumber + 1);
    294     formula << num;
    295 }
    296 //创建算式
    297 void CreateNumber(){
    298     for (int k = 1; k <= Amount; k++)
    299     {
    300         
    301         for (int i = 0; i < 2; i++){
    302             if (LBraket[i] == k)
    303                 formula << '(';
    304         }
    305         
    306         int cho;
    307         cho = rand() % 2;
    308         if (cho == 0)
    309         {
    310             Integer();
    311         }
    312         else
    313             Score();
    314         for (int j = 0; j < 2; j++){
    315             if ((RBraket[j] == k) && RBraket[j] != 0)
    316                 formula << ')';
    317         }
    318         if (k == Amount)
    319             formula << '=';
    320         else
    321             Operater();
    322     }
    323 }
    324 //检查是否重复及判断括号是否添加正确
    325 int Repeat(int time){
    326     buffer[time] = formula.str();
    327     int juege=0;
    328     int trance;
    329     for (int i = 0; i < time; i++)
    330     {
    331         if (buffer[i] == buffer[time])
    332         {
    333             juege = 1;
    334             break;
    335         }
    336     }
    337     if (IsBra != 1)
    338     {
    339         if (BracketNum == 1)
    340         {
    341             if (LBraket[0] == 1 && RBraket[0] == Amount)
    342                 juege = 1;
    343         }
    344         if (BracketNum == 2)
    345         {
    346             if (RBraket[0] < RBraket[1])
    347             {
    348                 trance = RBraket[0];
    349                 RBraket[0] = RBraket[1];
    350                 RBraket[1] = trance;
    351             }
    352             if (LBraket[1] == 1 && RBraket[0] == Amount&&LBraket[0] < RBraket[1])
    353                 juege = 1;
    354         }
    355     }
    356     return juege;
    357 
    358 }
    359 //利用栈计算结果      参考《数据结构---C语言》
    360 float EvaluateExpression(){
    361     OperChar OPTR;
    362     NumberLink OPND;
    363     InitOperStack(OPTR);
    364     PushOper(OPTR, '=');
    365     InitNumStack(OPND);
    366     int count = 0;
    367     float Num = 0, first, second;
    368     char oper1;
    369     char bracket1;
    370     while (true)
    371     {
    372         Num = 0;
    373         //读取数字
    374         while (formula.str()[count] >= '0'&&formula.str()[count] <= '9')
    375         {
    376             if (formula.str()[count] == '0')
    377             {
    378                 if (count == 0)
    379                     PushNum(OPND, 0);
    380                 if (count != 0 && !(formula.str()[count - 1] >= '0'&&formula.str()[count - 1] <= '9'))
    381                     PushNum(OPND, 0);
    382             }
    383                 
    384             Num = Num * 10;
    385             Num = Num + formula.str()[count] - 48;
    386             count++;
    387             
    388         }
    389         if (Num > 0)
    390         {
    391             PushNum(OPND, Num);
    392         }
    393         if (formula.str()[count] == '='&&GetTopOper(OPTR) == '=')
    394         {
    395             break;
    396         }
    397         //判断运算符优先级
    398         switch (Precede(GetTopOper(OPTR), formula.str()[count]))
    399         {
    400             case '<':
    401             {
    402                     PushOper(OPTR, formula.str()[count]);
    403                     count++;
    404                     break;
    405             }
    406             case '>':
    407             {
    408                     PopOper(OPTR, oper1);
    409                     PopNum(OPND, second);
    410                     PopNum(OPND, first);
    411                     PushNum(OPND, Operate(first, oper1, second));
    412                     break;
    413             }
    414             case '=':
    415             {
    416                         PopOper(OPTR, bracket1);
    417                         count++;
    418             }
    419         }
    420     }
    421     return GetTopNum(OPND);
    422 }
    423 int main()
    424 {
    425     ofstream out("1.txt", ios::out);
    426     int OutChoose = 0;
    427     int truenum=0;
    428     int choose;
    429     bool flag=true;
    430     int range=0;
    431     srand((unsigned)time(NULL));
    432     /*cin >> IsMulDlvExist;
    433     cin >> BaseNumber;
    434     cin >> TopNumber;
    435     cin >> IsNeg;
    436     cin >> IsRem;
    437     cin >> IsBra;*/
    438     cout << "            欢迎来到四则运算答题系统!" << endl;    
    439     cout << "说明:
    	初级只有加减法无括号无负数无余数(默认数值范围0-5)
    	中级有乘除有括号无负数无余数(默认范围0-20)
    	高级有乘除有括号有负数有余数(默认范围0-100)" << endl;
    440     while (flag)
    441     {
    442         cout << "现在有初级,中级,高级,三种关卡,你要挑战哪一关?" << endl;
    443         cout << "1.初级  2.中级  3.高级     请选择:";
    444         cin >> choose;
    445         switch (choose)
    446         {
    447             case 1:
    448             {
    449                      cout << "是否打印试卷?0、否  1、是  请选择 : ";
    450                      cin >> OutChoose;
    451                      cout << "是否需要数值重设范围?0、否  1、是  请选择 : ";
    452                      cin >> range;
    453                      IsMulDlvExist=0;
    454                      BaseNumber=0;
    455                      TopNumber=5;
    456                      if (range == 1){
    457                          cout << "请输入下限(正数):";
    458                          cin >> BaseNumber;
    459                          cout << "请输入上限(正数):";
    460                          cin >> TopNumber;
    461                      }
    462                      IsNeg=1;
    463                      IsRem=1;
    464                      IsBra=1;
    465                      flag = false;
    466                      break;
    467             }
    468             case 2:
    469             { 
    470                     cout << "是否打印试卷?0、否  1、是  请选择 : ";
    471                     cin >> OutChoose;
    472                     cout << "是否需要数值重设范围?0、否  1、是  请选择 : ";
    473                     cin >> range;
    474                       IsMulDlvExist = 1;
    475                       BaseNumber = 0;
    476                       TopNumber = 20;
    477                       if (range == 1){
    478                           cout << "请输入下限(正数):";
    479                           cin >> BaseNumber;
    480                           cout << "请输入上限(正数):";
    481                           cin >> TopNumber;
    482                       }
    483                       IsNeg = 1;
    484                       IsRem = 1;
    485                       IsBra = 0;
    486                       flag = false;
    487                       break;
    488             }
    489             case 3:
    490             {
    491                       cout << "是否打印试卷?0、否  1、是  请选择 : ";
    492                       cin >> OutChoose;
    493                       cout << "是否需要数值重设范围?0、否  1、是  请选择 : ";
    494                       cin >> range;
    495                       IsMulDlvExist = 1;
    496                       BaseNumber = 0;
    497                       TopNumber = 100;
    498                       if (range == 1){
    499                           cout << "请输入下限(正数):";
    500                           cin >> BaseNumber;
    501                           cout << "请输入上限(正数):";
    502                           cin >> TopNumber;
    503                       }
    504                       IsNeg = 0;
    505                       IsRem = 0;
    506                       IsBra = 0;
    507                       flag = false;
    508                       break;
    509             }
    510             default:
    511             {        
    512                        cout << "输入有误,请重新选择:" << endl;
    513                        flag = true;
    514                        break; 
    515             }
    516         }
    517 
    518     }
    519     
    520 
    521     float sum=0;
    522     for (int i = 0; i < 30; i++)
    523     {
    524         lastOp = '+';
    525         IsRep = 0;
    526         NumberAmount();
    527         if (IsBra == 0)
    528         {
    529             AddLbracket();
    530             AddRbracket();
    531         }
    532         CreateNumber();
    533         Result[i] = EvaluateExpression();
    534         while (Repeat(i) == 1 || IsRep == 1 || (IsNeg == 1 && (Result[i] < 0 || Result[i] == -0)) || (IsRem == 1 && (int(Result[i]*10000))%10000!=0))
    535         {
    536 
    537             IsRep = 0;
    538             lastOp = '+';
    539             formula.str("");
    540             NumberAmount();
    541             if (IsBra == 0)
    542             {
    543                 AddLbracket();
    544                 AddRbracket();
    545             }
    546             CreateNumber();
    547             Result[i] = EvaluateExpression();
    548         }
    549         cout << "" << i + 1 << "题:" ;
    550         cout << formula.str();
    551         
    552         cin >> sum;
    553         if (OutChoose == 1)
    554         {
    555             out << "" << i + 1 << "题:";
    556             out << formula.str() << sum;
    557         }
    558         if (abs(int(100 * sum) -int(100 * Result[i]))<=1)
    559         {
    560             cout << "小朋友太棒了,回答正确!
    ";
    561             if (OutChoose==1)
    562                 out << "小朋友太棒了,回答正确!
    ";
    563             truenum++;
    564         }            
    565         else
    566         {
    567             cout << "回答错误,小朋友继续加油!正确答案:" << Result[i] << endl;
    568             if (OutChoose == 1)
    569                 out << "回答错误,小朋友继续加油!正确答案:" << Result[i] << endl;
    570         }
    571             
    572         formula.str("");
    573     }
    574     cout << endl;
    575     cout << "你一共答对了" << truenum << "道题!" << endl;
    576     if (OutChoose==1)
    577         out << "你一共答对了" << truenum << "道题!" << endl;
    578     return 0;
    579 }
    复制代码

    cpp转换后 MyHtml.html代码:

    复制代码
     1 <!DOCTYPE html>
     2 <html>
     3   <head>
     4     <title>首页</title>
     5     <meta http-equiv="Content-Type" content="text/html; charset=gb2312" />
     6     <meta name="keywords" content="keyword1,keyword2,keyword3">
     7     <meta name="description" content="this is my page">
     8    
     9     
    10     <!--<link rel="stylesheet" type="text/css" href="./styles.css">-->
    11 
    12   </head>
    13   
    14   <body background="images/A1.jpg">
    15   <h1 align="center">四则运算</h1>
    16   <!-- 传递参数的值给index.jsp处理 -->
    17   <form action="index.jsp" method="post">
    18  
    19   <!-- 互斥选择,值只能有一个 -->
    20   <center>
    21   <br>请选择上限(分数无上下限):
    22   <select name="TopNumber">
    23        <option value=20>20</option>
    24       <option value=30>30</option>
    25       <option value=50>50</option>
    26       <option value=80>80</option>
    27       <option value=100>100</option>                                              
    28   </select><br>
    29   <br>请选择下限(分数无上下限):
    30   <select name="BaseNumber">
    31       <option value=0>0</option>
    32       <option value=1>1</option>
    33       <option value=3>3</option>
    34       <option value=5>5</option>
    35       <option value=10>10</option>      
    36   </select><br>
    37   <!-- 多项选择,值可以有多个,在jsp中用数组保存 -->
    38   <br>请在以下选项中打勾:
    39   <input type="checkbox" name="function" value=1>有乘除
    40   <input type="checkbox" name="function" value=2>无余数
    41   <input type="checkbox" name="function" value=3>有括号
    42   <br>
    43   <br>
    44   
    45   <input type="submit" name="submit" value="确定">
    46   </center>
    47   </form>
    48   </body>
    49 </html>
    复制代码

    index.jsp文件代码:

    复制代码
     1 <%@ page language="java" contentType="text/html; charset=utf-8"
     2 
     3     pageEncoding="utf-8"%>
     4 <%@ page import="test5.*"%>
     5 <%
     6 String path = request.getContextPath();
     7 String basePath = request.getScheme()+"://"+request.getServerName()+":"+request.getServerPort()+path+"/";
     8 %>
     9 
    10 <!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN">
    11 <html>
    12   <head>
    13     <base href="<%=basePath%>">
    14     
    15     <title>答题</title>
    16     <meta http-equiv="Content-Type" content="text/html; charset=gb2312" />
    17     <meta http-equiv="pragma" content="no-cache">
    18     <meta http-equiv="cache-control" content="no-cache">
    19     <meta http-equiv="expires" content="0">    
    20     <meta http-equiv="keywords" content="keyword1,keyword2,keyword3">
    21     <meta http-equiv="description" content="This is my page">
    22     <!--
    23     <link rel="stylesheet" type="text/css" href="styles.css">
    24     -->
    25   </head>
    26   
    27   <body background="images/A2.jpg">
    28 
    29    <%
    30    String[] s=new String[30];
    31    float k[]=new float[30];
    32    Calculate a=new Calculate();
    33  
    34    request.setCharacterEncoding("UTF-8");
    35    String[] function = request.getParameterValues("function");
    36    String TopNumber,BaseNumber;
    37    TopNumber=request.getParameter("TopNumber");
    38    BaseNumber=request.getParameter("BaseNumber");
    39     a.SetValue(a.StrToNum(TopNumber),a.StrToNum(BaseNumber));
    40    if(function!=null){
    41    for(int i=0;i<function.length;i++){
    42        if(function[i].equals("1"))
    43        {
    44            a.SetValue1(1);
    45        }
    46        if(function[i].equals("2"))
    47        {
    48            a.SetValue2(1);
    49        }
    50        if(function[i].equals("3"))
    51        {
    52            a.SetValue3(0);
    53        }
    54        }
    55    }
    56        
    57   
    58     %>
    59    <form action="MyJsp.jsp" method="post">
    60    <center>
    61    <%
    62      a.main();
    63      float temp;
    64      String[] se=new String[30];
    65      for(int i=0;i<30;i++){
    66      s[i]=a.cs(i);
    67            se[i]="name";
    68            se[i]=se[i]+i;
    69      k[i]=a.css(i);
    70      out.print("<tr>"+s[i]+"</tr>");
    71      out.println("<br>");
    72     
    73      
    74    %>   
    75    <input type="text" name=<%=se[i] %>><br>
    76    <%
    77        } 
    78         session.setAttribute( "v1",s);
    79         session.setAttribute( "v2",k); 
    80     %>
    81     <br><input type="submit" name="submit" value="提交">
    82     
    83     <%
    84     
    85    
    86     %>
    87      </center>
    88   </form>
    89   
    90   
    91    
    92     
    93   </body>
    94 </html>
    复制代码

    MyJsp.jsp文件代码:

    复制代码
     1 <%@ page language="java" import="java.util.*" pageEncoding="gb2312" contentType="text/html; charset=gb2312"%>
     2 <%@ page import="test5.*"%>
     3 <%
     4 String path = request.getContextPath();
     5 String basePath = request.getScheme()+"://"+request.getServerName()+":"+request.getServerPort()+path+"/";
     6 %>
     7 
     8 <!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN">
     9 <html>
    10   <head>
    11     <base href="<%=basePath%>">
    12     
    13     <title>结果</title>
    14     
    15     <meta http-equiv="pragma" content="no-cache">
    16     <meta http-equiv="cache-control" content="no-cache">
    17     <meta http-equiv="expires" content="0">    
    18     <meta http-equiv="keywords" content="keyword1,keyword2,keyword3">
    19     <meta http-equiv="description" content="This is my page">
    20     <!--
    21     <link rel="stylesheet" type="text/css" href="styles.css">
    22     -->
    23 
    24   </head>
    25   
    26  <body background="images/A3.jpg">
    27   <% request.setCharacterEncoding("UTF-8");%>
    28   <center>
    29   <%
    30    
    31     String[] name=new String[30];
    32     String[] sum=new String[30];
    33     Calculate a=new Calculate();
    34     float sum1[]=new float[30];
    35     int right=0;
    36     String[] s= (String[])session.getAttribute("v1");
    37     float[] k=(float[])session.getAttribute("v2");
    38   
    39     for(int i=0;i<30;i++)
    40         sum1[i]=0;
    41     for(int i=0;i<30;i++){
    42         name[i]="name";
    43         name[i]=name[i]+i;
    44         
    45         sum[i]=request.getParameter(name[i]);
    46         //sum[i]=request.getParameterValues(name[i])";
    47         out.print(s[i]);
    48         out.print("<tr>"+sum[i]+"</tr>");
    49         if(sum[i]=="")
    50            out.print("未回答!");
    51         else{
    52             if(((int)(k[i]*100))%100!=0)
    53                sum1[i]=a.StrToFloat1(sum[i]);
    54             else
    55                sum1[i]=a.StrToFloat(sum[i]);
    56             if(Math.abs(sum1[i]*100-k[i]*100)<4){
    57                out.print(" 回答正确!");
    58                right++;
    59             }
    60             else
    61                out.print(" 回答错误,正确答案是: "+k[i]);
    62         }   
    63         
    64          out.println("<br>");
    65     }
    66     out.print("总共做对 "+right+" 题,正确率为:"+(right/30.0)*100+" %");
    67   %><br>
    68    <input type="button" value="返回首页" onClick="window.location.href='MyHtml.html'">
    69    <input type="button" value="关闭" onClick="window.close();">
    70     </center>
    71   </body>
    72 </html>
    复制代码

    Calculate.java文件代码:

    复制代码
      1 package test5;
      2 import java.util.*;
      3 public class Calculate {
      4         static int MAX=100;
      5         static String formula = "";         //当前算式
      6         static String[] buffer = new String[MAX];     //缓冲区数组
      7         static int TopNumber;          //上限
      8         static int BaseNumber;         //下限
      9         static int IsMulDlvExist;      //是否有乘除
     10         static int Amount;             //操作数的个数
     11         static int BracketNum;         //括号个数
     12         static int[] LBraket = new int[2];         //左括号的位置
     13         static int[] RBraket = new int[2];         //右括号的位置
     14         static int IsNeg;              //是否有负数
     15         static int IsRem;              //是否有余数
     16         static int IsBra;              //是否有括号
     17         static int IsRep;              //是否重复
     18         static float[] Result= new float[MAX];      //正确结果数组
     19         static char lastOp;            //记录上个运算符是否为除号
     20         
     21         //优先级数组
     22         static char[][] prior = {
     23             { '>', '>', '<', '<', '<', '>', '>' },
     24             { '>', '>', '<', '<', '<', '>', '>' },
     25             { '>', '>', '>', '>', '<', '>', '>' },
     26             { '>', '>', '>', '>', '<', '>', '>' },
     27             { '<', '<', '<', '<', '<', '=', ' ' },
     28             { '>', '>', '>', '>', ' ', '>', '>' },
     29             { '<', '<', '<', '<', '<', ' ', '=' }
     30         };
     31         
     32         //将操作符转化为优先级数组的下标
     33         static int Change(String Oper)
     34         {
     35             switch (Oper.charAt(0))
     36             {
     37                 case '+': return 0;
     38                 case '-': return 1; 
     39                 case '*': return 2; 
     40                 case '/': return 3; 
     41                 case '(': return 4; 
     42                 case ')': return 5;
     43                 case '=': return 6; 
     44                 default:  return 6; 
     45             }
     46         }
     47         static int Change(char Oper)
     48         {
     49             switch (Oper)
     50             {
     51                 case '+': return 0;
     52                 case '-': return 1; 
     53                 case '*': return 2; 
     54                 case '/': return 3; 
     55                 case '(': return 4; 
     56                 case ')': return 5;
     57                 case '=': return 6; 
     58                 default:  return 6; 
     59             }
     60         }
     61         static //返回优先级的大小
     62         char Precede(char Oper, char ch)
     63         {
     64             return prior[Change(Oper)][Change(ch)];
     65         }
     66         
     67         //计算两个数的结果
     68         static float Operate(float first, char oper1, float second)
     69         {
     70             switch (oper1)
     71             {
     72             case '+':
     73             {
     74                         return (first + second);
     75             }
     76             case '-':
     77             {
     78                 if (first-second<0)
     79                 {
     80                     IsRep = 1;
     81                     return 0;
     82                 }
     83                         return (first - second);                    
     84             }
     85             case '*':
     86             {
     87                         return (first * second);                    
     88             }
     89             case '/':
     90             {
     91                         if (second == 0)
     92                         {
     93                             IsRep = 1;
     94                             return 0;
     95                         }
     96                         return (first / second);
     97             }
     98             default:  return 0; 
     99             }
    100         }
    101         
    102         //数字的个数
    103         static void NumberAmount()
    104         {
    105             Amount = 2 + (int)((Math.random())*3);
    106         }
    107         
    108         //加左括号   随机选择在第几个数字前面加括号
    109         static void AddLbracket(){
    110             for (int j = 0; j < 2; j++)
    111                 LBraket[j] = 0;
    112             if (Amount == 2)
    113             {
    114                 BracketNum = 0;
    115             }
    116             if (Amount == 3){
    117                 BracketNum = (int)((Math.random())*2);
    118             }
    119             if (Amount > 3)
    120             {
    121                 BracketNum = (int)((Math.random())*3);
    122             }
    123             for (int i = 0; i < BracketNum; i++){
    124                 LBraket[i] = 1 + (int)((Math.random())*(Amount - 2));
    125             }
    126         }
    127         //加右括号
    128         static void AddRbracket(){
    129             for (int j = 0; j < 2; j++)
    130                 RBraket[j] = 0;
    131             int choose;
    132             int trance;
    133             if (BracketNum == 1){
    134                 RBraket[0] = LBraket[0] + 1 +(int)((Math.random())* (Amount - LBraket[0]));
    135             }
    136             if (BracketNum == 2)
    137 
    138             {
    139                 //把最左边的左括号放在第一个数组中
    140                 if (LBraket[0] < LBraket[1])
    141                 {
    142                     trance = LBraket[0];
    143                     LBraket[0] = LBraket[1];
    144                     LBraket[1] = trance;
    145                 }
    146                 //当两个左括号之间相差有点远时有2中右括号添加方法
    147                 if (LBraket[0] - LBraket[1]>2){
    148                     choose = (int)((Math.random())*2);
    149                     if (choose == 0){
    150                         RBraket[0] = LBraket[0] + 1 + (int)((Math.random())*(Amount - LBraket[0]));
    151                         RBraket[1] = LBraket[0] + 1 + (int)((Math.random())*(Amount - LBraket[0]));
    152                     }
    153                     if (choose == 1)
    154                     {
    155                         RBraket[0] = LBraket[0] + 1 + (int)((Math.random())*(Amount - LBraket[0]));
    156                         RBraket[1] = LBraket[1] + 1 + (int)((Math.random())*(LBraket[0] - 2));
    157                     }
    158                 }
    159                 else
    160                 {
    161                     RBraket[0] = LBraket[0] + 1 + (int)((Math.random())*(Amount - LBraket[0]));
    162                     RBraket[1] = LBraket[0] + 1 + (int)((Math.random())*(Amount - LBraket[0]));
    163                     if (LBraket[0] == LBraket[1] && RBraket[0] == RBraket[1]){
    164                         LBraket[0] = LBraket[1] = 0;
    165                         RBraket[0] = RBraket[1] = 0;
    166                         BracketNum = 0;
    167 
    168                     }
    169                     if (LBraket[1] == 1 && (RBraket[0] == Amount || RBraket[1] == Amount))
    170                     {
    171                         LBraket[0] = LBraket[1] = 0;
    172                         RBraket[0] = RBraket[1] = 0;
    173                         BracketNum = 0;
    174                     }
    175 
    176                 }
    177             }
    178         }
    179         
    180         //随机产生最简真分数
    181         static void Score(){
    182             int Left, Right;
    183             Left = BaseNumber + (int)((Math.random())*(TopNumber - BaseNumber + 1));
    184             Right = BaseNumber + (int)((Math.random())*(TopNumber - BaseNumber + 1));
    185             while (Left >= Right || Left == 0)
    186             {
    187                 Left = BaseNumber + (int)((Math.random())*(TopNumber - BaseNumber + 1));
    188                 Right = BaseNumber + (int)((Math.random())*(TopNumber - BaseNumber + 1));
    189             }
    190             int max = 1;
    191             //求最大公约数
    192             for (int i = 2; i <= Left; i++)
    193             {
    194                 if (Left%i == 0 && Right%i == 0)
    195                 {
    196                     max = i;
    197                 }
    198             }
    199             if (max > 1)
    200             {
    201                 Left /= max;
    202                 Right /= max;
    203             }
    204             formula += "(" + Left + "/" + Right + ")";
    205         }
    206         
    207         //随机生成操作符
    208         static void Operater()
    209         {
    210             int choose;
    211             char op='+';
    212             if (IsMulDlvExist == 1)
    213                 choose = 1 + (int)((Math.random())* 4);
    214             else
    215                 choose = 1 + (int)((Math.random())*2);
    216 
    217             switch (choose)
    218             {
    219                 case 1:{op = '+'; lastOp = '+'; break; }
    220                 case 2:{op = '-';  lastOp = '-'; break; }
    221                 case 3:{op = '*';  lastOp = '*'; break; }
    222                 case 4:
    223                 {
    224                           //防止连续除法产生运算误区
    225                           op = '/';
    226                           if (lastOp == '/')
    227                               IsRep = 1;
    228                           else
    229                               lastOp = '/';
    230                           break;
    231                 }
    232             }
    233             formula += op;
    234         }
    235         
    236         //随机生成整数
    237         static void Integer(){
    238             int num;
    239             num = BaseNumber + (int)((Math.random())*(TopNumber - BaseNumber + 1));
    240             formula += num;
    241         }
    242         //创建算式
    243         static void CreateNumber(){
    244             for (int k = 1; k <= Amount; k++)
    245             {
    246 
    247                 for (int i = 0; i < 2; i++){
    248                     if (LBraket[i] == k)
    249                         formula += "(";
    250                 }
    251 
    252                 int cho;
    253                 cho = (int)((Math.random())*2);
    254                 if (cho == 0)
    255                 {
    256                     Integer();
    257                 }
    258                 else
    259                     Score();
    260                 for (int j = 0; j < 2; j++){
    261                     if ((RBraket[j] == k) && RBraket[j] != 0)
    262                         formula += ")";
    263                 }
    264                 if (k == Amount)
    265                     formula += "=";
    266                 else
    267                     Operater();
    268             }
    269         }
    270         //检查是否重复及判断括号是否添加正确
    271         static int Repeat(int time){
    272             buffer[time] = formula;
    273             int juege = 0;
    274             int trance;
    275             for (int i = 0; i < time; i++)
    276             {
    277                 if (buffer[i] == buffer[time])
    278                 {
    279                     juege = 1;
    280                     break;
    281                 }
    282             }
    283             if (IsBra != 1)
    284             {
    285                 if (BracketNum == 1)
    286                 {
    287                     if (LBraket[0] == 1 && RBraket[0] == Amount)
    288                         juege = 1;
    289                 }
    290                 if (BracketNum == 2)
    291                 {
    292                     if (RBraket[0] < RBraket[1])
    293                     {
    294                         trance = RBraket[0];
    295                         RBraket[0] = RBraket[1];
    296                         RBraket[1] = trance;
    297                     }
    298                     if (LBraket[1] == 1 && RBraket[0] == Amount&&LBraket[0] < RBraket[1])
    299                         juege = 1;
    300                 }
    301             }
    302             return juege;
    303         }
    304         static float EvaluateExpression(){
    305             //Stack OPTR=new Stack();
    306             //Stack OPND=new Stack();
    307             float[] OPND=new float[MAX];
    308             char[] OPTR =new char[MAX];
    309             int Ntop=0;
    310             int Ctop=0;
    311             //InitOperStack(OPTR);
    312             //OPTR.push("=");
    313             OPTR[Ctop++]='=';
    
    314             //PushOper(OPTR, '=');
    315             //InitNumStack(OPND);
    316             int count = 0;
    317             float Num = 0, first, second;
    318             char oper1;
    319             char bracket1;
    320             while (true)
    321             {
    322                 Num = 0;
    323                 //读取数字
    324                 while (formula.charAt(count) >= '0'&&formula.charAt(count) <= '9')
    325                 {
    326                     if (formula.charAt(count) == '0')
    327                     {
    328                         if (count == 0)
    329                             //OPND.push(new Float(0));
    330                             OPND[Ntop++]=0;
    331                         if (count != 0 && !(formula.charAt(count-1) >= '0'&&formula.charAt(count-1) <= '9'))
    332                             //OPND.push(new Float(0));
    333                             OPND[Ntop++]=0;
    334                     }
    335                         
    336                     Num = Num * 10;
    337                     Num = Num + formula.charAt(count) - 48;
    338                     count++;
    339                     
    340                 }
    341                 if (Num > 0)
    342                 {
    343                     //OPND.push(new Float(Num));
    344                     //PushNum(OPND, Num);
    345                     OPND[Ntop++]=Num;
    346                 }
    347                 if (formula.charAt(count) == '='&&OPTR[Ctop-1] == '=')
    348                 {
    349                     break;
    350                 }
    351                 //判断运算符优先级
    352                 
    353                 switch (Precede(OPTR[Ctop-1], formula.charAt(count)))
    354                 {
    355                     case '<':
    356                     {
    357                         //OPTR.push(formula.charAt(count));
    358                         OPTR[Ctop++]=formula.charAt(count);
    359                             //PushOper(OPTR, formula.str()[count]);
    360                             count++;
    361                             break;
    362                     }
    363                     case '>':
    364                     {
    365                             //PopOper(OPTR, oper1);
    366                         //oper1=(char) OPTR.pop();
    367                         oper1=OPTR[--Ctop];
    368                             //PopNum(OPND, second);
    369                         //second=(float) OPND.pop();
    370                         second=OPND[--Ntop];
    371                             //PopNum(OPND, first);
    372                         //first=(float)OPND.pop();
    373                         first=OPND[--Ntop];
    374                             //PushNum(OPND, Operate(first, oper1, second));
    375                         //OPND.push(Operate(first, oper1, second));
    376                         OPND[Ntop++]=Operate(first, oper1, second);
    377                             break;
    378                     }
    379                     case '=':
    380                     {
    381                                 //PopOper(OPTR, bracket1);
    382                         //bracket1=(char) OPTR.pop();
    383                         bracket1=OPTR[--Ctop];
    384                                 count++;
    385                     }
    386                 }
    387             }
    388             return OPND[Ntop-1];
    389         }
    390         
    391         public static void SetValue(int IsMulDlvExist1,int IsRem1,int IsBra1,int TopNumber1,int BaseNumber1)
    392         {
    393             IsMulDlvExist=0;
    394             BaseNumber=0;
    395             TopNumber=10;
    396             IsNeg=0;
    397             IsRem=0;
    398             IsBra=1;
    399             
    400             
    401             
    402             IsMulDlvExist=IsMulDlvExist1;
    403             TopNumber=TopNumber1;
    404             BaseNumber=BaseNumber1;
    405             IsRem=IsRem1;
    406             IsBra=IsBra1;
    407         }
    408         public static void SetValue1(int IMDE)
    409         {
    410             IsMulDlvExist=IMDE;
    411         }
    412         public static void SetValue2(int Rem)
    413         {
    414             IsRem=Rem;
    415         }
    416         public static void SetValue3(int Bra)
    417         {
    418             IsBra=Bra;
    419         }
    420         public static void SetValue(int TopNumber1,int BaseNumber1){
    421             IsMulDlvExist=0;
    422             BaseNumber=0;
    423             TopNumber=10;
    424             IsNeg=0;
    425             IsRem=0;
    426             IsBra=1;
    427             TopNumber=TopNumber1;
    428             BaseNumber=BaseNumber1;
    429             
    430         }
    431         
    432         public static int StrToNum(String x)
    433         {
    434               int num=0;
    435                for(int i=0;i<x.length();i++)
    436                {
    437                        num=num*10+x.charAt(i)-48;
    438                }
    439                return num;
    440         }
    441         public static float StrToFloat(String x)
    442         {
    443             float num=0;
    444             int doat=0;
    445             int mi=-1;
    446             for(int i=0;i<x.length();i++)
    447             {
    448                 if(x.charAt(i)=='.')
    449                 {
    450                     doat=1;
    451                     continue;
    452                 }
    453                 if(doat==1)
    454                 {
    455                     num=(float) (num+(x.charAt(i)-48)*Math.pow(10,mi));
    456                     mi--;
    457                 }else{
    458                     num=num*10+x.charAt(i)-48;
    459                 }
    460             }
    461             return num;
    462         
    463         }
    464         public static float StrToFloat1(String x)
    465         {
    466             float first=0;
    467             float second=0;
    468             int point=0;
    469             for(int i=0;i<x.length();i++)
    470             {
    471                 if(x.charAt(i)=='/')
    472                 {
    473                     point=1;
    474                     continue;
    475                 }
    476                 if(point==1)
    477                 {
    478                     second=second*10+x.charAt(i)-48;
    479                 }else
    480                 {
    481                     first=first*10+x.charAt(i)-48;
    482                 }
    483             }
    484             return (first/second);
    485         }
    486         static int ma()
    487         {
    488             int OutChoose = 0;
    489             int truenum = 0;
    490             int choose;
    491             boolean flag = true;                       
    492             int range = 0;
    493             
    494             //IsMulDlvExist=1;
    495             //BaseNumber=0;
    496             //TopNumber=10;
    497             //IsNeg=0;
    498             //IsRem=0;
    499             //IsBra=0;
    500             
    501             
    502             /*cout << "            欢迎来到四则运算答题系统!" << endl;
    503             cout << "说明:
    	初级只有加减法无括号无负数无余数(默认数值范围0-5)
    	中级有乘除有括号无负数无余数(默认范围0-20)
    	高级有乘除有括号有负数有余数(默认范围0-100)" << endl;
    504             while (flag)
    505             {
    506                 cout << "现在有初级,中级,高级,三种关卡,你要挑战哪一关?" << endl;
    507                 cout << "1.初级  2.中级  3.高级     请选择:";
    508                 cin >> choose;
    509                 switch (choose)
    510                 {
    511                 case 1:
    512                 {
    513                           cout << "是否打印试卷?0、否  1、是  请选择 : ";
    514                           cin >> OutChoose;
    515                           cout << "是否需要数值重设范围?0、否  1、是  请选择 : ";
    516                           cin >> range;
    517                           IsMulDlvExist = 0;
    518                           BaseNumber = 0;
    519                           TopNumber = 5;
    520                           if (range == 1){
    521                               cout << "请输入下限(正数):";
    522                               cin >> BaseNumber;
    523                               cout << "请输入上限(正数):";
    524                               cin >> TopNumber;
    525                           }
    526                           IsNeg = 1;
    527                           IsRem = 1;
    528                           IsBra = 1;
    529                           flag = false;
    530                           break;
    531                 }
    532                 case 2:
    533                 {
    534                           cout << "是否打印试卷?0、否  1、是  请选择 : ";
    535                           cin >> OutChoose;
    536                           cout << "是否需要数值重设范围?0、否  1、是  请选择 : ";
    537                           cin >> range;
    538                           IsMulDlvExist = 1;
    539                           BaseNumber = 0;
    540                           TopNumber = 20;
    541                           if (range == 1){
    542                               cout << "请输入下限(正数):";
    543                               cin >> BaseNumber;
    544                               cout << "请输入上限(正数):";
    545                               cin >> TopNumber;
    546                           }
    547                           IsNeg = 1;
    548                           IsRem = 1;
    549                           IsBra = 0;
    550                           flag = false;
    551                           break;
    552                 }
    553                 case 3:
    554                 {
    555                           cout << "是否打印试卷?0、否  1、是  请选择 : ";
    556                           cin >> OutChoose;
    557                           cout << "是否需要数值重设范围?0、否  1、是  请选择 : ";
    558                           cin >> range;
    559                           IsMulDlvExist = 1;
    560                           BaseNumber = 0;
    561                           TopNumber = 100;
    562                           if (range == 1){
    563                               cout << "请输入下限(正数):";
    564                               cin >> BaseNumber;
    565                               cout << "请输入上限(正数):";
    566                               cin >> TopNumber;
    567                           }
    568                           IsNeg = 0;
    569                           IsRem = 0;
    570                           IsBra = 0;
    571                           flag = false;
    572                           break;
    573                 }
    574                 default:
    575                 {
    576                            cout << "输入有误,请重新选择:" << endl;
    577                            flag = true;
    578                            break;
    579                 }
    580                 }
    581 
    582             }
    583             */
    584 
    585 
    586             //float sum = 0;
    587             for (int i = 0; i < 30; i++)
    588             {
    589                 lastOp = '+';
    590                 IsRep = 0;
    591                 NumberAmount();
    592                 if (IsBra == 0)
    593                 {
    594                     AddLbracket();
    595                     AddRbracket();
    596                 }
    597                 CreateNumber();
    598                 Result[i] = EvaluateExpression();                    
    599                 while (Repeat(i) == 1 || IsRep == 1 || (IsNeg == 1 && (Result[i] < 0 || Result[i] == -0)) || (IsRem == 1 && ((int)(Result[i]*10000))%10000!=0))
    600                 {
    601 
    602                     IsRep = 0;
    603                     lastOp = '+';
    604                     formula="";
    605                     NumberAmount();
    606                     if (IsBra == 0)
    607                     {
    608                         AddLbracket();
    609                         AddRbracket();
    610                     }
    611                     CreateNumber();
    612                     Result[i] = EvaluateExpression();                    
    613                 }
    614                 buffer[i]=formula;
    615                 formula="";
    616             }
    617 
    618             return 0;
    619         }
    620         public static float css(int number){
    621             return Result[number];
    622         }
    623         public static String cs(int number){
    624             return buffer[number];
    625         }
    626     public static void main()
    627     {
    628         int a;
    629         a=ma();
    630         for (int i = 0; i < 30; i++)
    631         {
    632             System.out.println(buffer[i]+Result[i]);
    633         }
    634         
    635     }
    636 }
    复制代码

    更改后:

      1 package test4;
      2 import java.util.*;
      3 public class Calculate {
      4         static int MAX=100;
      5         static String formula = "";         //当前算式
      6         static String[] buffer = new String[MAX];     //缓冲区数组
      7         static int TopNumber;          //上限
      8         static int BaseNumber;         //下限
      9         static int IsMulDlvExist;      //是否有乘除
     10         static int Amount;             //操作数的个数
     11         static int BracketNum;         //括号个数
     12         static int[] LBraket = new int[2];         //左括号的位置
     13         static int[] RBraket = new int[2];         //右括号的位置
     14         static int IsNeg;              //是否有负数
     15         static int IsRem;              //是否有余数
     16         static int IsBra;              //是否有括号
     17         static int IsRep;              //是否重复
     18         static float[] Result= new float[MAX];      //正确结果数组
     19         static char lastOp;            //记录上个运算符是否为除号
     20         
     21         //优先级数组
     22         static char[][] prior = {
     23             { '>', '>', '<', '<', '<', '>', '>' },
     24             { '>', '>', '<', '<', '<', '>', '>' },
     25             { '>', '>', '>', '>', '<', '>', '>' },
     26             { '>', '>', '>', '>', '<', '>', '>' },
     27             { '<', '<', '<', '<', '<', '=', ' ' },
     28             { '>', '>', '>', '>', ' ', '>', '>' },
     29             { '<', '<', '<', '<', '<', ' ', '=' }
     30         };
     31         
     32         //将操作符转化为优先级数组的下标
     33         static int Change(String Oper)
     34         {
     35             switch (Oper.charAt(0))
     36             {
     37                 case '+': return 0;
     38                 case '-': return 1; 
     39                 case '*': return 2; 
     40                 case '/': return 3; 
     41                 case '(': return 4; 
     42                 case ')': return 5;
     43                 case '=': return 6; 
     44                 default:  return 6; 
     45             }
     46         }
     47         static int Change(char Oper)
     48         {
     49             switch (Oper)
     50             {
     51                 case '+': return 0;
     52                 case '-': return 1; 
     53                 case '*': return 2; 
     54                 case '/': return 3; 
     55                 case '(': return 4; 
     56                 case ')': return 5;
     57                 case '=': return 6; 
     58                 default:  return 6; 
     59             }
     60         }
     61         static //返回优先级的大小
     62         char Precede(char Oper, char ch)
     63         {
     64             return prior[Change(Oper)][Change(ch)];
     65         }
     66         
     67         //计算两个数的结果
     68         static float Operate(float first, char oper1, float second)
     69         {
     70             switch (oper1)
     71             {
     72             case '+':
     73             {
     74                         return (first + second);
     75             }
     76             case '-':
     77             {
     78                 if (first-second<0)
     79                 {
     80                     IsRep = 1;
     81                     return 0;
     82                 }
     83                         return (first - second);                    
     84             }
     85             case '*':
     86             {
     87                         return (first * second);                    
     88             }
     89             case '/':
     90             {
     91                         if (second == 0)
     92                         {
     93                             IsRep = 1;
     94                             return 0;
     95                         }
     96                         return (first / second);
     97             }
     98             default:  return 0; 
     99             }
    100         }
    101         
    102         //数字的个数
    103         static void NumberAmount()
    104         {
    105             Amount = 2 + (int)((Math.random())*3);
    106         }
    107         
    108         //加左括号   随机选择在第几个数字前面加括号
    109         static void AddLbracket(){
    110             for (int j = 0; j < 2; j++)
    111                 LBraket[j] = 0;
    112             if (Amount == 2)
    113             {
    114                 BracketNum = 0;
    115             }
    116             if (Amount == 3){
    117                 BracketNum = (int)((Math.random())*2);
    118             }
    119             if (Amount > 3)
    120             {
    121                 BracketNum = (int)((Math.random())*3);
    122             }
    123             for (int i = 0; i < BracketNum; i++){
    124                 LBraket[i] = 1 + (int)((Math.random())*(Amount - 2));
    125             }
    126         }
    127         //加右括号
    128         static void AddRbracket(){
    129             for (int j = 0; j < 2; j++)
    130                 RBraket[j] = 0;
    131             int choose;
    132             int trance;
    133             if (BracketNum == 1){
    134                 RBraket[0] = LBraket[0] + 1 +(int)((Math.random())* (Amount - LBraket[0]));
    135             }
    136             if (BracketNum == 2)
    137 
    138             {
    139                 //把最左边的左括号放在第一个数组中
    140                 if (LBraket[0] < LBraket[1])
    141                 {
    142                     trance = LBraket[0];
    143                     LBraket[0] = LBraket[1];
    144                     LBraket[1] = trance;
    145                 }
    146                 //当两个左括号之间相差有点远时有2中右括号添加方法
    147                 if (LBraket[0] - LBraket[1]>2){
    148                     choose = (int)((Math.random())*2);
    149                     if (choose == 0){
    150                         RBraket[0] = LBraket[0] + 1 + (int)((Math.random())*(Amount - LBraket[0]));
    151                         RBraket[1] = LBraket[0] + 1 + (int)((Math.random())*(Amount - LBraket[0]));
    152                     }
    153                     if (choose == 1)
    154                     {
    155                         RBraket[0] = LBraket[0] + 1 + (int)((Math.random())*(Amount - LBraket[0]));
    156                         RBraket[1] = LBraket[1] + 1 + (int)((Math.random())*(LBraket[0] - 2));
    157                     }
    158                 }
    159                 else
    160                 {
    161                     RBraket[0] = LBraket[0] + 1 + (int)((Math.random())*(Amount - LBraket[0]));
    162                     RBraket[1] = LBraket[0] + 1 + (int)((Math.random())*(Amount - LBraket[0]));
    163                     if (LBraket[0] == LBraket[1] && RBraket[0] == RBraket[1]){
    164                         LBraket[0] = LBraket[1] = 0;
    165                         RBraket[0] = RBraket[1] = 0;
    166                         BracketNum = 0;
    167 
    168                     }
    169                     if (LBraket[1] == 1 && (RBraket[0] == Amount || RBraket[1] == Amount))
    170                     {
    171                         LBraket[0] = LBraket[1] = 0;
    172                         RBraket[0] = RBraket[1] = 0;
    173                         BracketNum = 0;
    174                     }
    175 
    176                 }
    177             }
    178         }
    179         
    180         //随机产生最简真分数
    181         static void Score(){
    182             int Left, Right;
    183             Left = BaseNumber + (int)((Math.random())*(TopNumber - BaseNumber + 1));
    184             Right = BaseNumber + (int)((Math.random())*(TopNumber - BaseNumber + 1));
    185             while (Left >= Right || Left == 0)
    186             {
    187                 Left = BaseNumber + (int)((Math.random())*(TopNumber - BaseNumber + 1));
    188                 Right = BaseNumber + (int)((Math.random())*(TopNumber - BaseNumber + 1));
    189             }
    190             int max = 1;
    191             //求最大公约数
    192             for (int i = 2; i <= Left; i++)
    193             {
    194                 if (Left%i == 0 && Right%i == 0)
    195                 {
    196                     max = i;
    197                 }
    198             }
    199             if (max > 1)
    200             {
    201                 Left /= max;
    202                 Right /= max;
    203             }
    204             formula += "(" + Left + "/" + Right + ")";
    205         }
    206         
    207         //随机生成操作符
    208         static void Operater()
    209         {
    210             int choose;
    211             char op='+';
    212             if (IsMulDlvExist == 1)
    213                 choose = 1 + (int)((Math.random())* 4);
    214             else
    215                 choose = 1 + (int)((Math.random())*2);
    216 
    217             switch (choose)
    218             {
    219                 case 1:{op = '+'; lastOp = '+'; break; }
    220                 case 2:{op = '-';  lastOp = '-'; break; }
    221                 case 3:{op = '*';  lastOp = '*'; break; }
    222                 case 4:
    223                 {
    224                           //防止连续除法产生运算误区
    225                           op = '/';
    226                           if (lastOp == '/')
    227                               IsRep = 1;
    228                           else
    229                               lastOp = '/';
    230                           break;
    231                 }
    232             }
    233             formula += op;
    234         }
    235         
    236         //随机生成整数
    237         static void Integer(){
    238             int num;
    239             num = BaseNumber + (int)((Math.random())*(TopNumber - BaseNumber + 1));
    240             formula += num;
    241         }
    242         //创建算式
    243         static void CreateNumber(){
    244             for (int k = 1; k <= Amount; k++)
    245             {
    246 
    247                 for (int i = 0; i < 2; i++){
    248                     if (LBraket[i] == k)
    249                         formula += "(";
    250                 }
    251 
    252                 int cho;
    253                 cho = (int)((Math.random())*2);
    254                 if (cho == 0)
    255                 {
    256                     Integer();
    257                 }
    258                 else
    259                     Score();
    260                 for (int j = 0; j < 2; j++){
    261                     if ((RBraket[j] == k) && RBraket[j] != 0)
    262                         formula += ")";
    263                 }
    264                 if (k == Amount)
    265                     formula += "=";
    266                 else
    267                     Operater();
    268             }
    269         }
    270         //检查是否重复及判断括号是否添加正确
    271         static int Repeat(int time){
    272             buffer[time] = formula;
    273             int juege = 0;
    274             int trance;
    275             for (int i = 0; i < time; i++)
    276             {
    277                 if (buffer[i] == buffer[time])
    278                 {
    279                     juege = 1;
    280                     break;
    281                 }
    282             }
    283             if (IsBra != 1)
    284             {
    285                 if (BracketNum == 1)
    286                 {
    287                     if (LBraket[0] == 1 && RBraket[0] == Amount)
    288                         juege = 1;
    289                 }
    290                 if (BracketNum == 2)
    291                 {
    292                     if (RBraket[0] < RBraket[1])
    293                     {
    294                         trance = RBraket[0];
    295                         RBraket[0] = RBraket[1];
    296                         RBraket[1] = trance;
    297                     }
    298                     if (LBraket[1] == 1 && RBraket[0] == Amount&&LBraket[0] < RBraket[1])
    299                         juege = 1;
    300                 }
    301             }
    302             return juege;
    303         }
    304         static float EvaluateExpression(){
    305             //Stack OPTR=new Stack();
    306             //Stack OPND=new Stack();
    307             float[] OPND=new float[MAX];
    308             char[] OPTR =new char[MAX];
    309             int Ntop=0;
    310             int Ctop=0;
    311             //InitOperStack(OPTR);
    312             //OPTR.push("=");
    313             OPTR[Ctop++]='=';
    314             //PushOper(OPTR, '=');
    315             //InitNumStack(OPND);
    316             int count = 0;
    317             float Num = 0, first, second;
    318             char oper1;
    319             char bracket1;
    320             while (true)
    321             {
    322                 Num = 0;
    323                 //读取数字
    324                 while (formula.charAt(count) >= '0'&&formula.charAt(count) <= '9')
    325                 {
    326                     if (formula.charAt(count) == '0')
    327                     {
    328                         if (count == 0)
    329                             //OPND.push(new Float(0));
    330                             OPND[Ntop++]=0;
    331                         if (count != 0 && !(formula.charAt(count-1) >= '0'&&formula.charAt(count-1) <= '9'))
    332                             //OPND.push(new Float(0));
    333                             OPND[Ntop++]=0;
    334                     }
    335                         
    336                     Num = Num * 10;
    337                     Num = Num + formula.charAt(count) - 48;
    338                     count++;
    339                     
    340                 }
    341                 if (Num > 0)
    342                 {
    343                     //OPND.push(new Float(Num));
    344                     //PushNum(OPND, Num);
    345                     OPND[Ntop++]=Num;
    346                 }
    347                 if (formula.charAt(count) == '='&&OPTR[Ctop-1] == '=')
    348                 {
    349                     break;
    350                 }
    351                 //判断运算符优先级
    352                 
    353                 switch (Precede(OPTR[Ctop-1], formula.charAt(count)))
    354                 {
    355                     case '<':
    356                     {
    357                         //OPTR.push(formula.charAt(count));
    358                         OPTR[Ctop++]=formula.charAt(count);
    359                             //PushOper(OPTR, formula.str()[count]);
    360                             count++;
    361                             break;
    362                     }
    363                     case '>':
    364                     {
    365                             //PopOper(OPTR, oper1);
    366                         //oper1=(char) OPTR.pop();
    367                         oper1=OPTR[--Ctop];
    368                             //PopNum(OPND, second);
    369                         //second=(float) OPND.pop();
    370                         second=OPND[--Ntop];
    371                             //PopNum(OPND, first);
    372                         //first=(float)OPND.pop();
    373                         first=OPND[--Ntop];
    374                             //PushNum(OPND, Operate(first, oper1, second));
    375                         //OPND.push(Operate(first, oper1, second));
    376                         OPND[Ntop++]=Operate(first, oper1, second);
    377                             break;
    378                     }
    379                     case '=':
    380                     {
    381                                 //PopOper(OPTR, bracket1);
    382                         //bracket1=(char) OPTR.pop();
    383                         bracket1=OPTR[--Ctop];
    384                                 count++;
    385                     }
    386                 }
    387             }
    388             return OPND[Ntop-1];
    389         }
    390         
    391         public static void SetValue(int IsMulDlvExist1,int IsRem1,int IsBra1,int TopNumber1,int BaseNumber1)
    392         {
    393             IsMulDlvExist=0;
    394             BaseNumber=0;
    395             TopNumber=10;
    396             IsNeg=0;
    397             IsRem=0;
    398             IsBra=1;
    399             
    400             
    401             
    402             IsMulDlvExist=IsMulDlvExist1;
    403             TopNumber=TopNumber1;
    404             BaseNumber=BaseNumber1;
    405             IsRem=IsRem1;
    406             IsBra=IsBra1;
    407         }
    408         public static void SetValue1(int IMDE)
    409         {
    410             IsMulDlvExist=IMDE;
    411         }
    412         public static void SetValue2(int Rem)
    413         {
    414             IsRem=Rem;
    415         }
    416         public static void SetValue3(int Bra)
    417         {
    418             IsBra=Bra;
    419         }
    420         public static void SetValue(int TopNumber1,int BaseNumber1){
    421             IsMulDlvExist=0;
    422             BaseNumber=0;
    423             TopNumber=10;
    424             IsNeg=0;
    425             IsRem=0;
    426             IsBra=1;
    427             TopNumber=TopNumber1;
    428             BaseNumber=BaseNumber1;
    429             
    430         }
    431         
    432         public static int StrToNum(String x)
    433         {
    434               int num=0;
    435                for(int i=0;i<x.length();i++)
    436                {
    437                        num=num*10+x.charAt(i)-48;
    438                }
    439                return num;
    440         }
    441         public static float StrToFloat(String x)
    442         {
    443             float num=0;
    444             int doat=0;
    445             int mi=-1;
    446             for(int i=0;i<x.length();i++)
    447             {
    448                 if(x.charAt(i)=='.')
    449                 {
    450                     doat=1;
    451                     continue;
    452                 }
    453                 if(doat==1)
    454                 {
    455                     num=(float) (num+(x.charAt(i)-48)*Math.pow(10,mi));
    456                     mi--;
    457                 }else{
    458                     num=num*10+x.charAt(i)-48;
    459                 }
    460             }
    461             return num;
    462         
    463         }
    464         public static float StrToFloat1(String x)
    465         {
    466             float a=0;
    467             float first=0;
    468             float second=0;
    469             int point=0;
    470             int has=0;
    471             for(int i=0;i<x.length();i++)
    472             {
    473                 if(x.charAt(i)=='/')
    474                 {
    475                     has=1;
    476                 }
    477             }
    478             if(has==1)
    479             {
    480             for(int i=0;i<x.length();i++)
    481             {
    482                 if(x.charAt(i)=='/')
    483                 {
    484                     point=1;
    485                     continue;
    486                 }
    487                 if(point==1)
    488                 {
    489                     second=second*10+x.charAt(i)-48;
    490                 }else
    491                 {
    492                     first=first*10+x.charAt(i)-48;
    493                 }
    494             }
    495             return (first/second);
    496             }
    497             else
    498                 {
    499                 a=StrToFloat(x);
    500                 return a;
    501                 }
    502         }
    503         static int ma()
    504         {
    505             int OutChoose = 0;
    506             int truenum = 0;
    507             int choose;
    508             boolean flag = true;                       
    509             int range = 0;
    510             
    511             //IsMulDlvExist=1;
    512             //BaseNumber=0;
    513             //TopNumber=10;
    514             //IsNeg=0;
    515             //IsRem=0;
    516             //IsBra=0;
    517             
    518             
    519             /*cout << "            欢迎来到四则运算答题系统!" << endl;
    520             cout << "说明:
    	初级只有加减法无括号无负数无余数(默认数值范围0-5)
    	中级有乘除有括号无负数无余数(默认范围0-20)
    	高级有乘除有括号有负数有余数(默认范围0-100)" << endl;
    521             while (flag)
    522             {
    523                 cout << "现在有初级,中级,高级,三种关卡,你要挑战哪一关?" << endl;
    524                 cout << "1.初级  2.中级  3.高级     请选择:";
    525                 cin >> choose;
    526                 switch (choose)
    527                 {
    528                 case 1:
    529                 {
    530                           cout << "是否打印试卷?0、否  1、是  请选择 : ";
    531                           cin >> OutChoose;
    532                           cout << "是否需要数值重设范围?0、否  1、是  请选择 : ";
    533                           cin >> range;
    534                           IsMulDlvExist = 0;
    535                           BaseNumber = 0;
    536                           TopNumber = 5;
    537                           if (range == 1){
    538                               cout << "请输入下限(正数):";
    539                               cin >> BaseNumber;
    540                               cout << "请输入上限(正数):";
    541                               cin >> TopNumber;
    542                           }
    543                           IsNeg = 1;
    544                           IsRem = 1;
    545                           IsBra = 1;
    546                           flag = false;
    547                           break;
    548                 }
    549                 case 2:
    550                 {
    551                           cout << "是否打印试卷?0、否  1、是  请选择 : ";
    552                           cin >> OutChoose;
    553                           cout << "是否需要数值重设范围?0、否  1、是  请选择 : ";
    554                           cin >> range;
    555                           IsMulDlvExist = 1;
    556                           BaseNumber = 0;
    557                           TopNumber = 20;
    558                           if (range == 1){
    559                               cout << "请输入下限(正数):";
    560                               cin >> BaseNumber;
    561                               cout << "请输入上限(正数):";
    562                               cin >> TopNumber;
    563                           }
    564                           IsNeg = 1;
    565                           IsRem = 1;
    566                           IsBra = 0;
    567                           flag = false;
    568                           break;
    569                 }
    570                 case 3:
    571                 {
    572                           cout << "是否打印试卷?0、否  1、是  请选择 : ";
    573                           cin >> OutChoose;
    574                           cout << "是否需要数值重设范围?0、否  1、是  请选择 : ";
    575                           cin >> range;
    576                           IsMulDlvExist = 1;
    577                           BaseNumber = 0;
    578                           TopNumber = 100;
    579                           if (range == 1){
    580                               cout << "请输入下限(正数):";
    581                               cin >> BaseNumber;
    582                               cout << "请输入上限(正数):";
    583                               cin >> TopNumber;
    584                           }
    585                           IsNeg = 0;
    586                           IsRem = 0;
    587                           IsBra = 0;
    588                           flag = false;
    589                           break;
    590                 }
    591                 default:
    592                 {
    593                            cout << "输入有误,请重新选择:" << endl;
    594                            flag = true;
    595                            break;
    596                 }
    597                 }
    598 
    599             }
    600             */
    601 
    602 
    603             //float sum = 0;
    604             for (int i = 0; i < 30; i++)
    605             {
    606                 lastOp = '+';
    607                 IsRep = 0;
    608                 NumberAmount();
    609                 if (IsBra == 0)
    610                 {
    611                     AddLbracket();
    612                     AddRbracket();
    613                 }
    614                 CreateNumber();
    615                 Result[i] = EvaluateExpression();                    
    616                 while (Repeat(i) == 1 || IsRep == 1 || (IsNeg == 1 && (Result[i] < 0 || Result[i] == -0)) || (IsRem == 1 && ((int)(Result[i]*10000))%10000!=0))
    617                 {
    618 
    619                     IsRep = 0;
    620                     lastOp = '+';
    621                     formula="";
    622                     NumberAmount();
    623                     if (IsBra == 0)
    624                     {
    625                         AddLbracket();
    626                         AddRbracket();
    627                     }
    628                     CreateNumber();
    629                     Result[i] = EvaluateExpression();                    
    630                 }
    631                 buffer[i]=formula;
    632                 formula="";
    633             }
    634 
    635             return 0;
    636         }
    637         public static float css(int number){
    638             return Result[number];
    639         }
    640         public static String cs(int number){
    641             return buffer[number];
    642         }
    643     public static void main()
    644     {
    645         int a;
    646         a=ma();
    647         for (int i = 0; i < 30; i++)
    648         {
    649             System.out.println(buffer[i]+Result[i]);
    650         }
    651         
    652     }
    653 }

    结果展示:

    总结:这一次作业因为有原先的程序,所以在具体实现代码基本是原样照搬,我感觉这一次主要考验我们的是java web的编写能力,我又再一次对java做了温习。

    项目计划总结:

    时间记录日志:

    缺陷记录日志:

  • 相关阅读:
    Codeforces Round #534 Div. 1
    Codeforces Round #540 Div. 3 F2
    Educational Codeforces Round 60 Div. 2
    Luogu4389 付公主的背包(生成函数+多项式exp)
    BZOJ1005 HNOI2008明明的烦恼(prufer+高精度)
    Codeforces Round #539 Div. 1
    js --- 关于DOM的事件操作
    js函数
    js常用内置对象
    js数据类型转换 ----流程控制
  • 原文地址:https://www.cnblogs.com/qwer111/p/5352087.html
Copyright © 2011-2022 走看看