【问题标题】:Deviating from instructions, fun yet informative Structure exercise偏离指令,有趣但内容丰富的结构练习
【发布时间】:2016-08-25 04:03:10
【问题描述】:

我正在准备期末考试,担心我在这个主题上花费了太多时间,所以我想要一些意见。这将是一个长篇阅读,所以我希望有人看到这个。以下问题在《初学C,从新手到专业》一书中:

"定义一个名为 Length 的结构类型,它表示以码、英尺为单位的长度, 和英寸。定义一个 add() 函数,它将添加两个 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 %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 的总和, &IN 独立完成。现在我痴迷于完成我的版本,但我碰壁了。有人可以带我去吗?

【问题讨论】:

  • 你撞到了什么墙?就个人而言,我会将整个批次转换为最低单位(英寸),进行数学运算,然后将结果转换回码和英尺以进行显示。这意味着您不能拥有例如 3 码 17 英尺和 24 英寸,但我认为这是一个奖励!
  • 我碰到的墙是我想不出一种方法来存储第一个循环的总数,然后将其添加到下一个循环的总数中,将其存储为一个variable ,然后根据用户的需要一遍又一遍地重复此操作。
  • scanf("%s",&amp;cmd); --> scanf(" %c", &amp;cmd);
  • 您可以将指针传递给返回结构以存储 add 的结果,或者通过将其转换为英寸,返回并将结果保存为整数。
  • 我想说我想到了,但是我传递的指针不会在循环中每一次覆盖值,并且返回结构只接受一个参数吗?我觉得理解这对我来说是一个升级。

标签: c linked-list structure


【解决方案1】:

我遇到的难题是我想不出一种方法来存储总数 首先循环,然后从下一个循环将其添加到总数中 周围,​​将其全部存储为一个变量,然后重复此操作 只要用户愿意,一遍又一遍。

你可以像书中的解决方案那样做:

  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);

【讨论】:

    猜你喜欢
    • 2021-07-17
    • 1970-01-01
    • 1970-01-01
    • 2023-03-05
    • 1970-01-01
    • 2020-11-14
    • 1970-01-01
    • 2022-08-07
    • 2021-11-14
    相关资源
    最近更新 更多