int main (void) { int degree; degree=1; while (degree>=0) { int coef[11] = {0}; printf("Please enter the maximum degree of the polynomial: "); scanf("%d", °ree); if (degree>=0) /* exit immediately if degree is negative */ { printf("Please enter the coefficients: "); int i; for (i=0; i<=degree; i++) {scanf("%d", &coef[i]);} printf("The polynomial is "); polyprint (coef, degree); printf("\n"); int deriv[10] = {0}; for (i=0; i<degree; i++) {deriv[i]=(coef[i]*(degree-i));} /* calculate the derivative */ printf("Its derivative is "); polyprint (deriv, (degree-1)); if (degree==0) {printf("0");} printf("\n\n"); } } return 0; }
int main (void) { int degree=1; while (degree>=0) { printf("Please enter the maximum degree of the polynomial: "); scanf("%d", °ree); if (degree>=0) /* exit immediately if degree is negative */ { int *coef; coef = (int *) calloc((degree+1), sizeof(int)); /* as we are using C++, casting is required in calloc and free functions */ if (coef != NULL) /* prevent from working if the memory is not successfully allocated */ { printf("Please enter the coefficients: "); int i; for (i=0; i<=degree; i++) {scanf("%d", &coef[i]);} printf("The polynomial is "); polyprint (coef, degree); printf("\n"); int *deriv; deriv = (int *) calloc(degree, sizeof(int)); if (deriv != NULL) /* same as for first calloc */ { for (i=0; i<degree; i++) {deriv[i]=(coef[i]*(degree-i));} /* calculate the derivative */ printf("Its derivative is "); polyprint (deriv, (degree-1)); free( (void *) deriv); } if (degree==0) {printf("0");} printf("\n\n"); free((void *) coef); } } } return 0; }
int main(int argc, char **argv) { if(argc>=2) { switch(atoi(argv[1])) { case 1: return polyprint(argc-1, argv+1); case 2: return polypoly(argc-1, argv+1,2); // + case 3: return polypoly(argc-1, argv+1,3); // - case 4: return polypoly(argc-1, argv+1,4); // * case 5: return matrixmult(argc-1, argv+1,5); case 6: return polymatmult(argc-1, argv+1,6); } } // Ein Fehler ist aufgetreten. Gebe Syntax aus: cout << "Syntax: test1_02 <n> " << endl; cout << "1 - Ausgabe eines Polynoms" << endl; cout << "2 - Polynom+Polynom" << endl; cout << "3 - Polynom-Polynom" << endl; cout << "4 - Polynom*Polynom" << endl; cout << "5 - Matrix*Matrix" << endl; cout << "6 - Polynom*Matrix" << endl; return 1; // Rueckgabewert !=0, d.h. Fehler }