Don't like ads? PRO users don't see any ads ;-)
Guest

Untitled

By: a guest on Sep 2nd, 2012  |  syntax: None  |  size: 8.98 KB  |  hits: 11  |  expires: Never
download  |  raw  |  embed  |  report abuse  |  print
Text below is selected. Please press Ctrl+C to copy to your clipboard. (⌘+C on Mac)
  1.  
  2. #include <stdio.h>               /* Include standard input/output library*/
  3.  
  4. typedef struct           /* Defines data type for complex numbers */        
  5. {
  6.  float real;
  7.  float imaginary;
  8.  
  9. } complex;               /* Name of structure */
  10.  
  11.  
  12. /* Function prototypes */
  13.  
  14. int mainMenu();                                    /* Prototype for main menu*/
  15.  
  16. complex inputComplexNumber1(void);             /* prototype to get first complex number */
  17. complex inputComplexNumber2(void);             /* prototype to get second complex number */
  18.  
  19. complex addTwoComplexNumbers(complex a, complex b);             /* Prototype for adding two complex numbers */
  20. complex subTwoComplexNumbers(complex a, complex b);         /* Prototype for subtracting two complex numbers */
  21. complex multwoComplexNumbers(complex a, complex b);         /* Prototype for multiplying two complex numbers */
  22.  
  23. void clearStdin(void);                             /*prototype for removing old input values*/
  24.  
  25. int main (void)
  26. {  
  27.         int selection;  /* Number selected from the menu */
  28.  
  29.  
  30.    do
  31.   {                
  32.         selection = mainMenu();                                                          /* Function call for the main menu */
  33.      
  34.             switch(selection)
  35.       {
  36.         case 1:   {  complex a = inputComplexNumber1();                                                                                               /* function call for the complex numbers */
  37.                                  complex b = inputComplexNumber2();
  38.                          complex result = addTwoComplexNumbers(a, b);                                                                                     /* function call to add the two numbers */
  39.                  
  40.                      printf("(%.2f %.2f) - (%.2f %.2f) = (%.2f %.2f)\n\n",a.real, a.imaginary, b.real, b.imaginary, result.real, result.imaginary);   /* prints the results*/
  41.                          break;
  42.                                   }        
  43.          
  44.             case 2:   {  complex a = inputComplexNumber1();                                                                                               /* function call for the complex numbers */
  45.                                  complex b = inputComplexNumber2();
  46.                          complex result = subTwoComplexNumbers(a, b);                                                                                     /* function call to subtract the two numbers */
  47.                  
  48.                      printf("(%.2f %.2f) - (%.2f %.2f) = (%.2f %.2f)\n\n",a.real, a.imaginary, b.real, b.imaginary, result.real, result.imaginary);   /* prints the results*/
  49.                          break;  
  50.                                   }    
  51.      
  52.             case 3:   {  complex a = inputComplexNumber1();                                                                                               /* function call for the complex numbers */
  53.                                  complex b = inputComplexNumber2();
  54.                          complex result = multwoComplexNumbers(a, b);                                                                                     /* function call to multiply the two numbers */
  55.                  
  56.                      printf("(%.2f %.2f) - (%.2f %.2f) = (%.2f %.2f)\n\n",a.real, a.imaginary, b.real, b.imaginary, result.real, result.imaginary);   /* prints the results*/
  57.                          break;
  58.                                   }        
  59.          
  60.             case 4:   printf("The program is completed successfully");
  61.                       break;     
  62.      
  63.          
  64.             default:  printf("Too many attempts at menu selction");
  65.                   break;
  66.      }
  67.  
  68.          
  69.             }while (selection > 0 & selection < 4);
  70.          
  71.  
  72.          
  73. return(0);
  74.    
  75. }
  76.  
  77.  
  78.  
  79.  
  80.  
  81. int mainMenu()                                                               /* function for menu input */
  82.  
  83.   { int menuIn,                                                          /* Value for the menu input */
  84.         counter;                                                         /* Counter is used to give a limited number of attempts */
  85.                
  86.           counter=1;
  87.     printf("-------------Menu-------------\n");                          /* Prints the menu */
  88.         printf("1.Add two complex numbers\n");
  89.         printf("2.Subtract two complex numbers\n");
  90.         printf("3.Multiply two complex numbers\n");
  91.         printf("4.Exit the program\n");        
  92.         do                                                      
  93.          {      ++counter;
  94.             printf("Please select an option (1, 2, 3 or 4):");
  95.         scanf("%d", &menuIn);      
  96.                
  97.            if ((menuIn >0) && (menuIn <5) )                                  /* If statement checks if the input is valid */
  98.                     return (menuIn);
  99.            else printf("\ninput error: %d is not a valid option\n", menuIn);
  100.          }         
  101.      while (counter <3);                                                 /* Allows only two attempts at the menu */  
  102.          
  103.         return (-1);
  104.   }
  105.  
  106.  
  107. complex inputComplexNumber1(void)                                                                      /* Function to get the first complex number */
  108.  
  109. {  float actuallyEntrd;                                                                                /* Used to test if valid input */
  110.    complex a;    
  111.        
  112.      do                                                                                                                                /* do-while statement allows the input to repeat*/
  113.            {
  114.                    printf("Please enter the first complex number in the format a b, then press enter: ");
  115.                  
  116.                  
  117.                    if ( (actuallyEntrd = scanf("%f %f", &a.real, &a.imaginary)) != 2 )           
  118.                   {      printf("! Input Failure !\n");
  119.                                  clearStdin();                                                                         /* Function call to clear the input if not valid */  
  120.                           }
  121.                    else  (actuallyEntrd !=2);    
  122.            }while (actuallyEntrd !=2);
  123.            
  124.        
  125.         return (a);                                                                                        /* Returns complex number a */        
  126.  }
  127.  
  128. complex inputComplexNumber2(void)                                                                      /* Function to get the first complex number */
  129.  
  130. {  float actuallyEntrd;                                                                                /* Used to test if valid input */
  131.    complex b;    
  132.        
  133.  
  134.    do                                                                                                                                  /* do-while statement allows the input to repeat*/
  135.            {
  136.                    printf("Please enter the first complex number in the format a b, then press enter: ");
  137.                  
  138.                  
  139.                    if ( (actuallyEntrd = scanf("%f %f", &b.real, &b.imaginary)) != 2 )           
  140.                   {      printf("! Input Failure !\n");
  141.                                  clearStdin();                                                                         /* Function call to clear the input if not valid */  
  142.                           }
  143.                    else  (actuallyEntrd !=2);    
  144.            }while (actuallyEntrd !=2);
  145.            
  146.        
  147.         return (b);                                                                                            /* Returns complex number B */  
  148.  }
  149.  
  150.  
  151. complex addTwoComplexNumbers(complex a, complex b)                                                     /* Function for adding two complex numbers */
  152. {
  153.    complex result;                                                                                     /* Declares the result as a struct */
  154.    
  155.    result.real= a.real + b.real;                                                                       /* Calculates the real result */
  156.    
  157.    result.imaginary = a.imaginary + b.imaginary;                                                       /* Calculates the imaginary result */
  158.    
  159.    return result;                                                                                      /* Returns the result */
  160.    
  161.  
  162. }
  163.  
  164.  
  165. complex subTwoComplexNumbers(complex a, complex b)                                                     /* Function for subtracting two complex numbers */
  166. {
  167.    complex result;                                                                                     /* Declares the result as a struct */
  168.    
  169.    result.real= a.real - b.real;                                                                       /* Calculates the real result */
  170.    
  171.    result.imaginary = a.imaginary - b.imaginary;                                                       /* Calculates the imaginary result */
  172.    
  173.    return result;                                                                                      /* Returns the result */
  174.    
  175.  
  176. }
  177.  
  178.  
  179. complex multwoComplexNumbers(complex a, complex b)                                                     /* Function for multiplying two complex numbers */
  180. {
  181.    complex result;                                                                                     /* Declares the result as a struct */
  182.    
  183.    result.real= (a.real*b.real) - (a.imaginary*b.imaginary);                                           /* Calculates the real result */
  184.    
  185.    result.imaginary = (a.real*b.real) + (a.imaginary*b.imaginary);                                     /* Calculates the imaginary result */
  186.    
  187.    return result;                                                                                      /* Returns the result */
  188.    
  189.  
  190. }
  191.  
  192.  
  193.  
  194. void clearStdin(void)                                                                                                                          /* Function for clearing the input*/
  195.  
  196.   {
  197.        
  198.         scanf("%*[^\n]");                                                                                                                                  /* skip to the end-of-line */
  199.         scanf("%*1[\n]");                                                                                                                                  /* skip one new-line */
  200.         return;
  201.   }