gpt4 book ai didi

c - 无论如何,是否可以创建一个 if 语句来打印输出,说明我已经超出了多少公斤?

转载 作者:行者123 更新时间:2023-11-30 20:29:19 24 4
gpt4 key购买 nike

另外,公斤可以准确地测量到 0.5 公斤吗?例如,物体的最大重量为 20 公斤,但我输入的物体重量为 22.5 公斤。因此输出将是“您已超出 2.5 公斤”。反之亦然(输入=15公斤,输出=您尚未达到最小重量。您还缺少x公斤)。而且距离也可以吗?例如,最小距离为 10 公里,最大距离为 2000 公里,但我输入 x 公里,它要么小于 10 公里,要么大于 2000 公里。输出将是“您的距离减少/增加了 y 公里。”

<小时/>
#include <stdio.h>   //standard i/o
#include <math.h> //roundoff

//Do not accept package’s weight less than 2 kg or more than 20 kg. And do not accept
//distance less than 10 km or more than 2000 km. These are the company’s policies.

int main()
{
float weight, charges, rate;
int distance;

printf("Enter weight in kg: ");
scanf_s("%f", &weight);

printf("Shipping in distance in km: ");
scanf_s("%d", &distance);

if (weight < 2 || weight > 20 || distance < 10 || distance > 2000)
{
printf("The minimum weight is 2kg and the maximum weight is 20kg.\nThe minimum distance is 10km and the maximum distance is 2000km.");
return 0;
}
else if (weight < 6)
{
rate = 2.2;
}
else if (weight < 10)
{
rate = 3.7;
}
else
{
rate = 4.8;
}

charges = (weight * rate);
printf("Charges for %f", weight);
printf(" kg and shipping distance of %d", distance);
printf(" km are : RM%f", charges);
return 0;
}

最佳答案

其实很简单。分别使用 if 语句来增加或减少重量或距离。即

#include<stdio.h> 

int main()
{
int distance;
float weight;
printf("Enter weight in kg:\n");
scanf("%f",&weight);
printf("Shipping in distance in km:\n");
scanf("%d",&distance);
if(weight>=20)
{
printf("you have exceeded by %f kg\n",weight-20);
}
else if(weight<=2)
{
printf("You have not achieved the minimum weight. You are short of %f kg\n",2-weight);
}
if(distance>=2000)
{
printf("Your distance is more by %d km\n",distance-2000);
}
else if(distance<=10)
{
printf("Your distance is less by %d km\n",10-distance);
}
}

关于c - 无论如何,是否可以创建一个 if 语句来打印输出,说明我已经超出了多少公斤?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/58450901/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com