最美情侣中文字幕电影,在线麻豆精品传媒,在线网站高清黄,久久黄色视频

歡迎光臨散文網 會員登陸 & 注冊

史上最強最細膩的linux嵌入式C語言學習教程【李慧芹老師】

2023-07-26 22:58 作者:fermatas  | 我要投稿

?
7.棧和隊列的應用-計算器實例2 P102 - 30:34
?

101 - 102 課堂代碼

``` C

#include <stdio.h>

#include <stdlib.h>

#include <ctype.h>

#include <string.h>


#include "sqstack.h"


static void compute(sqstack *snum, datatype *op) {

datatype n1, n2, n;

st_pop(snum, &n2);

st_pop(snum, &n1);

switch(*op) {

case '+':

n = n1 + n2;

break;

case '-':

n = n1 - n2;

break;

case '*':

n = n1 * n2;

break;

case '/':

n = n1 / n2;

break;

default:

exit(1);

}

st_push(snum, &n);

}


void deal_bracket(sqstack *snum, sqstack *sop) {

datatype old_op;

st_top(sop, &old_op);

while(old_op != '(') {

st_pop(sop, &old_op);

compute(snum, &old_op);

st_top(sop, &old_op);

}

st_pop(sop, &old_op);

}


static int get_pri(int op) {

switch(op) {

case '(':

return 0;

case '+':

case '-':

return 1;

case '*':

case '/':

return 2;

}

}


void deal_op(sqstack *snum, sqstack *sop, int op) {

datatype old_op;

if(st_isempty(sop) || op == '(') {

st_push(sop, &op);

return;

}


st_top(sop, &old_op);

if(get_pri(op) > get_pri(old_op)) {

st_push(sop, &op);

return;

}

while(get_pri(op) <= get_pri(old_op)) {

st_pop(sop, &old_op);

compute(snum, &old_op);

if(st_isempty(sop))

break;

st_top(sop, &old_op);

}

st_push(sop, &op);

}


int main() {

int i = 0;

char str[] = "(11+3)*2-5";

sqstack *snum, *sop;

int value = 0;

int flag = 0;


snum = st_create();

if(snum == NULL)

exit(1);


sop = st_create();

if(sop == NULL)?

exit(1);

while(str[i] != '\0') {

if(isdigit(str[i])) {

value = value * 10 + str[i] - '0';

flag = 1;

} else {

if(flag) {

st_push(snum, &value);

flag = 0;

value = 0;

}

if(str[i] == ')') {

deal_bracket(snum, sop);

} else {

deal_op(snum, sop, str[i]);

}

}

i++;

}

if(flag)

st_push(snum, &value);


int old_op;

while(!st_isempty(sop)) {

st_pop(sop, &old_op);

compute(snum, &old_op);

st_top(sop, &old_op);

}

int res;

st_top(snum, &res);

printf("result: %d\n", res);


st_destory(snum);

st_destory(sop);

exit(0);

}

```

史上最強最細膩的linux嵌入式C語言學習教程【李慧芹老師】的評論 (共 條)

分享到微博請遵守國家法律
库车县| 密山市| 团风县| 南安市| 武威市| 衡南县| 竹溪县| 肥乡县| 昌邑市| 荔浦县| 探索| 玛沁县| 苏尼特左旗| 大洼县| 加查县| 潼关县| 军事| 建始县| 樟树市| 佛学| 贵州省| 梅河口市| 两当县| 安丘市| 舒兰市| 苍南县| 将乐县| 防城港市| 彝良县| 安远县| 临沧市| 东乌珠穆沁旗| 山阴县| 衡阳县| 乳源| 平舆县| 巍山| 凤城市| 辽中县| 平泉县| 雅江县|