Howdy, Stranger!

It looks like you're new here. If you want to get involved, click one of these buttons!

Categories

Welcome to the new platform of Programmer's Heaven! We apologize for the inconvenience caused, if you visited us from a broken link of the previous version. The main reason to move to a new platform is to provide more effective and collaborative experience to you all. Please feel free to experience the new platform and use its exciting features. Contact us for any issue that you need to get clarified. We are more than happy to help you.

Evaluate the expression given as a string at runtime

Hi,

I think my problem will give you food for thinking process.What i want to do is just we have to give some expression like 12+3-4 (as a string) at runtime then we have to get the result as 11.Can anyone please tell me with coding.

Comments

  • LundinLundin Posts: 3,711Member
    First you have to break the string down into items. If + and - are the only things you need to handle, it is sufficient to just store an array of positive and negative integers based on the string (+ is ignored, - gives a negative number), then loop through the array and sum it all up.
  • vibin_mdcpvibin_mdcp Posts: 60Member
    : Hi,
    :
    : I think my problem will give you food for thinking process.What i
    : want to do is just we have to give some expression like 12+3-4 (as a
    : string) at runtime then we have to get the result as 11.Can anyone
    : please tell me with coding.
    :

    Here you have to evaluate an expression given as a string(i.e const char*).
    You can use [b]"atoi()"[/b] function declared in [b][/b] to convert the string to integer. The read integer should be stored in an [b]int[/b] variable. Then you have to read the signs( i.e '+' or '-'). Read next integer using [b]atoi()[/b]. Then Operate the first read [b]int[/b] with the second and store it in the first. Continue the process to end of string. For example look following code;
Sign In or Register to comment.