目录
写在前面
正文
第1关:快递费用计算
第2关:计算一元二次方程的根
第3关:产品信息格式化
写在最后
写在前面
本文代码是我自己所作,本人水平有限,可能部分代码看着不够简练,运行效率不高,但都能运行成功。另外,如果想了解更多,请订阅专栏头歌C语言程序与设计
正文
第1关:快递费用计算
本关任务:编写一个计算机快递费的程序。
上海市的某快递公司根据投送目的地距离公司的远近,将全国划分成5个区域:

- 起重(首重)1公斤按起重资费计算(不足1公斤,按1公斤计算),超过首重的重量,按公斤(不足1公斤,按1公斤计算)收取续重费;
- 同城起重资费10元,续重3元/公斤;
- 寄往1区(江浙两省)的邮件,起重资费10元,续重4元;
- 寄往其他地区的邮件,起重资费统一为15元。而续重部分,不同区域价格不同:2区的续重5元/公斤,3区的续重6.5元/公斤,4区的续重10元/公斤。
提示:续重部分不足一公斤,按1公斤计算。因此,如包裹重量2.3公斤:1公斤算起重,剩余的1.3公斤算续重,不足1公斤按1公斤计算,1.3公斤折合续重为2公斤。如果重量应大于0、区域编号不能超出0-4的范围。
#include<stdio.h>
#include<math.h>
int main()
{
	int i,j,num;
	float n,weight,price;
	
	scanf("%d,%f",&num,&n);
	weight=ceil(n);
	
	switch(num)
	{
		case 0:
			if(weight<=1)
			   price=10;
			else
			   price=10+3*(weight-1);
			break;
		case 1:
			if(weight<=1)
			   price=10;
			else
			   price=10+4*(weight-1);
			break;
		case 2:
		case 3:
		case 4:
		    if(num==2)
            {
			   price=15+5*(weight-1);
               break;
            }
			else if(num==3)
            {
			   price=15+6.5*(weight-1);
               break;
            }
			else if(num==4)
            {
			   price=15+10*(weight-1);
			   break; 
            }
		default:
			printf("Error in Area\n"); 
	}
	
	printf("Price: %.2f\n",price);
	return 0;
} 第2关:计算一元二次方程的根
本关任务:根据下面给出的求根公式,计算并输出一元二次方程
ax2+bx+c=0的两个实根,要求精确到小数点后4位。其中a,b,c的值由用户从键盘输入。如果用户输入的系数不满足求实根的要求,输出错误提示 "error!"。样例1
输入:
1,2,1输出:
Please enter the coefficients a,b,c:
x1=-1.0000, x2=-1.0000样例2
输入:
2,1,6输出:
Please enter the coefficients a,b,c:error!
#include<stdio.h>
#include<math.h>
int main()
{
	float a,b,c,x1,x2;
	printf("Please enter the coefficients a,b,c:\n");//提示信息 
	scanf("%f,%f,%f",&a,&b,&c);
	
	//计算模块 
	if(b*b-4*a*c>=0)
	{
	   x1=(-b-sqrt(b*b-4*a*c))/(2*a);
	   x2=(-b+sqrt(b*b-4*a*c))/(2*a);
       printf("x1=%.4f, x2=%.4f\n",x2,x1);
    }
    else 
       printf("error!\n");
	return 0;
}第3关:产品信息格式化
本关任务:编写一个程序, 对用户录入的产品信息进行格式化。
以下为程序的运行结果示例:
Enter item number:
385↙
Enter unit price:
12.5↙
Enter purchase date (mm/dd/yy):
12/03/2015↙
Item Unit Purchase
385 $ 12.50 12032015
#include<stdio.h>
int main(void)
{  
    int i=0; 
    int num,date[3];
    float price;
	printf("Enter item number:\n");
    scanf("%d",&num);
    printf("Enter unit price:\n");
    scanf("%f",&price);
    printf("Enter purchase date (mm/dd/yy):\n");
    printf("Item Unit Purchase\n");
    scanf("%d/%d/%d",&date[0],&date[1],&date[2]);
    printf("%-9d$ %-9.2f%02d%02d%02d",num,price,date[0],date[1],date[2]);
    return 0;
}写在最后
👍🏻点赞,你的认可是我创作的动力!
⭐收藏,你的青睐是我努力的方向!
✏️评论,你的意见是我进步的财富!




















