Skip to content
  • Categories
  • Recent
  • Tags
  • Popular
  • World
  • Users
  • Groups
Skins
  • Light
  • Cerulean
  • Cosmo
  • Flatly
  • Journal
  • Litera
  • Lumen
  • Lux
  • Materia
  • Minty
  • Morph
  • Pulse
  • Sandstone
  • Simplex
  • Sketchy
  • Spacelab
  • United
  • Yeti
  • Zephyr
  • Dark
  • Cyborg
  • Darkly
  • Quartz
  • Slate
  • Solar
  • Superhero
  • Vapor

  • Default (No Skin)
  • No Skin
Collapse
Code Project
  1. Home
  2. General Programming
  3. C#
  4. help with code to implement a calculator....

help with code to implement a calculator....

Scheduled Pinned Locked Moved C#
helpcsharpbeta-testingcode-review
4 Posts 3 Posters 0 Views 1 Watching
  • Oldest to Newest
  • Newest to Oldest
  • Most Votes
Reply
  • Reply as topic
Log in to reply
This topic has been deleted. Only users with topic management privileges can see it.
  • M Offline
    M Offline
    MinhajuddinK
    wrote on last edited by
    #1

    Hi all, I am new to C#.... I thought that implementing a simple calculator would help me understand the language... I have come up with a code.... It would be great if the gurus here give me feedback on this... using System; using System.Collections.Generic; using System.Text; namespace Calculator { class compute { private string[] inputNumbers; public string computeResult(string inputExpression) { string result = "There was an error while processing!"; inputExpression = inputExpression.Replace("+", "+A"); inputExpression = inputExpression.Replace("-", "-S"); inputExpression = inputExpression.Replace("*", "-M"); inputExpression = inputExpression.Replace("/", "/D"); if (inputExpression.Substring(0, 1) != "-" && inputExpression.Substring(0, 1) != "+") inputExpression = "A" + inputExpression; else inputExpression = inputExpression.Substring(1); inputNumbers = inputExpression.Split('+', '-', '*', '/'); decimal output=0; int i; int count = inputNumbers.Length; for (i = 0; i < count; i++) { string calc = ""; inputNumbers[i] = inputNumbers[i].Trim(); decimal temp; if (!decimal.TryParse(inputNumbers[i].Substring(1),out temp)) break; calc = inputNumbers[i].Substring(0,1); switch(calc) { case "A": output = output + temp; break; case "S": output = output - temp; break; case "M": output = output * temp; break; case "D": output = output / temp; break; } } if(i==count) result = output.ToString(); return result; } } }

    You don't have to be AFRAID!

    L L 2 Replies Last reply
    0
    • M MinhajuddinK

      Hi all, I am new to C#.... I thought that implementing a simple calculator would help me understand the language... I have come up with a code.... It would be great if the gurus here give me feedback on this... using System; using System.Collections.Generic; using System.Text; namespace Calculator { class compute { private string[] inputNumbers; public string computeResult(string inputExpression) { string result = "There was an error while processing!"; inputExpression = inputExpression.Replace("+", "+A"); inputExpression = inputExpression.Replace("-", "-S"); inputExpression = inputExpression.Replace("*", "-M"); inputExpression = inputExpression.Replace("/", "/D"); if (inputExpression.Substring(0, 1) != "-" && inputExpression.Substring(0, 1) != "+") inputExpression = "A" + inputExpression; else inputExpression = inputExpression.Substring(1); inputNumbers = inputExpression.Split('+', '-', '*', '/'); decimal output=0; int i; int count = inputNumbers.Length; for (i = 0; i < count; i++) { string calc = ""; inputNumbers[i] = inputNumbers[i].Trim(); decimal temp; if (!decimal.TryParse(inputNumbers[i].Substring(1),out temp)) break; calc = inputNumbers[i].Substring(0,1); switch(calc) { case "A": output = output + temp; break; case "S": output = output - temp; break; case "M": output = output * temp; break; case "D": output = output / temp; break; } } if(i==count) result = output.ToString(); return result; } } }

      You don't have to be AFRAID!

      L Offline
      L Offline
      led mike
      wrote on last edited by
      #2

      Minhaj.Net wrote:

      I have come up with a code.... It would be great if the gurus here give me feedback on this...

      Yes that does appear to be code Did you read the first message in the forum titled, "How to get an answer to your question"?

      1 Reply Last reply
      0
      • M MinhajuddinK

        Hi all, I am new to C#.... I thought that implementing a simple calculator would help me understand the language... I have come up with a code.... It would be great if the gurus here give me feedback on this... using System; using System.Collections.Generic; using System.Text; namespace Calculator { class compute { private string[] inputNumbers; public string computeResult(string inputExpression) { string result = "There was an error while processing!"; inputExpression = inputExpression.Replace("+", "+A"); inputExpression = inputExpression.Replace("-", "-S"); inputExpression = inputExpression.Replace("*", "-M"); inputExpression = inputExpression.Replace("/", "/D"); if (inputExpression.Substring(0, 1) != "-" && inputExpression.Substring(0, 1) != "+") inputExpression = "A" + inputExpression; else inputExpression = inputExpression.Substring(1); inputNumbers = inputExpression.Split('+', '-', '*', '/'); decimal output=0; int i; int count = inputNumbers.Length; for (i = 0; i < count; i++) { string calc = ""; inputNumbers[i] = inputNumbers[i].Trim(); decimal temp; if (!decimal.TryParse(inputNumbers[i].Substring(1),out temp)) break; calc = inputNumbers[i].Substring(0,1); switch(calc) { case "A": output = output + temp; break; case "S": output = output - temp; break; case "M": output = output * temp; break; case "D": output = output / temp; break; } } if(i==count) result = output.ToString(); return result; } } }

        You don't have to be AFRAID!

        L Offline
        L Offline
        Luc Pattyn
        wrote on last edited by
        #3

        Hi, I think this will work most of the time; I have several comments though: 1. please post multi-line code in PRE tags instead of CODE tags; that would preserve indentation. 2. your error handling is insufficient; at the bare minimum you should put all code inside a try-catch construct. 3. your handling of operators is quite creative; replacing them to keep a trace when they tend to disappear by the Split()... the line Replace("*", "-M"); is probably not what you intended, but it does nof affect the outcome. You don't really need the letters ASMD, you could have duplkcated the operators instead. 4. there are some functional shortcomings: - you only handle diadic operators (that's having two operands); what if you need a monadic minus, as in "-4 * -3" ? - you fail to obey the operator priorities: "2+3*4" should give 14, not 20. the right way to fix this is by using a stack machine, i.e. postpone the operator (+) and push the operands, until another operator of same or lower priority is seen (or end of expression), then pop operands, perform operation and push result. 5. programming details: - you could replace for (i = 0; i < count; i++) by foreach(string inputNumber in inputNumbers) - to get some character from a string, you don't need Substring(x,1); you can index as in inputNumbers[i][0] which gives the leading char (unless length is 0) Keep up the good work! May I suggest you read several CodeProject articles... :)

        Luc Pattyn [Forum Guidelines] [My Articles]


        this weeks tips: - make Visual display line numbers: Tools/Options/TextEditor/... - show exceptions with ToString() to see all information - before you ask a question here, search CodeProject, then Google


        M 1 Reply Last reply
        0
        • L Luc Pattyn

          Hi, I think this will work most of the time; I have several comments though: 1. please post multi-line code in PRE tags instead of CODE tags; that would preserve indentation. 2. your error handling is insufficient; at the bare minimum you should put all code inside a try-catch construct. 3. your handling of operators is quite creative; replacing them to keep a trace when they tend to disappear by the Split()... the line Replace("*", "-M"); is probably not what you intended, but it does nof affect the outcome. You don't really need the letters ASMD, you could have duplkcated the operators instead. 4. there are some functional shortcomings: - you only handle diadic operators (that's having two operands); what if you need a monadic minus, as in "-4 * -3" ? - you fail to obey the operator priorities: "2+3*4" should give 14, not 20. the right way to fix this is by using a stack machine, i.e. postpone the operator (+) and push the operands, until another operator of same or lower priority is seen (or end of expression), then pop operands, perform operation and push result. 5. programming details: - you could replace for (i = 0; i < count; i++) by foreach(string inputNumber in inputNumbers) - to get some character from a string, you don't need Substring(x,1); you can index as in inputNumbers[i][0] which gives the leading char (unless length is 0) Keep up the good work! May I suggest you read several CodeProject articles... :)

          Luc Pattyn [Forum Guidelines] [My Articles]


          this weeks tips: - make Visual display line numbers: Tools/Options/TextEditor/... - show exceptions with ToString() to see all information - before you ask a question here, search CodeProject, then Google


          M Offline
          M Offline
          MinhajuddinK
          wrote on last edited by
          #4

          Thank you very much Luc Pattyn! that will really help me.... I would like to go through the articles here.... But with so many of them, I find it a bit difficult to chose.... Thanks for the advice :)

          You don't have to be AFRAID!

          1 Reply Last reply
          0
          Reply
          • Reply as topic
          Log in to reply
          • Oldest to Newest
          • Newest to Oldest
          • Most Votes


          • Login

          • Don't have an account? Register

          • Login or register to search.
          • First post
            Last post
          0
          • Categories
          • Recent
          • Tags
          • Popular
          • World
          • Users
          • Groups