下面是用C从数控程序中提取坐标数据画图,又一些问题,特别是画弧部分和自己想得到的不一样,请高手改一下,附件是一小段数控程序
#include<stdio.h>
#include<graphics.h>
#include<math.h>
main()
{
FILE *fp;
char file[10],ch,str[4];
double a[100][7];
int flag[100];
int i=0,j=1;
double arc1,arc2,f1,f2;
int s;
int graphdriver=VGA,graphmode=VGAHI;
/* registerbgidriver(EGAVGA_driver); */
initgraph(&graphdriver,&graphmode,"");
printf("Enter the file name:\n");
scanf("%s",file);
if ((fp=fopen(file,"r"))==NULL)
{printf ("cannot open this file\n");
exit(0);
}
ch=fgetc(fp);
while (ch!=EOF)
{while ((ch=fgetc(fp))=='G')
{
fgets (str,3,fp);
if (strcmp(str,"01")==0) {i++;flag[i]=1;}
else if (strcmp(str,"02")==0) {i++;flag[i]=2;}
else if (strcmp(str,"03")==0) {i++;flag[i]=3;}
else if (strcmp(str,"00")==0) {i++;flag[i]=0;}
else break;
ch=fgetc(fp);
while (ch!='N' && ch!=EOF)
{int p=0;
if (ch=='X')
{
char st1[10]={'0'};
a[i][j]=a[i-1][j];
ch=fgetc(fp);
while('0'<=ch && ch<='9' || ch=='.' || ch=='-' )
{
st1[p]=ch;
ch=fgetc(fp); p++;
}
a[i][j]=atof(st1);
printf("%f",a[i][j]);getch();
}
if (ch=='Y')
{
char st2[10]={'0'};
int j=2;
a[i][j]=a[i-1][j];
ch=fgetc(fp);
while('0'<=ch && ch<='9' || ch=='.' || ch=='-' )
{
st2[p]=ch;
ch=fgetc(fp);p++;
}
a[i][j]=atof(st2);
printf("%f",a[i][j]);getch();
}
if (ch=='Z')
{
int j=3;
char st3[10]={'0'};
a[i][j]=a[i-1][j];
ch=fgetc(fp);
while('0'<= ch && ch<='9' || ch=='.' || ch=='-')
{
st3[p]=ch;
ch=fgetc(fp);p++;
}
a[i][j]=atof(st3);
printf("%f",a[i][j]);getch();
}
if (ch=='I')
{
char st4[10]={'0'};
int j=4;
ch=fgetc(fp);
while('0'<= ch && ch<='9' || ch=='.' ||ch=='-')
{
st4[p]=ch;
ch=fgetc(fp);p++;
}
a[i][j]=atof(st4);
printf("%f",a[i][j]);getch();
}
if (ch=='J')
{
int j=5;
char st5[10]={'0'};
ch=fgetc(fp);
while('0'<= ch && ch<='9' || ch=='.' ||ch=='-')
{
st5[p]=ch;
ch=fgetc(fp);p++;
}
a[i][j]=atof(st5);
printf("%f",a[i][j]);getch();
}
if (ch=='K')
{
int j=6;
char st6[10]={'0'};
ch=fgetc(fp);
while('0'<= ch && ch<='9' || ch=='.' ||ch=='-')
{
st6[p]=ch;
ch=fgetc(fp);p++;
}
a[i][j]=atof(st6);
printf("%f",a[i][j]);getch();
}
ch=fgetc(fp);
}
}
}
cleardevice();
setcolor(4);
setbkcolor(1);
/*printf("%d\n",i); */
for(s=1;s<=i;s++)
{
/* printf("%d\n",flag[s]); */
if(flag[s]=0) moveto(a[s][1],a[s][2]);getch();
if(flag[s]=1) line(a[s-1][1],a[s-1][2],a[s][1],a[s][2]);getch;
if(flag[s]=3)
{
arc1=atan2(a[s][5],a[s][4]);
f1=a[s][5]+a[s-1][2]-a[s][2];
f2=a[s][4]+a[s-1][1]-a[s][1];
arc2=atan2(f1,f2);
arc((a[s][4]+a[s-1][1]),(a[s][5]+a[s-1][2]),arc1*180/3.1415926,arc2*180/3.1415926,sqrt(a[s][4]*a[s][4]+a[s][5]*a[s][5]));
getch();
}
if(flag[s]=2)
{
arc1=atan2(a[s][5],a[s][4]);
f1=a[s][5]+a[s-1][2]-a[s][2];
f2=a[s][4]+a[s-1][1]-a[s][1];
arc2=atan2(f1,f2);
arc((a[s][4]+a[s-1][1]),(a[s][5]+a[s-1][2]),arc1*180/3.1415926,arc2*180/3.1415926,sqrt(a[s][4]*a[s][4]+a[s][5]*a[s][5]));
getch();
}
}
getch();
closegraph();
}