4常量表达式计算器
#include <iostream>
#include <cstdlib>
#include <cctype> //字符串判定
?
using namespace std;
const int MAX = 1024;
double operation(char *str);
char * extract(char *str, int &index)
{
??? char *pstr(nullptr);//處理字符串
??? int num(0);//記錄一下多少對括號
??? int bufindex(index);//記錄下標(biāo)
?
??? do
??? {
??????? switch (*(str + index))
??????? {
??????? case ')':
??????????? if (0 == num)
??????????? {
??????????????? ++index;
??????????????? pstr = new char[index - bufindex];
??????????????? if (!pstr)
??????????????? {
??????????????????? throw? "malloc fail";
??????????????? }
??????????????? //拷貝字符串
??????????????? strncpy_s(pstr, index - bufindex, str + bufindex, index - bufindex - 1);
??????????????? return pstr;
??????????? }
??????????? else
??????????? {
??????????????? num--;
??????????? }
??????????? break;
??????? case '(':
??????????? num++;
??????????? break;
??????? }
??? } while (*(str + index++) != '\0');
??? throw? "errorfail";
}
?
//獲得計(jì)算表達(dá)是中字符串的數(shù)字
double getNum(char *str, int &index)
{
??? double value(0.0);
?
??? if (*(str + index) == '(')
??? {
??????? char *substr(nullptr);
??????? substr = extract(str, ++index);
?
??????? value = operation(substr);
??????? delete[] substr;
?
??????? return value;
??? }
?
??? if (!isdigit(*(str + index)))
??? {
??????? char error[30] = "geterror";
??????? throw error;
??? }
?
??? //判斷數(shù)值是否是數(shù)值
??? while (isdigit(*(str + index)))
??? {
??????? value = 10 * value + (*(str + index++) - '0');
??? }
??? //帶有小數(shù)點(diǎn)時(shí)不做處理
??? if (*(str + index) != '.')
??? {
??????? return value;
??? }
??? else
??? {
??????? double decimals(1.0); //定義一個(gè)小數(shù)
??????? while (isdigit(*(str + (++index))))
??????? {
??????????? decimals /= 10;
??????????? value = value + (*(str + index) - '0') * decimals;
??????? }
??????? return value;
??? }
}
?
double term(char *str, int & index)
{
??? double value(0.0);
??? value = getNum(str, index);//獲取數(shù)據(jù)
??? while (1)
??? {
??????? if (*(str+index) == '*')
??????? {
??????????? //乘除法
??????????? value *= getNum(str, ++index);
??????? }
??????? else if (*(str + index) == '/')
??????? {
??????????? value /= getNum(str, ++index);
??????? }
??????? else
??????? {
??????????? break;
??????? }
??? }
??? return value;
}
?
double operation(char *str)
{
??? double value(0.0);
??? int index(0);
??? value += term(str, index);
??? for (;;)
??? {
??????? switch (*(str + (index++)))
??????? {
??????? case '\0':
??????????? return value;
??????????? break;
??????? case '+':
??????????? value += term(str,index);
??????????? break;
??????? case '-':
??????????? value -= term(str, index);
??????????? break;
??????? default:
??????????? break;
??????? }
??? }
}
?
void removeBlankSpace(char *str)
{
??? int i(0);
??? int j(0);
??? //*(str + i) = *(str + j++) :通過這種方式循環(huán)獲得字符串中的每個(gè)字符
??? //下面的意思是獲得字符不是'\0',也就是說沒有到達(dá)字符串末尾
??? //下面的方式是把不是空格的字符賦值給*(str+i)
??? while ((*(str + i) = *(str + j++))!= '\0')
??? {
??????? if (*(str+i) != ' ')
??????? {
??????????? i++;
??????? }
??? }
??? //兩個(gè)下標(biāo)輪替,往前移動(dòng),鏈表的算法一樣,循環(huán)向前挖
}
?
void main()
{
??? char str[MAX] = { 0 };
??? cout << "請輸入表達(dá)式";
??? cin.getline(str, MAX);
??? cout << "\n" << str << endl;
?
??? //去除空格
??? removeBlankSpace(str);
?
??? cout << str << endl;
?
??? cout << operation(str) << endl;
?
??? cin.get();
}
運(yùn)行結(jié)果:
總結(jié)
- 上一篇: 满怀可爱麻辣烫麻辣拌品牌加盟有什么优势?
- 下一篇: Git环境搭建与基本使用方法 (转自ht