console版计算器:
#define _CRT_SECURE_NO_WARNINGS // for strncpy , disable error C4996 #include <iostream> //#include <cstring> using std::cout; using std::cin; //function prototype char* extract(const char* str, size_t& index);//extract()提取圆括号包括的子字符串 double number(const char* str, size_t& index);//number()计算数值或括号中的表达式 double term(const char* str, size_t& index);//一项,考虑 * / double expr(char* str);//expr()计算表达式的值,考虑 + - void eatspaces(char* str);//eliminate blanks const size_t MAX = 100; //extract()提取圆括号包括的子字符串 char* extract(const char* str, size_t& index) { char* pstr = 0; //point a new string for return size_t numL = 0; //count of left parentheses size_t bufIndex = index; //save old index do { switch (str[index]) { case ')': if (0 == numL) { ++index; pstr = new char[index - bufIndex](); if (!pstr) throw "Memory alloction is failed."; strncpy(pstr, str + bufIndex, index - bufIndex - 1); return pstr; } else --numL; break; case '(': ++numL; break; } } while (str[index++] != '