题目
typedef 简化结构体类型名
思路
本题用 typedef 给结构体类型起别名,简化声明。
typedef struct { ... } Point; 之后可直接写 Point p;,而不必每次写 struct Point p。
解题分析
typedef 给 struct 起别名后不必每次写 struct 关键字。与 struct 标签不同,typedef 名可直接当类型用。
完整程序
#include <stdio.h>
#include <math.h>
typedef struct {
double x, y;
} Point;
int main(void)
{
Point a, b;
scanf("%lf %lf %lf %lf", &a.x, &a.y, &b.x, &b.y);
double dx = a.x - b.x, dy = a.y - b.y;
printf("%.2f\n", sqrt(dx * dx + dy * dy));
return 0;
}
运行示例
输入:
0 0 6 8输出:
10.00