-
April 22nd, 2008, 11:49 AM
#1
Translate C to C++...Help...
Can someone help me translate this C code below to C++...i need to use classes and objects to do that...the C code is explained very well using comments....please help...
This program add, multiplies, and evaluates polynomials from a file...the code compiles great on visual studio 2003...
Code:
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <math.h> /*math.h required for pow() function used in EVALUTE polynomial*/
/*pow(a,b) gives a raised to power b*/
struct node { /*Defines struct node for each node of a Linked List*/
int exponent; /*struct node is type defined to ListNode*/
double coefficient; /*ListNode is used through the program*/
struct node *next;
};
typedef struct node ListNode;
ListNode *addNode(ListNode *first,int exp,double coeff) /*Adds a new node at end of Linked List with first as pointer to first node*/
{ /*Puts given fields exp and coeff in new node*/
ListNode *temp, *newNode; /*And return pointer to first node*/
newNode = (ListNode *)malloc(sizeof(ListNode));
newNode->exponent = exp;
newNode->coefficient = coeff;
newNode->next = NULL;
if(first==NULL)
return newNode;
temp = first;
while(temp->next != NULL)
temp = temp->next;
temp->next = newNode;
return first;
}
ListNode *CreateList(char *str) /*Creates Linked List containing the polynomial given in the file*/
{ /*Returns pointer to the first node of this Linked List*/
int exp, sign;
double coeff;
ListNode *poly=NULL;
char *t, c;
t = strtok(str," "); /* get first term (parse with space delimiter)*/
if(strcmp(t,"-")==0)
{
sign = -1;
t = strtok(NULL," ");
}
else
sign = 1;
do
{
if(sscanf(t,"%lfx^%d",&coeff,&exp)==2) /* if term looks like 2.5x^4*/
;
else if(sscanf(t,"x^%d",&exp)==1) /* if term looks like x^4*/
coeff = 1.0;
else if((sscanf(t,"%lf%c",&coeff,&c)==2)&&(c=='x')) /* if term looks like 2.5x*/
exp = 1;
else if((sscanf(t,"%c",&c)==1)&&(c=='x')) /* if term looks like x*/
{
exp = 1;
coeff = 1.0;
}
else if(sscanf(t,"%lf",&coeff)==1) /* if term looks like 2.5*/
exp = 0;
else
printf("ERROR!\n");
coeff = coeff*sign; /* update coefficient: multiply with sign*/
poly = addNode(poly,exp,coeff);
if((t=strtok(NULL," "))==NULL) /* get next operator*/
break; /* find sign for next term*/
if(strcmp(t,"+")==0) /* sign: 1 for positive, -1 for negative*/
sign = 1;
else if(strcmp(t,"-")==0)
sign = -1;
else
printf("ERROR!\n");
} while((t = strtok(NULL," "))!=NULL); /* get the next term*/
return poly;
}
ListNode *freePoly(ListNode *poly) /*frees the memory of Linked List list*/
{
ListNode *i=poly, *temp;
while(i!=NULL)
{
temp = i;
i = i->next;
free(temp);
}
return NULL;
}
ListNode *addPoly(ListNode *poly1, ListNode *poly2) /*Adds two polynomials given in Linked Lists poly1 and poly2*/
{ /*Returns Linked List that contains the Sum*/
ListNode *i=poly1,*j=poly2, *sum = NULL;
double coeff;
while((i!=NULL)&&(j!=NULL))
{
if(i->exponent > j->exponent)
{
sum = addNode(sum,i->exponent,i->coefficient); /*Each time term with largest exponent is selected and added to sum*/
i = i->next; /*If both polynomials have term with same exponent, term with this exponent and coefficient as sum of the 2 coefficients is added to sum*/
}
else if(j->exponent > i->exponent) /*If coefficient turns out to be zero, no term is added to sum*/
{
sum = addNode(sum,j->exponent,j->coefficient);
j = j->next;
}
else
{
coeff = i->coefficient + j->coefficient;
if(coeff!=0)
sum = addNode(sum,i->exponent,coeff);
j = j->next;
i = i->next;
}
}
while(i!=NULL)
{
sum = addNode(sum,i->exponent,i->coefficient);
i = i->next;
}
while(j!=NULL)
{
sum = addNode(sum,j->exponent,j->coefficient);
j = j->next;
}
return sum;
}
ListNode *multiplyPoly(ListNode *poly1, ListNode *poly2) /*Multiplies two polynomials given in Linked Lists poly1 and poly2*/
{ /*Returns Linked List containing the Product*/
ListNode *i=poly1, *j=poly2, *product=NULL, *partialProduct=NULL, *temp;
int exp;
double coeff;
while(i!=NULL)
{
partialProduct = NULL; /*Create each partialProduct as 1 term of poly1 multiplied with poly2*/
j = poly2; /*And this partialProduct is added to Product*/
while(j!=NULL) /*Product will accumulate all partial product to get product of 2 polynomials*/
{
exp = i->exponent + j->exponent;
coeff = i->coefficient * j->coefficient;
partialProduct = addNode(partialProduct,exp,coeff);
j = j->next;
}
temp = product;
product = addPoly(product,partialProduct);
temp = freePoly(temp);
partialProduct = freePoly(partialProduct);
i = i->next;
}
return product;
}
void printPoly(ListNode *poly) /*Prints the polynomial given by Linked List list in proper format*/
{
ListNode *i=poly;
if(poly==NULL)
{
printf("0\n");
return;
}
while(i!=NULL)
{
printf("%g",i->coefficient);
if(i->exponent > 0)
printf("x");
if(i->exponent > 1)
printf("^%d",i->exponent);
if(i->next != NULL)
printf(" + ");
i = i->next;
}
printf("\n");
return;
}
double evalPoly(ListNode *poly, double xvalue) /*evalPoly value of poynomial given by Linked List list at x = xvalue*/
{ /*Returns this evalPolyd value of the polynomial*/
ListNode *i=poly;
double polyValue = 0.0;
while(i!=NULL)
{
polyValue = polyValue + (i->coefficient * pow(xvalue,i->exponent));
i = i->next;
}
return polyValue;
}
int checkValidFile(FILE *fp) /*Checks if fp points to a valid file (that is, if file given in fopen() exists)*/
{ /*Gives error message and exits is file could not be opened*/
if(fp == NULL)
{
printf("\nCan't Open File!\n");
printf("Try again\n\n");
return 0;
}
return 1;
}
int main()
{
ListNode *poly1, *poly2, *temp = NULL, *sum, *product;
FILE *fp;
int choice = 0, exp = 0, N, i, n1, n2;
double coeff = 0, xvalue, polyValue;
char filename[30], str[2000], str_temp[2000];
char **poly_strings, ch; /* poly_strings will hold all polynomial strings in the file*/
while(1)
{
printf("Enter the file where polynomials are found : "); /* open polynomial file*/
scanf("%s",filename);
while(getchar()!='\n');
fp = fopen(filename,"r");
if(!checkValidFile(fp))
continue;
printf("The polynomials available for operation are :\n");
fscanf(fp,"%d",&N);
fgets(str,2000,fp);
poly_strings = (char **)malloc(N*sizeof(char *)); /* read all polynomial strings into poly_strings */
for(i=0;i<N;i++) /* and print them out */
{
poly_strings[i] = (char *)malloc(2000*sizeof(char));
fgets(poly_strings[i],2000,fp);
printf("%d. %s",i+1, poly_strings[i]);
}
fclose(fp);
while(1)
{
printf("\nWhat operation would you like to perform?\n1. ADD polynomials\n2. MULTIPLY polynomials\n3. evalPoly polynomial\n\nEnter choice # => ");
scanf("%d",&choice); /* get choice */
while(getchar()!='\n');
printf("\n");
switch(choice)
{
case 1: printf("Enter the polynomials that you would like to work with: "); /*ADD polynomials*/
scanf("%d,%d",&n1,&n2); /* create the 2 polynomials poly1, poly2*/
while(getchar()!='\n');
if((n1<1)||(n1>N)||(n2<1)||(n2>N))
{
printf("invalid input. Start again\n");
continue;
}
strcpy(str_temp,poly_strings[n1-1]);
poly1 = CreateList(str_temp);
strcpy(str_temp,poly_strings[n2-1]);
poly2 = CreateList(str_temp);
sum = addPoly(poly1,poly2); /* add*/
printf("\nThe symbolic sum of the 2 polynomials is:\n");
printPoly(sum); /* print sum*/
poly1 = freePoly(poly1); /* free dynamically allocated memory*/
poly2 = freePoly(poly2);
sum = freePoly(sum);
printf("\n");
break;
case 2: printf("Enter the polynomials that you would like to work with: "); /*MULTIPLY polynomials*/
scanf("%d,%d",&n1,&n2); /* create the 2 polynomials poly1, poly2*/
while(getchar()!='\n');
if((n1<1)||(n1>N)||(n2<1)||(n2>N))
{
printf("\ninvalid input. Start again\n");
continue;
}
strcpy(str_temp,poly_strings[n1-1]);
poly1 = CreateList(str_temp);
strcpy(str_temp,poly_strings[n2-1]);
poly2 = CreateList(str_temp);
product = multiplyPoly(poly1,poly2); /* multiply*/
printf("\nThe Symbolic Product of the 2 polynomials is:\n");
printPoly(product); /* print product*/
poly1 = freePoly(poly1); /* free dynamically allocated memory*/
poly2 = freePoly(poly2);
product = freePoly(product);
printf("\n");
break;
case 3: printf("Enter the polynomial that you would like to work with: "); /*evalPoly polynomaial*/
scanf("%d",&n1); /* create the polynomial poly1*/
while(getchar()!='\n');
if((n1<1)||(n1>N))
{
printf("\ninvalid input. Start again\n");
continue;
}
strcpy(str_temp,poly_strings[n1-1]);
poly1 = CreateList(str_temp);
printf("\nEnter the evaluation point (the value of x) => ");
scanf("%lf",&xvalue);
while(getchar()!='\n');
polyValue = evalPoly(poly1,xvalue); /* evaluate*/
printf("Value of that polynomial at %g is: %g\n",xvalue,polyValue);
poly1 = freePoly(poly1); /* free dynamically allocated memory*/
printf("\n");
break;
default: printf("\ninvalid choice! Try again.\n");
continue;
}
printf("Do you want to perform additional operations on the existing file (Y/N)? ");
do{ /* more operations?*/
scanf("%c",&ch);
while(getchar()!='\n');
if((ch=='Y')||(ch=='y')||(ch=='N')||(ch=='n'))
break;
else
printf("invalid input. try again : ");
} while(1);
printf("\n");
if((ch=='n')||(ch=='N')) /* if no, stop operating on this file*/
break;
}
for(i=0;i<N;i++) /* free poly_strings for this file*/
free(poly_strings[i]);
free(poly_strings);
printf("Do you want to work with another file(Y/N)? ");
do{ /* more files?*/
scanf("%c",&ch);
while(getchar()!='\n');
if((ch=='Y')||(ch=='y')||(ch=='N')||(ch=='n'))
break;
else
printf("invalid input. try again : ");
} while(1);
printf("\n");
if((ch=='n')||(ch=='N')) /* if no, exit*/
break;
}
printf("Thank you for using this program.\nBye Bye\n");
getchar();
return 0;
}
-
April 22nd, 2008, 11:57 AM
#2
Re: Translate C to C++...Help...
See this FAQ. The same applies to your other thread, by the way.
-
April 22nd, 2008, 12:00 PM
#3
Re: Translate C to C++...Help...
Well, I'd create a Polynomial class, first of all, which would typically be passed in place of a ListNode*.
This class would have a constructor (like CreateList), a destructor (like FreePoly), operator+ and operator* for the obvious functions, etc.
How the polynomial is represented in the class is up to you. Since a linked list is being used now, I'd say use std::list to represent it.
-
April 22nd, 2008, 12:27 PM
#4
Re: Translate C to C++...Help...
In Addition to Lindleys comments you might want to make a constructor that passes *next and *previous addresses as well as the polynomial value.
ahoodin
To keep the plot moving, that's why.
-
April 22nd, 2008, 12:46 PM
#5
Re: Translate C to C++...Help...
-
April 22nd, 2008, 02:46 PM
#6
Re: Translate C to C++...Help...
In addition to medievalelks, i can add this
Code:
#include <list>
struct PolyNode
{
int exponent;
double coefficient;
};
std::list<PolyNode> polynomial;
-
May 1st, 2008, 06:23 PM
#7
Re: Translate C to C++...Help...
Posting Permissions
- You may not post new threads
- You may not post replies
- You may not post attachments
- You may not edit your posts
-
Forum Rules
|
Click Here to Expand Forum to Full Width
|