“求圆的面积”的版本间的差异

来自linux中国网wiki
跳到导航 跳到搜索
docker>Evan
 
(导入1个版本)
 

2019年10月14日 (一) 13:55的最新版本

增量式开发
 编写一个程序求圆的面积,圆的半径以两个端点的坐标(x1,y1) 和(x2,y2) 给出. 首先 分析和分解问题 ,把大问题分解成小问题,再对小问题分别求解

初始版本

#include <math.h>
#include <stdio.h>
double distance(double x1, double y1, double x2, double y2)
{
       double dx = x2 - x1;
        double dy = y2 - y1;
//        printf("dx is %f\n dy is %f\n",dx,dy);
        //printf("distance is %f\n",distance(1.0, 2.0, 4.0,6.0));
        double dsquared = dx * dx + dy * dy;
//        printf("dsquared is %f\n", dsquared);
        double result = sqrt(dsquared);
        return result;
}

double area(double radius)
{
        return 3.1416 * radius * radius;
}

int  main(void)
{
//       printf("distance is %f\n",distance(1.0, 2.0, 4.0,6.0));
// distance is 5.000000
//       printf("area is %f\n",);
       double radius=distance(1.0, 2.0, 4.0,6.0);
       double result = area(radius);
       printf(" area is %f\n",result);
       // area is 78.540000

       return 0;
}

/****
evan@kalipc:~/github/learn-c/one-stop-c$ gcc -Wall  p57.c 
/tmp/ccbMqPPq.o:在函数‘distance’中:
p57.c:(.text+0x69):对‘sqrt’未定义的引用
collect2: error: ld returned 1 exit status

gcc -Wall -lm   p57.c 

***/

改进

#include <math.h>
#include <stdio.h>
double distance(double x1, double y1, double x2, double y2)
{
       double dx = x2 - x1;
        double dy = y2 - y1;
//        printf("dx is %f\n dy is %f\n",dx,dy);
        //printf("distance is %f\n",distance(1.0, 2.0, 4.0,6.0));
        double dsquared = dx * dx + dy * dy;
//        printf("dsquared is %f\n", dsquared);
        double result = sqrt(dsquared);
        return result;
}

double area(double radius)
{
        return 3.1416 * radius * radius;
}

double area_point(double x1,  double y1, double x2, double y2)
{
        return area(distance(x1, y1, x2, y2));
}
int  main(void)
{
//       printf("distance is %f\n",distance(1.0, 2.0, 4.0,6.0));
// distance is 5.000000
// radius=distance(1.0, 2.0, 4.0,6.0);
        //     double result = area(radius);
       printf(" area is %f\n",area_point(1.0, 2.x, 4.0, 6.0));
       // area is 78.540000
       return 0;
}