int main (void) { puts("\n 작성자 : 201121403 한대호"); ListHead *A, *B, *C; A = createLinkedList(); B = createLinkedList(); C = createLinkedList(); addLastNode(A, 4,3); addLastNode(A, 3,2); addLastNode(A, 5,1); printf("\n A(x) = "); printPoly(A); addLastNode(B, 3,4); addLastNode(B, 1,3); addLastNode(B, 2,1); addLastNode(B, 1,0); printf("\n B(x) = "); printPoly(B); addPoly(A, B, C); printf("\n C(x) = "); printPoly(C); getchar(); return 0; }
// prints a 2d array with N columns void print2ArrayN(float A[][N][maxDegree+1], int len) { int m, n; printf("\n"); for(n = 0; n < len; ++n) { for(m = 0; m < N; ++m) { if (m == 0 && n == 0) { printf("(("); printPoly(A[n][m], 0); printf(", "); } else if (m == 0) { printf("("); printPoly(A[n][m], 0); printf(", "); } else if (m == M - 1 && n == len - 1) { printPoly(A[n][m], 0); printf("))"); } else if (m == M - 1) { printPoly(A[n][m], 0); printf(")"); } else { printPoly(A[n][m], 0); printf(", "); } } printf("\n"); } printf("\n"); }
int main(void) { polyptr a,b,c; readPoly2(&a); printPoly(a); readPoly2(&b); printPoly(b); c=add(a,b); printPoly(c); }
int main(void) { int maxdeg; //The maximum degree of the polynomial int i; //Iteration counter int * coeffs=NULL; //Creates a null pointer to allocate memory to bool first; //Boolean value to use for showing the first statement of a polynomial while (true) { //Creates an infinite loop so that program can keep asking for polynomials printf("Please enter the maximum degree of the polynomial: "); //Request the maximum degree of the polynomial scanf("%d", &maxdeg); if (maxdeg<0) { return 0; //Terminate program } coeffs = (int*) realloc(coeffs, (maxdeg+1)*sizeof(int)); //Allocates memory for an appropriately sized array for the coefficients printf("Please enter the coefficients: "); //Request the coefficients for (i=0;i<=maxdeg;i++) { //Request a value for each coefficient up to the maximum degree scanf("%d", &coeffs[i]); } printf("The polynomial is "); first=true; for (i=maxdeg;i>=0;i--) { //Iterate through all coefficients of polynomial first=printPoly(coeffs[maxdeg-i],i,first); //Print parts of polynomial } if (first) { printf("0"); //Catch for if nothing's still been printed } printf("\n"); printf("Its derivative is "); first=true; for (i=maxdeg;i>=0;i--) { //Iterate through all coefficients of polynomial first=printPoly(coeffs[maxdeg-i]*i,i-1,first); //Pass the coefficient multiplied by exponent, and exponent minus 1 } if (first) { printf("0"); //Catch for if nothing's still been printed } printf("\n\n"); free(coeffs); //Frees up allocated memory coeffs = NULL; //Sets coeffs back to a null pointer } }
int main() { Poly* p1 = readPoly(); Poly* p2 = readPoly(); Poly* sum; /* printPoly(p1); printPoly(p2); printf("\n\n\n"); */ if( (p1 == NULL) && (p2 == NULL) ) { fprintf(stderr, "Could not allocate memory\n"); return 1; } printPoly( sum = addPoly( p1, p2 ) ); freePoly(p1); freePoly(p2); freePoly(sum); return 0; }
int main(int argc, char const *argv[]){ int i,a,b; int tmp; Poly li, lp, mulp, sump; li = newPoly(); lp = newPoly(); if ( NULL == li || NULL == lp) return 1; li->exp = 0; lp->exp = 0; srand( time(NULL) ); for(i = 0 ; i< LSIZE; i++){ a = rand()/(RAND_MAX/LSIZE); b = rand()/(RAND_MAX/LSIZE); addItem( a, b, li); addItem( b, a, lp); } //removeDup( li); //removeDup( lp); printPoly( li ); printPoly( lp ); mulp = polyMultiply2( li, lp); printf("\nmultiply : " ); printPoly( mulp ); deletePoly( mulp); sump = polyAdd( li, lp); printf("\nadd : " ); printPoly( sump ); deletePoly( sump ); printf("\n li ^2: "); printPoly( polyExp(li,2) ); //swapNext( first(li), li ); deletePoly( li ); deletePoly( lp ); return 0; }
void printArray(float A[][maxDegree+1], int len) { int i; for(i = 0; i < len; ++i) { if (i == 0) { printf("("); printPoly(A[i], 0); printf(", "); } else if (i == len - 1) { printPoly(A[i], 0); printf("))"); } else { printPoly(A[i], 0); printf(", "); } } printf("\n"); }
/* * print details of a Poly object. * if str!=NULL, then write those details in str instead * (assuming str is big enough). * for debugging only. */ void printPoly(Poly *poly, char *str) { int delete_str = 0; int i; if(str==NULL) { str = (char*)malloc(1024); delete_str = 1; } if(poly==NULL) { sprintf(str, "invalid poly (NULL)"); } else { Vertex *v; char substr[256]; sprintf(str, "%d vertices: ", poly->num_verts); for(v=poly->verts; v<poly->verts+poly->num_verts; v++) { sprintf(substr, "(%+01.3f, %+01.3f)", v->x, v->y); strcat(str, substr); if(v<(poly->verts+(poly->num_verts-1))) { strcat(str, ", "); } } sprintf(substr, "; area: %.3f", poly->area); strcat(str, substr); if(poly->normals!=NULL && poly->dists!=NULL) { Vector *n; strcat(str, "; sides: "); for(n=poly->normals, i=0; i<poly->num_verts; i++, n++) { sprintf(substr, "{(%+01.3f, %+01.3f), %+01.3f}", n->x, n->y, poly->dists[i]); strcat(str, substr); if(i<(poly->num_verts-1)) { strcat(str, ", "); } } } if(poly->num_triangles>0) { strcat(str, "; triangulation: "); for(i=0; i<poly->num_triangles; i++) { printPoly(poly->triangles+i, substr); strcat(str, "{"); strcat(str, substr); strcat(str, "}"); if(i<(poly->num_triangles-1)) { strcat(str, ", "); } } } } if(delete_str) { strcat(str, "\n"); printf(str); free(str); } }
int main(int argc, char **argv) { double test[] = {1, 1, 2, 0, 16, 27, 66}; double p[] = {2, 2, 3, 5, 6, 7,8,9}; double q[] = {2, 2, 3, 5, 6, 7,8,9}; double r[255]={0}; int factor; factor = polyProdFast(8, p, 8, q, r); /* printPoly(4, p); printPoly(4, q);*/ printPoly(factor, r); return EXIT_SUCCESS; }
// helper method for printDiff void printDiffRow(float p[][maxDegree+1]) { int n; printf("("); for(n = 0; n < N; ++n) { if (equalsZero(p[n]) == 0) { printf("("); printPoly(p[n], 0); printf(")"); printf("*f_%d", n); if (n + 1 < N) { printf(" + "); } } } printf(")"); }
int main(){ int inputNum; int num1; int num2; int array1[ARRAY_SIZE]; int array2[ARRAY_SIZE]; int results[2 * ARRAY_SIZE]; fillArrayWithZeroes(array1, ARRAY_SIZE); fillArrayWithZeroes(array2, ARRAY_SIZE); fillArrayWithZeroes(results, (2 * ARRAY_SIZE)); char ch; scanf("%d%c%d", &num1, &ch, &num2); printf("num1 = %d\n", num1); printf("num2 = %d\n", num2); //Check for bad stdin inputs if ((num1 >= 100) || (num1 <= -100)) { printf("num1 not in the range [-100, 100]!\n"); return -1; } else if ((num2 >= 100) || (num2 <= -100)) { printf("num2 not in the range [-100, 100]!\n"); return -1; } else if ((num1 < 1) || (num1 < 1)){ printf("Input for n cannot be smaller than one!"); return -1; } else if ((num2 < 1) || (num2 < 1)){ printf("Input for n cannot be smaller than one!"); return -1; } else if (ch != ' '){ printf("Found a char that isn't space in stdin!"); return -1; } fillArrayWithCoefficients(array1, num1); fillArrayWithCoefficients(array2, num2); printArray(array1, num1); printArray(array2, num2); printf("f(x) = "); printPoly(array1, num1); printf("g(x) = "); printPoly(array2, num2); for(int i = 0; i < num2; i++) { for(int j = 0; j < num1; j++) { results[i + j] = results[i + j] + (array2[i] * array1[j]); } } printf("f(x)g(x) = "); printPoly(results, (num1 + num2 - 1)); printf("f(1)g(1) = %d\n", fofx(results, (num1 + num2 - 1), 1)); }