Updated: 在下搞错本人在 GitHub 上的 solution 提交历史了,现重新提交并更新本问题。
题目出自 K&R 中的 Exercise 4-3:
Given the basic framework, it's straightforward to extend the calculator. Add the modulus ( % ) operator and provisions for negative numbers.
但是,在本人自认为理论上应该正确的 solution, 还是 Bob Wightman 的 solution 中,只要在程序输入 12 34 - 并回车,都会反应如下:程序就会仍然等待输入,而不是直接返回计算结果;若在-后多输入一个空格再回车,倒能返回正确计算结果。
diff --git a/Chapter 4/4-3/getch.c b/Chapter 4/4-3/getch.c
index 316520b..4525655 100644
--- a/Chapter 4/4-3/getch.c
+++ b/Chapter 4/4-3/getch.c
@@ -37,5 +37,5 @@ extern void ungetch(const char c) {
if (buf_free_position > BUFFERSIZE)
printf("Error: buffer full, can not push %c\n", c);
else
另外注意 getchar 的原型是 int getchar(void);,你用 char 来存的话在判断 EOF 时可能会出问题。
版权声明:本文内容由阿里云实名注册用户自发贡献,版权归原作者所有,阿里云开发者社区不拥有其著作权,亦不承担相应法律责任。具体规则请查看《阿里云开发者社区用户服务协议》和《阿里云开发者社区知识产权保护指引》。如果您发现本社区中有涉嫌抄袭的内容,填写侵权投诉表单进行举报,一经查实,本社区将立刻删除涉嫌侵权内容。