一个高个子,一个低个子,求一棵树的高度,用相似三角形
- C/C++ code
#include <stdio.h>int main(void){ int shorty = 0L;//shorty's height in inches int lofty = 0L;//lofty's height in inches long feet = 0L; long inches = 0L; long shorty_to_lofty= 0L; //Distance from shorty to the tree in inches long lofty_to_tree = 0L;/*distance from lofty to the tree in inches*/ long tree_height = 0;/*height of the tree in inches*/ const long inches_per_foot =12L; /*Get lofty's height*/ printf("Enter lofty's hight to the top of his/her head in the whole feet:"); scanf("%ld",&feet); printf("...and then inches:"); scanf("%ld",&inches); lofty= feet * inches_per_foot +inches; /*Get shorty's height*/ printf("Enter shorty's hight to the top of his/her eyes in the whole feet:"); scanf("%ld",&feet); printf("...and then inches:"); scanf("%ld",&inches); shorty= feet*inches_per_foot +inches; /*Get the distance from Lofty to the tree*/ printf("Finally enter the distance to the tree to the nearest foot:"); scanf("%ld",&feet); lofty_to_tree = feet*inches_per_foot; //the code to calculate the height of the tree will go here //calculate the height of the tree in inches tree_height=shorty+(shorty_to_lofty+lofty_to_tree)*(lofty-shorty)/shorty_to_lofty; //the code to dispay the result will go here printf("The height of the tree is %ld feet and %ld inches.\n",tree_height/inches_per_foot, tree_height%inches_per_foot); return 0;}
代码如上, 运行时出现错误, 在 tree_height=shorty+(shorty_to_lofty+lofty_to_tree)*(lofty-shorty)/shorty_to_lofty;
请高手解答
[解决办法]
shorty_to_lofty这个怎么能是0,就算是0,怎么能被除。。。。
[解决办法]
感觉楼主应该还要输入一下shorty_to_lofty的值。。。