Advertisement
karbaev

linear-eq

Mar 8th, 2016
69
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 2.70 KB | None | 0 0
  1. //система линейных уравнений с 2мя переменными
  2. #include <stdio.h>
  3.  
  4. void displayMessage();
  5.  
  6. int hasSolution(double a, double b, double d, double e);
  7.  
  8. double getX(double a, double b, double c, double d, double e, double f);
  9.  
  10. double getY(double a, double b, double c, double d, double e, double f);
  11.  
  12. void displaySolutions(double x, double y);
  13.  
  14.  
  15. int menu();
  16.  
  17.  
  18. int main() {
  19.     double a, b, c, d, e, f;    
  20.     double x, y;                
  21.     int choice;              
  22.     displayMessage();
  23.  
  24. do
  25.     {
  26.         do
  27.         {
  28.          menu();
  29.          scanf( "%d", &choice );
  30.         } while (choice < 1 || choice > 3);
  31.         switch (choice)
  32.  
  33.         {
  34.  
  35.             case 1:    
  36.                 printf(  "Enter a, b, and c: " );
  37.                 scanf( "%lf", &a );
  38.                 scanf( "%lf", &b );
  39.                 scanf( "%lf", &c );
  40.                 printf(  "Enter d, e, and f: " );
  41.                 scanf( "%lf", &d );
  42.                 scanf( "%lf", &e );
  43.                 scanf( "%lf", &f );
  44.                 break;
  45.            case 2:    
  46.                 if (hasSolution(a, b, d, e))
  47.                 {  
  48.                     x = getX(a, b, c, d, e, f);
  49.                     y = getY(a, b, c, d, e, f);
  50.                     displaySolutions(x,y);
  51.                 }                
  52.                     else
  53.                     {    
  54.                         printf( "The system has no solution\n" );
  55.  
  56.                     }
  57.                break;
  58.            case 3:    
  59.                printf(  "Thanks for using my program\n" );
  60.         }
  61.     } while (choice != 3);    
  62.  
  63.  
  64.     return 0;
  65. }
  66.  
  67.  
  68.  
  69. void displayMessage(){
  70.        printf( "This program calculates solutions to systems of linear equations of the form\n\n");
  71.        printf( "     aX + bY = c\n" );
  72.        printf( "     dX + eY = f\n\n" );
  73.        return;
  74. }
  75.  
  76. int hasSolution(double a, double b, double d, double e){
  77.         int s;
  78.         s=(a*e - d*b != 0);
  79.         return s;
  80. }
  81.  
  82.  
  83. double getX(double a, double b, double c, double d, double e, double f){
  84.         double x;
  85.         x=(c * e - f * b) / (a * e - d * b);
  86.         return x;
  87. }
  88.  
  89.  
  90. double getY(double a, double b, double c, double d, double e, double f){
  91.         double  y;
  92.         y=(a * f - d * c) / (a * e - d * b);
  93.         return y;
  94. }
  95.  
  96.  
  97. void displaySolutions(double x, double y)
  98. {
  99. printf ("The solution is (%lf, %lf)\n", x, y );
  100. return;
  101. }
  102.  
  103. int menu()
  104. {
  105.             printf( "\nEnter  1 to enter a new system\n" );
  106.             printf( "       2 to find the solution of the system\n" );
  107.             printf( "       3 to exit\n" );
  108.             printf( "Choice: " );
  109. return 0;
  110. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement