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(){
}