# 基哥作業3 ```cpp= // Polynomial division provided that the quotient and remainder have integer coefficients #include <iostream> using std::cin; using std::cout; using std::endl; #include <fstream> using std::ifstream; using std::ios; // outputs the specified polynomial void output( int polynomial[], int degree ); // returns true if and only if the specified polynomial is zero polynomial bool isZero( int polynomial[], int degree ); // returns true if and only if polynomial1 == polynomial2 bool equal( int polynomial1[], int polynomial2[], int degree1, int degree2 ); // polynomial1 = -polynomial2 void minus( int polynomial1[], int polynomial2[], int &degree1, int degree2 ); // addend += adder void addition( int addend[], int adder[], int &addendDegree, int adderDegree ); // minuend -= subtrahend void subtraction( int minuend[], int subtrahend[], int &minuendDegree, int subtrahendDegree ); // product = multiplicand * multiplier void multiplication( int multiplicand[], int multiplier[], int product[], int multiplicandDegree, int multiplierDegree, int &productDegree ); // quotient = dividend / divisor; remainder = dividend % divisor // provided that dividendDegree >= divisorDegree void division( int dividend[], int divisor[], int quotient[], int remainder[], int dividendDegree, int divisorDegree, int &quotientDegree, int &remainderDegree ); const int numTestCases = 200; // the number of test cases const int arraySize = 20; int main() { ifstream inFile( "Polynomials.dat", ios::in | ios::binary ); //inFile.open("Polynomials.dat");/**/ // exit program if ifstream could not open file if( !inFile ) { cout << "File could not be opened\n"; system( "pause" ); exit( 1 ); } int numErrors = numTestCases; for( int i = 0; i < numTestCases; i++ ) { int dividend[ arraySize ] = {}; int divisor[ arraySize ] = {}; // input dividend and divisor from the file Polynomials.dat inFile.read( reinterpret_cast< char * >( dividend ), 80 ); inFile.read( reinterpret_cast< char * >( divisor ), 80 ); int dividendDegree = arraySize - 1; while( dividendDegree > 0 && dividend[ dividendDegree ] == 0 ) dividendDegree--; int divisorDegree = arraySize - 1; while( divisorDegree > 0 && divisor[ divisorDegree ] == 0 ) divisorDegree--; cout << "dividend: "; output( dividend, dividendDegree ); cout << "divisor : "; output( divisor, divisorDegree ); cout << endl; int quotient[ arraySize ] = {}; int remainder[ arraySize ] = {}; int quotientDegree = 0; int remainderDegree = 0; // quotient = dividend / divisor; remainder = dividend % divisor // thus, dividend == divisor * quotient + remainder division( dividend, divisor, quotient, remainder, dividendDegree, divisorDegree, quotientDegree, remainderDegree ); if( quotientDegree != 0 && quotient[ quotientDegree ] == 0 ) //有bug cout << "Leading coefficient of quotient cannot be zero!\n"; if( remainderDegree != 0 && remainder[ remainderDegree ] == 0 ) cout << "Leading coefficient of remainder cannot be zero!\n"; int buffer[ arraySize ] = {}; int bufferDegree = 1; // buffer = divisor * quotient multiplication( divisor, quotient, buffer, divisorDegree, quotientDegree, bufferDegree ); if( bufferDegree != 0 && buffer[ bufferDegree ] == 0 ) cout << "Leading coefficient of buffer cannot be zero!\n"; // buffer = buffer + remainder = divisor * quotient + remainder addition( buffer, remainder, bufferDegree, remainderDegree ); if( bufferDegree != 0 && buffer[ bufferDegree ] == 0 ) cout << "Leading coefficient of buffer cannot be zero!\n"; // if buffer != dividend, an error occurred! if( equal( buffer, dividend, bufferDegree, dividendDegree ) ) numErrors--; } inFile.close(); cout << "There are " << numErrors << " errors!\n\n"; system( "pause" ); } // outputs the specified polynomial void output( int polynomial[], int degree ) { if( isZero( polynomial, degree ) ) // zero polynomial cout << 0; else { if( degree == 0 ) // constant polynomial { if( polynomial[ 0 ] < 0 ) cout << "-" << -polynomial[ 0 ]; else if( polynomial[ 0 ] > 0 ) cout << polynomial[ 0 ]; } else { if( degree == 1 ) // polynomial of degree 1 { if( polynomial[ 1 ] < 0 ) cout << "-" << -polynomial[ 1 ] << "x"; else if( polynomial[ 1 ] > 0 ) cout << polynomial[ 1 ] << "x"; } else // polynomial of degree at least 2 { // display the leading term if( polynomial[ degree ] < 0 ) cout << "-" << -polynomial[ degree ] << "x^" << degree; else if( polynomial[ degree ] > 0 ) cout << polynomial[ degree ] << "x^" << degree; // display all other terms for( int i = degree - 1; i > 1; i-- ) if( polynomial[ i ] < 0 ) cout << " - " << -polynomial[ i ] << "x^" << i; else if( polynomial[ i ] > 0 ) cout << " + " << polynomial[ i ] << "x^" << i; // display the term of degree 1 if( polynomial[ 1 ] < 0 ) cout << " - " << -polynomial[ 1 ] << "x"; else if( polynomial[ 1 ] > 0 ) cout << " + " << polynomial[ 1 ] << "x"; } // display the constant term if( polynomial[ 0 ] < 0 ) cout << " - " << -polynomial[ 0 ]; else if( polynomial[ 0 ] > 0 ) cout << " + " << polynomial[ 0 ]; } } cout << endl; } // returns true if and only if the specified polynomial is zero polynomial bool isZero( int polynomial[], int degree ) { // leading term is 0 if( degree == 0 && polynomial[ 0 ] == 0 ) return true; return false; } // returns true if and only if polynomial1 == polynomial2 bool equal( int polynomial1[], int polynomial2[], int degree1, int degree2 ) { //直接先判斷Degree if( degree1 != degree2 ) return false; for( int i = 0; i <= degree1; i++ ) if( polynomial1[ i ] != polynomial2[ i ] ) return false; return true; } // polynomial1 = -polynomial2 (把多項式加負號) void minus( int polynomial1[], int polynomial2[], int &degree1, int degree2 ) { degree1 = degree2; for( int i = 0; i <= degree1; i++ ) polynomial1[ i ] = -polynomial2[ i ]; } // addend += adder void addition( int addend[], int adder[], int &addendDegree, int adderDegree ) //被加數的degree判斷 { ////////////////////////(debug完成) //執行加法 for (int i = 0; i <= addendDegree; i++) { addend[i] += adder[i]; } //判斷加法後的Degree //都是0的話 int index = 0; for (int i = 0; i < 20; i++) { if (addend[i] == 0) { index++; } } if (index == 20) { addendDegree = 0; } //不為0 for (int i = 19; i >= 0; i--) { if (addend[i] != 0) { addendDegree = i; break; } } if( addendDegree != 0 && addend[ addendDegree ] == 0 ) //最高次方不為0 and 最高次項係數為0 cout << "Leading coefficient of addend cannot be zero!\n"; } // minuend -= subtrahend (//remainder = remainder - buffer) void subtraction( int minuend[], int subtrahend[], int &minuendDegree, int subtrahendDegree ) { int buffer[ arraySize ] = {}; int bufferDegree = 0; // buffer = -subtrahend (把buffer加上負號讓餘數去加) minus( buffer, subtrahend, bufferDegree, subtrahendDegree ); // minuend = minuend + buffer = minuend - subtrahend (餘數+(-buffer)) addition( minuend, buffer, minuendDegree, bufferDegree ); if( minuendDegree != 0 && minuend[ minuendDegree ] == 0 ) cout << "Leading coefficient of minuend cannot be zero!\n"; } // product = multiplicand * multiplier void multiplication( int multiplicand[], int multiplier[], int product[], int multiplicandDegree, int multiplierDegree, int &productDegree ) { ///// //算出product(debug正確) for (int i = 0; i < arraySize; i++) { for (int j = 0; j < arraySize; j++) { product[i + j] += multiplicand[j] * multiplier[i]; } } //判斷乘法後值的Degree //值都是0 int index = 0; for (int i = 0; i < 20; i++) { if (product[i] == 0) { index++; } } if (index == 20) { productDegree = 0; } //值不為0 for (int i = 19; i >= 0; i--) { if (product[i] != 0) { productDegree = i; break; } } if( productDegree != 0 && product[ productDegree ] == 0 ) cout << "Leading coefficient of product cannot be zero!\n"; } // quotient = dividend / divisor; remainder = dividend % divisor // provided that dividendDegree >= divisorDegree void division( int dividend[], int divisor[], int quotient[], int remainder[], int dividendDegree, int divisorDegree, int &quotientDegree, int &remainderDegree ) { if( isZero( dividend, dividendDegree ) ) { quotientDegree = 0; quotient[ 0 ] = 0; remainderDegree = 0; remainder[ 0 ] = 0; return; } //&quotientDegree餘數(固定值) quotientDegree = dividendDegree - divisorDegree; //被除數存入餘數做計算 for (int i = 0; i < arraySize; i++) { remainder[i] = dividend[i]; } remainderDegree = dividendDegree; //計算 while (remainderDegree >= divisorDegree) //當餘數degree小於除數Degree跳出迴圈 (remainderDegree11 8 7 卡在9) { int i = remainderDegree - divisorDegree; //目前要計算商的位置 quotient[i] = remainder[remainderDegree] / divisor[divisorDegree]; //算出當前位置的商 //算當前buffer int buffer2[arraySize] = {}; int bufferDegree2 = 0; multiplication(divisor, quotient, buffer2, divisorDegree, i, bufferDegree2); //算餘數 subtraction(remainder, buffer2, remainderDegree, bufferDegree2); //餘數為0跳出迴圈 if (remainderDegree == 0 && remainder[0] == 0) { break; } //餘數Degree = 除數Degree = 0 且 餘數<除數 跳出迴圈 if ( remainderDegree == 0 && divisorDegree == 0 && remainder[0] < divisor[0]) { break; } } if( quotientDegree != 0 && quotient[ quotientDegree ] == 0 ) //有bug cout << "Leading coefficient of quotient cannot be zero!\n"; if( remainderDegree != 0 && remainder[ remainderDegree ] == 0 ) cout << "Leading coefficient of remainder cannot be zero!\n"; } ``` 祥威改過的 ```cpp= // Polynomial division provided that the quotient and remainder have integer coefficients #include <iostream> using std::cin; using std::cout; using std::endl; #include <fstream> using std::ifstream; using std::ios; // outputs the specified polynomial void output(int polynomial[], int degree); // returns true if and only if the specified polynomial is zero polynomial bool isZero(int polynomial[], int degree); // returns true if and only if polynomial1 == polynomial2 bool equal(int polynomial1[], int polynomial2[], int degree1, int degree2); // polynomial1 = -polynomial2 void minus(int polynomial1[], int polynomial2[], int& degree1, int degree2); // addend += adder void addition(int addend[], int adder[], int& addendDegree, int adderDegree); // minuend -= subtrahend void subtraction(int minuend[], int subtrahend[], int& minuendDegree, int subtrahendDegree); // product = multiplicand * multiplier void multiplication(int multiplicand[], int multiplier[], int product[], int multiplicandDegree, int multiplierDegree, int& productDegree); // quotient = dividend / divisor; remainder = dividend % divisor // provided that dividendDegree >= divisorDegree void division(int dividend[], int divisor[], int quotient[], int remainder[], int dividendDegree, int divisorDegree, int& quotientDegree, int& remainderDegree); const int numTestCases = 200; // the number of test cases const int arraySize = 20; int main() { ifstream inFile("Polynomials.dat", ios::in | ios::binary); //inFile.open("Polynomials.dat");/**/ // exit program if ifstream could not open file if (!inFile) { cout << "File could not be opened\n"; system("pause"); exit(1); } int numErrors = numTestCases; for (int i = 0; i < numTestCases; i++) { int dividend[arraySize] = {}; int divisor[arraySize] = {}; // input dividend and divisor from the file Polynomials.dat inFile.read(reinterpret_cast<char*>(dividend), 80); inFile.read(reinterpret_cast<char*>(divisor), 80); int dividendDegree = arraySize - 1; while (dividendDegree > 0 && dividend[dividendDegree] == 0) dividendDegree--; int divisorDegree = arraySize - 1; while (divisorDegree > 0 && divisor[divisorDegree] == 0) divisorDegree--; cout << "dividend: "; output(dividend, dividendDegree); cout << "divisor : "; output(divisor, divisorDegree); cout << endl; int quotient[arraySize] = {}; int remainder[arraySize] = {}; int quotientDegree = 0; int remainderDegree = 0; // quotient = dividend / divisor; remainder = dividend % divisor // thus, dividend == divisor * quotient + remainder division(dividend, divisor, quotient, remainder, dividendDegree, divisorDegree, quotientDegree, remainderDegree); if (quotientDegree != 0 && quotient[quotientDegree] == 0) //有bug cout << "Leading coefficient of quotient cannot be zero!\n"; if (remainderDegree != 0 && remainder[remainderDegree] == 0) cout << "Leading coefficient of remainder cannot be zero!\n"; int buffer[arraySize] = {}; int bufferDegree = 1; // buffer = divisor * quotient multiplication(divisor, quotient, buffer, divisorDegree, quotientDegree, bufferDegree); if (bufferDegree != 0 && buffer[bufferDegree] == 0) cout << "Leading coefficient of buffer cannot be zero!\n"; // buffer = buffer + remainder = divisor * quotient + remainder addition(buffer, remainder, bufferDegree, remainderDegree); if (bufferDegree != 0 && buffer[bufferDegree] == 0) cout << "Leading coefficient of buffer cannot be zero!\n"; // if buffer != dividend, an error occurred! if (equal(buffer, dividend, bufferDegree, dividendDegree)) numErrors--; } inFile.close(); cout << "There are " << numErrors << " errors!\n\n"; system("pause"); } // outputs the specified polynomial void output(int polynomial[], int degree) { if (isZero(polynomial, degree)) // zero polynomial cout << 0; else { if (degree == 0) // constant polynomial { if (polynomial[0] < 0) cout << "-" << -polynomial[0]; else if (polynomial[0] > 0) cout << polynomial[0]; } else { if (degree == 1) // polynomial of degree 1 { if (polynomial[1] < 0) cout << "-" << -polynomial[1] << "x"; else if (polynomial[1] > 0) cout << polynomial[1] << "x"; } else // polynomial of degree at least 2 { // display the leading term if (polynomial[degree] < 0) cout << "-" << -polynomial[degree] << "x^" << degree; else if (polynomial[degree] > 0) cout << polynomial[degree] << "x^" << degree; // display all other terms for (int i = degree - 1; i > 1; i--) if (polynomial[i] < 0) cout << " - " << -polynomial[i] << "x^" << i; else if (polynomial[i] > 0) cout << " + " << polynomial[i] << "x^" << i; // display the term of degree 1 if (polynomial[1] < 0) cout << " - " << -polynomial[1] << "x"; else if (polynomial[1] > 0) cout << " + " << polynomial[1] << "x"; } // display the constant term if (polynomial[0] < 0) cout << " - " << -polynomial[0]; else if (polynomial[0] > 0) cout << " + " << polynomial[0]; } } cout << endl; } // returns true if and only if the specified polynomial is zero polynomial bool isZero(int polynomial[], int degree) { // leading term is 0 if (degree == 0 && polynomial[0] == 0) return true; return false; } // returns true if and only if polynomial1 == polynomial2 bool equal(int polynomial1[], int polynomial2[], int degree1, int degree2) { //直接先判斷Degree if (degree1 != degree2) return false; for (int i = 0; i <= degree1; i++) if (polynomial1[i] != polynomial2[i]) return false; return true; } // polynomial1 = -polynomial2 (把多項式加負號) void minus(int polynomial1[], int polynomial2[], int& degree1, int degree2) { degree1 = degree2; for (int i = 0; i <= degree1; i++) polynomial1[i] = -polynomial2[i]; } // addend += adder void addition(int addend[], int adder[], int& addendDegree, int adderDegree) //被加數的degree判斷 { ////////////////////////(debug完成) //執行加法 for (int i = 0; i <= addendDegree; i++) { addend[i] += adder[i]; } //判斷加法後的Degree //都是0的話 int index = 0; for (int i = 0; i < 20; i++) { if (addend[i] == 0) { index++; } } if (index == 20) { addendDegree = 0; } //不為0 for (int i = 19; i >= 0; i--) { if (addend[i] != 0) { addendDegree = i; break; } } if (addendDegree != 0 && addend[addendDegree] == 0) //最高次方不為0 and 最高次項係數為0 cout << "Leading coefficient of addend cannot be zero!\n"; } // minuend -= subtrahend (//remainder = remainder - buffer) void subtraction(int minuend[], int subtrahend[], int& minuendDegree, int subtrahendDegree) { int buffer[arraySize] = {}; int bufferDegree = 0; // buffer = -subtrahend (把buffer加上負號讓餘數去加) minus(buffer, subtrahend, bufferDegree, subtrahendDegree); // minuend = minuend + buffer = minuend - subtrahend (餘數+(-buffer)) addition(minuend, buffer, minuendDegree, bufferDegree); if (minuendDegree != 0 && minuend[minuendDegree] == 0) cout << "Leading coefficient of minuend cannot be zero!\n"; } // product = multiplicand * multiplier void multiplication(int multiplicand[], int multiplier[], int product[], int multiplicandDegree, int multiplierDegree, int& productDegree) { ///// //算出product(debug正確) for (int i = 0; i < arraySize; i++) { for (int j = 0; j < arraySize; j++) { product[i + j] += multiplicand[j] * multiplier[i]; } } //判斷乘法後值的Degree //值都是0 int index = 0; for (int i = 0; i < 20; i++) { if (product[i] == 0) { index++; } } if (index == 20) { productDegree = 0; } //值不為0 for (int i = 19; i >= 0; i--) { if (product[i] != 0) { productDegree = i; break; } } if (productDegree != 0 && product[productDegree] == 0) cout << "Leading coefficient of product cannot be zero!\n"; } // quotient = dividend / divisor; remainder = dividend % divisor // provided that dividendDegree >= divisorDegree void division(int dividend[], int divisor[], int quotient[], int remainder[], int dividendDegree, int divisorDegree, int& quotientDegree, int& remainderDegree) { if (isZero(dividend, dividendDegree)) { quotientDegree = 0; quotient[0] = 0; remainderDegree = 0; remainder[0] = 0; return; } //&quotientDegree餘數(固定值) quotientDegree = dividendDegree - divisorDegree; //被除數存入餘數做計算 for (int i = 0; i < arraySize; i++) { remainder[i] = dividend[i]; } remainderDegree = dividendDegree; //計算 while (remainderDegree >= divisorDegree) //當餘數degree小於除數Degree跳出迴圈 (remainderDegree11 8 7 卡在9) { int i = remainderDegree - divisorDegree; //目前要計算商的位置 //-> int temp[arraySize] = {}; temp[i] = remainder[remainderDegree] / divisor[divisorDegree]; //<- quotient[i] = remainder[remainderDegree] / divisor[divisorDegree]; //算出當前位置的商 //算當前buffer int buffer2[arraySize] = {}; int bufferDegree2 = 0; //含有改這裡 multiplication(divisor,temp, buffer2, divisorDegree, i, bufferDegree2); //算餘數 subtraction(remainder, buffer2, remainderDegree, bufferDegree2); //餘數為0跳出迴圈 if (remainderDegree == 0 && remainder[0] == 0) { break; } //餘數Degree = 除數Degree = 0 且 餘數<除數 跳出迴圈 if (remainderDegree == 0 && divisorDegree == 0 && remainder[0] < divisor[0]) { break; } } if (quotientDegree != 0 && quotient[quotientDegree] == 0) //有bug cout << "Leading coefficient of quotient cannot be zero!\n"; if (remainderDegree != 0 && remainder[remainderDegree] == 0) cout << "Leading coefficient of remainder cannot be zero!\n"; } ``` e93e9eee改 ```cpp= // Polynomial division provided that the quotient and remainder have integer coefficients #include <iostream> using std::cin; using std::cout; using std::endl; #include <fstream> using std::ifstream; using std::ios; // outputs the specified polynomial void output(int polynomial[], int degree); // returns true if and only if the specified polynomial is zero polynomial bool isZero(int polynomial[], int degree); // returns true if and only if polynomial1 == polynomial2 bool equal(int polynomial1[], int polynomial2[], int degree1, int degree2); // polynomial1 = -polynomial2 void minus(int polynomial1[], int polynomial2[], int& degree1, int degree2); // addend += adder void addition(int addend[], int adder[], int& addendDegree, int adderDegree); // minuend -= subtrahend void subtraction(int minuend[], int subtrahend[], int& minuendDegree, int subtrahendDegree); // product = multiplicand * multiplier void multiplication(int multiplicand[], int multiplier[], int product[], int multiplicandDegree, int multiplierDegree, int& productDegree); // quotient = dividend / divisor; remainder = dividend % divisor // provided that dividendDegree >= divisorDegree void division(int dividend[], int divisor[], int quotient[], int remainder[], int dividendDegree, int divisorDegree, int& quotientDegree, int& remainderDegree); const int numTestCases = 200; // the number of test cases const int arraySize = 20; int main() { ifstream inFile("Polynomials.dat", ios::in | ios::binary); //inFile.open("Polynomials.dat");/**/ // exit program if ifstream could not open file if (!inFile) { cout << "File could not be opened\n"; system("pause"); exit(1); } int numErrors = numTestCases; for (int i = 0; i < numTestCases; i++) { int dividend[arraySize] = {}; int divisor[arraySize] = {}; // input dividend and divisor from the file Polynomials.dat inFile.read(reinterpret_cast<char*>(dividend), 80); inFile.read(reinterpret_cast<char*>(divisor), 80); int dividendDegree = arraySize - 1; while (dividendDegree > 0 && dividend[dividendDegree] == 0) dividendDegree--; int divisorDegree = arraySize - 1; while (divisorDegree > 0 && divisor[divisorDegree] == 0) divisorDegree--; cout << "dividend: "; output(dividend, dividendDegree); cout << "divisor : "; output(divisor, divisorDegree); cout << endl; int quotient[arraySize] = {}; int remainder[arraySize] = {}; int quotientDegree = 0; int remainderDegree = 0; // quotient = dividend / divisor; remainder = dividend % divisor // thus, dividend == divisor * quotient + remainder division(dividend, divisor, quotient, remainder, dividendDegree, divisorDegree, quotientDegree, remainderDegree); if (quotientDegree != 0 && quotient[quotientDegree] == 0) //有bug cout << "Leading coefficient of quotient cannot be zero!\n"; if (remainderDegree != 0 && remainder[remainderDegree] == 0) cout << "Leading coefficient of remainder cannot be zero!\n"; int buffer[arraySize] = {}; int bufferDegree = 1; // buffer = divisor * quotient multiplication(divisor, quotient, buffer, divisorDegree, quotientDegree, bufferDegree); if (bufferDegree != 0 && buffer[bufferDegree] == 0) cout << "Leading coefficient of buffer cannot be zero!\n"; // buffer = buffer + remainder = divisor * quotient + remainder addition(buffer, remainder, bufferDegree, remainderDegree); if (bufferDegree != 0 && buffer[bufferDegree] == 0) cout << "Leading coefficient of buffer cannot be zero!\n"; // if buffer != dividend, an error occurred! if (equal(buffer, dividend, bufferDegree, dividendDegree)) numErrors--; } inFile.close(); cout << "There are " << numErrors << " errors!\n\n"; system("pause"); } // outputs the specified polynomial void output(int polynomial[], int degree) { if (isZero(polynomial, degree)) // zero polynomial cout << 0; else { if (degree == 0) // constant polynomial { if (polynomial[0] < 0) cout << "-" << -polynomial[0]; else if (polynomial[0] > 0) cout << polynomial[0]; } else { if (degree == 1) // polynomial of degree 1 { if (polynomial[1] < 0) cout << "-" << -polynomial[1] << "x"; else if (polynomial[1] > 0) cout << polynomial[1] << "x"; } else // polynomial of degree at least 2 { // display the leading term if (polynomial[degree] < 0) cout << "-" << -polynomial[degree] << "x^" << degree; else if (polynomial[degree] > 0) cout << polynomial[degree] << "x^" << degree; // display all other terms for (int i = degree - 1; i > 1; i--) if (polynomial[i] < 0) cout << " - " << -polynomial[i] << "x^" << i; else if (polynomial[i] > 0) cout << " + " << polynomial[i] << "x^" << i; // display the term of degree 1 if (polynomial[1] < 0) cout << " - " << -polynomial[1] << "x"; else if (polynomial[1] > 0) cout << " + " << polynomial[1] << "x"; } // display the constant term if (polynomial[0] < 0) cout << " - " << -polynomial[0]; else if (polynomial[0] > 0) cout << " + " << polynomial[0]; } } cout << endl; } // returns true if and only if the specified polynomial is zero polynomial bool isZero(int polynomial[], int degree) { // leading term is 0 if (degree == 0 && polynomial[0] == 0) return true; return false; } // returns true if and only if polynomial1 == polynomial2 bool equal(int polynomial1[], int polynomial2[], int degree1, int degree2) { //直接先判斷Degree if (degree1 != degree2) return false; for (int i = 0; i <= degree1; i++) if (polynomial1[i] != polynomial2[i]) return false; return true; } // polynomial1 = -polynomial2 (把多項式加負號) void minus(int polynomial1[], int polynomial2[], int& degree1, int degree2) { degree1 = degree2; for (int i = 0; i <= degree1; i++) polynomial1[i] = -polynomial2[i]; } // addend += adder void addition(int addend[], int adder[], int& addendDegree, int adderDegree) //被加數的degree判斷 { ////////////////////////(debug完成) //執行加法 for (int i = 0; i <= addendDegree; i++) { addend[i] += adder[i]; } //判斷加法後的Degree //都是0的話 int index = 0; for (int i = 0; i < 20; i++) { if (addend[i] == 0) { index++; } } if (index == 20) { addendDegree = 0; } //不為0 for (int i = 19; i >= 0; i--) { if (addend[i] != 0) { addendDegree = i; break; } } if (addendDegree != 0 && addend[addendDegree] == 0) //最高次方不為0 and 最高次項係數為0 cout << "Leading coefficient of addend cannot be zero!\n"; } // minuend -= subtrahend (//remainder = remainder - buffer) void subtraction(int minuend[], int subtrahend[], int& minuendDegree, int subtrahendDegree) { int buffer[arraySize] = {}; int bufferDegree = 0; // buffer = -subtrahend (把buffer加上負號讓餘數去加) minus(buffer, subtrahend, bufferDegree, subtrahendDegree); // minuend = minuend + buffer = minuend - subtrahend (餘數+(-buffer)) addition(minuend, buffer, minuendDegree, bufferDegree); if (minuendDegree != 0 && minuend[minuendDegree] == 0) cout << "Leading coefficient of minuend cannot be zero!\n"; } // product = multiplicand * multiplier void multiplication(int multiplicand[], int multiplier[], int product[], int multiplicandDegree, int multiplierDegree, int& productDegree) { ///// //算出product(debug正確) for (int i = 0; i < multiplicandDegree; i++) { product[multiplierDegree + i] += multiplicand[i] * multiplier[multiplicandDegree]; } //判斷乘法後值的Degree //值都是0 int index = 0; for (int i = 0; i < 20; i++) { if (product[i] == 0) { index++; } } if (index == 20) { productDegree = 0; } //值不為0 for (int i = 19; i >= 0; i--) { if (product[i] != 0) { productDegree = i; break; } } if (productDegree != 0 && product[productDegree] == 0) cout << "Leading coefficient of product cannot be zero!\n"; } // quotient = dividend / divisor; remainder = dividend % divisor // provided that dividendDegree >= divisorDegree void division(int dividend[], int divisor[], int quotient[], int remainder[], int dividendDegree, int divisorDegree, int& quotientDegree, int& remainderDegree) { if (isZero(dividend, dividendDegree)) { quotientDegree = 0; quotient[0] = 0; remainderDegree = 0; remainder[0] = 0; return; } //&quotientDegree餘數(固定值) quotientDegree = dividendDegree - divisorDegree; //被除數存入餘數做計算 for (int i = 0; i < arraySize; i++) { remainder[i] = dividend[i]; } remainderDegree = dividendDegree; //計算 while (remainderDegree >= divisorDegree) //當餘數degree小於除數Degree跳出迴圈 (remainderDegree11 8 7 卡在9) { int i = remainderDegree - divisorDegree; //目前要計算商的位置 quotient[i] = remainder[remainderDegree] / divisor[divisorDegree]; //算出當前位置的商 //算當前buffer int buffer2[arraySize] = {}; int bufferDegree2 = 0; multiplication(divisor, quotient, buffer2, divisorDegree, i, bufferDegree2); //算餘數 subtraction(remainder, buffer2, remainderDegree, bufferDegree2); //餘數為0跳出迴圈 if (remainderDegree == 0 && remainder[0] == 0) { break; } //餘數Degree = 除數Degree = 0 且 餘數<除數 跳出迴圈 if (remainderDegree == 0 && divisorDegree == 0 && remainder[0] < divisor[0]) { break; } } if (quotientDegree != 0 && quotient[quotientDegree] == 0) //有bug cout << "Leading coefficient of quotient cannot be zero!\n"; if (remainderDegree != 0 && remainder[remainderDegree] == 0) cout << "Leading coefficient of remainder cannot be zero!\n"; } ```