Advertisement
thespeedracer38

Complex

Dec 7th, 2018
136
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C 1.82 KB | None | 0 0
  1. #include <stdio.h>
  2. #include <stdlib.h>
  3.  
  4. typedef struct
  5. {
  6.     double x;
  7.     double y;
  8. }Complex;
  9.  
  10. void input(Complex *num)
  11. {
  12.     printf("Enter the real part ");
  13.     scanf("%lf",&num->x);  
  14.     printf("Enter the imaginary part ");
  15.     scanf("%lf",&num->y);
  16. }
  17.  
  18. Complex add(Complex num1,Complex num2)
  19. {
  20.     Complex sum;
  21.     sum.x = num1.x + num2.x;
  22.     sum.y = num1.y + num2.y;
  23.     return sum;
  24. }
  25.  
  26. Complex subtract(Complex num1,Complex num2)
  27. {
  28.     Complex diff;
  29.     diff.x = num1.x - num2.x;
  30.     diff.y = num1.y - num2.y;
  31.     return diff;
  32. }
  33.  
  34. Complex multiply(Complex num1,Complex num2)
  35. {
  36.     Complex product;
  37.     product.x = (num1.x*num2.x) - (num1.y*num2.y);
  38.     product.y = (num1.x*num2.y) + (num2.x*num1.y);
  39.     return product;
  40. }
  41.  
  42. Complex divide(Complex num1,Complex num2)
  43. {
  44.     Complex div;
  45.     div.x = ((num1.x*num2.x) + (num1.y*num2.y))/((num2.x*num2.x)+(num2.y*num2.y));
  46.     div.y = ((num2.x*num1.y) - (num1.x*num2.y))/((num2.x*num2.x)+(num2.y*num2.y));
  47.     return div;
  48. }
  49.  
  50. void display(Complex num)
  51. {
  52.     printf("%.2lf+i%.2lf \n",num.x,num.y); 
  53. }
  54.  
  55. int main()
  56. {
  57.     Complex num1,num2;
  58.     int choice;
  59.     do
  60.     {
  61.         printf("Entering the first complex number \n");
  62.         input(&num1);  
  63.         printf("Entering the second complex number \n");
  64.         input(&num2);
  65.         printf("Press 1 to add \n ");
  66.         printf("Press 2 to subtract \n ");
  67.         printf("Press 3 to multiply \n ");
  68.         printf("Press 4 to divide \n ");
  69.         printf("Press 5 to exit \n ");
  70.         printf("Enter your choice [1-5] ");
  71.         scanf("%d",&choice);
  72.         switch(choice)
  73.         {
  74.             case 1:
  75.                 printf("The sum is ");
  76.                 display(add(num1,num2));
  77.                 break;
  78.             case 2:
  79.                 printf("The difference is ");
  80.                 display(subtract(num1,num2));
  81.                 break;
  82.             case 3:
  83.                 printf("The product is ");
  84.                 display(multiply(num1,num2));
  85.                 break;
  86.             case 4:
  87.                 printf("The quotient is ");
  88.                 display(divide(num1,num2));
  89.                 break;
  90.             case 5:
  91.                 break;
  92.             default:
  93.                 printf("Bhag bc");             
  94.         }
  95.     }
  96.     while(choice!=5);
  97.     return 0;
  98. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement