我正在为期末考试做准备,担心我在这个话题上花了太多时间,所以我想要一些意见。这将是一个很长的阅读,所以我希望有人看到这一点。以下问题出现在书中,"从新手到专业人士的C阶段":
"定义一个结构类型,其名称为Length,表示以码、英尺、,和英寸。定义一个add()函数,该函数将添加两个Length参数并返回总和为类型Length。定义第二个函数show(),它将显示值的Length参数。编写一个使用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 %sn",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: %.2fn",Convert.FT );
printf("Conversions YD: %.2fn",Convert.YD );
printf("Conversions IN: %.2fn",Convert.IN );
}
void ShowSum(struct length total)
{
printf("total FT: %.2fn",total.FT );
printf("total YD: %.2fn",total.YD );
printf("total IN: %.2fn",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和&IN独立完成。现在我痴迷于完成我的版本,但我遇到了困难。有人能让我走吗?
我碰到的墙是,我想不出一种方法来存储首先循环,然后将其添加到下一个循环的总数中大约,将总数存储为一个变量,然后重复此操作一次又一次,只要用户想要。
你可以像书中的解决方案一样完成它:
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);