All pastes #487716 Raw Edit

Miscellany

public text v1 · immutable
·published 2007-05-14 16:36 UTC
rendered paste body
#include <stdio.h>
#include <stdlib.h>
#include <string.h>

#include "error.h"
#include "poly.h"

struct poly_t {

	char* exp;

	char* numb;

};



static poly_t* allocatePoly(){

    poly_t *ptr = (poly_t*)malloc(sizeof(poly_t));

	ptr->exp = (char *)malloc(128*sizeof(char));

	ptr->numb = (char *)malloc(128*sizeof(char));

	memset(ptr->exp, '\0', 128);

	memset(ptr->numb, '\0', 128);

	return ptr;

}



poly_t *new_poly_from_string(const char* theString){

	poly_t *ptr = allocatePoly();

		

	char *writablestring;

	char *token = NULL;



	writablestring = (char *)malloc(strlen(theString));

	strcpy(writablestring, theString);

	token = strtok(writablestring, " ");

	while(token != NULL){;
		if(strcmp(token, "-") == 0 || strcmp(token, "+") == 0 ){

			if(strcmp(token, "-")==0){
				strcat(ptr->numb, "-");
			}
		}else{
			if( strpbrk(token, "x") != NULL ){
				char * pch;
				pch = strchr(token,'x');

				if(pch-token != 0){
					strncat(ptr->numb, token, pch-token);
				}else{
					strcat(ptr->numb, "1");
				}
				strcat(ptr->numb, " ");
				
				strcat(ptr->exp, strchr(token, 'x') );
				strcat(ptr->exp, " ");

			}else{
				strcat(ptr->numb, token);
				strcat(ptr->numb, " ");
				strcat(ptr->exp,  "1 ");
			}
		}

		token = strtok(NULL, " ");

	}

	return ptr;

}





void free_poly(poly_t* thePoly){
	free(thePoly->numb);
	free(thePoly->exp);

	free(thePoly);

}



char* poly_to_string(poly_t* thePoly){

	//Behövs ej göras.

	return NULL;

}



poly_t* add(poly_t* a, poly_t* b){

	return a;

}





poly_t* mul(poly_t* a, poly_t* b){

	return a;

}



void print_poly(poly_t* toPrint){

	char* token;	
	int counter = 0;
	char tempToker[128];
	strcpy(tempToker, toPrint->numb);
	token = strtok(tempToker, " ");
	while(token != NULL){
		counter++;
		token = strtok(NULL, " ");
	}
	int i; int j;
	char temp1[counter][10];
	char temp2[counter][10];
	for(i = 0; i < counter; i++){
		for(j = 0; j < 10; j++){
			temp1[i][j] = '\0';
			temp2[i][j] = '\0';
		}
	}
	counter = 0;
	token = strtok(toPrint->numb, " ");
	while(token != NULL){
		strcat(temp1[counter], token);
		counter++;
		token = strtok(NULL, " ");
	}

	counter = 0;
	token = strtok(toPrint->exp, " ");
	while(token != NULL){
		strcat(temp2[counter], token);
		counter++;
		token = strtok(NULL, " ");
	}

	printf("\n\n\n\n");
	for(i=0; i < counter; i++){
		if(i == 0){
			if( temp1[0][0] == '1'){
				printf("%s ", temp2[0]);
			}else if( temp2[0][0] == '1'){
				printf("%s ", temp1[0]);
			}else{
				printf("%s %s ", temp1[0], temp2[0]);
			}
		}else{
			if( temp1[counter][0] == '1'){
				printf("+ %s ", temp2[i]);
			}else if( temp2[i][0] == '1'){
				if( temp1[i][0] == '-'){
					printf("%s ", temp1[i]); 
				}else{
					
					printf("+ %s ", temp1[i]);
				}
			}else{
				if( temp1[i][0] == '-'){
					printf("- %c %s ", temp1[i][1], temp2[i]); 
				}else{
					printf("+ %s %s ", temp1[i], temp2[i]);
				}
			}
		}
	}
	printf("\n\n\n\n");

}