我正在爲最後的學習而努力,並擔心我在這個主題上花費太多時間,所以我想要一些輸入。這將是一種長時間的閱讀,所以我希望有人看到這一點。以下問題在本書「Beginning C,from newvice to professional」中:偏離說明,有趣但內容翔實結構練習
「定義一個名稱爲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 %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);
}
當我看到這個答案,我的第一個想法是,「我可以做到這一點」,但它聽起來像他們想要所有這些長度分別進行每兩個單獨進行,並單獨完成個人金融時報,YD,總計。現在我癡迷於完成我的版本,但我已經打了一堵牆。有人能讓我走嗎?
你打什麼牆?就個人而言,我會將整個區域轉換爲最低單位(英寸),然後進行數學運算,然後將結果轉換回顯示的碼和腳。這意味着你不能有,例如,3碼17英尺和24英寸,但我認爲這是一個獎金! – Mike
我打的牆是我想不出一種方式來存儲第一個循環的總數,然後將它添加到下一個循環周圍的總數中,將總數作爲一個變量存儲,然後重複此操作,只要用戶需要。 – MessatsuX
'scanf(「%s」,&cmd);' - >'scanf(「%c」,&cmd);' – BLUEPIXY