/********************************************* * COMPLEX CALCULATOR * * ------------------------------------------ * * Ref.: "Mathématiques en Turbo-Pascal By * * M. Ducamp and A. Reverchon (vol 2), * * Eyrolles, Paris, 1988" [BIBLI 05]. * * ------------------------------------------ * * SAMPLE RUN: * * * * Calculate: sqrt((5-i)*Ln(3+2i)) * * * * Enter operation: xa Real part: 5 * * Imaginary part: -1 * * Enter operation: sto0 * * Enter operation: xa Real part: 3 * * Imaginary part: 2 * * Enter operation: sto1 * * Enter operation: ln * * Enter operation: sto2 * * Enter operation: * * * Enter operation: sto3 * * Enter operation: sqr * * Enter operation: sto4 * * * * (result is: 2.6640487 + i 0.3110939) * * * * C++ version by J-P Moreau, Paris. * * (www.jpmoreau.fr) * ********************************************** The screen should look like that: COMPLEX NUMBERS CALCULATOR IN INVERSE POLISH NOTATION ------------------------------------------------------------------- ALGEBRAIC FORM POLAR FORM T 0.0000000 Z 0.0000000 Y 0.0000000 X 2.6640487 + i 0.3110939 2.6821511 EXP+i 0.1162483 ------------------------------------------------------------------- M0 5.0000000 - i 1.0000000 5.0990195 EXP-i 0.1973956 M1 3.0000000 + i 2.0000000 3.6055513 EXP+i 0.5880026 M2 1.2824747 + i 0.5880026 1.4108467 EXP+i 0.4298922 M3 7.0003760 + i 1.6575383 7.1939348 EXP+i 0.2324967 M4 2.6640487 + i 0.3110939 2.6821511 EXP+i 0.1162483 M5 0.0000000 M6 0.0000000 M7 0.0000000 M8 0.0000000 M9 0.0000000 ------------------------------------------------------------------- + - * / 1/X X^2 SQR ^ EXP LN SIN COS TAN SH CH TH ARCSIN ARCCOS ARCTAN ARGCH ARGSH ARGTH XA XP CLX CLS CLM STOx RCLx Q ------------------------------------------------------------------- Enter operation: Command explanations: XA: enter a complex number x+iy, by giving x and y. XP: enter a complex number r*exp(i*t), by giving r and t. CLX: stack register X is set to 0 and stack is shifted downwards. This allows to correct an input mistake. CLS: All four stack registers are set to 0. STOx: x from 0 to 9. stores X content in memory number x. RCLx: x from 0 to 9, recalls content of memory x in X register. The stack is previously shifted upwards. CLM: All ten memories are set to 0. Q: Quit program. */ #include #include "complex1.h" #define NBSTACK 5 #define NBMEM 10 // The COMPLEX structure is defined in complex1.h COMPLEX stack[NBSTACK]; COMPLEX memory[NBMEM]; char oper[5]; int i,j,k; unsigned int ii; //set a complex number z to ZERO void Empty(COMPLEX *z) { z->x=0; z->y=0; z->r=0; z->t=0; } //copy z1 in z void Zcopy(COMPLEX z1, COMPLEX *z) { z->x=z1.x; z->y=z1.y; z->r=z1.r; z->t=z1.t; } void DownStack(int n) { int i; if (n<1) n=1; for (i=n; i1; i--) Zcopy(stack[i-1],&stack[i]); Empty(&stack[1]); } //print a complex number (algebraic form + polar form) void DisplayNumber(COMPLEX n) { printf(" %10.7f",n.x); if (n.y!=0) { if (n.y>0) printf(" +"); else printf(" -"); printf(" i %10.7f",fabs(n.y)); } else printf(" "); if (n.r!=0) printf(" %10.7f",n.r); else printf(" "); if (n.t!=0) { printf(" EXP"); if (n.t>0) printf("+"); else printf("-"); printf("i %10.7f\n",fabs(n.t)); } else printf(" \n"); } void InitDisplay() { printf("\n COMPLEX NUMBERS CALCULATOR IN INVERSE POLISH NOTATION \n"); printf("--------------------------------------------------------------\n"); printf(" ALGEBRAIC FORM POLAR FORM \n"); printf(" T"); DisplayNumber(stack[4]); printf(" Z"); DisplayNumber(stack[3]); printf(" Y"); DisplayNumber(stack[2]); printf(" X"); DisplayNumber(stack[1]); printf("--------------------------------------------------------------\n"); printf(" M0"); DisplayNumber(memory[0]); printf(" M1"); DisplayNumber(memory[1]); printf(" M2"); DisplayNumber(memory[2]); printf(" M3"); DisplayNumber(memory[3]); printf(" M4"); DisplayNumber(memory[4]); printf(" M5"); DisplayNumber(memory[5]); printf(" M6"); DisplayNumber(memory[6]); printf(" M7"); DisplayNumber(memory[7]); printf(" M8"); DisplayNumber(memory[8]); printf(" M9"); DisplayNumber(memory[9]); printf("--------------------------------------------------------------\n"); printf(" + - * / 1/X X^2 SQR ^ EXP LN \n"); printf(" SIN COS TAN SH CH TH ARCSIN ARCCOS ARCTAN ARGCH\n"); printf(" ARGSH ARGTH XA XP CLX CLS CLM STOx RCLx Q \n"); printf("--------------------------------------------------------------\n"); } //execute command calling elementary routines of module complex1.cpp void Compute(char *oper) { int i; if (strcmp(oper,"XA")==0) { UpStack(); printf(" Real part Imaginary part: "); scanf("%lf %lf",&stack[1].x, &stack[1].y); RectPol(&stack[1]); return; } if (strcmp(oper,"XP")==0) { UpStack(); printf(" Modulus Argument: "); scanf("%lf %lf",&stack[1].r, &stack[1].t); PolRect(&stack[1]); return; } if (strcmp(oper,"CLX")==0) { DownStack(1); return; } if (strcmp(oper,"CLS")==0) { for (i=1; i<5; i++) Empty(&stack[i]); return; } if (strcmp(oper,"CLM")==0) { for (i=0; i<10; i++) Empty(&memory[i]); return; } if (oper[0]=='S' && oper[1]=='T' && oper[2]=='O') { i=(int) oper[3]-48; if(i>-1 && i<10) Zcopy(stack[1],&memory[i]); return; } if (oper[0]=='R'&&oper[1]=='C'&&oper[2]=='L') { i=(int) oper[3]-48; if(i>-1 && i<10) { UpStack(); Zcopy(memory[i],&stack[1]); } return; } if (strcmp(oper,"+")==0) if (ZSum(stack[2],stack[1],&stack[1])) { DownStack(2); return; } if (strcmp(oper,"-")==0) if (ZMinus(stack[2],stack[1],&stack[1])) { DownStack(2); return; } if (strcmp(oper,"*")==0) if (ZMult(stack[2],stack[1],&stack[1])) { DownStack(2); return; } if (strcmp(oper,"/")==0) if (ZDiv(stack[2],stack[1],&stack[1])) { DownStack(2); return; } if (strcmp(oper,"^")==0) if (ZPower(stack[2],stack[1],&stack[1])) { DownStack(2); return; } if (strcmp(oper,"EXP")==0) if (ZExp(stack[1],&stack[1])) return; if (strcmp(oper,"LN")==0) if (ZLn(stack[1],&stack[1])) return; if (strcmp(oper,"1/X")==0) if (ZInv(stack[1],&stack[1])) return; if (strcmp(oper,"X^2")==0) if (ZSqr(stack[1],&stack[1])) return; if (strcmp(oper,"SQR")==0) if (ZSqrt(stack[1],&stack[1])) return; if (strcmp(oper,"SIN")==0) if (ZSin(stack[1],&stack[1])) return; if (strcmp(oper,"COS")==0) if (ZCos(stack[1],&stack[1])) return; if (strcmp(oper,"TAN")==0) if (ZTan(stack[1],&stack[1])) return; if (strcmp(oper,"SH")==0) if (ZSh(stack[1],&stack[1])) return; if (strcmp(oper,"CH")==0) if (ZCh(stack[1],&stack[1])) return; if (strcmp(oper,"TH")==0) if (ZTh(stack[1],&stack[1])) return; if (strcmp(oper,"ARCSIN")==0) if (ZArcsin(stack[1],&stack[1])) return; if (strcmp(oper,"ARCCOS")==0) if (ZArccos(stack[1],&stack[1])) return; if (strcmp(oper,"ARCTAN")==0) if (ZArctan(stack[1],&stack[1])) return; if (strcmp(oper,"ARGSH")==0) if (ZArgsh(stack[1],&stack[1])) return; if (strcmp(oper,"ARGCH")==0) if (ZArgch(stack[1],&stack[1])) return; if (strcmp(oper,"ARGTH")==0) if (ZArgth(stack[1],&stack[1])) return; } //compute void main() { for (i=1;i<5;i++) Empty(&stack[i]); //set stack to zero for (i=0;i<10;i++) Empty(&memory[i]); //set memory to zero InitDisplay(); //initial display do { //main loop printf(" Enter operation: "); scanf("%s",oper); //enter command for (ii=0; ii