Posted: Wed Mar 02, 2005 5:04 am
Please help me!!!!!!!!! This problem is driving me mad 

Code: Select all
if(x.a> y.a )
...
Code: Select all
if(1e-6 < x.a-y.a )
...
Code: Select all
#include <stdio.h>
#include <math.h>
#include <stdlib.h>
struct result
{
double x, y;
};
int compare(const void *arg1, const void *arg2)
{
result el1, el2;
el1 = *(result*)arg1;
el2 = *(result*)arg2;
/* if (el1.x==el2.x)
return (el1.y<el2.y);
return (el1.x<el2.x);*/
if (fabs(el1.x - el2.x)>1e-6)
{
if((1e-6) > (el1.x - el2.x)) return 1;
if((-1e-6) < (el1.x - el2.x)) return -1;
}
if (fabs(el1.y - el2.y)<1e-6)
return 0;
if((1e-6) > (el1.y - el2.y)) return 1;
if((-1e-6) < (el1.y - el2.y)) return -1;
return 0;
}
double fix(double v)
{
if (v>0) return v;
if (v>-0.0005) return 0.0;
return v;
}
int main()
{
int x, y, n, i = 0, k, k2;
double r, f, r2, f2;
result res[100];
char string[1024], tmp[5];
//Complex number: z=x+yi
while(gets(string))
{
i++;
k = 1;
k2 = 0;
tmp[0] = string[0];
while ((string[k]!='+')&&(string[k]!='-'))
{
tmp[k] = string[k];
k++;
}
tmp[k] = '\0';
x = atoi(tmp);
while (string[k]!='i')
{
tmp[k2] = string[k];
k2++;
k++;
}
tmp[k2] = '\0';
y = atoi(tmp);
k=k+2;
k2 = 0;
while (string[k]!='\0')
{
tmp[k2++] = string[k];
k++;
}
tmp[k2] = '\0';
n = atoi(tmp);
//Trigonometric form: z=re^if
r = sqrt(x*x+y*y);
f = asin(y/r);
printf("Case %d:\n", i);
for (k = 0; k<n; k++)
{
r2 = pow(r, 1.0/n));
f2 = (f+2*k*3.1415926535897932384626433832795)/n;
res[k].x = cos(f2)*r2;
res[k].y = sin(f2)*r2;
}
qsort(res, n, sizeof(res[0]), compare);
for (k = 0; k<n; k++)
printf("%.3lf%+.3lfi\n", res[k].x, res[k].y);
printf("\n");
}
return 0;
}
Code: Select all
Removed...
Code: Select all
88+1i 50
2+56i 79
Code: Select all
void print(double a,double b)
{
char temp[50];
sprintf(temp,"%.3lf %.3lf",a,b);
sscanf(temp,"%lf %lf",&a,&b);
printf("%.3lf%+.3lfi\n",a+1e-6,b+1e-6);
}
I helps...!THX:D:D:D:DJan wrote:Your code prints -0.000 in some cases. Just check your output carefully for the following input set.
Input:I used almost similar method, but my printing method was different. The idea is to first print the values in a string and then scan from the string.Code: Select all
88+1i 50 2+56i 79
Hope these help.Code: Select all
void print(double a,double b) { char temp[50]; sprintf(temp,"%.3lf %.3lf",a,b); sscanf(temp,"%lf %lf",&a,&b); printf("%.3lf%+.3lfi\n",a+1e-6,b+1e-6); }
Answers:kn wrote:But I have a quesiton...
1. how should we choose our EPSILON?
2. And, must this method work in problem dealing with floating point numbers...?
Code: Select all
#include <iostream>
#include <stdio.h>
#include <algorithm>
#include <cmath>
using namespace std;
struct Complex{
double x,y;
bool operator<(const Complex &c)const{
if(abs(x-c.x)>0.000001) return(x>c.x);
else return(y>c.y);
}
};
int main(){
int a,b,n,t=0; double r,theta,temp,pi=acos(0)*2; char c,d;
while(cin>>a>>c>>b>>d>>n){
++t; Complex complex[n];
if(c=='-') b=-b;
r=sqrt(a*a+b*b);
theta=atan(1.0*b/a);
r=pow(r,1.0/n);
for(int i=0; i<n; ++i){
temp=(theta+2*i*pi)/n;
complex[i].x=r*cos(temp);
complex[i].y=r*sin(temp);
}
sort(complex,complex+sizeof(complex)/sizeof(Complex));
printf("Case %d:\n",t);
for(int i=0; i<n; ++i){
int h=int(complex[i].x*10000),A,B;
if(h<=-10) {printf("-"); h=-h;}
if(h%10>4) h=h/10+1; else h/=10;
A=h/1000; B=h%1000;
printf("%d.",A);
if(B<10) printf("0");
if(B<100) printf("0");
printf("%d",B);
h=int(complex[i].y*10000);
if(h<=-10) {printf("-"); h=-h;}
else printf("+");
if(h%10>4) h=h/10+1; else h/=10;
A=h/1000; B=h%1000;
printf("%d.",A);
if(B<10) printf("0");
if(B<100) printf("0");
printf("%di\n",B);
}
puts("");
}
}
Code: Select all
-59+76i 15
69-26i 5
Code: Select all
Case 1:
1.341+0.201i
1.307-0.362i
1.143+0.729i
1.047-0.862i
0.748+1.131i
0.605-1.213i
0.223+1.337i
0.060-1.355i
-0.340+1.313i
-0.496-1.262i
-0.844+1.061i
-0.967-0.951i
-1.203+0.626i
-1.270-0.475i
-1.353+0.082i
Case 2:
2.357-0.170i
0.890+2.189i
0.567-2.294i
-1.807+1.523i
-2.007-1.248i
Code: Select all
88+1i 100
Code: Select all
Case 1:
1.046+0.000i
1.044-0.066i
1.044+0.066i
1.038-0.131i
1.038+0.131i
1.027-0.196i
1.027+0.196i
1.013-0.260i
1.013+0.260i
0.995-0.323i
0.995+0.323i
0.972-0.385i
0.972+0.385i
0.946-0.445i
0.946+0.445i
0.916-0.504i
0.916+0.504i
0.883-0.560i
0.883+0.560i
0.846-0.615i
0.846+0.615i
0.806-0.667i
0.806+0.667i
0.762-0.716i
0.762+0.716i
0.716-0.762i
0.716+0.762i
0.667-0.806i
0.667+0.806i
0.615-0.846i
0.615+0.846i
0.560-0.883i
0.560+0.883i
0.504-0.916i
0.504+0.916i
0.445-0.946i
0.445+0.946i
0.385-0.972i
0.385+0.972i
0.323-0.995i
0.323+0.995i
0.260-1.013i
0.260+1.013i
0.196-1.027i
0.196+1.027i
0.131-1.038i
0.131+1.038i
0.066-1.044i
0.066+1.044i
0.000-1.046i
0.000+1.046i
-0.066-1.044i
-0.066+1.044i
-0.131-1.038i
-0.131+1.038i
-0.196-1.027i
-0.196+1.027i
-0.260-1.013i
-0.260+1.013i
-0.323-0.995i
-0.323+0.995i
-0.385-0.972i
-0.385+0.972i
-0.445-0.946i
-0.445+0.946i
-0.504-0.916i
-0.504+0.916i
-0.560-0.883i
-0.560+0.883i
-0.615-0.846i
-0.615+0.846i
-0.667-0.806i
-0.667+0.806i
-0.716-0.762i
-0.716+0.762i
-0.762-0.716i
-0.762+0.716i
-0.806-0.667i
-0.806+0.667i
-0.846-0.615i
-0.846+0.615i
-0.883-0.560i
-0.883+0.560i
-0.916-0.504i
-0.916+0.504i
-0.946-0.445i
-0.946+0.445i
-0.972-0.385i
-0.972+0.385i
-0.995-0.323i
-0.995+0.323i
-1.013-0.260i
-1.013+0.260i
-1.027-0.196i
-1.027+0.196i
-1.038-0.131i
-1.038+0.131i
-1.044-0.066i
-1.044+0.066i
-1.046+0.000i