Thread: Adding/Multiplying/Evaluating Polynomials

  1. #1
    Registered User
    Join Date
    Feb 2011
    Posts
    25

    Adding/Multiplying/Evaluating Polynomials

    Program involves File I/O

    Example, there's a file with the following:

    2 ; number of polynomials
    4x^5 - 2x^3 + 2x + 3 ; polynomial 1
    8x^4 + 4x^3 - 3x + 9 ; polynomial 2

    Have to use a linked list.
    Appreciate it if someone could help me understand the program on a higher/algorithmic level. How exactly would I read in the polynomials from the file into a linked list and perform operations on them (add / multiply/ evaluate)

    Thanks!

  2. #2
    and the hat of int overfl Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    39,661
    What do you know already about reading files?

    What do you know already about linked lists?

    What have you searched for on those subjects?
    I mean, there are "similar threads" listed at the bottom of this thread - that ought to be a start.

    Lists are discussed on a regular basis here, and probably that specific problem as well.
    If you dance barefoot on the broken glass of undefined behaviour, you've got to expect the occasional cut.
    If at first you don't succeed, try writing your phone number on the exam paper.

  3. #3
    Registered User
    Join Date
    Feb 2011
    Posts
    25
    Right now, I've read through the file and determined the coefficient and exponent for each term in the polynomial.

    I plan to pass these values to a "Build" function which actually builds the linked list here.

    How exactly would I build a linked list from the values I have obtained?

  4. #4
    Registered User
    Join Date
    Feb 2011
    Posts
    25
    Here's my code:
    Code:
    #include <stdio.h> 
    #include <string.h>
    #include <stdlib.h>
    #include <ctype.h>
    
    
    #define TRUE 1 
    #define FALSE 0
    
    /* Function Headers */
    
    int readSource (char *input); 
    int Build(); 
    int Add();
    int Multiply();
    
    /*Global Variables */
    char input[50];
    int numPolynomials;
    int Coefficient; 
    int Exponent; 
    
    
    int main () {
    
    char sentinel[5] = "STOP" ; 
    
    printf ("\nEnter Name of File (Enter 'STOP' to Quit):  "); 
    
       
       gets(input); 
    
    
        while ((strcmp (input, sentinel) != 0)) {
    
    	  readSource(input); 
    	
    	  printf("\nEnter Name of File (Enter 'STOP' to Quit):  "); 
          gets(input); 
          
    
        }
    
    
    
    }
    
    
    int readSource (char *input) {
    
    int i, j, len = 0, len2=0, coeffLength, expLength; 
    char line[100] =""; 
    char power[3] = "x^"; 
    char newline = '\n';
    char coeffString[100] = "";
    char expString[100]="";
    char plus = '+'; 
    char minus ='-'; 
    char x = 'x'; 
    char carrot ='^'; 
    char *storage; 
    
    
    /*Boolean Variables Initialized */
    int collectC = TRUE; 
    int collectE = FALSE;
    int foundX = FALSE;
    int signPos = TRUE;
    
    
    FILE *fp; 
        fp = fopen(input, "r");
    	if (fp == NULL)
    	{
    		printf("Error: There was a problem opening the specified input file (Make Sure the File Exists and is Readable)\n"); 
    		return -1;
    		
    	}
    
    	else 
    	{
    		     
    		    
    	 fscanf (fp, "%d", &numPolynomials); /*Stores Number of Polynomials in File */ 
    	 printf("The number of polynomials in the file is: %d", numPolynomials);
    	 fscanf (fp, "%c", &newline); /* Absorbs New Line character */
            
    
    		for (i = 0 ; i < numPolynomials ; i++) {
    
    			fgets(line, 100, fp);  /*Gets Line */
    			len = strlen(line);   /*Stores Length of Line */
    
    
    			 collectC = TRUE; 
                 collectE = FALSE;
    			 foundX = FALSE;
    			 signPos = TRUE;
               
    
    			 strcpy(coeffString, "");
    			 strcpy(expString, "");
    
    
    				for (j = 0 ; j < len ; j++) { /* Goes through polynomial, character by character */
    
    					if (isdigit(line[j]))
    
    					{
                        
    				    printf("\nThe digit is: %c", line[j]);
    
    							if (collectC) {
    								strncat(coeffString, &line[j],1); /*Builds Coefficient String */
    									}
    
    							else if (collectE) {
    
    								strncat(expString, &line[j],1);  /*Builds Exponent String */
    
    							}
    			
    					}
    
    					else if ((line[j] == plus) || (line[j] == minus) || (line[j] == newline))
    
    
    					{
    
    					if (foundX) {
    
    					   if (strcmp(expString,"") == 0) {
    						strcpy(expString, "1");
    					   }
    					}
    					else 
    					{
    						strcpy(expString, "0");
    					}
    						
    						printf("\nBeginning of New Term"); 
    						Coefficient = atoi(coeffString);
    							if(!signPos) {
    							Coefficient = -Coefficient;
    						}
    						printf("Coefficient:  %d", Coefficient);
    						Exponent = atoi(expString);
    	                    printf("Exponent:  %d", Exponent);
    						Build(); 
    
    						collectC = TRUE; 
    						collectE = FALSE; 
    						foundX = FALSE;
    						signPos = TRUE;
    
    						strcpy(coeffString, "");
    			            strcpy(expString, "");
    
                 
    
                         if(line[j] == minus) 
    
    					 {
    						 signPos = FALSE; 
    
    					 }
    					 else if (line[j] ==plus)
    
    					 {
    
    						 signPos = TRUE;
    					 }
    
    
    
    
    					}
    
    
    					else if ((line[j] == carrot) || (line[j] == x))
    
    					{
    						collectC = FALSE; 
    						collectE = TRUE; 
    
                             if(line[j] == x) {
    							foundX = TRUE;
    							if (strcmp(coeffString,"") == 0) {
    
    								strcpy(coeffString, "1");
    
    							}
    
    						 }
    
    
    
    					}
    		
    			
    		}
    
    	   
    	
    	  fclose(fp);
    
    	}
    	
    return 0; 
    
    }
    
    	}
    
    
    
    
    int Build(){ 
    
    struct node
    {
    int coeff;
    int deg;
     
    
    	struct node *next;
    	struct node *prev;
    };
    
    typedef struct node * nodeptr;
    
    
    
    
    }
    
    int Add(){
    }
    
    int Multiply(){
    
    }

  5. #5
    Registered User claudiu's Avatar
    Join Date
    Feb 2010
    Location
    London, United Kingdom
    Posts
    2,094
    A few things.

    1) int main() would preferably be int main(void).

    2) Never, and I mean NEVER use gets(). Use fgets() instead. (See FAQ)

    3) If you are using C99 you don't need to redefine your own boolean types. You can just include stdbool.h and use the default ones.

    4) Avoid as much as possible using global variables. (See FAQ)

    5) There are a ton of threads on pretty much any data structure you could imagine on this forum. Do a search for linked lists and look at how they are implemented.
    1. Get rid of gets(). Never ever ever use it again. Replace it with fgets() and use that instead.
    2. Get rid of void main and replace it with int main(void) and return 0 at the end of the function.
    3. Get rid of conio.h and other antiquated DOS crap headers.
    4. Don't cast the return value of malloc, even if you always always always make sure that stdlib.h is included.

  6. #6
    and the hat of int overfl Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    39,661
    Code is too hard to follow, unless you indent properly.
    SourceForge.net: Indentation - cpwiki
    If you dance barefoot on the broken glass of undefined behaviour, you've got to expect the occasional cut.
    If at first you don't succeed, try writing your phone number on the exam paper.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Multiplying polynomials
    By budala in forum C Programming
    Replies: 6
    Last Post: 08-19-2009, 03:19 PM
  2. polynomials
    By Emeighty in forum C++ Programming
    Replies: 4
    Last Post: 08-18-2008, 08:52 AM
  3. Multiplying Two Polynomials
    By CaptainMorgan in forum C++ Programming
    Replies: 6
    Last Post: 10-30-2006, 02:34 PM
  4. adding two polynomials
    By sahil_m in forum C Programming
    Replies: 3
    Last Post: 11-04-2005, 06:24 AM
  5. polynomials
    By spinner in forum C Programming
    Replies: 2
    Last Post: 04-10-2003, 06:23 AM

Tags for this Thread