我目前正在课堂上学习有关C语言自上而下编程的知识,但我不知何故不能真正掌握它。
我一直在尝试通过此编程练习来学习它,在该练习中,您必须根据某人的到达时间(以及以公里/小时为单位的速度和以公里为单位的距离)来计算何时应该离开,但是自输出以来,我做错了非常非常错误的事情一直在四百万左右
我正在使用《用C语言解决问题和程序设计》这本书,相关章节为3.5。谁能告诉我我做错了什么?还可以有人解释形式参数如何以ELI5方式工作吗?
#include <stdio.h>
#define MINUTES_IN_HOUR 60
int find_dprt_time(int diffhrs, int diffmin, int arvl_time, int trvl_time);
double find_trvl_time(int trvl_time, int distance, int speed, double result);
int main()
{
double distance;
int time,
speed,
diffmin,
diffhrs;
printf("Enter the time you need to arrive in military time:\n");
scanf("%d",&time);
printf("Enter the distance to your destination in kilometers:\n");
scanf("%lf",&distance);
printf("Enter the speed you plan to average in km/hr:\n");
scanf("%d",&speed);
printf("Your departure time is %d%d.\n",diffhrs,diffmin);
return 0;
}
int find_dprt_time(int diffhrs, int diffmin, int arvl_time, int trvl_time)
{
diffhrs = arvl_time / 100 - trvl_time / 100;
diffmin = arvl_time % 100 - trvl_time % 100;
return (diffhrs, diffmin);
}
double find_trvl_time(int trvl_time, int distance, int speed, double result)
{
result = distance / speed;
trvl_time = MINUTES_IN_HOUR * result;
return (trvl_time);
}
最佳答案
您的return (diffhrs, diffmin)
语句没有做任何特别有用的事情-它只是丢弃diffhrs
并返回diffmin
(在the comma operator in C上读取)。
更改:
return (diffhrs, diffmin);
至:
return diffhrs * 100 + diffmin;
关于c - C编程,自上而下,出发时间,我们在Stack Overflow上找到一个类似的问题:https://stackoverflow.com/questions/40045926/