#include #include #include typedef enum {ADD, SUBTRACT, MULTIPLY, DIVIDE, POWER} operation_t; typedef struct { double operand1; operation_t operation; double operand2; } expressionStruct; expressionStruct parseText(char* inputText); void performAndPrintOperation(expressionStruct *operation); /* * This is the main method for the program. It essentially causes the program to loop until the program is to exit. * Parameters: * argc(int) Not used * argv(char*) Not used * Return value: * int always returns 0. */ int main(int argc, char* argv[]) { char inputText[100]; memset(&inputText[0], '\0', sizeof(inputText)); do { printf("Enter an expression (quit to quit): "); // Read in a new string fgets(&inputText[0], sizeof(inputText), stdin); // Process it accordingly if it is not quit. Quit is the text that will cause this program to exit. if (strcmp(&inputText[0], "quit\n")!=0) { // Parse the text that was input. expressionStruct op = parseText(&inputText[0]); // Perform the operation and print the results. performAndPrintOperation(&op); } } while (strcmp(&inputText[0], "quit\n")!=0); } /* * This method will parse the input string and return a structure representing the expression supplied. * Parameters: * inputText (char*) This is the input string that was entered by the user. It is a null terminated string. If NULL, the return for the structure will be somewhat undefined. * Return value: * expressionStruct -> This is the structure which represents the pared expression that was entered. */ expressionStruct parseText(char* inputText) { expressionStruct retVal; char operation[2]; if (inputText != NULL) { sscanf(inputText, "%lf %s %lf", &retVal.operand1, &operation[0], &retVal.operand2); switch (operation[0]) { case '+': retVal.operation = ADD; break; case '-': retVal.operation = SUBTRACT; break; case '*': retVal.operation = MULTIPLY; break; case '/': retVal.operation = DIVIDE; break; case '^': retVal.operation = POWER; break; default: break; } } return retVal; } /* * This method will perform the given operation and print the results to the console. * Parameters: * expression (expressionStruct*) This is a pointer to the structure that is going to be processed. If NULL, an error is to be printed. * Return value: * None */ void performAndPrintOperation(expressionStruct *expression) { double result = 0.0; char operand = '+'; if (expression== NULL) { printf("An error occurred. The expression was NULL."); } else { switch (expression->operation) { case ADD: result = expression->operand1 + expression->operand2; operand = '+'; break; case SUBTRACT: result = expression->operand1 - expression->operand2; operand = '-'; break; case MULTIPLY: result = expression->operand1* expression->operand2; operand = '*'; break; case DIVIDE: result = expression->operand1 / expression->operand2; operand = '/'; break; case POWER: operand = '^'; result = pow(expression->operand1,expression->operand2); break; default: result = 0.0; } printf("%lg %c %lg = %lg\n", expression -> operand1, operand, expression -> operand2, result); } }