Converting data to code [closed] - c

Closed. This question needs to be more focused. It is not currently accepting answers.
Want to improve this question? Update the question so it focuses on one problem only by editing this post.
Closed 4 years ago.
Improve this question
In C, I'd like to make a macro called TEST() that takes a valid C arithmetic expression and prints and evaluates it. So as an example, if I were to give it TEST(5*2+3) it would print to console 5*2+3 = 13. Unfortunately, I don't know how to convert an expression to a string nor do I know how to take a given string and evaluate it as code. How would I do this?

You can use the stringification operator to turn the argument into a string, then expand into a printf() call that prints the string and the result. My code assumes that the expression is always an int.
#define TEST(EXP) printf("%s = %d\n", #EXP, (EXP))

For the parsing part of your question:
The Shunting-yard algorithm is what you're after.
It is the most common way (that I know of) to convert a mathematical expression represented by a string of characters into a postfixed version(operators come after the operands instead of between), which is much easier to interpret with code.
Good luck!

Related

Why and when to use pointers in C? [closed]

Closed. This question needs to be more focused. It is not currently accepting answers.
Want to improve this question? Update the question so it focuses on one problem only by editing this post.
Closed 9 months ago.
Improve this question
I'm still very new to C, have only been learning for about a week at this point, but something I don't get is the use of pointers. I get how pointers work and I know how to use them, but I don't see why or when they should be used. Can someone please explain this to me?
It is enough to mention that arrays used in expressions with very rare exceptions are implicitly converted to pointers to their first elements.
For example if you will write the expression
arr[i]
where arr is the array name then even in this expression the array is converted implicitly to a pointer to its first element.
Or another simple example: all C string functions deal with pointers of the type char * because passed arrays again are implicitly converted to pointers to their first elements.
Think about how to write a general sort function for arrays of any types.

How to find a matrix declaration pattern in C [closed]

Closed. This question needs to be more focused. It is not currently accepting answers.
Want to improve this question? Update the question so it focuses on one problem only by editing this post.
Closed 5 years ago.
Improve this question
I'm building a two-pass assembler in C.
A part of its job is to be able to work on matrices.
Let's say that there is the following line:
mov m[r2][r5], XYZ
mov is the operation.
and m[r2][r5] and XYZ are the operands.
I need to find out if an operand is a matrix. and get the:
1. matrix name.
2. row.
3. column.
How is it possible?
Tried to use sscanf without any success.
Thanks in advance!
Unfortunately writing an assembler is not as easy as using the scanf. Simplifying: You need to divide the input stream into the tokens, then you need to parse it and build the semantic tree, then you need to do the semantic analysis, reduce the tree (by evaluating constant expressions, finding addresses etc etc), and eventually generate the machine code.

Plotting with C [closed]

Closed. This question needs details or clarity. It is not currently accepting answers.
Want to improve this question? Add details and clarify the problem by editing this post.
Closed 8 years ago.
Improve this question
I am working on a program plot.c to plot a function f(t). I can't use nested For loops unfortunately which make this harder for me.For example a function like : f(t)=t^2-4t+5.
The values of t will be between two values specified as low and high in the program. For each value of t, i want to store an asterisk in the element of a string (i.e. an array of chars) corresponding to the function value f(t), while all leading elements before the asterisk are blank.
This is of course assuming that the f(t) values are rounded to integers.In terms of the array in C, its size could be a variable. For instance:
int m=3*6;
char ex[m];
Here is what the outputs are supposed to look like :
If you have integer values, you could abuse printf format specifications.
If you want to print a * preceeded by n spaces, you could use something like:
printf("%*s\n", n, "*");
Bear in mind tha tif this is for a class assignment, you also need to be able to explain why this works, but the relevant section should be in the man page for printf.

Printf its variations and its prototype understanding [closed]

Closed. This question needs details or clarity. It is not currently accepting answers.
Want to improve this question? Add details and clarify the problem by editing this post.
Closed 9 years ago.
Improve this question
Can anybody explain the variations in printf syntax..i am confused in some of it
like
printf(5+"hello my friend");//i have problem this addition of 5
printf("hello""my""friend");//i have problem with double apostrophe
what kind of printf prototype do these follows ?
Is this have anything to do with dynamic linking?
Can anybody show some other weird printfs and explain them.
A string in C is accessed via a pointer to a char (see H2CO3's comment for a more precise definition). If you add 5 to a pointer to a char, you start the string 5 characters later. So 5+"hello my friend" points to " my friend", skipping "hello".
When a C compiler sees two strings with nothing (except possibly whitespace) in between, it treats them as a single string. This makes it easier to break long strings in multiple lines.
So "hello""my""friend" compiles into exactly the same thing as "hellomyfriend" or
"hello"
"my"
"friend"
None of these has anything to do with printf, and a lot to do with strings.

Evaluate Mathematical Function from String [closed]

Closed. This question needs to be more focused. It is not currently accepting answers.
Want to improve this question? Update the question so it focuses on one problem only by editing this post.
Closed 7 years ago.
Improve this question
Can you give me some ideas about how can I make a simple mathematical expression parser in C?
User enters a mathematical function in a string and from the string I want to create the function in C.
eg. x + sin(2*x)
-> return x + sin(2x);
Thanks in advance.
You can parse the expression based "Shunting-Yard Algorithm" http://en.wikipedia.org/wiki/Shunting-yard_algorithm. You will need to extend to handle the function calls such as sin, cos etc...
This is not a simple thing to do at all, in face, it's a hard thing. You need a full grammar parser, combined with pre-defined constants/functions (sin, log, pi, etc).
If you have no extensive previous experience with C I would disrecommend doing this, but if you really want to do this look at recursive descent parsing which is arguably the easiest way to do this (without putting a burden on the user, like reverse polish notation).
Last but not least you say you want to create a C function from the user-generated input. This is almost always a wrong thing to do - generating code from user input, instead the easiest approach is pre-processing to create a intermediate representation that can be efficiently executed.
Writing an expression parser and evaluator is one of the usual examples used when discussions parser writing techniques.
For example you could look the documentation for flex/bison or lex/yacc. That will have examples of constructing parsers/expression evaluators.
One way to do it is to use reverse polish notation for the expressions and a stack for the operands.
Some quick pseudo-code:
if element is operand
push in stack
else if element is operation
pop last 2 elements
perform operation
push result in stack
Repeat till end of expression. Final result is the only element in stack.

Resources