c# 中的控制台计算器,带有一系列计算

Console Calculator in c# with a chain of calculations

我的代码需要一些帮助。 这是任务的样子:

开发一个控制台程序"Calculator",在循环迭代中读取算术运算符, 其中用户输入(“+”、“-”、“*”、“/”)及其操作数(实数),然后计算, 将操作的结果带到屏幕上,然后在下一个操作中用作 第一个操作数(因此,程序将类似于连续计算链)。 程序必须正确处理错误(除以零,输入数字而不是 算术运算符等)并在不中断工作的情况下通知用户。

这是我目前得到的:

Console.WriteLine("Введите число x");
            double x = Convert.ToDouble(Console.ReadLine());
      while (true)
         {   Console.WriteLine("Введите число y");
            double y = Convert.ToDouble(Console.ReadLine());
            Console.WriteLine("// Выберите операцию //");
            Console.WriteLine("+ - сложение");
            Console.WriteLine("- - вычитание");
            Console.WriteLine("* - умножение");
            Console.WriteLine("/ - деление");
            string z = Console.ReadLine();
                switch (z)
                {
                    case "+":
                        Console.WriteLine("// Результат //");
                        Console.WriteLine(x + y);
                        break;
                    case "-":
                        Console.WriteLine("// Результат //");
                        Console.WriteLine(x - y);
                        break;
                    case "*":
                        Console.WriteLine("// Результат //");
                        Console.WriteLine(x * y);
                        break;
                    case "/":
                        if (y == 0)
                        {
                            Console.WriteLine("Не дели на 0");
                        }
                        else
                        {
                            Console.WriteLine("// Результат //");
                            Console.WriteLine(x / y);
                        }
                        break;
                }

}

我已经成功地添加了除以零的限制,但仅此而已。 我不知道,怎么做这个 "chain of calculations"。当然,我知道我必须使用循环,但不能想出任何东西。我没有得到的是 - 两个数字正在运行,仅此而已,程序停止了。我尝试添加

do
  {
    //main program body
    Console.WriteLine("Press ENTER to continue");
    keyInfo = Console.ReadKey(true);
  }
while (keyInfo.Key == ConsoleKey.Enter);

但这只会让程序从头开始。我需要第一个操作的结果成为第一个操作数等等。然后用户必须键入一些内容才能结束此程序。

对不起我的英语,对不起我的 C# 知识,不幸的是,我是初学者。 希望早日听到,提前致谢。

        while(true){
        Console.WriteLine("Введите число x");
        double x = Convert.ToDouble(Console.ReadLine());
        Console.WriteLine("Введите число y");
        double y = Convert.ToDouble(Console.ReadLine());
        Console.WriteLine("// Выберите операцию //");
        Console.WriteLine("+ - сложение");
        Console.WriteLine("- - вычитание");
        Console.WriteLine("* - умножение");
        Console.WriteLine("/ - деление");
        string z = Console.ReadLine();
            switch (z)
            {
                case "+":
                    Console.WriteLine("// Результат //");
                    Console.WriteLine(x + y);
                    break;
                case "-":
                    Console.WriteLine("// Результат //");
                    Console.WriteLine(x - y);
                    break;
                case "*":
                    Console.WriteLine("// Результат //");
                    Console.WriteLine(x * y);
                    break;
                case "/":
                    if (y == 0)
                    {
                        Console.WriteLine("Не дели на 0");
                    }
                    else
                    {
                        Console.WriteLine("// Результат //");
                        Console.WriteLine(x / y);
                    }
                    break;
            }
            Console.WriteLine("Do you want to exit ?! Y:N?");
            char Terminat= Console.ReadKey().KeyChar;
           if(Terminat=='Y'||Terminat=='y'){
             break;
              }

}

不是直接打印计算结果,而是将结果存入x变量,打印出来然后让用户输入下一个y。这样,x 将包含上一个计算的结果,允许您 "chain" 它到下一个。您只在第一次询问 x 并在每次循环重复时询问 y 。

Console.WriteLine("Введите число x");
double x = Convert.ToDouble(Console.ReadLine());

while (true)
{
    Console.WriteLine("Введите число y");
    double y = Convert.ToDouble(Console.ReadLine());
    Console.WriteLine("// Выберите операцию //");
    Console.WriteLine("+ - сложение");
    Console.WriteLine("- - вычитание");
    Console.WriteLine("* - умножение");
    Console.WriteLine("/ - деление");
    string z = Console.ReadLine();

    switch (z)
    {
       case "+":
           Console.WriteLine("// Результат //");
           x = (x + y);  //Store the result in x
           Console.WriteLine(x);
           break;
       case "-":
           Console.WriteLine("// Результат //");
           x = (x - y);  //Store the result in x
           Console.WriteLine(x);
           break;
       case "*":
           Console.WriteLine("// Результат //");
           x = (x * y);  //Store the result in x
           Console.WriteLine(x);
           break;
       case "/":
           if (y == 0)
           {
               Console.WriteLine("Не дели на 0");
           }
           else
           {
               Console.WriteLine("// Результат //");
               x = (x / y);
               Console.WriteLine(x);
           }
           break;
    }
}

这是我对那些寻找基于语法分析的计算器的人的回答(用 C# 实现)。我改编自 Stroustrup 的 "Programming principles and practice using C++"

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace CalculatorApplication
{
    class Calculator
    {
        string input;
        char[] szArr;
        int length ;
        int current = -1;
        char ch;
        public Calculator(String input)
        {
            this.input = input;
            szArr = input.ToCharArray();
           length = szArr.Length;
        }
        char nextToken()
        {
            ++current;
            if(current==length) return 'p';
            return szArr[current];
        }
        public double expression()
        {
            double d = term();
            while(true){
            switch (ch)
            {
                case '+':
                    d+= term();
                    break;
                case  '-':
                    d -= term();
                    break;
                default:
                    break;

            }
            if (ch == 'p') break;
        }
            return d;

        }


        public double term()
        {
            double d = primary();
            switch (ch)
            {
                case '*':
                    d *= primary();
                    break;
                case '/':
                     d /= primary();
                    break;
                default:
                    break;

            }
            return d;
        }
        public double primary()
        {
            string str = "";
            double value = 0.0;
            while (true)
            {

                ch = nextToken();
                if (isDigit(ch))
                {
                    str += ch;
                }
                else
                {
                    break;
                }
            }
            value = double.Parse(str);
            return value;

        }

        bool isDigit(char ch)
        {
            return ch == '0' || ch == '1' || ch == '2' || ch == '3' || ch == '4' || ch == '5' || ch == '6' || ch == '7' || ch == '8' || ch == '9';
        }
    }

    class Program
    {
        static void Main(string[] args)
        {
            Console.WriteLine("Enter expression");
            string input = Console.ReadLine();
            Console.WriteLine(new Calculator(input).expression());          
        }
    }
}

我制作了一个原始计算器,它可以使用以下运算符评估带括号 ( ) 和运算符优先级的复杂数学表达式:+ - * / % 和 ^。

https://github.com/henon/PrimitiveCalculator

这是它能做的(交互式控制台输出):

calc> 1/2*2
 = 1
calc> 2^1+1
 = 3
calc> 7777777777777+8888888888888
 = 16666666666665
calc> 1+2+3+4+5+7+8
 = 30
calc> 1+2*3
 = 7
calc> 1+2*3-1
 = 6
calc> (1+2)*(3-1)
 = 6
calc> (1+2)*(3-2)
 = 3
calc> 1+2*3-2
 = 5
calc> (1+2)*3
 = 9
calc> 44%10
 = 4
calc>  1+ 2 * 7
 = 15
calc> 10/3
 = 3.3333333333333335
calc> 0.3*9
 = 2.6999999999999997
calc> -0.22345 + -1.1
 = -1.32345