Thread: Help on a program that solves math equations!

  1. #1
    Registered User
    Join Date
    Apr 2013
    Posts
    2

    Help on a program that solves math equations!

    I wrote a program that solves an equation of two numbers, but in addition to that, I want it to be able to continue to solve longer equations. Ex: ( solves 2 * 4, or 2 * 4 - 5).
    I want to put this part of the program's result into a variable and go from there. How do I place the result of the calculation into a variable, and where would it go?
    Code:
    #include <stdio.h>
    #include <string.h>
    
    
    int main(void) {
       int a;
       scanf("%d", &a);
       char s[2];
       scanf("%s", s);
       int b;
       scanf("%d", &b);
    
    
       if (strcmp(s, "+") == 0) {
          printf("%d", a + b);
          return 1;
       }
       if (strcmp(s, "-") == 0) {
          printf("%d", a - b);
          return 1;
       }
       if (strcmp(s, "*") == 0) {
          printf("%d", a * b);
          return 1;
       }
       if (strcmp(s, "/") == 0) {
          printf("%d", a / b);
          return 1;
       }
       return 0;
    }

  2. #2
    Registered User
    Join Date
    Jan 2013
    Posts
    24
    read the equation in as a string and have your program solve it in steps starting with the ')' that has priority
    Last edited by jamesedmonds; 04-30-2013 at 01:42 PM.

  3. #3
    Registered User
    Join Date
    Mar 2011
    Posts
    546
    parsing arbitrary length expressions with proper precedence rules is quite a bit more complicated than comparing strings. but as a start, if you want to skip the operator precedence and just evaluate expressions left to right that are of the form <term> <op> <term> op <term> ....
    do something like this

    [code]
    read number and store it in variable 'result'
    loop
    read next operator. if EOF, you are done
    read number and store it in variable 'val'. if EOF error
    execute the operation result <op> val, store in result
    end loop

  4. #4
    Registered User
    Join Date
    Apr 2013
    Posts
    2
    thanks for breaking it down a bit, dmh. i think i've got it now

  5. #5
    Registered User
    Join Date
    May 2012
    Posts
    505
    Go to my website and look around the MiniBasic pages. There's also a book.

    It explains how to set about the problem of reading arbitrarily complex equations.
    I'm the author of MiniBasic: How to write a script interpreter and Basic Algorithms
    Visit my website for lots of associated C programming resources.
    https://github.com/MalcolmMcLean


Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Math equations help....with C code
    By lilbo4231 in forum C Programming
    Replies: 1
    Last Post: 03-08-2011, 04:49 PM
  2. Math equations not working
    By MaaaTtY in forum C++ Programming
    Replies: 3
    Last Post: 03-09-2009, 04:40 AM
  3. Rendering math equations
    By glo in forum Game Programming
    Replies: 7
    Last Post: 02-01-2009, 04:21 PM
  4. Math equations
    By C-Struggler in forum C Programming
    Replies: 6
    Last Post: 04-05-2003, 08:21 AM
  5. Replies: 7
    Last Post: 02-12-2003, 11:43 PM