Click here to Skip to main content
15,911,139 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
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 ?

C#
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


What I have tried:

What I can do it is something like this 1+1 and it give the result 2
Posted
Updated 18-Aug-16 2:58am
v3
Comments
Richard MacCutchan 18-Aug-16 7:41am    
Yes, you can do anything you want (within reason) in C#. But without seeing some of your code (not all) we have no idea why your output may be incorrect.
TatsuSheva 18-Aug-16 7:46am    
I edited my question.
Richard MacCutchan 18-Aug-16 7:52am    
I cannot make head or tail of what that code is supposed to be doing. You would be better throwing all of that away and starting again. Start with a StringBuilder, and just add each input value, operator, next value, "=", and result, as you go through each operation. At the point you have a result you just set the text of the TextBox to the contents of the StringBuilder. When the user presses the clear button you just reinitialise the StringBuilder and repeat the entire process.
TatsuSheva 18-Aug-16 7:56am    
How to use StringBuilder ? I have never used it.
Richard MacCutchan 18-Aug-16 8:27am    
As with all controls you start by reading the documentation.

if(textBox1.Text.contains("="))
{
strAfterEqual = textBox1.Text.Split('=').Last();
//pass this strAfterEqual to your original method and add the result with textBox1.Text
}
 
Share this answer
 
Comments
TatsuSheva 18-Aug-16 8:52am    
I put this inside my method ? inside of this ?? if (operator == "+")
{...}
divya behera 18-Aug-16 9:02am    
You need to put it before your function so that if it detects "=" operator it only execute the last part otherwise execute full.
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,
C#
// 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.
 
Share this answer
 

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900