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;
}
- C语言练习之第二章
- C语言第二课练习
- c语言之指针练习
- 【C语言基础】C语言第二章
- C语言之路 第二章 判断控制结构
- C和指针第二章编程练习
- C Primer Plus第二章编程练习
- C++Primer第五版 第二章练习
- c语言第二章总结
- 第二章 C语言概述
- 第二章 C语言概述
- C语言学习第二章
- 第二章 c语言概述
- c语言之文件操作练习
- C语言练习之程序编写
- C语言练习之基础题
- C语言练习之单词首字母大写
- c语言练习之规范编写
- C++Primer第三版习题0325
- MP4文件格式详解——文件类型ftyp
- Linux虚拟文件系统之文件打开(sys_open())
- 返回内部成员变量的指针
- 软件测试方法大汇总
- C语言练习之第二章
- mvc 3.0 自定义 AuthorizeAttribute 权限管理
- 缺陷管理(一)
- 齐次坐标的理解
- Hadoop Job Scheduling
- Android记住用户
- Linux虚拟文件系统之文件系统卸载(sys_umount())
- 软件测试学习网站小全
- 局域网内linux系统读取windows的共享目录