Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #include <iostream>
- using namespace std;
- #define err "Nespravny vstup." << endl
- int main ( int argc, char * argv[] )
- {
- // read and check polynomial A
- int n;
- cout << "Zadejte stupen polynomu A:" << endl;
- cin >> n;
- if ( cin.fail() || n < 0 )
- {
- cout << err;
- return 1;
- }
- int * pA = new int[n+1];
- cout << "Zadejte koeficienty polynomu A:" << endl;
- for (int i = n ; i >= 0; i--)
- {
- cin >> pA[i];
- if ( cin.fail() )
- {
- cout << err;
- delete[] pA;
- return 1;
- }
- }
- // read and check polynomial B
- int m;
- cout << "Zadejte stupen polynomu B:" << endl;
- cin >> m;
- if ( cin.fail() || m < 0)
- {
- cout << err;
- delete[] pA;
- return 1;
- }
- int * pB = new int[m+1];
- cout << "Zadejte koeficienty polynomu B:" << endl;
- for ( int i = m ; i >= 0; i--)
- {
- cin >> pB[i];
- if ( cin.fail() )
- {
- cout << err;
- delete[] pA;
- delete[] pB;
- return 1;
- }
- }
- //declare and initialize array for result
- int * res = new int[m+n+1];
- for ( int i = m+n; i >= 0 ; i-- )
- res[i] = 0;
- // count each witch each coz im not brave enought to implement FFT :-)
- for ( int i = n; i >= 0; i-- )
- {
- for ( int j = m; j >= 0; j-- )
- {
- res[i+j] += pA[i] * pB[j];
- }
- }
- //print result
- bool first = true;
- bool zero = true;
- for ( int i = m+n; i >= 0 ; i-- )
- {
- if ( res[i] == 0 ) //suppress print of elements with zero coefficient
- continue;
- if ( first ) //print first element
- {
- if ( res[i] > 1 || res[i] < -1 )
- {
- cout << res[i];
- zero = false;
- }
- else if ( res[i] == -1 )
- {
- cout << "-";
- zero = false;
- }
- first = false;
- }
- else //print other elements
- {
- if ( res[i] > 1 || res[i] < -1 || i == 0 )
- {
- cout << showpos << res[i];
- zero = false;
- }
- else if ( res[i] == 1 )
- cout << "+";
- else
- cout << "-";
- }
- if ( i != 0 ) // suppress print of x with zero exponent
- {
- cout << "x";
- zero = false;
- }
- if ( i != 1 && i != 0 ) // print only non-one and non-zero exponents
- cout << "<sup>" << noshowpos << i << "</sup>";
- }
- if ( zero )
- cout << "0";
- cout << endl;
- //free memory and terminate
- delete[] pA;
- delete[] pB;
- delete[] res;
- return 0;
- }
Advertisement
Add Comment
Please, Sign In to add comment