/**************************************************** CPSC 211 Author: Teresa Leyk Complex numbers, adding and multiplying complex numbers, reading in complex numbers (interactively or from a file). If we read from a file we use the following convention: * complex numbers are represented as pairs of double numbers * after this pair goes an operation code: 1 for adding 2 for multiplication * end of file ends reading in. * after each pair read in we print out the result. We assume that interactively we enter only one complex pair. The result is printed on output. *****************************************************/ #include /*** complex type ***/ typedef struct { double re, im; } Complex; /*** operations on complex numbers ***/ Complex complex_add(Complex a, Complex b) { Complex c; c.re = a.re + b.re; c.im = a.im + b.im; return c; } Complex complex_mult(Complex a, Complex b) { Complex c; c.re = a.re*b.re - a.im*b.im; c.im = a.re*b.im + a.im*b.re; return c; } Complex read_complex_number(FILE *fp) { double re, im; Complex a; fscanf (fp," %lf %lf",&re, &im); a.re = re; a.im = im; return a; } /*** some constants ***/ typedef enum { Quit = 0, Interactively, FromFile } Reading_mode; typedef enum { None = 0, Add = 1, Multiply = 2 } Operation_mode; /*** choose reading mode: 1 - interactively 2 - from a file check correctness of entered mode ***/ void choose_reading(Reading_mode *rm) { int i; printf("\n\nChoose reading mode:\n" "1. interactively\n" "2. from a file\n" "0. quit\n\n"); scanf(" %d", &i); if (i < 0 || i > 2) { printf("ERROR: wrong choice: %d\n\n",i); exit(1); } *rm = i; } /*** choose operation mode: 1 - adding 2 - multiplying check correctness of entered mode ***/ void choose_operation(Operation_mode *op) { int i; printf("\n\nChoose operation:\n" "1. adding\n" "2. multiplying\n\n"); scanf(" %d", &i); if (i < 1 || i > 2) { printf("ERROR: wrong choice: %d\n\n",i); exit(1); } *op = i; } Operation_mode read_operation(FILE *fp) { int i; fscanf(fp, " %d",&i); if (i < 0 || i > 2) { printf("ERROR: wrong operation: %d\n\n",i); exit(1); } return i; } /*** get a file name ***/ void choose_file_name(FILE **fp) { char name[50]; printf("Enter file name:\n"); scanf(" %49s", name); *fp = fopen(name, "r"); if (*fp == NULL) { printf("ERROR: the file \"%s\" cannot be open\n\n", name); exit(1); } } /* nice print-out */ void print_operation(Operation_mode op, Complex a, Complex b, Complex c) { char op_char; switch (op) { case None: return; case Add: op_char = '+'; break; case Multiply: op_char = '*'; break; } printf("(%g, %g) %c (%g, %g) = (%g, %g)\n", a.re, a.im, op_char, b.re, b.im, c.re, c.im); } void read_numbers_from_file(void) { FILE *fp; Complex a, b, c; Operation_mode op; choose_file_name(&fp); while (!feof(fp)) { a = read_complex_number(fp); b = read_complex_number(fp); /* read operation */ op = read_operation(fp); printf("Result is:\n"); switch (op) { case None: printf("End of file\n"); return; case Add: c = complex_add(a,b); print_operation(op, a, b, c); break; case Multiply: c = complex_mult(a, b); print_operation(op, a, b, c); break; } printf("\n"); } } void read_numbers_interactively(void) { Complex a, b, c; Operation_mode op; printf("Enter the first complex number (pair of real numbers): \n"); a = read_complex_number(stdin); printf("Enter the second complex number (pair of real numbers): \n"); b = read_complex_number(stdin); choose_operation(&op); printf("\nResult is:\n"); switch (op) { case None: return; case Add: c = complex_add(a,b); print_operation(op, a, b, c); break; case Multiply: c = complex_mult(a, b); print_operation(op, a, b, c); break; } printf("\n\n"); } void menu(void) { Reading_mode rm; choose_reading(&rm); switch (rm) { case Quit: exit(0); case Interactively: read_numbers_interactively(); break; case FromFile: read_numbers_from_file(); break; } } int main(void) { while (1) /* infinite loop */ menu(); }