michael_hartman_cz

PA1 - úkol 5.2 (Násobení polynomů)

Mar 28th, 2013
379
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 2.45 KB | None | 0 0
  1. #include <iostream>
  2. using namespace std;
  3. #define err "Nespravny vstup." << endl
  4.  
  5. int main ( int argc, char * argv[] )
  6.  {
  7.     // read and check polynomial A
  8.     int n;
  9.     cout << "Zadejte stupen polynomu A:" << endl;
  10.     cin >> n;  
  11.     if ( cin.fail() || n < 0 )
  12.      {
  13.         cout << err;
  14.         return 1;
  15.      }
  16.  
  17.     int * pA = new int[n+1];
  18.     cout << "Zadejte koeficienty polynomu A:" << endl;
  19.     for (int i = n ; i >= 0; i--)
  20.      {
  21.         cin >> pA[i];
  22.         if ( cin.fail() )
  23.          {
  24.             cout << err;
  25.             delete[] pA;
  26.             return 1;
  27.          }
  28.      }      
  29.        
  30.     // read and check polynomial B
  31.     int m;
  32.     cout << "Zadejte stupen polynomu B:" << endl;
  33.     cin >> m;
  34.     if ( cin.fail() || m < 0)
  35.      {
  36.         cout << err;
  37.         delete[] pA;
  38.         return 1;
  39.      }
  40.    
  41.     int * pB = new int[m+1];
  42.     cout << "Zadejte koeficienty polynomu B:" << endl;
  43.     for ( int i = m ; i >= 0; i--)
  44.      {
  45.         cin >> pB[i];
  46.         if ( cin.fail() )
  47.          {
  48.             cout << err;
  49.             delete[] pA;
  50.             delete[] pB;
  51.             return 1;
  52.          }
  53.      }
  54.    
  55.     //declare and initialize array for result
  56.     int * res = new int[m+n+1];
  57.     for ( int i = m+n; i >= 0 ; i-- )
  58.         res[i] = 0;
  59.    
  60.     // count each witch each coz im not brave enought to implement FFT :-)
  61.     for ( int i = n; i >= 0; i-- )
  62.      {
  63.         for ( int j = m; j >= 0; j-- )
  64.          {
  65.             res[i+j] += pA[i] * pB[j];
  66.          }
  67.      }
  68.      
  69.     //print result
  70.     bool first = true;
  71.     bool zero = true;
  72.     for ( int i = m+n; i >= 0 ; i-- )
  73.      {
  74.         if ( res[i] == 0 ) //suppress print of elements with zero coefficient
  75.             continue;
  76.         if ( first ) //print first element
  77.          {
  78.             if ( res[i] > 1 || res[i] < -1 )
  79.              {
  80.                 cout << res[i];
  81.                 zero = false;
  82.              }
  83.             else if ( res[i] == -1 )
  84.              {
  85.                 cout << "-";
  86.                 zero = false;
  87.              }
  88.             first = false;          
  89.          }
  90.         else //print other elements
  91.          {
  92.             if ( res[i] > 1 || res[i] < -1  ||  i == 0  )
  93.              {
  94.                 cout << showpos << res[i];
  95.                 zero = false;
  96.              }
  97.             else if ( res[i] == 1 )
  98.                     cout << "+";
  99.                  else
  100.                     cout << "-";
  101.          }
  102.         if ( i != 0 ) // suppress print of x with zero exponent
  103.          {
  104.             cout << "x";
  105.             zero = false;
  106.          }
  107.            
  108.         if ( i != 1 && i != 0 ) // print only non-one and non-zero exponents
  109.             cout << "<sup>" << noshowpos << i << "</sup>";
  110.      }
  111.     if ( zero )
  112.         cout << "0";
  113.     cout << endl;
  114.    
  115.     //free memory and terminate
  116.     delete[] pA;
  117.     delete[] pB;
  118.     delete[] res;
  119.     return 0;
  120.  }
Advertisement
Add Comment
Please, Sign In to add comment