我正在学习决赛,并担心我在这个话题上花了太多时间,所以我想要一些意见。这将是一个长篇大论,所以我希望有人看到这一点。以下问题出现在“从初学者到专业人士”的书中:
“定义一个名称为Length的结构类型,表示以码为单位的长度,英尺, 和英寸。定义一个add()函数,它将添加两个Length参数并返回 总和为长度类型。定义第二个函数show(),它将显示该值 其长度参数。编写一个将使用Length类型和add()的程序 和show()函数以码,英尺和英寸的形式对任意数量的长度求和 从键盘输入并输出总长度。“
我的解释如下:
#include "stdio.h"
#include "stdlib.h"
#include"string.h"
struct length
{
float FT;
float YD;
float IN;
};
struct length ReadIT(char sym[])
{
struct length Convert;
float dummy;
printf("Enter %s\n",sym);
scanf("%f",&dummy);
Convert.FT = dummy;
Convert.YD = dummy/3;
Convert.IN = dummy*12;
return(Convert);
};
struct length Add(struct length first, struct length second)
{
struct length total;
total.FT = first.FT+second.FT;
total.YD = first.YD + second.YD;
total.IN = first.IN + second.IN;
return total;
};
void Show(struct length Convert )
{
printf("Conversions FT: %.2f\n",Convert.FT );
printf("Conversions YD: %.2f\n",Convert.YD );
printf("Conversions IN: %.2f\n",Convert.IN );
}
void ShowSum(struct length total)
{
printf("total FT: %.2f\n",total.FT );
printf("total YD: %.2f\n",total.YD );
printf("total IN: %.2f\n",total.IN );
}
int main(void)
{
char cmd = 'n';
struct length L1,L2;
do
{
L1 = ReadIT("Length 1");
Show(L1);
L2 = ReadIT("Length 2");
Show(L2);
Add(L1,L2);
ShowSum(Add(L1,L2));//adds up the two lengths , but how do I store them?
printf("Would you like to add more lengths? Type 'y' to continue");
scanf("%s",&cmd);
}
while(tolower(cmd)=='y');
printf("The total of all lengths is : ");
return 0;
}
另外,因为这本书出来了:我发现我应该像这样解释它:
#include <stdio.h>
#include <ctype.h>
#define INCHES_PER_FOOT 12
#define FEET_PER_YARD 3
struct Length
{
unsigned int yards;
unsigned int feet;
unsigned int inches;
};
struct Length add(struct Length first, struct Length second);
void show(struct Length length);
int main(void)
{
char answer = 'n';
struct Length length;
struct Length total = { 0,0,0};
int i = 0;
do
{
printf("Enter a length in yards, feet, and inches: ");
scanf(" %d %d %d", &length.yards, &length.feet, &length.inches);
total = add(total,length);
printf("Do you want to enter another(y or n)?: ");
scanf(" %c", &answer);
fflush(stdin);
}while(tolower(answer) == 'y');
printf("The total of all the lengths is: ");
show(total);
printf("\n");
return 0;
}
struct Length add(struct Length first, struct Length second)
{
unsigned long inches = 0;
struct Length sum;
inches = first.inches + second.inches+
INCHES_PER_FOOT*(first.feet+second.feet+FEET_PER_YARD* (first.yards+second.yards));
sum.inches = inches%INCHES_PER_FOOT;
sum.feet = inches/INCHES_PER_FOOT;
sum.yards = sum.feet/FEET_PER_YARD;
sum.feet %= FEET_PER_YARD;
return sum;
}
void show(struct Length length)
{
printf("%d yards %d feet %d inches", length.yards,length.feet,length.inches);
}
当我看到这个答案时,我的第一个想法是,“我本可以做到这一点”,但这听起来好像他们希望所有这两个长度分别在每两个输入中完成,以及单个FT,YD的总数,&amp; IN独立完成。现在我痴迷于完成我的版本,但我已经碰壁了。有人可以让我去吗?
答案 0 :(得分:0)
我击中的墙是我无法想到一种方法来存储总数 首先循环,然后将其添加到下一循环的总数中 把它作为一个变量存储,然后重复这个 只要用户想要,就会一遍又一遍。
你可以做的非常类似于本书的解决方案:
struct Length total = { 0, 0, 0 }, subtotal;
…
ShowSum(subtotal = Add(L1, L2)); // adds up the two lengths and stores the sum
total = add(total, subtotal);