| 网站首页 | 业界新闻 | 小组 | 威客 | 人才 | 下载频道 | 博客 | 代码贴 | 在线编程 | 编程论坛
欢迎加入我们,一同切磋技术
用户名:   
 
密 码:  
共有 925 人关注过本帖
标题:抄了一个打印设定上下范围的所有整数的平方和程序!
取消只看楼主 加入收藏
visor
Rank: 1
等 级:新手上路
帖 子:9
专家分:0
注 册:2022-4-13
结帖率:0
收藏
 问题点数:0 回复次数:2 
抄了一个打印设定上下范围的所有整数的平方和程序!
#include <stdio.h>
#include <stdbool.h>

long get_long(void);
bool bad_limits(long begin, long end, long low, long high);
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.\nThe lower bound should not"
           "be less than -10000000 and\nthe upper bound"
           "should not be more than +10000000.\nEnter the"
           "limits (enter 0 for both limits to quit);\n"
           "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.\n");
        else {
            answer = sum_squares(start, stop);
            printf("The sum of the square of the integers ");
            printf("from %ld to %ld is %g\n", start, stop, answer);
        }
        printf("Enter the limits(enter 0 for both limits to quit):\n");
        printf("lower limit: ");
        start = get_long();
        printf("upper limit: ");
        stop = get_long();
    }
    printf("Done!.\n");
    return 0;
}

long get_long(void) {
    long input;
    char ch;

    while (scanf("%ld", &input) != 1) {
        while ((ch = getchar()) != '\n')
            putchar(ch);
        printf(" is not an integer.\nplease enter an ");
        printf("integer value,such as 25,-178,or 3: ");
    }
    return input;
}

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 .\n", begin, end);
        not_good = true;
    }
    if (begin < low || end < low) {
        printf("Values must be %ld or greater.\n", low);
        not_good = true;
    }
    if (begin > high || end > high) {
        printf("Values must be %ld or less", high);
        not_good = true;
    }
    return not_good;
}

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;
}

[此贴子已经被作者于2022-4-20 18:40编辑过]

搜索更多相关主题的帖子: the start double long printf 
2022-04-20 18:39
visor
Rank: 1
等 级:新手上路
帖 子:9
专家分:0
注 册:2022-4-13
收藏
得分:0 
你这是高手啊,我是初学者,没有研究这么深啊。不过我看书上写double一般是64位?到底是多少位?
2022-04-21 10:49
visor
Rank: 1
等 级:新手上路
帖 子:9
专家分:0
注 册:2022-4-13
收藏
得分:0 
谢谢,
2022-04-21 11:54
快速回复:抄了一个打印设定上下范围的所有整数的平方和程序!
数据加载中...
 
   



关于我们 | 广告合作 | 编程中国 | 清除Cookies | TOP | 手机版

编程中国 版权所有,并保留所有权利。
Powered by Discuz, Processed in 0.018783 second(s), 9 queries.
Copyright©2004-2024, BCCN.NET, All Rights Reserved