Giúp fix lỗi Segmentation fault code chuyển biểu thức trung tố thành hậu tố

Đây là bài code về chuyển biểu thức trung tố thành hậu tố sử dụng stack lưu trữ bằng link list đơn

#include<stdio.h>
#include<stdlib.h>
#include<ctype.h>
struct node
{
	char data;
	struct node *link;
};
typedef struct node *stacknode;

typedef struct
{
	stacknode T;
}stack;



//Khoi tao ngan xep rong
void khoitao(stack *s)
{
	(s->T=NULL);
}
int ktrong(stack s)
{
	return (s.T==NULL);
}
//Bo sung phan tu vao dinh ngan xep luu tru bang danh sach lien ket don
void push(stack *s, int x) 
{
	stacknode p;
	p=(stacknode)malloc(sizeof(struct node));
	
	p->data=x;
	p->link=s->T;
	s->T=p;
}
//Loai bo phan tu dinh ngan xep luu tru bang danh sach lien ket don
int pop(stack *s)
{
	stacknode p;
	if(ktrong(*s))
	{
		printf("Ngan xep rong!");
	}
	else
	{
		p=s->T;
		s->T=s->T->link;
	}
	return p->data;
}
//Tra ve phan tu dinh ngan xep
int top(stack *s)
{
	return (s->T->data);
}
//Thu tu uu tien phep toan
int ttuutien(char c)
{
	if(c=='(')
        return 0;
    if(c=='+'||c=='-')
        return 1;
    if(c=='*'||c=='/'||c=='%')
        return 2;
 
    return 3;
}
//Chuyen bieu thuc tu dang trung to sang hau to
void chuyenbieuthuc(char trungto[],char hauto[])
{
	stack *st;
	char x;
	int i,j;
	char token;
	khoitao(st);
	j=0;
	
 	for(i=0;trungto[i]!='\0';i++)
    {
        token=trungto[i];
        
        if(token=='(')
               push(st,'(');
        else
            if(token==')')
                while((x=pop(st))!='(')
                      hauto[j++]=x;
                else
                {
                    while(!ktrong(*st)&&ttuutien(token)<=ttuutien(top(st)))
                    {
                        x=pop(st);
                        hauto[j++]=x;
                    }
                    push(st,token);
                }
    }
	while(!ktrong(*st))
    {
        x=pop(st);
        hauto[j++]=x;
    }
 
hauto[j]='\0';
}
//Chuong trinh chinh
int main()
{
	char trungto[50],hauto[50];
	
	printf("Hay nhap bieu thuc trung to: ");
	scanf("%c",trungto);
	
	chuyenbieuthuc(trungto,hauto);
	
	
	return 0;
}

Lỗi của bạn:

stack *st; // chưa khởi tạo st=(stack*)malloc(sizeof(stack));

  • Ham main đọc xâu
scanf("%c",trungto); //scanf("%s",trungto);
  • hàm ttuutien thiếu th isdigit
  • xây dựng biểu thức thiếu isdigit
    ##sửa lại
#include<stdio.h>
#include<stdlib.h>
#include<ctype.h>
struct node
{
    char data;
    struct node *link;
};
typedef struct node *stacknode;

typedef struct
{
    stacknode T;
}stack;



//Khoi tao ngan xep rong
void khoitao(stack *s)
{
    (s->T=NULL);
}
int ktrong(stack s)
{
    return (s.T==NULL);
}
//Bo sung phan tu vao dinh ngan xep luu tru bang danh sach lien ket don
void push(stack *s, int x) 
{
    stacknode p;
    p=(stacknode)malloc(sizeof(struct node));
    
    p->data=x;
    p->link=s->T;
    s->T=p;
}
//Loai bo phan tu dinh ngan xep luu tru bang danh sach lien ket don
int pop(stack *s)
{
    stacknode p;
    if(ktrong(*s))
    {
        printf("Ngan xep rong!");
    }
    else
    {
        p=s->T;
        s->T=s->T->link;
    }
    return p->data;
}
//Tra ve phan tu dinh ngan xep
int top(stack *s)
{
    return (s->T->data);
}
//Thu tu uu tien phep toan
int ttuutien(char c)
{
    if(c=='(')
        return -1;
    if(isdigit(c)) return 0;
    if(c=='+'||c=='-')
        return 1;
    if(c=='*'||c=='/'||c=='%')
        return 2;
 
   
}
//Chuyen bieu thuc tu dang trung to sang hau to
void chuyenbieuthuc(char trungto[],char hauto[])
{
    stack *st=(stack*)malloc(sizeof(stack));
    char x;
    int i,j;
    char token;
    khoitao(st);
    j=0;
    
     for(i=0;trungto[i]!='\0';i++)
    {
        token=trungto[i];
        
        if(token=='(')
               push(st,'(');
        else
        if(isdigit(token)){
            hauto[j++]=token;
        }
        else    if(token==')')
                    while((x=pop(st))!='(')
                          hauto[j++]=x;
                else
                {
                    while(!ktrong(*st)&&ttuutien(token)<=ttuutien(top(st)))
                    {
                        x=pop(st);
                        hauto[j++]=x;
                    }
                    push(st,token);
                }
    }
    while(!ktrong(*st))
    {
        x=pop(st);
        hauto[j++]=x;
    }
 
hauto[j]='\0';
}
//Chuong trinh chinh
int main()
{
    char trungto[50],hauto[50];
    
    printf("Hay nhap bieu thuc trung to: ");
    scanf("%s",trungto);
    
    chuyenbieuthuc(trungto,hauto);
    
    puts(hauto);
    return 0;
}

cảm ơn b nhiều :smiley:

83% thành viên diễn đàn không hỏi bài tập, còn bạn thì sao?