hi
i have to do a programe to read many polonomials from file
and then seperate every one to power and coffecient and store them at array of linked list
i do it but it didnt work
i dont know how to fix the error
i need it tonight
can u help me please ?
the code
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
typedef struct node *ptr;
typedef struct node2 *ptr2;
struct node
{
char part[20];
ptr next;
};
struct node2
{
char power[20];
char num[20];
ptr2 next;
};
typedef ptr list;
typedef ptr position;
typedef ptr2 list2;
typedef ptr2 position2;
struct node cut(char l[50]);
int main()
{
FILE *in=fopen("data.txt","r");
char l[50];
char *tokptr,*tokptr2;
while( fscanf(in,"%s",l))
{
tokptr=strtok(l,"+");
list parts=(list) malloc(sizeof( struct node));
position temp =(position) malloc(sizeof(struct node));
temp=parts;
while (tokptr != NULL)
{
position name =(position) malloc(sizeof(struct node));
strcpy(name->part,tokptr);
printf(" \n %s",name->part);
temp->next=name;
temp=name;
tokptr=strtok(NULL,"+");
}
position2 temp1=(position2)malloc(sizeof(struct node2));
position temp2=(position)malloc(sizeof(struct node));
list2 p=(list2)malloc(sizeof(struct node2));
temp1=p;
temp2=parts;
while(temp2->next != NULL)
{
tokptr2=strtok(temp2->next->part,"x^");
position2 fun=(position2) malloc(sizeof(struct node2));
if(tokptr2=NULL)
strcpy( fun->num,"1");
else
strcpy( fun->num,tokptr2);
printf(" \n %s " ,fun->num );
tokptr2=strtok(NULL,"^");
if(tokptr2=NULL)
strcpy( fun->power,"1");
else
strcpy( fun->power,tokptr2);
printf(" %s" , fun->power);
temp1->next=fun;
temp1=fun;
temp2=temp2->next;
}
}
fclose(0);
return 0;
}
16 Replies - 668 Views - Last Post: 15 February 2010 - 11:54 PM
Replies To: error in program with linked list
#2
Re: error in program with linked list
Posted 14 February 2010 - 09:20 AM
Can you attach the data.txt file please.
#4
Re: error in program with linked list
Posted 14 February 2010 - 09:38 AM
sorry i caoudnt attach it .... i dont know why
but we can add any polonomials we need to it
it may have any numbers of polonomials
the name of file is " data.txt"
do it with those polonomials
65x^4+x^9-43
9x^5+6x^9-4x^3-6
X^6-13x^4+8x^19+6
6x^6+x^3-43
9x^2+6x^9+4x^3-6
X^6-13x^4+8x^19+6
65x^4+x^9+43
9x^5+6x^9-4x^8-6
X^6-13x^4+8x^19+6
5x^4+3x-43
9x^5+6x^9-4x-6
but we can add any polonomials we need to it
it may have any numbers of polonomials
the name of file is " data.txt"
do it with those polonomials
65x^4+x^9-43
9x^5+6x^9-4x^3-6
X^6-13x^4+8x^19+6
6x^6+x^3-43
9x^2+6x^9+4x^3-6
X^6-13x^4+8x^19+6
65x^4+x^9+43
9x^5+6x^9-4x^8-6
X^6-13x^4+8x^19+6
5x^4+3x-43
9x^5+6x^9-4x-6
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
typedef struct node *ptr;
typedef struct node2 *ptr2;
struct node
{
char part[20];
ptr next;
};
struct node2
{
char power[20];
char num[20];
ptr2 next;
};
typedef ptr list;
typedef ptr position;
typedef ptr2 list2;
typedef ptr2 position2;
struct node cut(char l[50]);
int main()
{
FILE *in=fopen("data.txt","r");
char l[50];
char *tokptr,*tokptr2;
while( fscanf(in,"%s",l))
{
tokptr=strtok(l,"+");
list parts=(list) malloc(sizeof( struct node));
position temp =(position) malloc(sizeof(struct node));
temp=parts;
while (tokptr != NULL)
{
position name =(position) malloc(sizeof(struct node));
strcpy(name->part,tokptr);
printf(" \n %s",name->part);
temp->next=name;
temp=name;
tokptr=strtok(NULL,"+");
}
position2 temp1=(position2)malloc(sizeof(struct node2));
position temp2=(position)malloc(sizeof(struct node));
list2 p=(list2)malloc(sizeof(struct node2));
temp1=p;
temp2=parts;
while(temp2->next != NULL)
{
tokptr2=strtok(temp2->next->part,"x^");
position2 fun=(position2) malloc(sizeof(struct node2));
if(tokptr2=NULL)
strcpy( fun->num,"1");
else
strcpy( fun->num,tokptr2);
printf(" \n %s " ,fun->num );
tokptr2=strtok(NULL,"^");
if(tokptr2=NULL)
strcpy( fun->power,"1");
else
strcpy( fun->power,tokptr2);
printf(" %s" , fun->power);
temp1->next=fun;
temp1=fun;
temp2=temp2->next;
}
}
fclose(0);
return 0;
}
#5
Re: error in program with linked list
Posted 14 February 2010 - 09:58 AM
As far as I can see, your pointers are all messed up. Could you explain to me exactly what you are trying to do here. For example, I presume you are trying to break 65x^4+x^9-43 into 65x^4 and x^9-43 in a chained list, with the code
Is that correct. If so, what you have will not work.
list parts=(list) malloc(sizeof( struct node));
position temp =(position) malloc(sizeof(struct node));
temp=parts;
while (tokptr != NULL)
{
position name =(position) malloc(sizeof(struct node));
strcpy(name->part,tokptr);
printf(" \n %s",name->part);
temp->next=name;
temp=name;
tokptr=strtok(NULL,"+");
}
Is that correct. If so, what you have will not work.
This post has been edited by Martyn.Rae: 14 February 2010 - 09:59 AM
#6
Re: error in program with linked list
Posted 14 February 2010 - 10:06 AM
Martyn.Rae, on 14 February 2010 - 08:58 AM, said:
As far as I can see, your pointers are all messed up. Could you explain to me exactly what you are trying to do here. For example, I presume you are trying to break 65x^4+x^9-43 into 65x^4 and x^9-43 in a chained list, with the code
Is that correct. If so, what you have will not work.
list parts=(list) malloc(sizeof( struct node));
position temp =(position) malloc(sizeof(struct node));
temp=parts;
while (tokptr != NULL)
{
position name =(position) malloc(sizeof(struct node));
strcpy(name->part,tokptr);
printf(" \n %s",name->part);
temp->next=name;
temp=name;
tokptr=strtok(NULL,"+");
}
Is that correct. If so, what you have will not work.
with this code i must break 65x^4+x^9-43 into 65x^4 and x^9 and -43 in a chained list but i couldnt do it will cause i dont know how to break using multiple delimiters ( - or + )
after that i must break every part into power and coffecient
ex :
65x^4 i must break it to 65 and 4
and store them at new linked list every node in it have 2 parts called power and cofficient ( i called it nym in my program)
but this didnt work will , i dont know how to fix it
#7
Re: error in program with linked list
Posted 14 February 2010 - 12:06 PM
please help me to fix it
#8
Re: error in program with linked list
Posted 14 February 2010 - 12:08 PM
Do you have to use strtok, or can you use another method?
#9
Re: error in program with linked list
Posted 14 February 2010 - 12:27 PM
we can use any method we need
#10
Re: error in program with linked list
Posted 14 February 2010 - 12:33 PM
ok - I have been studying your code a stage further, and to use strtok, was not a wise choice. Lets go back to basics. Lets take an example, 9x^5+6x^9-4x^3-6. We are looking for <integer> <x> <^> <integer> <+/-> etc etc. I presume you could have
9x^5+6x^9-4x^3-6-9x^5+6x^9-4x^3-6+9x^5+6x^9-4x^3-6 etc. Are you interested in the + or minus signs?
9x^5+6x^9-4x^3-6-9x^5+6x^9-4x^3-6+9x^5+6x^9-4x^3-6 etc. Are you interested in the + or minus signs?
#11
Re: error in program with linked list
Posted 14 February 2010 - 12:51 PM
i didnt understand what u mean
#12
Re: error in program with linked list
Posted 14 February 2010 - 12:54 PM
ok
If we take 9x^2 - 16
9x^2 is taken as a coefficient of 9 and a power is 2.
-16 is taken as a coefficient of -16 and a power of 1.
x^6 is taken as a coefficient of 1 and a power of 6
is that what you are looking for?
If we take 9x^2 - 16
9x^2 is taken as a coefficient of 9 and a power is 2.
-16 is taken as a coefficient of -16 and a power of 1.
x^6 is taken as a coefficient of 1 and a power of 6
is that what you are looking for?
#13
Re: error in program with linked list
Posted 14 February 2010 - 01:07 PM
exactly
i want to apply this for all polonomial in the file and store this in array of linked list
i want to apply this for all polonomial in the file and store this in array of linked list
#14
Re: error in program with linked list
Posted 14 February 2010 - 01:22 PM
ok,
That produces a list containing all coefficients and power pairs in the file.
The head of the list is head.
EDIT: Ahhh .. just spotted you want this in an array of linked lists!
I have just provided a linked list.
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
typedef struct node *ptr;
struct node
{
ptr next;
char coefficient[20];
char power[20];
};
int main()
{
FILE *in=fopen("data.txt","r");
char l[50];
ptr head = NULL;
ptr tail = NULL;
while( 1 )
{
char *input_string = l;
fscanf(in,"%s",l);
if ( feof(in) ) break;
while ( *input_string != 0 )
{
char power[50];
char coefficient[50];
char * pointer = coefficient;
power[0] = 0;;
coefficient[0] = 0;;
if ( *input_string == '+' || *input_string == '-' )
*pointer++ = *input_string++;
else
*pointer++ = '+';
while ( *input_string >= '0' && *input_string <= '9' ) *pointer++ = *input_string++;
if ( pointer == coefficient+1 ) *pointer++ = '1';
*pointer = 0;
if ( *input_string == 'x' || *input_string == 'X' )
{
input_string++;
if ( *input_string == '^' ) input_string++;
pointer = power;
if ( *input_string == '+' || *input_string == '-' )
*pointer++ = *input_string++;
else
*pointer++ = '+';
while ( *input_string >= '0' && *input_string <= '9' ) *pointer++ = *input_string++;
if ( pointer == power+1 ) *pointer++ = '1';
*pointer = 0;
}
else
{
power[0] = '+';
power[1] = '1';
power[2] = 0;
}
ptr node = (ptr) malloc(sizeof(struct node));
node->next = NULL;
strcpy(node->coefficient, coefficient);
strcpy(node->power, power);
if ( head == NULL )
head = node;
else
tail->next = node;
tail = node;
}
}
fclose(in);
return 0;
}
That produces a list containing all coefficients and power pairs in the file.
The head of the list is head.
EDIT: Ahhh .. just spotted you want this in an array of linked lists!
I have just provided a linked list.
This post has been edited by Martyn.Rae: 14 February 2010 - 01:24 PM
#15
Re: error in program with linked list
Posted 14 February 2010 - 01:55 PM
this code save all in the same linked list ..... right ?
i try to make an array of linked list but i coudnt !!
i try to make an array of linked list but i coudnt !!
|
|

New Topic/Question
Reply




MultiQuote




|