關於 struct 的完整程式
(高顯忠, sjgau4311@gmail.com, 2011-07-27 05:59)
1樓
#if 0
ip= 1436, max= 121.247680
x1= 0, y1= 70, x2= 99, y2= 0
請按任意鍵繼續 . . .
#endif
// --------------------------------------------------------
#include <cstdlib>
#include <iostream>
#include <math.h>
using namespace std;
// --------------------------------------------------------
// #include "aaa.h"
typedef unsigned int uint;
// a= rnd1();
int rnd1(void)
{
int a, b, c;
a= rand();
b= rand();
c= (a<<15) + b;
// c= (a*(2^15)) + b;
return(c);
}
// ax= rnd2();
double rnd2(void)
{
int a= rnd1();
return(((double) a)/(pow(2, 30)));
}
// no= irnd(1, 6);
int irnd(int a, int b)
{
int c;
// we hope a <= b
if (a > b) {
a^= b^= a^= b;
}
c= (int) ((b - a + 1)*(rnd2()) + a);
return(c);
}
typedef struct Point {
int x;
int y;
}; //特別注意 要加分號
typedef struct Line {
Point p1;
Point p2;
int color;
double length;
}; //特別注意 要加分號
#define sqr(a) ((a)*(a))
// get_length(&ls[i]);
void get_length(Line &a)
{
double l2=sqrt(sqr(a.p1.x-a.p2.x)+sqr(a.p1.y-a.p2.y));
a.length=l2;
//return(l2);
}
int main(int argc, char *argv[])
{
int a, i, no;
double ax;
Line ls[2000];
srand(0x0342);
for (i=0;i<1500;i++) {
//no=irnd(1, 6);
//printf("%d", no);
ls[i].p1.x=irnd(0, 100);
ls[i].p1.y=irnd(0, 100);
ls[i].p2.x=irnd(0, 100);
ls[i].p2.y=irnd(0, 100);
ls[i].color= irnd(2, 7);
get_length(ls[i]);
}
double max=-1;
int ip;
for (i=0;i<1500;i++) {
// max= ((max>=ls[i].length)?max:ls[i].length);
// we hope max >= any length
if (max < ls[i].length) {
ip= i;
max= ls[i].length;
}
}
ls[ip].color= 1;
printf("\n ip= %d, max= %.6lf \n", ip, max);
printf("x1= %5d, y1= %5d, x2= %5d, y2= %5d \n", ls[ip].p1.x, ls[ip].p1.y,
ls[ip].p2.x, ls[ip].p2.y);
printf("\n\n\n");
system("pause");
return EXIT_SUCCESS;
}