zoukankan      html  css  js  c++  java
  • 四则运算出题3

    1、题目要求:

        在之前自动出题程序的基础之上,添加新的功能:

            ①能判断用户的输入答案是否正确,正确与否,给出提示,若错误,显示正确答案;

            ② 能处理四则运算的混合算式。

    2、实现思路:

         出题思路不变,对原来程序生成的算式文本,进行读取每一个算式并进行计算,若输入的答案与结果相等,即计算正确。

    3、思路整理(实现步骤):

         出题步骤不变,每一行读取生成的算式文本,将其中的算式作为char数组保存,将char数组用栈保存,利用栈来决定计算顺序并计算出结果。

    4、源代码:

      1 using System;
      2 using System.Collections.Generic;
      3 using System.Linq;
      4 using System.Text;
      5 using System.IO;
      6 
      7 namespace siz
      8 {
      9     class Program
     10     {
     11         static int top;
     12         static int x1, y1;
     13         static int size = 256;
     14         static string [] s=new string[size];
     15 
     16         static double jisuan_ma(char [] str)
     17         {
     18             int l;
     19             double right1;
     20             int k=0;
     21             char ww;
     22             string []str1=new string[size];
     23             string []post=new string[size];
     24     
     25             l=str.Length;
     26             for (int i=0;i<l;i++)
     27             {
     28                 ww=str[i];
     29 
     30                 switch (ww)
     31                 {
     32                     case '=':
     33                     str1[k]="=";
     34                     k++;
     35                     break;
     36                     case '(':
     37                     str1[k]="(";
     38                     k++;
     39                     break;
     40                     case '+':
     41                     str1[k]="+";
     42 
     43                     k++;
     44                     break;
     45                     case '-':
     46                     str1[k]="-";
     47                     k++;
     48                     break;
     49                     case '*':
     50                     str1[k]="*";
     51                     k++;
     52                     break;
     53                     case '/':
     54                     str1[k]="/";
     55                     k++;
     56                     break;
     57                     case ')':
     58                     str1[k]=")";
     59                     k++;
     60                     break;
     61 
     62                     default:
     63                     for (int ss=i;;ss++)
     64                     {
     65 
     66                         if (str[ss]=='0')
     67                         {
     68                             str1[k]+="0";
     69                         }
     70                         else if (str[ss]=='1')
     71                         {
     72                             str1[k]+="1";
     73                         }
     74                         else if (str[ss]=='2')
     75                         {
     76                             str1[k]+="2";
     77                         }
     78                         else if (str[ss]=='3')
     79                         {
     80                             str1[k]+="3";
     81                         }
     82                         else if (str[ss]=='4')
     83                         {
     84                             str1[k]+="4";
     85                         }
     86                         else if (str[ss]=='5')
     87                         {
     88                             str1[k]+="5";
     89                         }
     90                         else if (str[ss]=='6')
     91                         {
     92                             str1[k]+="6";
     93                         }
     94                         else if (str[ss]=='7')
     95                         {
     96                             str1[k]+="7";
     97                         }
     98                         else if (str[ss]=='8')
     99                         {
    100                             str1[k]+="8";
    101                         }
    102                         else if (str[ss]=='9')
    103                         {
    104                             str1[k]+="9";
    105                         }
    106                         else
    107                         {
    108                             i=ss-1;
    109                             break;
    110                         }
    111                     }
    112                     k++;
    113                     break;
    114 
    115                 }
    116             }
    117             right1=mid_post(str1,post);
    118             return right1;
    119         }
    120         static void clearstack()//初始化栈
    121         {
    122             top = -1;
    123         }
    124         static int emtystack()//判断栈是否为空
    125         {
    126             if (top < 0)
    127             {
    128                 return 1;
    129             }
    130             else
    131             {
    132                 return 0;
    133             }
    134         }
    135         static string gettop()//得到栈顶元素
    136         {
    137             if(emtystack()==1)
    138             {
    139                 return null;
    140             }
    141             else
    142             {
    143                 return s[top];
    144             }
    145         }
    146         static void Push(string mid_v)//压栈
    147         {
    148             if(top>=size-1)
    149             {
    150 
    151             }
    152             else
    153             {
    154                 top++;
    155                 s[top]=mid_v;
    156             }
    157         }
    158         static string pop()//出栈
    159         {
    160             if(emtystack()==1)
    161             {
    162                 return null;
    163             }
    164             else
    165             {
    166                 top--;
    167                 return s[top+1];
    168             }
    169         }
    170         static int precede(string x,string y)//判断优先级
    171         {
    172             
    173             if (x=="=")
    174             {
    175                 x1=0;
    176             }
    177             if (x=="(")
    178             {
    179                 x1=1;
    180             }
    181             if (x=="+")
    182             {
    183                 x1=2;
    184             }
    185             if (x=="-")
    186             {
    187                 x1=2;
    188             }
    189             if (x=="*")
    190             {
    191                 x1=3;
    192             }
    193             if (x=="/")
    194             {
    195                 x1=3;
    196             }
    197 
    198             if (y=="=")
    199             {
    200                 y1=0;
    201             }
    202             if (y=="+")
    203             {
    204                 y1=2;
    205             }
    206             if (y=="-")
    207             {
    208                 y1=2;
    209             }
    210             if (y=="*")
    211             {
    212                 y1=3;
    213             }
    214             if (y=="/")
    215             {
    216                 y1=3;
    217             }
    218             if (y=="(")
    219             {
    220                 y1=4;
    221             }
    222             if (x1>=y1)
    223             {
    224                 return 1;
    225             } 
    226             else
    227             {
    228                 return 0;
    229             }
    230         }
    231         static double mid_post(string []str,string []post)//
    232         {
    233             double right;
    234             int i = 0, j = 0;
    235             string x;
    236             clearstack();
    237             Push("=");
    238             do 
    239             {
    240                 x=str[i++];
    241                 if (x=="=")
    242                 {
    243                     while (emtystack()==0)
    244                     {
    245                         post[j++]=pop();
    246                     }
    247                 }
    248                 else if (x==")")
    249                 {
    250                     while (gettop()!="(")
    251                     {
    252                         post[j++]=pop();
    253                     }
    254                     pop();
    255                 }
    256                 else if (x=="+"||x=="-"||x=="*"||x=="/"||x=="(")
    257                 {
    258                     while (precede(gettop(),x)==1)
    259                     {
    260                         post[j++]=pop();
    261                     }
    262                     Push(x);
    263                 }
    264                 else
    265                 {
    266                     post[j++]=x;
    267                 }
    268         
    269             } while (x!="=");
    270             
    271             right = postcount(post);
    272             return right;
    273         }
    274         static double postcount(string []post)//计算
    275         {
    276             int i=0;
    277             double z,a,b;
    278     
    279             clearstack();
    280             string x;
    281             string xx;
    282             while (post[i]!="=")
    283             {
    284                 
    285                 x=post[i];                                                          
    286                 if (x=="+")
    287                 {
    288                     b = (double)Convert.ToSingle(pop());
    289                     a = (double)Convert.ToSingle(pop());
    290                     z=a+b;
    291                     xx = z.ToString();
    292                     Push(xx);
    293                 }
    294                 else if (x=="-")
    295                 {
    296                     b = (double)Convert.ToSingle(pop());
    297                     a = (double)Convert.ToSingle(pop());
    298                     z=a-b;
    299                     xx=z.ToString();
    300                     Push(xx);
    301                 }
    302                 else if (x=="*")
    303                 {
    304                     b = (double)Convert.ToSingle(pop());
    305                     a = (double)Convert.ToSingle(pop());
    306                     z=a*b;
    307                     xx=z.ToString();
    308                     Push(xx);
    309                 }
    310                 else if (x=="/")
    311                 {
    312                     b = (double)Convert.ToSingle(pop());
    313                     a = (double)Convert.ToSingle(pop());
    314                     z=a/b;
    315                     xx=z.ToString();
    316                     Push(xx);
    317                 }
    318                 else
    319                 {
    320                     Push(x);
    321                 }
    322                 i++;
    323             }
    324             if (emtystack() == 0)
    325             {
    326                 x=gettop();
    327                 b = (double)Convert.ToSingle(pop());
    328                 return b;
    329             }
    330             else
    331             {
    332                 return 0;
    333             }
    334         }
    335         static Random rnd = new Random();
    336         static void Main(string[] args)
    337         {
    338             String path;
    339             int num_min, num_max;//数据范围的上限和下限
    340             int num_topic;//题目个数
    341             int num_number;//表达式所含数字个数
    342             int num_milde;
    343             String word_ch;//判断乘除
    344             String word_ys = "N";//判断余数
    345             String word_k = "N";//判断括号
    346             String word_fs = "N";//判断有无负数
    347 
    348 
    349             Console.WriteLine("请输入题目个数:");
    350             num_topic = Convert.ToInt32(Console.ReadLine());
    351 
    352             Console.WriteLine("请输入表达式所含数字个数:");
    353             num_number = Convert.ToInt32(Console.ReadLine());
    354 
    355             Console.WriteLine("请输入范围下限:");
    356             num_min = Convert.ToInt32(Console.ReadLine());
    357 
    358             Console.WriteLine("请输入范围上限:");
    359             num_max = Convert.ToInt32(Console.ReadLine());
    360 
    361             Console.WriteLine("是否有乘除(Y/N):");
    362             word_ch = Console.ReadLine();
    363 
    364             if (num_number == 2)
    365             {
    366                 if (word_ch == "Y")
    367                 {
    368                     Console.WriteLine("是否有余数(Y/N):");
    369                     word_ys = Console.ReadLine();
    370                 }
    371                 if (num_min >= 0)
    372                 {
    373                     Console.WriteLine("是否有负数(Y/N):");
    374                     word_fs = Console.ReadLine();
    375                 }
    376 
    377             }
    378             else
    379             {
    380                 Console.WriteLine("是否有括号(Y/N):");
    381                 word_k = Console.ReadLine();
    382             }
    383 
    384             int[,] Data = new int[num_topic, num_number];//保存数据
    385 
    386             int[,] Operator = new int[num_topic, num_number];//保存运算符
    387 
    388             int[] Data_first = new int[num_topic];//保存每个表达式的首个字符
    389 
    390             int[,] kuohao = new int[num_topic, num_number];
    391             if (word_ch == "Y")
    392             {
    393                 for (int ii = 0; ii < num_topic; ii++)
    394                 {
    395                     for (int jj = 0; jj < num_number - 1; jj++)
    396                     {
    397                         Operator[ii, jj] = rnd.Next(1, 5);
    398                     }
    399                     Operator[ii, num_number - 1] = 5;
    400                 }
    401             }
    402             else
    403             {
    404                 for (int i = 0; i < num_topic; i++)
    405                 {
    406                     for (int j = 0; j < num_number - 1; j++)
    407                     {
    408                         Operator[i, j] = rnd.Next(1, 3);
    409                     }
    410                     Operator[i, num_number - 1] = 5;
    411                 }
    412 
    413             }
    414 
    415 
    416             Data_first[0] = rnd.Next(num_min, num_max);//以第一个操作数的不同来使表达式不会重复
    417 
    418             for (int s = 1; s < num_topic; s++)
    419             {
    420                 Data_first[s] = rnd.Next(num_min, num_max);
    421                 for (int h = 0; h < s - 1; h++)
    422                 {
    423                     if (Data_first[s] == Data_first[h])
    424                     {
    425                         s--;
    426                     }
    427                 }
    428             }
    429             for (int x = 0; x < num_topic; x++)
    430             {
    431                 Data[x, 0] = Data_first[x];
    432                 for (int y = 1; y < num_number; y++)
    433                 {
    434                     if (Operator[x, y - 1] == 4)
    435                     {
    436                         for (; ; )
    437                         {
    438                             Data[x, y] = rnd.Next(num_min, num_max);
    439                             if (Data[x, y] != 0)
    440                             {
    441                                 break;
    442                             }
    443                         }
    444                     }
    445                     else
    446                     {
    447                         Data[x, y] = rnd.Next(num_min, num_max);
    448                     }
    449                 }
    450             }
    451             if (word_fs == "N")
    452             {
    453                 for (int i1 = 0; i1 < num_topic; i1++)
    454                 {
    455                     if (Operator[i1, 0] == 2 && Data[i1, 0] < Data[i1, 1])
    456                     {
    457                         num_milde = Data[i1, 1];
    458                         Data[i1, 1] = Data[i1, 0];
    459                         Data[i1, 0] = num_milde;
    460 
    461                     }
    462                 }
    463             }
    464 
    465             if (word_ch == "Y")
    466             {
    467 
    468                 if (word_ys == "N")
    469                 {
    470 
    471                     for (int i2 = 0; i2 < num_topic; i2++)
    472                     {
    473 
    474                         if (Operator[i2, 0] == 4)
    475                         {
    476 
    477                             num_milde = Data[i2, 0] % Data[i2, 1];
    478                             while (num_milde != 0)
    479                             {
    480                                 Data[i2, 1] = rnd.Next(1, Data[i2, 0]+1);
    481                                 num_milde = Data[i2, 0] % Data[i2, 1];
    482                             }
    483                         }
    484                     }
    485                 }
    486 
    487             }
    488 
    489             if (word_k == "Y")
    490             {
    491                 for (int j1 = 0; j1 < num_topic; j1++)
    492                 {
    493                     for (int j2 = 0; j2 < num_number - 2; j2++)
    494                     {
    495                         kuohao[j1, j2] = rnd.Next(0, 2);
    496                     }
    497                     kuohao[j1, num_number - 2] = 0;
    498                     kuohao[j1, num_number - 1] = 0;
    499                 }
    500             }
    501             path = @"1.txt";
    502             StreamWriter strm1 = File.CreateText(path);
    503             for (int x1 = 0; x1 < num_topic; x1++)
    504             {
    505                 for (int x2 = 0; x2 < num_number; x2++)
    506                 {
    507                     if (kuohao[x1, x2] == 1)
    508                     {
    509                         strm1.Write("(");
    510                         strm1.Write(Data[x1, x2]);
    511                         if (Operator[x1, x2] != 5)
    512                         {
    513                             if (Operator[x1, x2] == 1)
    514                             {
    515                                 strm1.Write("+");
    516                             }
    517                             if (Operator[x1, x2] == 2)
    518                             {
    519                                 strm1.Write("-");
    520                             }
    521                             if (Operator[x1, x2] == 3)
    522                             {
    523                                 strm1.Write("*");
    524                             }
    525                             if (Operator[x1, x2] == 4)
    526                             {
    527                                 strm1.Write("/");
    528                             }
    529 
    530                         }
    531 
    532                     }
    533                     else
    534                     {
    535                         strm1.Write(Data[x1, x2]);
    536                         if (Operator[x1, x2] != 5)
    537                         {
    538                             if (Operator[x1, x2] == 1)
    539                             {
    540                                 strm1.Write("+");
    541                             }
    542                             if (Operator[x1, x2] == 2)
    543                             {
    544                                 strm1.Write("-");
    545                             }
    546                             if (Operator[x1, x2] == 3)
    547                             {
    548                                 strm1.Write("*");
    549                             }
    550                             if (Operator[x1, x2] == 4)
    551                             {
    552                                 strm1.Write("/");
    553                             }
    554 
    555                         }
    556                         else
    557                         {
    558                             for (int x3 = 0; x3 < num_number; x3++)
    559                             {
    560                                 if (kuohao[x1, x3] == 1)
    561                                 {
    562                                     strm1.Write(")");
    563                                 }
    564                             }
    565                             strm1.Write("=");
    566 
    567                         }
    568                     }
    569                 }
    570                 strm1.WriteLine();
    571             }
    572             strm1.Close();
    573             StreamReader strm2 = File.OpenText(path);
    574             string s_mid = "";
    575             char[] strr = new char[size];
    576             double answer;
    577             double right;
    578             int right_an=0;
    579             int wrong_an=0;
    580             for (int o = 0; o < num_topic; o++)
    581             {
    582                 s_mid = strm2.ReadLine();
    583                 strr = s_mid.ToCharArray(0, s_mid.Length);
    584                 Console.WriteLine(s_mid);
    585                 right = jisuan_ma(strr);
    586                 Console.WriteLine("请输入答案:");
    587                 answer = Convert.ToDouble(Console.ReadLine());
    588                 if (answer == right)
    589                 {
    590                     Console.WriteLine("回答正确!");
    591                     right_an++;
    592                 }
    593                 else
    594                 {
    595                     Console.WriteLine("回答错误!正确答案为:{0}",right);
    596                     wrong_an++;
    597                 }
    598             }
    599             Console.WriteLine("本次答题共计正确:{0}道题,错误{1}道题!", right_an, wrong_an);
    600         }
    601     }
    602 }

    5、运行结果

    6、工作照片:

                                                             项目记录日志(单位:小时(h)):

      听课 编写程序 阅读相关书籍 网上查找资料 日总计

     周一

    2  2 1  0.5 5.5

    周二

     2  1 0  3

    周三

    4 0  0.5 4.5

    周四

    2  3  1 6

    周五

    6 0 1.5  7.5

    周六

    1  0   0 1

    周总计

    4 18 3  2.5 27.5

                                                     时间记录日志(单位:min):

    日期 开始时间 结束时间 中断时间 净时间 活动 备注
    星期一 14:00 15:50 10(课间) 100 听课 软件工程上课
      19:30 21:40 0 130 搜集资料,编程 探讨四则运算3的程序的思路并找资料
    星期二 19:30 21:30 0 120 编程 四则运算2的程序
    星期三 14:00  15:00 0  60 编程 四则运算3的程序
      19:00 21:30 0 150 编程
    星期四 14:00 15:50 10(课间) 100 听课 软件工程上课
      19:30 21:50 0 100 编程 四则运算2的程序
    星期五 14:00 17:00 0 180 编程 四则运算3的程序
      18:00 21:00 0 120 编程 四则运算3的程序
    星期六 10:00 12:00 20(洗漱) 100 修改,调试 四则运算3程序进行修改、调试
      14:00 15:40   100 修改博客  写博客并发布

    队友地址:http://www.cnblogs.com/mengyinianhua/

  • 相关阅读:
    Dubbo
    支持微服务架构落地的Java框架
    thinkphp6的主要特性
    thinkphp5的主要特性
    RPC
    HTTP1.0 HTTP1.1 HTTP2.0 主要特性对比
    RabbitMQ 生产环境配置详解
    分布式AKF拆分原则
    通过Hystrix了解分布式接口级的高可用
    Python中使用grpc与consul
  • 原文地址:https://www.cnblogs.com/wangyw/p/5295243.html
Copyright © 2011-2022 走看看