Hi im using turbo C++ compiler and im currently learning C.
Iand have an assignment that i need to make a program that solves mathematical problems.
Well first is that. the user would have to input a problem in whichever way the user wants. like this example: (5*2)+2-2/2 it could be random arithemtic symbols like "+" first or "-" first.

and then the program would compute it and then it would ouput its answer in individual answers. like this:
5*2 = 10
10+2 = 12
12- 2 = 10
10/2 = 5

I need help on how to do this. I was thinking of coding the program manually so i did it. but then i thought what if the user inputs a 2 digit number or a 3 digit number? how can i make this work? I need to pass this tomorrow. aand i dont know what to do. So please help me.

Recommended Answers

All 5 Replies

That's somewhat complex for someone learning C. An algorithm I'd use for parsing these kind of formula's would be the Shunting-yard algorithm by Dijkstra. The article contains example code for C too.

Yeah that's what i thought too. I managed to make one but it can only hold 1 digit numbers. without open and close parentheses. like a normal problem: 2+2+2+2+2
But i'll try something else.

Thanks though! :D

but then i thought what if the user inputs a 2 digit number or a 3 digit number?

You'd need to extract tokens rather than characters. For example:

#include <ctype.h>
#include <stdio.h>
#include <string.h>

typedef enum token { UNKNOWN, VALUE, OPERATOR, VARIABLE } token_t;

token_t next_token(char buf[], size_t size)
{
    const char *operators = "+-*/()";
    token_t type = UNKNOWN;
    int ch, i = 0;

    while (i < size - 1 && (ch = getc(stdin)) != EOF && ch != '\n') {
        if (isspace(ch))
            continue;

        /* Ignore negative values for brevity */
        if (isdigit(ch)) {
            if (type == UNKNOWN)
                type = VALUE;
            else if (type != VALUE) {
                ungetc(ch, stdin);
                break;
            }

            buf[i++] = (char)ch;
        }
        else if (strchr(operators, ch)) {
            if (type == UNKNOWN)
                type = OPERATOR;
            else {
                ungetc(ch, stdin);
                break;
            }

            /* Only allow single character operators for brevity */
            buf[i++] = (char)ch;
            break;
        }
        else {
            if (type == UNKNOWN)
                type = VARIABLE;
            else if (type != VARIABLE) {
                ungetc(ch, stdin);
                break;
            }

            /* Use VARIABLE as a catch-all */
            buf[i++] = (char)ch;
        }
    }

    buf[i] = '\0';

    return type;
}

int main(void)
{
    char buf[BUFSIZ];
    token_t type;

    while ((type = next_token(buf, sizeof buf)) != UNKNOWN) {
        switch (type) {
        case VALUE:    fputs("VALUE:    ", stdout); break;
        case OPERATOR: fputs("OPERATOR: ", stdout); break;
        case VARIABLE: fputs("VARIABLE: ", stdout); break;
        }

        printf("'%s'\n", buf);
    }

    return 0;
}

Wow! Thanks Dude. The program was very helpful. ill make my way around here. THANKS

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.