Welcome to ShenZhenJia Knowledge Sharing Community for programmer and developer-Open, Learning and Share
menu search
person
Welcome To Ask or Share your Answers For Others

Categories

I want to write a C program that takes a simple math problem from the user and use different processes to solve each part, and then send the value back to the parent process. This program should not pay attention to the order of operations. For instance, if I have the problem:

3 + 4 / 7 + 4 * 2

I want my program to output the following:

Enter problem: 3 + 4 / 7 + 4 * 2
PID 20419 calculated 3+4 as 7
PID 20420 calculated 7/7 as 1
PID 20421 calculated 1+4 as 5
PID 20422 calculated 5*2 as 10
Final result: 10

I am having a bit of trouble parsing the equation I am entering. I am thinking of using the getline() method in C to help me parse input. This is what I have so far:

#include <stdio.h>

#define MAX_LINE_LENGTH 200

int main()
{
    printf("Enter equation: ");
    //getline(&buffer, &size, stdin)
    //&buffer is the address of the first character
    //&size is the address of the variable that holds the size of the input buffer
    //stdin is the input file handle
    size_t n = MAX_LINE_LENGTH;

    //Line becomes a pointer to 200 bytes of memory for you to use
    char *line = malloc(n)
    while ((getline(&line, &n, stdin)) {
        //Parse numbers and operators here
    }

    return 0;
}

Does anyone have any suggestions on how to parse the numbers and operators that I will enter from the standard input? Once I think I read in a number, operator, and a number, I would like to use fork(). Thank you all in advance.

See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
thumb_up_alt 0 like thumb_down_alt 0 dislike
222 views
Welcome To Ask or Share your Answers For Others

1 Answer

This doesn't completely answer the question, but was a fun little toy. This does not handle the final data point, and undoubtedly needs to be robustified (is "robustification" a word? It ought to be!). As an exercise for the reader, refactor the code to handle the final output, and deal with non-integer values.

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

int main(void) {
        int c;
        int last_op = 0;
        float a[2] = {0};

        while( ( c = getchar()) != EOF ) {
                if(isspace(c)) {
                        continue;
                }
                if(isdigit(c)) {
                        a[1] = 10 * a[1] + c - '0';
                } else {
                        float tmp = a[1];
                        if( last_op ) {
                                switch(last_op) {
                                case '+': tmp = a[0] + a[1]; break;
                                case '/': tmp = a[0] / a[1]; break;
                                case '*': tmp = a[0] * a[1]; break;
                                case '-': tmp = a[0] - a[1]; break;
                                default: fprintf( stderr, "invalid input: %c", last_op );
                                }
                                printf ("calculated %f %c %f = %f
", a[0], last_op, a[1], tmp);
                        }
                        a[0] = tmp;
                        a[1] = 0;
                        last_op = c;
                }
        }
}

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
thumb_up_alt 0 like thumb_down_alt 0 dislike
Welcome to ShenZhenJia Knowledge Sharing Community for programmer and developer-Open, Learning and Share

548k questions

547k answers

4 comments

86.3k users

...