知識社群登入
位置: AutoCAD開放式教學 > 討論區 > 討論
叫 AUTOCAD 自動畫圖,STRUCT 練習
1樓
#include <cstdlib>
#include <iostream>

#include <stdio.h>
#include <stdlib.h>
#include <process.h>
#include <time.h>
#include <math.h>

using namespace std;
// --------------------------------------------------------

struct Point {
int x;
int y;
} ;
// --------------------------------------------------------

struct Line {
int cno;
struct Point p1;
struct Point p2;
} ;
// --------------------------------------------------------

double sqr(double x)
{
return(x*x);
}
// --------------------------------------------------------

// a= get_length(L);
double get_length(struct Line a)
{
return(sqrt((sqr(a.p1.x - a.p2.x)) + 
(sqr(a.p1.y - a.p2.y))));
}
// --------------------------------------------------------

int main(int argc, char *argv[])
{
FILE *f1;
int no= 10, i;
struct Line L[1000];
srand(time(NULL));
double max= -999, a;
f1= fopen("ccc.scr", "wt");
for (i=0;i<no;i++) {
L[i].cno= rand()%7 + 1;
fprintf(f1, "color\n%d\n", L[i].cno);
L[i].p1.x= rand()%100;
L[i].p1.y= rand()%100;
L[i].p2.x= rand()%100;
L[i].p2.y= rand()%100;
fprintf(f1, "line\n%d,%d\n%d,%d\n\n", 
L[i].p1.x, L[i].p1.y, L[i].p2.x, L[i].p2.y);
a= get_length(L[i]);
// max >= a
if (max < a) max= a;
}
printf("\n max= %.6lf \n", max);
fclose(f1);
system("pause");
return 0;
}// end of main()