计算器输出C锐利 [英] Calculator output C sharp

查看:77
本文介绍了计算器输出C锐利的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

您好我正在做一个计算器,我需要以这种方式显示计算器的输出,例如:1 + 1 = 2 + 2 = 4

是否可以?



Hello I am doing a calculator and I need to show the output of the calculator in this way for example: 1+1=2+2=4
Is it possible ?

if (operator == "+")
           {
               double a = 0;
               string[] chaine = textBox1.Text.Split('+');
               textBox1.Text += Environment.NewLine + "=" + Environment.NewLine + (double.Parse(chaine[0]) + double.Parse(chaine[1])).ToString(); //first choice of the operation

               for (int i = 0; i < chaine.Length; i++)
               {
                   a += double.Parse(chaine[i]);

               }

               textBox1.Text = a.ToString();//second choice of the operation





我的尝试:



我可以做的是这样的1 + 1,它给出结果2



What I have tried:

What I can do it is something like this 1+1 and it give the result 2

推荐答案

if( textBox1.Text.contains(=))

{

strAfterEqual = textBox1.Text.Split('=')。Last();

//将此strAfterEqual传递给原始方法并使用textBox1.Text添加结果

}
if(textBox1.Text.contains("="))
{
strAfterEqual = textBox1.Text.Split('=').Last();
//pass this strAfterEqual to your original method and add the result with textBox1.Text
}


由于这不是单个操作,因此计算机会需要一个复杂的程序来做到这一点。有两种方法可以做到这一点。通常,开发简单的计算器程序,您只需解析单个表达式, a + b =?。你的更像是将输出流水线化为另一个表达式的输入。



首先是创建一个计算器应用程序来解析所提供的表达式(它不会开始解析它们)。该方法涉及堆栈推/弹操作以构建计算器的表达式。一个简单的数据结构课程将涵盖这一点。有关示例,请参阅:使用堆栈的C中的计算器 - 堆栈溢出 [ ^ ],反向波兰表示法 - 维基百科,免费的百科全书 [ ^ ]。常见的例子是,(1 + 1)+ 2 =?



那么还有另一种做法这个。这需要您解析一个表达式,当用户尝试重用输出时(例如,(1 + 1 = 2)+ 2 =?)然后存储前一个答案并根据该结果构建下一个表达式。这需要一个条件来确定是否有以前的结果,

Since this is not a single operation, computer would require a complex program to do this. There are two way of doing this. Typically, simple calculator programs are developed where you only resolve a single expression, "a + b = ?". Yours is more like pipelining the output as input to another expression.

First of the ways is to create a calculator application that resolves the expressions that are provided (it doesn't start resolving them). That method involves a stack push/pop operation to build up an expression for the calculator. A simple data structures lesson would have this covered. For an example, please see: Calculator in C using stack - Stack Overflow[^], Reverse Polish notation - Wikipedia, the free encyclopedia[^]. Common examples are, (1 + 1) + 2 = ?.

Then there is another way of doing this. That requires you to resolve one expression and when user tries to reuse the output (such as, (1 + 1 = 2) + 2 = ?) then you store the previous answer and build the next expression based on that result. That would require a condition to determine if there was a previous result,
// Before operator check
public void operator_Click (object sender, EventArgs e) {
    if(resultAvailable) {
        // Change the values
        a = result;
    }
    op = operatorTxt.Text;
}

// In the resolution function
public void evaluate(object sender, EventArgs e) {
   // Evaluate the answer in result variable for further use.
}



这样,您就可以在解析之前的表达式后处理它们。这就是现代移动计算器所做的工作。


This way, you will be able to process the expressions after resolving the previous ones. That is what is done in the modern mobile calculators.


这篇关于计算器输出C锐利的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆