实现带有括号的四则运算

来源:百度知道 编辑:UC知道 时间:2024/05/24 14:28:53
功能要求: 输入一个表达式,计算出其正确结果。
例如:输入:123+213-67*34+345/23*45*(34+34-345+245+567)回车
计算结果为:359183

C++的,用VisualC++2005运行。
// EX6_09Extended.CPP
// A program to implement a calculator accepting parentheses

#include <iostream> // For stream input/output
#include <cstdlib> // For the exit() function
#include <cctype> // For the isdigit() function
#include <cstring> // For the strcpy() function
using std::cin;
using std::cout;
using std::endl;

void eatspaces(char* str); // Function to eliminate blanks
double expr(char* str); // Function evaluating an expression
double term(char* str, int& index); // Function analyzing a term
double number(char* str, int& index); // Function to recognize a number
char* extract(char* str, int& index); // Function to extract a substring
const int MAX = 80; // Maximum expression length,