Advertisement

中国大学mooc编程题在线测试第四周

阅读量:

程序改错

题目内容:
下面代码的功能是将百分制成绩转换为5分制成绩,具体功能是:如果用户输入的是非法字符或者不在合理区间内的数据(例如输入的是a,或者102,或-45等),则程序输出 Input error!,否则将其转换为5分制输出。目前程序存在错误,请将其修改正确。并按照下面给出的运行示例检查程序。

复制代码
    #include<stdio.h>
       int main()
       {
       int score;
       char grade;
       printf("Please input  score:");
       scanf("%d", &score);
       if (score < 0 || score > 100)   
             printf("Input error!\n");
        else if (score >= 90) 
             grade = 'A’;
        else if (score >= 80)
             grade = 'B';   
        else if (score >= 70)
             grade = 'C';  
        else if (score >= 60)
             grade = 'D'; 
        else
             grade = 'E'; 
        printf("grade:%c\n", grade);
        return 0;
    }

程序运行结果示例1:
Please input score:
-1↙
Input error!

程序运行结果示例2:
Please input score:
95↙
grade: A

程序运行结果示例3:
Please input score:
82↙
grade: B

程序运行结果示例4:
Please input score:
72↙
grade: C

程序运行结果示例5:
Please input score:
66↙
grade: D

程序运行结果示例6:
Please input score:
32↙
grade: E

程序运行结果示例7:
Please input score:
127↙
Input error!
输入提示信息:”Please input score:\n”
输入格式: “%d”
输出格式:
用户输入存在错误时,输出提示信息:”Input error!\n”
输出格式:”grade: %c\n” (注意:%c前面有一个空格)

此题要格外注意输出

复制代码
    #include<stdio.h>
       int main()
       {
       int score;
       char grade;
       printf("Please input score:\n");
       if ((scanf("%d", &score) != 1) || (score < 0 || score > 100))
       {
           printf("Input error!\n");return 0;
       }
        else if (score >= 90)
             grade = 'A';
        else if (score >= 80)
             grade = 'B';
        else if (score >= 70)
             grade = 'C';
        else if (score >= 60)
             grade = 'D';
        else
             grade = 'E';
        printf("grade: %c\n", grade);
        return 0;
    }

快递费用计算

题目内容:

0区 1区 2区 3区 4区
同城 临近两省 1500公里(含)以内 1500~2500公里 2500公里以上
上海 江苏,浙江 北京,天津,河北,辽宁,河南,安微,陕西,湖北,江西,湖南,福建,广东,山西。 吉林,辽宁,甘肃,四川,重庆,青海,广西,云南,海南,内蒙古,黑龙江,贵州。 新疆,西藏。

快递费按邮件重量计算,由起重费用、续重费用两部分构成:
(1) 起重(首重)1公斤按起重资费计算(不足1公斤,按1公斤计算),超过首重的重量,按公斤(不足1公斤,按1公斤计算)收取续重费;
(2) 同城起重资费10元,续重3元/公斤;
(3) 寄往1区(江浙两省)的邮件,起重资费10元,续重4元;
(4) 寄往其他地区的邮件,起重资费统一为15元。而续重部分,不同区域价格不同:2区的续重5元/公斤,3区的续重6.5元/公斤,4区的续重10元/公斤。

编写程序,从键盘输入邮件的目的区域编码和重量,计算并输出运费,计算结果保留2位小数。程序中所有浮点数的数据类型均为float。
提示:续重部分不足一公斤,按1公斤计算。因此,如包裹重量2.3公斤:1公斤算起重,剩余的1.3公斤算续重,不足1公斤按1公斤计算,1.3公斤折合续重为2公斤。如果重量应大于0、区域编号不能超出0-4的范围。

程序运行结果示例1:
4,4.5↙
Price: 55.00

程序运行结果示例2:
5,3.2↙
Error in Area
Price: 0.00

输入格式:
用逗号分隔的两个数字,第一个表示区域、第二个是重量:”%d,%f”

输出格式:
价格的输出格式:”Price: %5.2f\n”
区域错误的提示信息:”Error in Area\n”
为避免出现格式错误,请直接拷贝粘贴题目中给的格式字符串和提示信息到你的程序中。

复制代码
    #include <stdio.h>
    #include <math.h>
    
    int main()
    {
    int area;
    float weight,price = 0;
    scanf("%d,%f",&area,&weight);
    if(area < 0 || area > 4)
    {
        printf("Error in Area\n");
    }
    else if(area == 0)
    {
        if(weight < 1)
        {
            price=10;
        }
        else
        {
            weight = ceil(weight);
            price = 10 + (weight-1)*3;
        }
    }
    else if(area == 1)
    {
        if(weight < 1)
        {
            price=10;
        }
        else
        {
            weight = ceil(weight);
            price = 10 + (weight-1)*4;
        }
    }
    else
    {
        if(weight < 1)
        {
            price=15;
        }
        else
        {
            if(area == 2)
            {
                weight = ceil(weight);
                price = 15 + (weight-1)*5;
            }
            else if(area == 3)
            {
                weight = ceil(weight);
                price = 15 + (weight-1)*6.5;
            }
            else
            {
                weight = ceil(weight);
                price = 15 + (weight-1)*10;
            }
        }
    }
    printf("Price: %5.2f\n",price);
    return 0;
    }

全部评论 (0)

还没有任何评论哟~