UPSB v4
Off-topic / Cprogramming help
-
Date: Tue, Feb 14 2012 01:35:19
If anyone knows cprogramming could you help me out with my project. This is the question The C calculator program allows the user to enter an equation that consists of numerical values (digits) and arithmetic operators (+, -, *, /) in a valid format and the “=” sign to terminate the equation so as for the program to compute for an answer. The program will examine the equation and reject an invalid input by printing “ERROR!” The Backspace key can be used to correct input errors. The calculator must consider the precedence of the arithmetic operators when computing the equation. The answer is displayed right after the ‘=’ sign. Note that the numerical result is printed with an accuracy of decimal places only if the answer is a decimal number. The program repeats the commutating service until an empty equation is entered.The C technologies used for the project are I/O, arithmetic operators, control structures,arrays and function; in particular array element manipulation, conversion and calculation. Try to use basic C knowledge to write the program and do not use Linked Stacks which you may not familiar with. -- I can't figure out how to do the calculation part. I was thinking of using a float array to store all the inputs then using a nested while loop to do all the calculations but I don't know how to the calculations. handling all the * and / first but I don't it won't work for the backspace and I'm not sure how to print out the accurate number of decimals. Would this work or is there an easier way to answer the question? I'm using visual studios by the way
-
Date: Tue, Feb 14 2012 01:49:51
store the equation as a binary tree, higher priority operators lower on the tree, lower priority higher up. do a DFS on the tree to calculate the answer http://en.wikipedia.org/wiki/Binary_expression_tree
-
Date: Tue, Feb 14 2012 03:37:18
I haven't learnt binary trees yet so I can't really understand how to store the eqn into the tree :/
-
Date: Tue, Feb 14 2012 09:31:01
When simply typing in the console you can backspace errors anyway. If you want accuracy on decimal sums: Multiply out all the floating point numbers (by 10, 100, 1000) the user enters until they are integers. Do all the maths Reduce all the numbers by the factor of 10 you increased them by to print the answer. Remember to not allow / 0 ;)
-
Date: Wed, Feb 15 2012 09:40:44
Hippo2626 wrote: ...do not use Linked Stacks...
Ouch. Assuming this just means "stacks" (I assume it to mean a stack implemented through a linked list but I've never heard the term). Not sure why they don't want to use the most popular and elegant method to solve the problem. EDIT: Says that you can use arrays so you could just try to implement a stack as an array. Look up the shunting yard algorithm to convert eqns to reverse polish and then solving from there is incredibly easy. -
Date: Fri, Feb 17 2012 11:26:22
thanks Zombo, Mats and Timbo! :D I almost finished it already. Really appreciate it
-
Date: Fri, Feb 17 2012 14:14:54