C语言练习之第二章

来源:互联网 发布:欧洲圣母 知乎 编辑:程序博客网 时间:2024/05/21 14:43

习题2.1 编写一个程序,提示用户用英寸输入一个距离,然后将该距离值输出为码、英尺和英寸的形式。

原码:

#include <stdio.h>
const long inches_per_foot=12L;
const long feet_per_yard=3L;
int main(void){
    long yards;
    long inches;
    long feet;
    /* get inches number from user */
    printf("Enter the number in inches\n");
    scanf("%ld",&inches);

    //calcutate the number format the number
    yards=inches/(feet_per_yard*inches_per_foot);
    inches=inches%(feet_per_yard*inches_per_foot);
    feet=inches/inches_per_foot;
    inches=inches%inches_per_foot;
   
    //Output the result
    printf("Your number is formated:\n");
    printf("%ld yards %ld feet %ld inches",yards,feet,inches);
 return 0;
}

改进:

/* Exercise 2.1 Convert inches to yards,feet adn inches */

#include <stdio.h>

int main(void)
{
 int yards = 0;
 int feet = 0;
 int inches = 0;
 const int inches_per_foot=12;
 const int feet_per_yard=3;

 printf("Enter a distance in inches: ");
 scanf("%d", &inches);

 feet = inches/inches_per_foot; /* Get whole feet */
 yards = feet/feet_per_yard; /* Get whole yards in feet */
 feet %= feet_per_yard; /* Get residual feet */
 inches %=inches_per_foot; /* Get residual inches */

 printf("\nThat is equivalent to %d yards %d feet and %d inches.\n",
              yards, feet, inches);
 return 0;
}

 

习题2.2  编写一个程序,提示用户用英尺和英寸输入一个房间的长和宽,然后计算并输出面积,单位是平方码,精度为小数点后有两位数。

原码:

#include <stdio.h>

int main(void)
{
    long feet=0L;
    long inches=0L;
    long width_house=0L;
    long length_house=0L;
    long square_house=0L;
    const long inches_per_foot=12L;

    //get the width of house
    printf("Enter the width of house,in whole feet: ");
    scanf("%ld", &feet);
    printf(" ... and then inches: ");
    scanf("%ld",&inches);
    width_house=feet*inches_per_foot+inches;

    //get the length of house
    printf("Enter the length of house,in whole feet: ");
    scanf("%ld",&feet);
    printf(" ... and the inches: ");
    scanf("%ld",&inches);
    length_house=feet*inches_per_foot+inches;

    //Calculate the square of house
    square_house=width_house*length_house;
   
    //Output the result
    printf("The square of house is %.2ld",square_house);
}

以上结果输出单位是平方英寸而不是平方码,故错误

改进

/* Exercise 2.2 Calculating the area of a room */

#include <stdio.h>

int main(void)
{
double length = 0.0; /* Room length in yards */
double width = 0.0; /* Room width in yards */
long inches = 0L;
long feet = 0L;
const long inches_per_foot = 12L;
const double inches_per_yard = 36L;

/* Get the length of the room */
printf("Enter the length of the room in feet and inches - whole feet first: ");
scanf("%ld", &feet);
printf(" ... and then in inches: ");
scanf("%ld", &inches);
length = (feet*inches_per_foot+inches)/inches_per_yard;

/* Get the width of the room */
printf("Enter the width of the room in feet and inches - whole feet first: ");
scanf("%ld", &feet);
printf(" ... and then in inches: ");
scanf("%ld", &inches);
width = (feet*inches_per_foot+inches)/inches_per_yard;

/* Output the area */
printf("\nThe area of the room is %.2f square yards.\n", length*width);
return 0;

}

 

原创粉丝点击