Cpp程序例子
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++] != '\0'); throw "Ran off the end of expression, must be bad input."; } ///////////////////////////////////////////////////////////////// // To recognize an expression in parentheses or a number in a string. // return: the value of this expession or number. // @Param // str : the expression of input // index : the index of str ///////////////////////////////////////////////////////////////// double number(const char* str, size_t& index) { double value = 0.0; if (str[index] == '(') { char* psubs = 0; psubs = extract(str, ++index); value = expr(psubs); delete[] psubs; return value; } if (!isdigit(str[index])) { char szMsg[31] = "Invalid character in number: "; strncat(szMsg, str + index, 1); throw szMsg; } while (isdigit(str[index])) { value = value * 10 + (str[index++] - '0'); } if ('.' != str[index]) return value; double factor = 1.0; while (isdigit(str[++index])) { factor *= 0.1; value = value + (str[index] - '0')*factor; } return value; } // To get the value of term(项) double term(const char* str, size_t& index) { double value = 0.0; value = number(str, index); while (true) { if ('*' == str[index]) value *= number(str, ++index); else if ('/' == str[index]) value /= number(str, ++index); else break; } return value; } // To calculate an arithmetic expression double expr(char* str) { double value = 0.0; size_t index = 0; value = term(str, index);//Get first term for (;;) { switch (str[index++]) { case '\0': return value; case '+': value += term(str, index); break; case '-': value -= term(str, index); break; default: char szMsg[38] = "Expression evaluate error. Find: "; strncat(szMsg, str + index - 1, 1); throw szMsg; break; } } return value; } // 去掉空格 void eatspaces(char* str) { int i, j; i = j = 0; while ((str[i] = str[j]) != '\0') { ++j; if (str[i] != ' ' && str[i] != '\t' ) ++i; } } int main() { try { char buffer[MAX] = { 0 }; std::cout << "Welcome to your friendly calcultor.\n" << "Please enter an expression, or a empty line for quit.\n"; for (;;) { cin.getline(buffer, sizeof(buffer)); eatspaces(buffer); if (!buffer[0]) { cout << "You quitted!\n"; return 0; } cout << "\t= " << expr(buffer) << '\n'; } } catch (std::string& s) { std::cerr << s.c_str() << '\n'; } catch (const char* s) { std::cerr << s << '\n'; } catch (...) { std::cerr << "Unknown error.\n"; } system("pause"); return 0; }
常记溪亭日暮,沉醉不知归路。兴尽晚回舟,误入藕花深处。争渡,争渡,惊起一滩鸥鹭。
昨夜雨疏风骤,浓睡不消残酒。试问卷帘人,却道海棠依旧。知否?知否?应是绿肥红瘦。