From: Per W. <pw...@ia...> - 2004-03-08 17:13:26
|
After a cursory glance, I have the following notes: 1) use <iostream> etc instead of <iostream.h> 2) specify "using std::cout" etc for things declared in header files without the .h extension. 3) the Complesso::show() method has the printf arguments AFTER closing parenthesis. You will get random numbers printed... 4) for efficiency, a program should not use printf() and std::cout at the same time, since the two output functions needs to perform flush() all the time to allow intermixed output. 5) your program should have: #ifndef M_PI #define M_PI 3.14159265358979323846 #endif 6) your funz() may NOT call main - you should nevery try to call main() yourself. It is only the startup code that may call main(). 7) when you implement factorial, you either do it as a loop, or as a recursive function taking the number as a parameter. Never create a mathematical function that takes it's parameter on the stack! /Per W On Mon, 8 Mar 2004, Erika Brattich wrote: > > > > > Hi guys! > I have experienced a lot of problems programming with Dev-cpp 4.0; though, I > continue to think that it is one of the most well-working application in order > to program with C++ using Windows. > So, I will make a list of my problems. > First of all, I have some problems with the system of input/output. It seems > that the program does not recognise cin >>...But it's probably my fault. > This is the guilty program: > > > #include <iostream.h> > #include <stdio.h> > #include <stdlib.h> > #include <math.h> > #include <complex.h> > > > > class Complesso > { > double re, im; /* le due componenti di un numero complesso*/ > > public: > Complesso(); /* costruttore di default */ > Complesso(double r, double i); /* costruttore con le due componenti */ > Complesso operator+ (Complesso c); /* overloading di '+' per la somma fra > numeri complessi */ > Complesso operator-(Complesso d); /* overloading di '-' per la differenza > fra numeri complessi */ > Complesso operator*(Complesso e); /* overloading di '*' per il prodotto fra > numeri complessi */ > Complesso operator/(Complesso f); /* overloading di '/' per la divisione fra > numeri complessi */ > Complesso operator=(double re); > double show(); > }; > /* fine dichiarazioni */ > > > > Complesso::Complesso() > { > re = 0; > im = 0; > } > > Complesso::Complesso(double r, double i) > { > re = r; > im = i; > } > > Complesso Complesso::operator+(Complesso c){ > /* overloading di '+' per la somma fra numeri complessi */ > Complesso a, b; > c.re = a.re + b.re; > c.im = a.im + b.im; > return c; > } > Complesso Complesso::operator-(Complesso d){ > /* overloading di '-' per la differenza fra numeri complessi */ > Complesso a, b; > d.re = a.re - b.re; > d.im = a.im - b.im; > return d; > } > Complesso Complesso::operator*(Complesso e){ > /* overloading di '*' per il prodotto fra numeri complessi. > NOTA: il prodotto del numero Complesso (a + ib) per il numero complesso > (c + id) da' come risultato il numero complesso > (ac-bd) + i(ad+bc) */ > Complesso a, b; > e.re = a.re*b.re - b.im*b.im; > e.im = a.re*b.im + b.im*b.re; > return e; > } > Complesso Complesso::operator/(Complesso f){ > /* overloading di '/' per la divisione fra numeri complessi. > NOTA: la divisione del numero complesso (a + ib) per il numero complesso > (c + id) e' definita solo se almeno uno fra c e d e' diverso da 0. > Il risultato e' il numero complesso > (ac + bd)/(c^2+d^2)+ i (bc - ad)/(c^2+d^2) */ > > Complesso a, b; > if (f.re!=0 || f.im!=0){ > f.re = (a.re*b.re + a.im*b.im)/(a.re*b.re + b.im*b.im); > f.im = (a.im*b.re - a.re*b.im)/(a.re*b.re + b.im*b.im); > } > return f; > }; > Complesso a, b, c, d, e, f; > > Complesso Complesso::operator=(double re) > { > c = c.re + c.im; > return *this; > } > double Complesso::show() > { > printf("%lf, %lf"), c.re, c.im ; > printf("c.re\n"); > printf("c.im\n"); > > } > > > int main() > { > cout <<"dammi a: ", cin >> (Complesso a); > cout <<"\ndammi b: ", cin >> (Complesso b); > c = a + b ; > cout<< "la somma di a e b \350 c \n" ; > c.show(); > system("PAUSE"); > return 0; > } > > It is a classs that should do all the operstions between complex numbers, but > it does'nt work very well. > Dev-cpp doesn't show any fault, though. > > There is another version that works well on others pc, but on mine M_PI is not > recognoised. Here it is: > > #include <iostream.h> > #include <stdio.h> > #include <math.h> > > class complex > { > double a[200], b[200], radice[2], somma[2], divisione[2], prodotto[2], x1[2], > x2[2]; > double x, y, z, t, p1, p2, p3, Delta, RAD, modulo, theta; > int i, n, h; > > public: > > int Avvio(int r); > int Importa(); > int Somma(); > int Prodotto(); > int Divisione(); > int Radice(); > int Polinomio(); > }; > > int complex::Avvio(int r) > { > switch(r) > { > case 1: {Somma(); break;} > case 2: {Prodotto(); break;} > case 3: {Divisione(); break;} > case 4: {Radice(); break;} > default: Polinomio(); > } > } > > int complex::Importa() > { > printf("\nQuanti numeri complessi vuoi usare max[200]? "); > scanf("%d", &n); > > printf("\nInserisci i coefficienti dei %d numeri.\n", n); > > for (i=0; i<n; i++) > { > printf("\na%d: ", i); > scanf("%lf", &a[i]); > printf("b%d: ", i); > scanf("%lf", &b[i]); > } > } > > int complex::Somma() > { > Importa(); > > somma[0]= a[0]; > somma[1]= b[0]; > > for(i=1; i<n; i++) > { > somma[0]+= a[i]; > somma[1]+= b[i]; > } > > printf("\nSomma: %f", somma[0]); > if (somma[1]>=0) printf("+%fi\n", somma[1]); > else printf("%fi\n", somma[1]); > } > > int complex::Prodotto() > { > Importa(); > > prodotto[0]= a[0]; > prodotto[1]= b[0]; > > for(i=1; i<n; i++) > { > x= prodotto[0]* a[i]; > y= prodotto[0]* b[i]; > z= prodotto[1]* a[i]; > t= prodotto[1]* b[i]; > > prodotto[0]= x - t; > prodotto[1]= y + z; > } > > printf("\nProdotto: %f", prodotto[0]); > if (prodotto[1]>=0) printf("+%fi\n", prodotto[1]); > else printf("%fi\n", prodotto[1]); > } > > int complex::Divisione() > { > printf("\nScrivi i coefficienti dei due numeri della divisione:\n"); > printf("\na0= "); > scanf("%lf", &a[0]); > printf("b0= "); > scanf("%lf", &b[0]); > printf("\na1= "); > scanf("%lf", &a[1]); > printf("b1= "); > scanf("%lf", &b[1]); > > if(a[1]==0 && b[1]==0) printf("\nERRORE! DIVISIONE PER ZERO NON AMMESSA.\n"); > else > { > > divisione[0]=(a[0]*a[1] - b[0]*(-b[1]))/(a[1]*a[1] + b[1]*b[1]); > divisione[1]=(a[0]*(-b[1]) + b[0]*a[1])/(a[1]*a[1] + b[1]*b[1]); > > printf("\nDivisione: %f", divisione[0]); > if (divisione[1]>=0) printf("+%fi\n", divisione[1]); > else printf("%fi\n", divisione[1]); > } > } > > int complex::Radice() > { > printf("\nScrivi i coefficenti [a,b] e l'indice [k] della radice:\n"); > > printf("\na= "); > scanf("%lf", &a[0]); > printf("b= "); > scanf("%lf", &b[0]); > printf("k= "); > scanf("%d", &n); > > modulo=sqrt(a[0]*a[0] + b[0]*b[0]); > theta=atan2(b[0],a[0]); > x=pow(modulo,1./n); > > for(h=0; h<n; h++) > { > radice[0]=x*(cos((theta + 2*M_PI*h)/n)); > radice[1]=x*(sin((theta + 2*M_PI*h)/n)); > > printf("\nZ(%d)= ", h); printf("%f", radice[0]); > if (radice[1]>0) printf("+%fi\n", radice[1]); > else printf("%fi\n", radice[1]); > } > } > > int complex::Polinomio() > { > printf("\nScrivi i coefficienti del polinomio\n"); > printf("\na= "); > scanf("%lf", &p1); > printf("b= "); > scanf("%lf", &p2); > printf("c= "); > scanf("%lf", &p3); > > Delta=(p2*p2) - (4*p1*p3); > > if (Delta>=0) > { > RAD= sqrt(Delta); > x1[0]= (-p2 + RAD)/(2*p1); > x2[0]= (-p2 - RAD)/(2*p1); > > printf("\nx1= %f", x1[0]); > printf("\nx2= %f\n", x2[0]); > } > > else > { > RAD= ((-1)*(Delta)); > > x1[0]= (-p2)/(2*p1); > x1[1]= (RAD)/(2*p1); > x2[0]= (-p2)/(2*p1); > x2[1]= (-RAD)/(2*p1); > > printf("\nx1= %f", x1[0]); > if (x1[1]>=0) printf("+%fi\n", x1[1]); > else printf("%fi\n", x1[1]); > > printf("\nx2= %f", x2[0]); > if (x2[1]>=0) printf("+%fi\n", x2[1]); > else printf("%fi\n", x2[1]); > } > } > > int Richiesta() > { > int scelta; > complex e; > > printf("\n[1] SOMMA di n numeri;"); > printf("\n[2] PRODOTTO di n numeri;"); > printf("\n[3] DIVISIONE tra 2 numeri;"); > printf("\n[4] RADICE k-esima di un numero;"); > printf("\n[5] RADICI di un polinomio di secondo grado;\n"); > > printf("\nNumero scelto: "); > scanf("%d", &scelta); > if(scelta<1 || scelta>5) > { > printf("\nERRORE!!! NUMERO NON AMMESSO\n"); > Richiesta(); > } > else > { > e.Avvio(scelta); > > char c; > printf("\nSE SI VUOLE ESEGUIRE UN ALTRO CALCOLO PREMERE [s]o [S]: "); > scanf("%s", &c); > if(c=='s' || c=='S') Richiesta(); > } > } > > int main() > { > printf("Questo programma esegue calcoli con i NUMERI COMPLESSI."); > printf("\nDigita il numero indicato per utilizzare le varie operazioni:\n"); > > Richiesta(); > } // > > > But the most unexpecteded thing is in the last program: it works well on two > others pcs, one has dev-cpp. It must give the factorial of a number, but when I > execute it , it only returns the number followed by 0.000000... > #include <stdio.h> > #include <stdlib.h> > > long double l, n; > > int fattoriale (); > int valori(); > int funz(); > > int main() > { > printf("Scrivi il numero da fattorializzare: "); > scanf("%Lf", &n); > > valori(); > fattoriale(); > funz(); > system("PAUSE"); > return 0; > } > > int valori() > { > if((n>0) && (n<2000)) > { > l=n-1; > fattoriale(); > printf("Il fattoriale \350: %Le\n", n); > } > else if(n==0) printf ("Il fattoriale \350: 1\n"); > else if(n<0) printf ("Non posso calcolare il fattoriale di un numero > negativo\n"); > else printf ("Non posso calcolare il fattoriale, il numero \350 troppo > elevato\n"); > funz(); > } > > int fattoriale() > { > while(l>1) > { > n=n*l--; > fattoriale(); > } > } > > int funz() > { > char c; > printf ("Vuoi calcolare un altro fattoriale? Premere [s] o [S] se si: \n", c); > scanf("%s", &c); > if(c=='s'|| c=='S') main(); > else printf ("Esecuzione terminata\n"); > } > > And it worked well even with another C program. Now I will try to execute them > with dev-cpp 5.0 because I think that it isn't only my fault and my loss of > experience. Could you help me to find the bugs? > > Thank you > > Erika > > > > > > > ------------------------------------------------------- > This SF.Net email is sponsored by: IBM Linux Tutorials > Free Linux tutorial presented by Daniel Robbins, President and CEO of > GenToo technologies. Learn everything from fundamentals to system > administration.http://ads.osdn.com/?ad_id=1470&alloc_id=3638&op=click > _______________________________________________ > Dev-cpp-users mailing list > Dev...@li... > TO UNSUBSCRIBE: http://www23.brinkster.com/noicys/devcpp/ub.htm > https://lists.sourceforge.net/lists/listinfo/dev-cpp-users > |