Click here to Skip to main content
15,893,161 members
Please Sign up or sign in to vote.
1.75/5 (4 votes)
See more:
hi,
if we give input on text by typing 1+2*8 then how to read this string and how to split opeartor and operand.
give code for calculator for function +,-,*,/

plz send code urgently




C#
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using System.Collections;


namespace WindowsFormsApplication6
{
    public partial class Form1 : Form
    {
        public Form1()
        {
            InitializeComponent();
        }

private void button1_Click(object sender, EventArgs e)
{
int counter = 0;
string inputText = Input.Text;
Stack stack = new Stack();
char[] chars = inputText.ToCharArray();
string lastNumber = string.Empty;



foreach (char c in chars)
{
if (char.IsDigit(c))
{
lastNumber = lastNumber + c.ToString();
}
else
{

stack.Push(lastNumber);
stack.Push(c);
lastNumber = string.Empty;
}
counter++;
}
stack.Push(lastNumber);


int prevNum, currNum=0;
string operand;
bool isEnd = true;

prevNum = int.Parse(stack.Pop().ToString());
operand = stack.Pop().ToString();


do {

operand = stack.Pop().ToString();
if (operand == "+")
{
currNum = int.Parse(stack.Pop().ToString());

prevNum = currNum + prevNum;
answer.Text = prevNum.ToString();
}

if (operand == "-")
{
currNum = int.Parse(stack.Pop().ToString());
prevNum = currNum - prevNum;
answer.Text = prevNum.ToString();
}

if (operand == "/")
{
currNum = int.Parse(stack.Pop().ToString());
prevNum = currNum / prevNum;
answer.Text = prevNum.ToString();
}

if (operand == "*")
{
currNum = int.Parse(stack.Pop().ToString());
prevNum = currNum * prevNum;
answer.Text = prevNum.ToString();
}
}while (operand!=" ");

}


}
}





I do these coding but have an error int ToString() method . any1 help to solve it.
Posted
Updated 3-Feb-13 22:27pm
v3
Comments
Mantu Singh 1-Feb-13 7:46am    
Have you tried something yet!! sounds like H/W
[no name] 1-Feb-13 8:07am    
What problem r u facing now?
please feel free but please don't put same question again and again.
Hemant Singh Rautela 1-Feb-13 8:39am    
I just use for something else to reading specific character in string you can get some help from it....

http://hemantrautela.blogspot.com/2012/09/remove-html-tags-from-string-cnet-aspnet.html

Sounds like homework to me - and we don't do your homework.

If it isn't, then have a look at this: Math Parser .NET C#[^] it does what you want, and a whole lot more.
 
Share this answer
 
Comments
[no name] 1-Feb-13 8:01am    
+5 Sir.
I suppose you won't find anything on such a new topic, just using Google[^] .
 
Share this answer
 
Comments
DinoRondelly 1-Feb-13 17:08pm    
+5 .. made my day
 
Share this answer
 
You ask about tokenizing[^] your input string.

You can either take a tool to tokenize (see link above for some tools), or you can write your own tokenizer.

Writing your own tokenizer is either by using the C# Regex Class[^] or by fully hand-craft it yourself.

For such simple tasks like this one, I usually use Regex, first since I know it well and secondly it is easy, if you know Regex ;-)

Your language is tokenized as follows (ignoring white spaces and non-matching characters):
C#
string s = "..."; // you input string...
string[] tokens = Regex.Matches(s, @"(\d+|[-+*/])")
                       .Cast<Match>()
                       .Where(m=>m.Groups[1].Success)
                       .Select(m=>m.Groups[1].Value)
                       .ToArray();


Cheers
Andi
 
Share this answer
 
v4
You can use C# itself as an advanced calculator, or, say, VB.NET. This is because the compiles are always bundled with (redistributable) .NET framework and are always supported during run time via CodeDOM. Please see my past answers:
code generating using CodeDom[^],
Create WPF Application that uses Reloadable Plugins...[^].

With this approach, you don't have to parse anything, as it is already done for you. You can quickly build short user's code, execute it under you host, obtain results of calculations and output it in your UI. Most difficult part is reloading every time the user enters new code without memory leak, but I explained it all in my answers. I have such a "calculator" myself.

—SA
 
Share this answer
 
Here is an article on CP that can show you how it's done. Just remember that your instructor will likely know about CP and knows enough to check the code against what is here. Use the code to learn from, not to hand in.
Simple(x) Numerical Formula Parser[^]
 
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