Click here to Skip to main content
Rate this: bad
good
Please Sign up or sign in to vote.
See more: C# GimmeCode urgentz
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
 

 

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 1-Feb-13 2:44am
Edited 3-Feb-13 23:27pm
v3
Comments
Mantu Singh at 1-Feb-13 7:46am
   
Have you tried something yet!! sounds like H/W
onurag19 at 1-Feb-13 8:07am
   
What problem r u facing now?
please feel free but please don't put same question again and again.
hemantrautela at 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
Rate this: bad
good
Please Sign up or sign in to vote.

Solution 1

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.
  Permalink  
Comments
onurag19 at 1-Feb-13 8:01am
   
+5 Sir.
Rate this: bad
good
Please Sign up or sign in to vote.

Solution 2

I suppose you won't find anything on such a new topic, just using Google[^] .
  Permalink  
Comments
DinoRondelly at 1-Feb-13 17:08pm
   
+5 .. made my day
Rate this: bad
good
Please Sign up or sign in to vote.

Solution 3

  Permalink  
Rate this: bad
good
Please Sign up or sign in to vote.

Solution 6

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 Wink | ;-)
 
Your language is tokenized as follows (ignoring white spaces and non-matching characters):
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
  Permalink  
v4
Rate this: bad
good
Please Sign up or sign in to vote.

Solution 7

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
  Permalink  
Rate this: bad
good
Please Sign up or sign in to vote.

Solution 4

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[^]
  Permalink  

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

  Print Answers RSS
0 OriginalGriff 459
1 Maciej Los 325
2 Sergey Alexandrovich Kryukov 234
3 DamithSL 233
4 BillWoodruff 200
0 OriginalGriff 7,168
1 Sergey Alexandrovich Kryukov 6,377
2 DamithSL 5,461
3 Manas Bhardwaj 4,876
4 Maciej Los 4,450


Advertise | Privacy | Mobile
Web01 | 2.8.1411023.1 | Last Updated 4 Feb 2013
Copyright © CodeProject, 1999-2014
All Rights Reserved. Terms of Service
Layout: fixed | fluid

CodeProject, 503-250 Ferrand Drive Toronto Ontario, M3C 3G8 Canada +1 416-849-8900 x 100