Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

第六章 第四节逆波兰计算器示例代码错误 #49

Open
gaomei052 opened this issue Jan 3, 2025 · 2 comments
Open

第六章 第四节逆波兰计算器示例代码错误 #49

gaomei052 opened this issue Jan 3, 2025 · 2 comments

Comments

@gaomei052
Copy link

原代码
{ for(i=1;i<=NF;i++){ if($i ~ /^[+-]?([0-9]+[.]?[0-9]*|[.][0-9]+$)/){ stack[++top] = $i continue }else if($i == "+" && top > 1){ stack[top-1] += stack[top];top-- }else if($i == "-" && top >1){ stack[top-1] -= stack[top] top-- }else if($i == "*" && top > 1){ stack[top-1] *= stack[top];top-- }else if($i == "/" && top > 1){ stack[top-1] /= stack[top];top-- }else if($i == "^" && top > 1){ stack[top-1] ^= stack[top];top-- }else{ printf("error: cannot evaluate %s\n",$i) top = 0 next } if(top == 1){ printf("\t%.8g\n",stack[top--]) }else if(top > 1){ printf("error: too many operands\n") top = 0 } } }
更改后的代码
{ top = 0 for(i=1;i<=NF;i++){ if($i ~ /^[+-]?([0-9]+[.]?[0-9]*|[.][0-9]+$)/){ stack[++top] = $i continue }else if($i == "+" && top > 1){ stack[top-1] += stack[top];top-- }else if($i == "-" && top >1){ stack[top-1] -= stack[top] top-- }else if($i == "*" && top > 1){ stack[top-1] *= stack[top];top-- }else if($i == "/" && top > 1){ stack[top-1] /= stack[top];top-- }else if($i == "^" && top > 1){ stack[top-1] ^= stack[top];top-- }else{ printf("error: cannot evaluate %s\n",$i) top = 0 next } if(top == 1 && i == NF){ printf("\t%.8g\n",stack[top]) }else if(top > 1 && i == NF){ printf("error: too many operands\n") top = 0 } } }

@wuzhouhui
Copy link
Owner

wuzhouhui commented Jan 4, 2025 via email

@gaomei052
Copy link
Author

已提交pr

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants