Jump to content

How to input multiplications in C using scanf() or similar library functions

- - - - -

  • Please log in to reply
1 reply to this topic

#1
Dman

Dman

    Newbie

  • Members
  • PipPip
  • 18 posts
I tried using scanf() making as inputs integers with %d, characters with %c and etc.
But now I have a serious question as a beginner in programming in C...
That question is this:
I wonder if there is any possible way which allows me to input sums of integers while some of the integers are multiplications of other integers.
Without multiplications I used scanf() like this :

printf("Type an equation using only + or - for arithmetic equations including up to four numbers:");

scanf("%d %c %d %c %d %c %d", &a, &s1, &b, &s2, &c, &s3, &d);
That way scanf() reads the numbers and the coresponding symbols for the sum and afterwards I use them in my [if()]s in finding the sum which are in the rest of the program.
Is there any way I can use a library function in order in place of either a, b, c or d I can enter a multiplication like 3*5 instead of an integer like 9?
Thanks in advance for the help.

Edited by Dman, 30 December 2011 - 12:27 PM.


#2
Alexander

Alexander

    It's Science!

  • Moderators
  • 4,118 posts
  • Location:Vancouver, Eh! Cleverness: 200
You would probably get away with a solution encompassing a fixed number of operations. You probably would run in to issues with orders of operations later on.

It is probably easier to write a parser program to tokenize (split) each number and operation on to a stack and step through it to create a result.

Possibly a good explanation is done here, with example C source at the end: Simple calculator Your goal would be to try to make this from the ground up as much as possible.

Library wise there are a few out there for C notably GNU's libmatheval.
Be sure to read the updated FAQ! || Health is achieved through the same 10,000 steps.
If a suggested code/method fails, informing us is less important than telling us why or what errors occurred.




1 user(s) are reading this topic

0 members, 1 guests, 0 anonymous users