Question:
A question on data types in C programming?
Bibo
2010-08-31 08:09:58 UTC
alright, here's the thing. i have written a working c code which converts a string of mathematical expression containing characters to integers. i.e. suppose i have two arrays, one character array and one integer array. now if the user enters a mathematical expression like: 12+(45+78) my program would convert the same expression into the integer array as 12 43 40 45 43 78 41(this is necessary as a character variable can store only one character ie 2 and 3 but not 23 like an integer variable). here 43, 40 and 41 are the ascii values of '+', '(', ')'. now my program evaluates the expression. my problem is if the user enters a 'number' which corresponds to the ascii value of one of the operators, my program obviously gets confused. say 43 + 43, my program would think it as + + +. how do i avoid this. is there any way to store integers and characters at the same place without mixing up?
Four answers:
AnalProgrammer
2010-08-31 08:26:58 UTC
Always keep it simple.

Have one int array and one char array.

Your sum

12+(45+78)

will then be stored as

char operators[] = {'n', '+', '(', 'n', '+', 'n', ')')

int values[] = {12, 45, 78}



Because you are constructing these arrays in your program the 'n' characters will match the correct number of values. If not then you have a logic error.

Now you have to de-construct the arrays to do the sum.



Have fun.
?
2010-08-31 08:16:06 UTC
Using the method that you are currently using, it is always a possibility that this will happen. The int in the array will always have to be an int and that int always runs the risk of being a number that the user enters. Idealy, you would be using another language like python which can store both chars and ints in one array. but if you absolutely need to use C then I would recommend finding all of the mathematical operators and using a second array where if the value in the char array is a symbol you store the value in another char array where all numerical values are stored as zeroes or /n (null) characters. and checking both arrays in coordination with each other.
Chris C
2010-08-31 08:39:00 UTC
When I was starting programming, I remember learning RPN (reverse Polish Notation). We would use a stack to push values onto the stack, until we were done evaluating everything.

RPN for the above mathematical expression would be 12 45 78 + +



The stack was a generic stack that we built to store any type (int, float, double, and char for operations).





Here's a link discussing RPN if yo need it: http://en.wikipedia.org/wiki/Reverse_Polish_Notation
Jim
2010-09-01 21:42:29 UTC
sounds like you need to write a parser to me if you want to do this right. the front end of this needs to be a lexical analyzer which reads the input stream and feeds tokens to your parser. tokens consist of a data type (integer or operator or peren or EOF) and a value, if any.



the parser takes in the token stream and builds a syntax tree.



then something must walk the syntax tree and produce output. that is the translation phase I think, but my memory is a little fuzzy after 20 years.



c stdlib.h (or was it string.h?) has a function for doing this kind of stuff called strtok() which may or may not help.

flex and bison, anyone?



you really should jump with both feet into compiler class.


This content was originally posted on Y! Answers, a Q&A website that shut down in 2021.
Loading...