c 字符输入与验证,毁人不倦

蛤蛤蛤铪蛤铪蛤。

为了刚好的认识 scanf(),getchar(),输入行缓冲之间的坑。

我决定写一个练习题。

当然,主要是为了装逼。

编写一个提供加减乘除的菜单选择程序。提供交互与输出。。。。
(原题太长,来源不可考,直接给出源码。代码本身很简单。主要是处理输入行缓冲)

【交互效果还行~】

#include <stdio.h>
#include <ctype.h>
#include <stdlib.h>
#define FIRST 1
#define SECOND 2

char get_operation();
float get_number(int seq);
char get_operation();
float get_nonzero();

int main(void)
{
    char op;
    float a,b;
    int quit = 0;
    while( (op = get_operation()) != 'q' ) {
        switch(op) {
            case 'a':
                a = get_number(FIRST);
                b = get_number(SECOND);
                printf("%g + %g = %g\n", a, b, a+b);
                break;
            case 's':
                a = get_number(FIRST);
                b = get_number(SECOND);
                printf("%g - %g = %g\n", a, b, a-b);
                break;
            case 'm':
                a = get_number(FIRST);
                b = get_number(SECOND);
                printf("%g * %g = %g\n", a, b, a*b);
                break;
            case 'd':
                a = get_number(FIRST);
                b = get_number(SECOND);
                if( 0 == b ){
                    b = get_nonzero();
                }
                printf("%g / %g = %g\n", a, b, a/b);
                break;
            case 'q':
                quit = 1;
                break;
            default:
                printf("%c is a undefined operation!\n"
                        "Error!!!! Please check the code!!!!\n ", op);
                quit = 1;
                break;
        }
        if(quit)
            break;
    }
    printf("Bye.\n");
    return 0;
}

float get_nonzero()
{
    printf("Enter a number other than 0:");
    float num;
    int ch;
    while( 1 != scanf("%f", &num) || num == 0 ) {
        while( (ch = getchar()) !='\n')
            putchar(ch);
        printf(" is illegal input of an non zero number.\n"
                "Please try again:");
    }
    return num;
}

float get_number(int seq)
{
    float a;
    char ch;
    if( seq == FIRST ) {
        printf("Enter first number:");
    } else if( seq == SECOND ) {
        printf("Enter second number:");
    }else {
        printf("Unknown error occured in this program!");
        return 33;
        exit(33);
    }
    while( 1 != scanf("%f", &a) ) {
        while( (ch = getchar()) != '\n' )
            putchar(ch);
        printf(" is not an number.\n"
                "Please enter a number, such as 2.5,-1.78e6,or 3.1\n");
    }
    return a;
}

char get_operation()
{
    printf("Enter the operation of your choice:\n");
    printf("a. add           s. subtract\n");
    printf("m. multiply      d. divide\n");
    printf("q. quit\n");
    char ch;
    int ok = 0;
    while( (ch = getchar()) != EOF ) {
        if( isspace(ch) )
            continue;
        switch(ch) {
            case 'a':
            case 's':
            case 'm':
            case 'd':
            case 'q':
                ok = 1;
                break;
            default:
                printf("[");
                putchar(ch);
                while( (ch = getchar()) != '\n')
                    putchar(ch);
                printf("]");
                printf(" is not a volid operation.\n"
                        "Please try again!\n");
        }
        if(ok)
            break;
    }
    while( getchar() != '\n' )
        continue;
    return ch;

}

运行效果如下:
Enter the operation of your choice:
a. add           s. subtract
m. multiply      d. divide
q. quit
 I love you!
[I love you!] is not a volid operation.
Please try again!
a
Enter first number:34
Enter second number:2
34 + 2 = 36
Enter the operation of your choice:
a. add           s. subtract
m. multiply      d. divide
q. quit
m
Enter first number:12
Enter second number:12
12 * 12 = 144
Enter the operation of your choice:
a. add           s. subtract
m. multiply      d. divide
q. quit
d
Enter first number:21.4
Enter second number:9
21.4 / 9 = 2.37778
Enter the operation of your choice:
a. add           s. subtract
m. multiply      d. divide
q. quit
q
Bye.
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值