help with code to implement a calculator....
-
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!
-
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!
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"?
-
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!
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 replacefor (i = 0; i < count; i++)
byforeach(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
-
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 replacefor (i = 0; i < count; i++)
byforeach(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
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!