|
马上注册,结交更多好友,享用更多功能^_^
您需要 登录 才可以下载或查看,没有账号?立即注册
x
本帖最后由 小蒙 于 2017-10-24 09:05 编辑
图床上的图在手机版网页中刷不出来,我用手机chrome切换到桌面版就能看到了。
网上有很多答案,但是做的很粗糙,没有考虑到特殊情况,比如需要输入有效数字的时候却输入了文件结束符,我的答案包括了对任意时刻文件结束符的处理代码,当然,即使繁琐,代码本身没大的逻辑错误,无奈初学,自己感觉都过于繁琐,为了一个文件结束符,都得需要从被调函数一志追到主调函数,我相信肯定有更优雅的方式处理文件结束符,希望得到大牛的指点,谢谢。
- #include <stdio.h>
- #include <stdbool.h>
- #include <string.h>
- #include <iso646.h>
- #include <ctype.h>
- #include <math.h>
- #define ZERO 0
- #define ADD 'a'
- #define SUB 's'
- #define MUL 'm'
- #define DIV 'd'
- char get_operator(void);
- float get_num(void);
- float add(float a, float b);
- float sub(float a, float b);
- float mul(float a, float b);
- float div(float a, float b);
- int main(void) {
- // char operator;
- char oper;
- float first;
- float second;
- while ((oper = get_operator()) != 'q') {
- if (EOF == oper) {
- break;
- }
- if (oper != ADD && oper != SUB && oper != MUL && oper != DIV) {
- printf("Invalid operator, try again.\n");
- continue;
- }
- printf("Enter first num:");
- first = get_num();
- // get_num()出现的结束符在这里进行处理,但是结束符根本无法处理,所以只能break,直接结束程序。
- if (getchar() == EOF) break;
- printf("Enter second num:");
- second = get_num();
- // get_num()出现的结束符在这里进行处理,但是结束符根本无法处理,所以只能break,直接结束程序。
- if (getchar() == EOF) break;
- while ((oper == DIV) && (ZERO == second)) {
- printf("Enter a num other than 0:");
- second = get_num();
- //同样也是结束符的处理,但是在这里break只能跳出本层while,之前second作为ZERO,现在还是0,0作为除法会进行计算
- // 所以在switch前加了一层逻辑判断,如果second等于0且oper是DIV(相当于在"Enter a num other than 0:"处键入文件结束符)再次跳出最外层循环。
- if (getchar() == EOF) break;
- if (ZERO != second) {
- break;
- }
- }
- // 注意,用的是非逻辑操作符
- if (!(ZERO == second && oper == DIV)) {
- switch (oper) {
- case ADD:
- printf("%.2f + %.2f = %.2f\n", first, second, add(first, second));
- break;
- case SUB:
- printf("%.2f - %.2f = %.2f\n", first, second, sub(first, second));
- break;
- case MUL:
- printf("%.2f * %.2f = %.2f\n", first, second, mul(first, second));
- break;
- case DIV:
- printf("%.2f / %.2f = %.2f\n", first, second, div(first, second));
- break;
- }
- } else {
- break;
- }
- }
- printf("Bye.\n");
- return 0;
- }
- char get_operator(void) {
- char c;
- printf("Enter the operation of your choice:\n");
- printf("%-20s%-20s\n%-20s%-20s\n%-20s\n", "a. add", "s. substract", "m. multiply", "d. divide", "q. quit");
- while ((c = getchar()) != EOF) {
- if (isspace(c)) {
- continue;
- }
- while (getchar() != '\n') {
- continue;
- }
- return c;
- }
- return EOF;
- }
- float get_num(void) {
- float num;
- char c;
- int item = scanf("%f", &num);
- while (item != 1) {
- // 因为程序本身返回数字类型,所以不可以让他返回EOF,容易混淆,所以只能让文件结束符继续向上走,走到主调函数中。
- if (EOF == item) {
- break;
- }
- while ((c = getchar()) != '\n') {
- putchar(c);
- }
- printf(" is not a num.\n");
- printf("Please enter a num, such as 2.5, -1.78E8, or 3:");
- item = scanf("%f", &num);
- }
- if (1 == item) {
- return num;
- }
- }
- float add(float a, float b) {
- return a + b;
- }
- float sub(float a, float b) {
- return a - b;
- }
- float mul(float a, float b) {
- return a * b;
- }
- float div(float a, float b) {
- return a / b;
- }
复制代码 |
|