zoukankan      html  css  js  c++  java
  • 8.7 checking.c

    // checking.c -- 输入验证

    #include <stdio.h>
    #include <stdbool.h>
    // 验证输入是一个整数
    long get_long(void);
    // 验证范围的上下限是否有效
    bool bad_limits(long begin, long end,
                    long low, long high);
    // 计算a~b之间的整数平方和
    double sum_squares(long a, long b);
    int main(void)
    {
        const long MIN = -10000000L;      // 范围的下限
        const long MAX = +10000000L;      // 范围的上限
        long start;                       // 用户指定的范围最小值
        long stop;                        // 用户指定的范围最大值
        double answer;
    
        printf("This program computes the sum of the squares of "
               "integers in a range. 
    The lower bound should not "
               "be less than -10000000 and 
    the upper bound "
               "should not be more then +10000000.
    Enter the "
               "limits (enter 0 for both limits to quit): 
    "
               "lower limit: ");
        start = get_long();
        printf("upper limit: ");
        stop = get_long();
        while (start != 0 || stop != 0)
        {
            if (bad_limits(start, stop, MIN, MAX))
                printf("Please try again.
    ");
            else
            {
                answer = sum_squares(start, stop);
                printf("The sum of the squares of the integers ");
                printf("from %ld to %ld is %g
    ",
                       start, stop, answer);
            }
            printf("Enter the limits (enter 0 for both "
                   "limits to quit): 
    ");
            printf("lower limit: ");
            start = get_long();
            printf("upper limit: ");
            stop = get_long();
        }
        printf("Done.
    ");
    
        return 0;
    }
    
    
    long get_long(void)
    {
        long input;
        char ch;
        while (scanf("%ld", &input) != 1)
        {
            while ((ch = getchar()) != '
    ')
                putchar(ch);                          // 处理错误输入
            printf(" is not an integer.
    Please enter an ");
            printf("integer value, such as 25, -178, or 3: ");
        }
    
        return input;
    }
    
    double sum_squares(long a, long b)
    {
        double total = 0;
        long i;
    
        for (i = a; i <= b; i++)
            total += (double)i * (double)i;
    
        return total;
    }
    
    bool bad_limits(long begin, long end, 
                    long low, long high)
    {
        bool not_good = false;
    
        if (begin > end)
        {
            printf("%ld isn't smaller than %ld.
    ", begin, end);
            not_good = true;
        }
        if (begin < low || end < low)
        {
            printf("Values must be %ld or greater.
    ", low);
            not_good = true;
        }
        if (begin > high || end > high)
        {
            printf("Values must be %ld or less.
    ", high);
            not_good = true;
        }
    
        return not_good;
    }
  • 相关阅读:
    【Monkey】Monkey稳定性测试常用命令
    【Monkey】Monkey基础概念
    推荐一些前端开发常用框架
    MySql 分表复制表结构和数据脚本
    通过apo切面,动态切换数据源
    MySq 数据迁移,把单字段数据解析出插入到另一张表中
    hadoop 集群搭建与mapreduce开发实战(二)
    hadoop mvn项目 pom配置
    hadoop 集群搭建与mapreduce开发实战(一)
    MySql 存储过程及调用方法
  • 原文地址:https://www.cnblogs.com/EisNULL/p/10854426.html
Copyright © 2011-2022 走看看