Exemplo n.º 1
0
int main()
{
    int i;

    Polynomial polylinklist1, polylinklist2;
    PtrToNode p, temp;

    polylinklist1 = MakeEmpty(NULL);
    polylinklist2 = MakeEmpty(NULL);

    p = polylinklist1;
    for (i = 2; i > 0; i--) {
        Insert(i, i, polylinklist1, p);
        p = p->next;
    }
    PrintPoly(polylinklist1);

    temp = polylinklist1->next == NULL ? polylinklist1 : polylinklist1->next;
    printf("%d\n", temp->exponent);

    AddPolynomial(MakeEmpty(NULL), polylinklist1, polylinklist2);
    PrintPoly(polylinklist2);

    AddPolynomial(polylinklist2, polylinklist2, polylinklist1);
    PrintPoly(polylinklist1);


}
Exemplo n.º 2
0
int main(void){

	int degree = 0;
	int * coefficients;
	while(1){
		// Gets the maximum degree of the polynomial
		printf("Please enter the maximum degree of the polynomial: ");
		scanf("%d", &degree);
		degree++;
		coefficients = (int*)calloc(degree, sizeof(int));
		
		// checks if the user entered -1, is so frees the alicated memory and closes
		if(degree <= 0){
			free(coefficients);
			break;
		}
			
		// Assingns the enterd coefficients into the array
		printf("Please enter the coefficients: ");
		for (int  i = 0; i < degree; i++){
			scanf("%d", &coefficients[i]);
		}
		
		// Outputs the polynomial
		printf("The polynomial is ");
		PrintPoly(coefficients, degree, false);
	
		// Outputs the reciprical
		printf("Its reciprocal is ");
		PrintPoly(coefficients, degree, true);
		
		// Checks fif it is a self-reciprical
		bool selfRec = true;
		for(int i = 0; i < (degree+1)/2; i++){
			if (coefficients[i] != coefficients[degree -i -1]) {
				selfRec = false;
				//Stops the loop to avoid uneccesry processing
				break;
			}
		}
		// Outputs if it is a self reciprical
		printf(selfRec ? "The polynomial is self-reciprocal\n\n" :"The polynomial is not self-reciprocal\n\n");
		//Frees the memory
		free(coefficients);
		// Exits and free the memory if it is a self reciprical
		if (selfRec){
			free(coefficients);
			break;
		}
			
		
	}
	return 0;
}
Exemplo n.º 3
0
void PrintDerivitive( int coeffs){

	// This function will back up the polynomial, then work out the derivitve of it
	// It will then place the derivitive in the poly[] array, then use the PrintPoly function
	// to print it onto the screen.


	int deriv[11]; 
	int i; //loop index


	// Starting from the RHS (i.e. power = 0, i = 0):
	// the coeff of each power is the product of the power above it and it's coeff

	for (i=0;i<=10;i++) {
		deriv[i] =  (i+1) * poly[i+1];
	}
	deriv[11] = 0; // as there is no 12th power, the derivitive of it will always be 0

	//Move value from deriv[] into poly[] for printing
	for (i=0;i<=11;i++) {
		poly[i] = deriv[i];
	}

	//Print the poly[]
	PrintPoly(coeffs, 1);


}
Exemplo n.º 4
0
int main(void){

	int degree = 0;
	int * coefficients;
	while(1){

		// Gets the maximum degree of the polynomial
		printf("Please enter the maximum degree of the polynomial: ");
		scanf("%d", &degree);
		degree++;
		// assigns the memory for the coefficients
		coefficients = (int*)calloc(degree, sizeof(int));
		
		// checks if the user entered -1, is so frees the alicated memory and closes
		if(degree <= 0){
			free(coefficients);
			break;
		}
			
		// Assingns the enterd coefficients into the array
		printf("Please enter the coefficients: ");
		for (int  i = 0; i < degree; i++){
			scanf("%d", &coefficients[i]);
		}
		
		// Outputs the polynomial
		printf("The polynomial is ");
		PrintPoly(coefficients, degree, false);
	
		// Outputs the differential
		printf("Its derivative is ");
		PrintPoly(coefficients, degree, true);
			
		// frees the memory
		free(coefficients);
	
	}
	return 0;
}
Exemplo n.º 5
0
int main (void) 
{

	// Variable definitions */

	int numberOfCoeffs; //number of powers for the polynomial
	int i; // A loop index
	int thisCoeff; // Stores the value of the coefficient that's just been entered

	while (1) {

		// User prompt and input */
		printf("Please enter the maximum degree of the polynomial:");
		scanf("%d", &numberOfCoeffs);

		// Check if degree is negative */
		if (numberOfCoeffs < 0 ) {
			// If so, exit */
			return 0;
		}

		// Print text prompting user for coefficent input
		printf("Please enter the coefficients:");
		// Start a loop to read each of the co-efficeient's values 
		for (i=(numberOfCoeffs);i>=0;i--) {
			// Prompt user for the co-effiecent 
			scanf("%d", &thisCoeff);
			// Store co-effiecents in array (in reverse order, i.e. highest power is last, so that x^3 gets stores in poly[3] 
			poly[i] = thisCoeff;
		}


		// Start a new line
			printf("\n");

		// Print polynomial

		PrintPoly(numberOfCoeffs, 0);

		// Print the derivitive of the polynomial 
		PrintDerivitive(numberOfCoeffs);

		//keep looping round
	}

	return 0;
	
}
Exemplo n.º 6
0
void   Triangulate( void )
{
   tVertex v0, v1, v2, v3, v4;	/* five consecutive vertices */
   int   n = nvertices;		/* number of vertices; shrinks to 3. */
   bool earfound;		/* for debugging and error detection only. */

   EarInit();
   printf("\nnewpath\n");
   /* Each step of outer loop removes one ear. */
   while ( n > 3 ) {     
      /* Inner loop searches for an ear. */
      v2 = vertices;
      earfound = FALSE;
      do {
         if (v2->ear) {
            earfound = TRUE;
            /* Ear found. Fill variables. */
            v3 = v2->next; v4 = v3->next;
            v1 = v2->prev; v0 = v1->prev;

            /* (v1,v3) is a diagonal */
            PrintDiagonal( v1, v3 );
            
            /* Update earity of diagonal endpoints */
            v1->ear = Diagonal( v0, v3 );
            v3->ear = Diagonal( v1, v4 );
            
            /* Cut off the ear v2 */
            v1->next = v3;
            v3->prev = v1;
            vertices = v3;	/* In case the head was v2. */
            n--;
            break;   /* out of inner loop; resume outer loop */
         } /* end if ear found */
         v2 = v2->next;
      } while ( v2 != vertices );

      if ( !earfound ) {
         printf("%%Error in Triangulate:  No ear found.\n");
         PrintPoly();
         printf("showpage\n%%%%EOF\n");
         exit(EXIT_FAILURE);
      }
   } /* end outer while loop */
   printf("closepath stroke\n\n");
}
Exemplo n.º 7
0
int main(int argc, char **argv)
{
	TRANS *T;
	ARGS *A;
	int Nc=0;
	int Np=0;
	int NewT=0, i;
	POS *P;
	int *BR, Nbr;
	if (argc<2) 
	{
        	fprintf(stderr," usage: polytransform <file> [trans 1] [trans 2] ... \n");
        	return 1;
    	}
	T=malloc(argc*sizeof(TRANS));
	A=malloc(argc*sizeof(ARGS));
	i=2;
	while (i<argc)
	{
		if (NewT==0)
		{
			if (strncmp(argv[i],"move",4)==0)
			{
				T[Nc]=MOVE;
				Nc++;
				NewT++;
			}
			if (strncmp(argv[i],"rotate",6)==0)
			{
				T[Nc]=ROT;
				Nc++;
				NewT++;
			}
			else if (strncmp(argv[i],"flipx",5)==0)
			{
				T[Nc]=FLIPX;
				Nc++;
			}
			else if (strncmp(argv[i],"flipy",5)==0)
			{
				T[Nc]=FLIPY;
				Nc++;
			}
			else if (strncmp(argv[i],"scalex",6)==0)
			{
				T[Nc]=SCALEX;
				Nc++;
				NewT++;
			}
			else if (strncmp(argv[i],"scaley",6)==0)
			{
				T[Nc]=SCALEY;
				Nc++;
				NewT++;
			}
			else if (strncmp(argv[i],"scale",5)==0)
			{
				T[Nc]=SCALE;
				Nc++;
				NewT++;
			}
			else if (strncmp(argv[i],"bb_fixR",7)==0)
			{
				T[Nc]=BBFIXR;
				Nc++;
				NewT++;
			} else	if (strncmp(argv[i],"bb",2)==0)
			{
				T[Nc]=BB;
				Nc++;
				NewT++;
			}
				
		}
		else
		{
			if (T[Nc-1]==MOVE)
			{
				if (NewT==1)
				{
					A[Nc-1].P0.x=atof(argv[i]);
					NewT++;
				}
				else
				{
					A[Nc-1].P0.y=atof(argv[i]);
					NewT=0;
				}
			}
			if (T[Nc-1]==ROT)
			{
				if (NewT==1)
				{
					A[Nc-1].P0.x=atof(argv[i]);
					NewT++;
				}
				else if (NewT==2)
				{
					A[Nc-1].P0.y=atof(argv[i]);
					NewT++;
				}
				else if (NewT==3)
				{
					A[Nc-1].f=atof(argv[i]);
					NewT=0;
				}
			}
			if ((T[Nc-1]==SCALE)||(T[Nc-1]==SCALEX)||(T[Nc-1]==SCALEY))
			{
				A[Nc-1].f=atof(argv[i]);
				NewT=0;
			}
			if ((T[Nc-1]==BB)||(T[Nc-1]==BBFIXR))
			{
				if (NewT==1)
				{
					A[Nc-1].P0.x=atof(argv[i]);
					NewT++;
				}
				else if (NewT==2)
				{
					A[Nc-1].P0.y=atof(argv[i]);
					NewT++;
				}
				else if (NewT==3)
				{
					A[Nc-1].P1.x=atof(argv[i]);
					NewT++;
				}
				else
				{
					A[Nc-1].P1.y=atof(argv[i]);
					NewT=0;
				}
			}
		}
		i++;			
	}
	P=ReadPoly(argv[1], &BR, &Nbr, &Np);
	TransForm(P, Np, T, A, Nc);
	PrintPoly(P, Np, BR, Nbr);
	fprintf(stderr,"Poly Length: %e\n", LengthPoly(P, Np, BR, Nbr));
 	free(T);
 	free(A);
 	free(P);
 	free(BR);
}