Advertisement
AlexanderRomanovEltx

Lesson: Structure, task 1

Jun 30th, 2015
207
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C 2.82 KB | None | 0 0
  1. # include <stdio.h>
  2.  
  3. void fn_add (float x1, float x2, float y1, float y2, float *res_x, float *res_y);
  4. void fn_sub (float x1, float x2, float y1, float y2, float *res_x, float *res_y);
  5. void fn_mul (float x1, float x2, float y1, float y2, float *res_x, float *res_y);
  6. void fn_div (float x1, float x2, float y1, float y2, float *res_x, float *res_y);
  7.  
  8. main (void)
  9. {
  10.         struct compl {
  11.                 float x;
  12.                 float y;
  13.         };
  14.         struct compl z1, z2, res;
  15.         float *zx, *zy;
  16.         int op;
  17.         zx=&res.x;
  18.         zy=&res.y;
  19.         while (1) {
  20.                 op=5; // quit by default
  21.                 z1.x=z1.y=z2.x=z2.y=0;
  22.                 printf ("1. Add\n2. Substract\n3. Multiplication\n4. Division\n5. Quit\nPlese select operation: ");
  23.                 scanf ("%d", &op);
  24.                 if ((op>=1)&&(op<=4)) {
  25.                         printf ("\nPlease input real part of first complex value: ");
  26.                         scanf ("%f", &z1.x);
  27.                         printf ("\nPlease input imaginary part of first complex value: ");
  28.                         scanf ("%f", &z1.y);
  29.                         printf ("\nPlease input real part of second complex value: ");
  30.                         scanf ("%f", &z2.x);
  31.                         printf ("\nPlease input imaginary part of second complex value: ");
  32.                         scanf ("%f", &z2.y);
  33.                         if ((z2.x==0)&&(z2.y==0)&&(op==4)) {
  34.                                 printf ("\nDivision by zero is impossible\n\n");
  35.                         } else {
  36.                                 switch (op) {
  37.                                         case 1: fn_add (z1.x, z2.x, z1.y, z2.y, zx, zy); break;
  38.                                         case 2: fn_sub (z1.x, z2.x, z1.y, z2.y, zx, zy); break;
  39.                                         case 3: fn_mul (z1.x, z2.x, z1.y, z2.y, zx, zy); break;
  40.                                         case 4: fn_div (z1.x, z2.x, z1.y, z2.y, zx, zy); break;
  41.                                 }
  42.                                 printf ("\nResult z=(%2.2f, %2.2f)\n\n", res.x, res.y);
  43.                         }
  44.                 } else if (op==5) {
  45.                                 break;
  46.                 }
  47.         }
  48. }
  49.  
  50. void fn_add (float x1, float x2, float y1, float y2, float *res_x, float *res_y)
  51. {
  52.         *res_x=x1+x2;
  53.         *res_y=y1+y2;
  54. }
  55.  
  56. void fn_sub (float x1, float x2, float y1, float y2, float *res_x, float *res_y)
  57. {
  58.         *res_x=x1-x2;
  59.         *res_y=y1-y2;
  60. }
  61.  
  62. void fn_mul (float x1, float x2, float y1, float y2, float *res_x, float *res_y)
  63. {
  64.         *res_x=x1*x2-y1*y2;
  65.         *res_y=x2*y1+x1*y2;
  66. }
  67.  
  68. void fn_div (float x1, float x2, float y1, float y2, float *res_x, float *res_y)
  69. {
  70.         *res_x=(x1*x2+y1*y2)/(x2*x2+y2*y2);
  71.         *res_y=(y1*x2-x1*y2)/(x2*x2+y2*y2);
  72. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement