Click here to Skip to main content
15,885,027 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
can i have a simple sample program that lets the user input a function then evaluates the function then gives the output value.
the values of the variables are pre-defined.

lets say i have this code that accepts int values from the user

C
#include<stdio.h>
main
{
int A,B,X,VAL;
printf("enter a number:");
scanf("%d",&A);

printf("enter a number:");
scanf("%d",&B);

printf("enter a number:");
scanf("%d",&X);

Val= (A * X) + B;

printf("the value of the function is %d",Val);
}


now I need a really simple code in which the user this time will be the one to input the function. my knowledge on C is that wide. I've never saw a program that asks the user to input a function instead a value so this the first time I've encountered this sort of problem.

Im just going to use the sample code on understanding how a program like that is done so I can proceed with the machine problem our teacher tasked us to do.


thank you :)))
Posted
Updated 16-Nov-14 22:32pm
v2
Comments
Jochen Arndt 17-Nov-14 4:35am    
You are asking for a mathematical expression parser. Searching for that definition you will find some including sources. But such parsers are usually complex and you might have problems understanding them as a beginner.

I don't think that your teacher has such parsers in mind. The task should be related to the current course topics. So you should find out what your teacher may expect.

That is not a trivial task because C is not an interpreted language. While this operation is feasible in scripting and interpreted languages (like Bash, Java and so on), C requires that the program is compiled and linked before executing it.

So, it is not possible. It is possible to use some C parser and code manually the interprete, but consider that it is a task as difficult as writing a simple compiler (still a level higher than the averageprogrammer).

Hope to be of help,
Denis
 
Share this answer
 
Comments
den2k88 17-Nov-14 4:42am    
I comment myself: I though "function" as "subroutine that returns a value" not function as the mathematical element. Sorry!
Welcome in the world of mathematics expression parser. You may use the Shunting-yard algorithm[^].
 
Share this answer
 
to all the people who answered my questions, thank you.
basically the machine problem we were tasked to make was about the "flip flop design"

im having problems with the 5th input that the users need to provide which is the function. well basically a mathematical expression (sorry about that :) )
unfortunately, im not yet taking Java Classes and C++. i only know C so thats why its hard to find a way to implement that kind of evaluation.


I have another question though. same problem but this time the IDE that I will use is Visual Basic 6. is that possible or Java is the best programming language to use for this problem?
 
Share this answer
 
Comments
den2k88 17-Nov-14 5:15am    
It is not a question of programming language: there are math function parsers for every language invented. The choice of the language and IDE is a matter of your liking.

I would suggest using C, C++ or Java because VB6 is "dead" (almost, there are a number of companies including mine where it is still widely used for production code) but it has some limitations that mede sense 15 years ago (and many not even then). Also your acquired knowledge will be more useful. Still, the quickness of VB6 for scrap projects is unmatched IMHO.
Reuben Cabrera 17-Nov-14 5:24am    
so I have no other choice but to go with C. anyway ill come back here after I reached the problem of my code. thank you for your help :)

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900