中国大学mooc编程题在线测试第四周
来源:互联网 发布:海关数据购买 编辑:程序博客网 时间:2024/04/28 00:03
程序改错
题目内容:
下面代码的功能是将百分制成绩转换为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;}
快递费用计算
题目内容:
快递费按邮件重量计算,由起重费用、续重费用两部分构成:
(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;}
- 中国大学mooc编程题在线测试第四周
- 中国大学mooc编程题在线测试第五周
- 中国大学MOOC-翁恺-C语言程序习题第四周
- 中国大学 mooc
- java 中国大学MOOk第四周题目
- 中国大学MOOC课程《Python语言程序设计》课后练习第一周
- 第7周编程题在线测试
- 中国大学 MOOC 课程 《Python 语言程序设计》 课后练习(第 2 周)
- 中国大学 MOOC 课程 《Python 语言程序设计》 课后练习(第 4 周)
- C++中国大学mooc网----西安交大计算机程序设计第12周作业
- Python BeautifuSoup 库 mooc 中国大学学习
- MOOC C语言程序设计 第一周编程题
- 中国大学MOOC-陈越、何钦铭-数据结构-2016秋2题
- 中国大学MOOC-陈越、何钦铭-数据结构-起步能力自测题&代码
- 中国大学MOOC-翁恺-C语言程序设计习题集(学习笔记)
- 中国大学MOOC-数据结构基础习题集、04-2、File Transfer
- 中国大学MOOC-翁恺-C语言程序习题第三周
- 中国大学MOOC-翁恺-C语言程序习题第五周
- 1123
- 公钥与私钥 数字签名是什么?
- 文章标题
- nginx配置文件nginx.conf配置内容和中文详解(内容整理)
- Spring 注入集合、依赖检查、自动组件扫描、过滤组件自动扫描、自动装配属性
- 中国大学mooc编程题在线测试第四周
- WEB测试应该注意哪些地方,怎样才能做好WEB测试
- Android studio 原生自带下拉加载控件SwipeRefreshLayout使用介绍
- go语言里边的关键字
- React Native android could not connect to development server
- 一个程序员的进化史-第十五章
- 【java学习记录】7.定义一个接口ArearInterface,其中包含一个方法,计算面积三角形、矩形、圆形的面积
- 解决Java中 uploadify http error 413 错误的方法
- 类的实例对象需要下面的顺序