码迷,mamicode.com
首页 > 其他好文 > 详细

四则运算3

时间:2016-03-19 14:44:05      阅读:311      评论:0      收藏:0      [点我收藏+]

标签:

题目:

要求用户输入结果,并判断正误,如果错误,给出正确结果。

分析:

将产生式保存到文本文档,然后在以字符串的形式保存到字符数组,然后通过字符数组保存到字符串数组,然后再以栈的形式计算结果。

源程序:

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.IO;

namespace siz
{
    class Program
    {
        static int top;
        static int x1, y1;
        static int size = 256;
        static string [] s=new string[size];

        static double jisuan_ma(char [] str)
        {
            int l;
            double right1;
            int k=0;
            char ww;
            string []str1=new string[size];
            string []post=new string[size];
    
            l=str.Length;
            for (int i=0;i<l;i++)
            {
                ww=str[i];

                switch (ww)
                {
                    case =:
                    str1[k]="=";
                    k++;
                    break;
                    case (:
                    str1[k]="(";
                    k++;
                    break;
                    case +:
                    str1[k]="+";

                    k++;
                    break;
                    case -:
                    str1[k]="-";
                    k++;
                    break;
                    case *:
                    str1[k]="*";
                    k++;
                    break;
                    case /:
                    str1[k]="/";
                    k++;
                    break;
                    case ):
                    str1[k]=")";
                    k++;
                    break;

                    default:
                    for (int ss=i;;ss++)
                    {

                        if (str[ss]==0)
                        {
                            str1[k]+="0";
                        }
                        else if (str[ss]==1)
                        {
                            str1[k]+="1";
                        }
                        else if (str[ss]==2)
                        {
                            str1[k]+="2";
                        }
                        else if (str[ss]==3)
                        {
                            str1[k]+="3";
                        }
                        else if (str[ss]==4)
                        {
                            str1[k]+="4";
                        }
                        else if (str[ss]==5)
                        {
                            str1[k]+="5";
                        }
                        else if (str[ss]==6)
                        {
                            str1[k]+="6";
                        }
                        else if (str[ss]==7)
                        {
                            str1[k]+="7";
                        }
                        else if (str[ss]==8)
                        {
                            str1[k]+="8";
                        }
                        else if (str[ss]==9)
                        {
                            str1[k]+="9";
                        }
                        else
                        {
                            i=ss-1;
                            break;
                        }
                    }
                    k++;
                    break;

                }
            }
            right1=mid_post(str1,post);
            return right1;
        }
        static void clearstack()//初始化栈
        {
            top = -1;
        }
        static int emtystack()//判断栈是否为空
        {
            if (top < 0)
            {
                return 1;
            }
            else
            {
                return 0;
            }
        }
        static string gettop()//得到栈顶元素
        {
            if(emtystack()==1)
            {
                return null;
            }
            else
            {
                return s[top];
            }
        }
        static void Push(string mid_v)//压栈
        {
            if(top>=size-1)
            {

            }
            else
            {
                top++;
                s[top]=mid_v;
            }
        }
        static string pop()//出栈
        {
            if(emtystack()==1)
            {
                return null;
            }
            else
            {
                top--;
                return s[top+1];
            }
        }
        static int precede(string x,string y)//判断优先级
        {
            
            if (x=="=")
            {
                x1=0;
            }
            if (x=="(")
            {
                x1=1;
            }
            if (x=="+")
            {
                x1=2;
            }
            if (x=="-")
            {
                x1=2;
            }
            if (x=="*")
            {
                x1=3;
            }
            if (x=="/")
            {
                x1=3;
            }

            if (y=="=")
            {
                y1=0;
            }
            if (y=="+")
            {
                y1=2;
            }
            if (y=="-")
            {
                y1=2;
            }
            if (y=="*")
            {
                y1=3;
            }
            if (y=="/")
            {
                y1=3;
            }
            if (y=="(")
            {
                y1=4;
            }
            if (x1>=y1)
            {
                return 1;
            } 
            else
            {
                return 0;
            }
        }
        static double mid_post(string []str,string []post)//
        {
            double right;
            int i = 0, j = 0;
            string x;
            clearstack();
            Push("=");
            do 
            {
                x=str[i++];
                if (x=="=")
                {
                    while (emtystack()==0)
                    {
                        post[j++]=pop();
                    }
                }
                else if (x==")")
                {
                    while (gettop()!="(")
                    {
                        post[j++]=pop();
                    }
                    pop();
                }
                else if (x=="+"||x=="-"||x=="*"||x=="/"||x=="(")
                {
                    while (precede(gettop(),x)==1)
                    {
                        post[j++]=pop();
                    }
                    Push(x);
                }
                else
                {
                    post[j++]=x;
                }
        
            } while (x!="=");
            
            right = postcount(post);
            return right;
        }
        static double postcount(string []post)//计算
        {
            int i=0;
            double z,a,b;
    
            clearstack();
            string x;
            string xx;
            while (post[i]!="=")
            {
                
                x=post[i];                                                          
                if (x=="+")
                {
                    b = (double)Convert.ToSingle(pop());
                    a = (double)Convert.ToSingle(pop());
                    z=a+b;
                    xx = z.ToString();
                    Push(xx);
                }
                else if (x=="-")
                {
                    b = (double)Convert.ToSingle(pop());
                    a = (double)Convert.ToSingle(pop());
                    z=a-b;
                    xx=z.ToString();
                    Push(xx);
                }
                else if (x=="*")
                {
                    b = (double)Convert.ToSingle(pop());
                    a = (double)Convert.ToSingle(pop());
                    z=a*b;
                    xx=z.ToString();
                    Push(xx);
                }
                else if (x=="/")
                {
                    b = (double)Convert.ToSingle(pop());
                    a = (double)Convert.ToSingle(pop());
                    z=a/b;
                    xx=z.ToString();
                    Push(xx);
                }
                else
                {
                    Push(x);
                }
                i++;
            }
            if (emtystack() == 0)
            {
                x=gettop();
                b = (double)Convert.ToSingle(pop());
                return b;
            }
            else
            {
                return 0;
            }
        }
        static Random rnd = new Random();
        static void Main(string[] args)
        {
            String path;
            int num_min, num_max;//数据范围的上限和下限
            int num_topic;//题目个数
            int num_number;//表达式所含数字个数
            int num_milde;
            String word_ch;//判断乘除
            String word_ys = "N";//判断余数
            String word_k = "N";//判断括号
            String word_fs = "N";//判断有无负数


            Console.WriteLine("请输入题目个数:");
            num_topic = Convert.ToInt32(Console.ReadLine());

            Console.WriteLine("请输入表达式所含数字个数:");
            num_number = Convert.ToInt32(Console.ReadLine());

            Console.WriteLine("请输入范围下限:");
            num_min = Convert.ToInt32(Console.ReadLine());

            Console.WriteLine("请输入范围上限:");
            num_max = Convert.ToInt32(Console.ReadLine());

            Console.WriteLine("是否有乘除(Y/N):");
            word_ch = Console.ReadLine();

            if (num_number == 2)
            {
                if (word_ch == "Y")
                {
                    Console.WriteLine("是否有余数(Y/N):");
                    word_ys = Console.ReadLine();
                }
                if (num_min >= 0)
                {
                    Console.WriteLine("是否有负数(Y/N):");
                    word_fs = Console.ReadLine();
                }

            }
            else
            {
                Console.WriteLine("是否有括号(Y/N):");
                word_k = Console.ReadLine();
            }

            int[,] Data = new int[num_topic, num_number];//保存数据

            int[,] Operator = new int[num_topic, num_number];//保存运算符

            int[] Data_first = new int[num_topic];//保存每个表达式的首个字符

            int[,] kuohao = new int[num_topic, num_number];
            if (word_ch == "Y")
            {
                for (int ii = 0; ii < num_topic; ii++)
                {
                    for (int jj = 0; jj < num_number - 1; jj++)
                    {
                        Operator[ii, jj] = rnd.Next(1, 5);
                    }
                    Operator[ii, num_number - 1] = 5;
                }
            }
            else
            {
                for (int i = 0; i < num_topic; i++)
                {
                    for (int j = 0; j < num_number - 1; j++)
                    {
                        Operator[i, j] = rnd.Next(1, 3);
                    }
                    Operator[i, num_number - 1] = 5;
                }

            }


            Data_first[0] = rnd.Next(num_min, num_max);//以第一个操作数的不同来使表达式不会重复

            for (int s = 1; s < num_topic; s++)
            {
                Data_first[s] = rnd.Next(num_min, num_max);
                for (int h = 0; h < s - 1; h++)
                {
                    if (Data_first[s] == Data_first[h])
                    {
                        s--;
                    }
                }
            }
            for (int x = 0; x < num_topic; x++)
            {
                Data[x, 0] = Data_first[x];
                for (int y = 1; y < num_number; y++)
                {
                    if (Operator[x, y - 1] == 4)
                    {
                        for (; ; )
                        {
                            Data[x, y] = rnd.Next(num_min, num_max);
                            if (Data[x, y] != 0)
                            {
                                break;
                            }
                        }
                    }
                    else
                    {
                        Data[x, y] = rnd.Next(num_min, num_max);
                    }
                }
            }
            if (word_fs == "N")
            {
                for (int i1 = 0; i1 < num_topic; i1++)
                {
                    if (Operator[i1, 0] == 2 && Data[i1, 0] < Data[i1, 1])
                    {
                        num_milde = Data[i1, 1];
                        Data[i1, 1] = Data[i1, 0];
                        Data[i1, 0] = num_milde;

                    }
                }
            }

            if (word_ch == "Y")
            {

                if (word_ys == "N")
                {

                    for (int i2 = 0; i2 < num_topic; i2++)
                    {

                        if (Operator[i2, 0] == 4)
                        {

                            num_milde = Data[i2, 0] % Data[i2, 1];
                            while (num_milde != 0)
                            {
                                Data[i2, 1] = rnd.Next(1, Data[i2, 0]+1);
                                num_milde = Data[i2, 0] % Data[i2, 1];
                            }
                        }
                    }
                }

            }

            if (word_k == "Y")
            {
                for (int j1 = 0; j1 < num_topic; j1++)
                {
                    for (int j2 = 0; j2 < num_number - 2; j2++)
                    {
                        kuohao[j1, j2] = rnd.Next(0, 2);
                    }
                    kuohao[j1, num_number - 2] = 0;
                    kuohao[j1, num_number - 1] = 0;
                }
            }
            path = @"1.txt";
            StreamWriter strm1 = File.CreateText(path);
            for (int x1 = 0; x1 < num_topic; x1++)
            {
                for (int x2 = 0; x2 < num_number; x2++)
                {
                    if (kuohao[x1, x2] == 1)
                    {
                        strm1.Write("(");
                        strm1.Write(Data[x1, x2]);
                        if (Operator[x1, x2] != 5)
                        {
                            if (Operator[x1, x2] == 1)
                            {
                                strm1.Write("+");
                            }
                            if (Operator[x1, x2] == 2)
                            {
                                strm1.Write("-");
                            }
                            if (Operator[x1, x2] == 3)
                            {
                                strm1.Write("*");
                            }
                            if (Operator[x1, x2] == 4)
                            {
                                strm1.Write("/");
                            }

                        }

                    }
                    else
                    {
                        strm1.Write(Data[x1, x2]);
                        if (Operator[x1, x2] != 5)
                        {
                            if (Operator[x1, x2] == 1)
                            {
                                strm1.Write("+");
                            }
                            if (Operator[x1, x2] == 2)
                            {
                                strm1.Write("-");
                            }
                            if (Operator[x1, x2] == 3)
                            {
                                strm1.Write("*");
                            }
                            if (Operator[x1, x2] == 4)
                            {
                                strm1.Write("/");
                            }

                        }
                        else
                        {
                            for (int x3 = 0; x3 < num_number; x3++)
                            {
                                if (kuohao[x1, x3] == 1)
                                {
                                    strm1.Write(")");
                                }
                            }
                            strm1.Write("=");

                        }
                    }
                }
                strm1.WriteLine();
            }
            strm1.Close();
            StreamReader strm2 = File.OpenText(path);
            string s_mid = "";
            char[] strr = new char[size];
            double answer;
            double right;
            int right_an=0;
            int wrong_an=0;
            for (int o = 0; o < num_topic; o++)
            {
                s_mid = strm2.ReadLine();
                strr = s_mid.ToCharArray(0, s_mid.Length);
                Console.WriteLine(s_mid);
                right = jisuan_ma(strr);
                Console.WriteLine("请输入答案:");
                answer = Convert.ToDouble(Console.ReadLine());
                if (answer == right)
                {
                    Console.WriteLine("回答正确!");
                    right_an++;
                }
                else
                {
                    Console.WriteLine("回答错误!正确答案为:{0}",right);
                    wrong_an++;
                }
            }
            Console.WriteLine("本次答题共计正确:{0}道题,错误{1}道题!", right_an, wrong_an);
        }
    }
}

运行结果:

技术分享

合作截图:

技术分享

技术分享

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

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

周一 

2    1  0.5 3.5

周二

   3      3

周三

  8 1   9

周四

2       2

周五

  3     3

周六

  2     2

周日

         

周总计

4 16 2  0.5 22.5

 

时间记录日志:

日期 开始时间 结束时间 中间时间(分钟) 净时间(分钟) 活动 备注
3.14 14:00 15:50 10 100 听课 软件工程上课
  20:00 21:00   60 阅读 阅读构建之法
3.15 18:00 21:00  30 150 编写程序 编写四则运算程序
3.16 13:00 21:00 60 420 编写程序 编写四则运算程序
  20:00 21:00   60 阅读 阅读构建之法
3.17 14:00 15:50 10 100 上课 软件工程上课
3.18 18:00 21:00   180 编写程序 编写四则运算程序
3.19 10:00 12:00   120 编写程序 四则运算程序的修改,调试,完善

四则运算3

标签:

原文地址:http://www.cnblogs.com/mengyinianhua/p/5295056.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!