日韩性视频-久久久蜜桃-www中文字幕-在线中文字幕av-亚洲欧美一区二区三区四区-撸久久-香蕉视频一区-久久无码精品丰满人妻-国产高潮av-激情福利社-日韩av网址大全-国产精品久久999-日本五十路在线-性欧美在线-久久99精品波多结衣一区-男女午夜免费视频-黑人极品ⅴideos精品欧美棵-人人妻人人澡人人爽精品欧美一区-日韩一区在线看-欧美a级在线免费观看

歡迎訪問 生活随笔!

生活随笔

當(dāng)前位置: 首頁 > 编程资源 > 编程问答 >内容正文

编程问答

4常量表达式计算器

發(fā)布時(shí)間:2024/9/27 编程问答 33 豆豆
生活随笔 收集整理的這篇文章主要介紹了 4常量表达式计算器 小編覺得挺不錯(cuò)的,現(xiàn)在分享給大家,幫大家做個(gè)參考.


#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é)

以上是生活随笔為你收集整理的4常量表达式计算器的全部內(nèi)容,希望文章能夠幫你解決所遇到的問題。

如果覺得生活随笔網(wǎng)站內(nèi)容還不錯(cuò),歡迎將生活随笔推薦給好友。