2014 10 利用共用体和链表完成对几何数据的读入和输出

【代码】

#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <math.h>
#define maxSize 255
#define PI 3.1415926

typedef struct
{
	double x1, y1;
}Point;

typedef union
{
	Point p;
	double r;
}share;

typedef struct LNode
{
	char name[maxSize];
	double x0, y0;
	share data;
	struct LNode* next;
}LNode;

void draw_line(double x1, double y1, double x2, double y2) 
{

}

void draw_rect(double x1, double y1, double w, double h)
{

}

void draw_circle(double x, double y, double r)
{
	Point p;//p为圆弧上的一点
	double a;//a为p到圆心o的连线与x轴的夹角

	for (a = 0; a <= 2 * PI; a += 0.01)
	{
		//计算p的坐标
		p.x1 = x + r * cos(a);
		p.y1 = y + r * sin(a);

		draw_line(x, y, p.x1, p.y1);
	}
}


LNode* ReadData()
{
	FILE* fp;
	if ((fp = fopen("201410data.txt", "r")) == NULL)
		exit(0);

	LNode* L;
	LNode* r, *s;

	char str[maxSize];

	fscanf(fp, "%s", str);

	L = (LNode*)malloc(sizeof(LNode));
	L->next = NULL;
	r = L;

	while (!feof(fp) && strcmp(str, "END") != 0)
	{
		s = (LNode*)malloc(sizeof(LNode));
		strcpy(s->name, str);

		if (strcmp(str, "LINE") == 0 || strcmp(str, "RECT") == 0)
			fscanf(fp,"%d %d %d %d", &s->x0, &s->y0, &s->data.p.x1, &s->data.p.y1);
		else
			fscanf(fp, "%d %d %d", &s->x0, &s->y0, &s->data.r);

		r->next = s;
		r = s;
		fscanf(fp, "%s", str);
	}

	r->next = NULL;
	fclose(fp);

	return L;
}

//查找输出图形
void FindPolygon(LNode* L)
{
	char s[maxSize] = "RECT" ;

	//scanf("%s",s);

	LNode* p;
	p = L->next;

	while (p != NULL)
	{
		if (strcmp(p->name, s) == 0)
		{
			if (strcmp(p->name, "RECT") == 0)
				draw_rect(p->x0, p->y0, p->data.p.x1, p->data.p.y1);
			else if (strcmp(p->name, "LINE") == 0)
				draw_line(p->x0, p->y0, p->data.p.x1, p->data.p.y1);
			else
				draw_circle(p->x0, p->y0, p->data.r);
		}
		p = p->next;
	}
}

void Delete_All(LNode*L)
{
	LNode*p, *q;

	p = L->next;
	L->next = NULL;

	while (p != NULL)
	{
		q = p->next;
		free(p);
		p = q;
	}
	free(L);
}


int main()
{

	return 0;
}

 

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值