What's new

Closed Sa mga C experts po dyan, patulong po

Status
Not open for further replies.

DestroyThem

Eternal Poster
Joined
Sep 26, 2017
Posts
505
Reaction
79
Points
307
Age
24
Passing na po bukas linked list in c infix to postfix, sana matulungan nyo po ako, okay na pero dapat invalid or valid lalabas kapag nag input ako, pag 1+1 result is 11+ kaya dapat valid sya pero pag 1+1+ dapat invalid kasi operator ang last . Tsaka dapat nakafunction yung infix to postfix ko kaso nasa main function, baka matulungan nyo po ako naghahanap ako sa google kaso nakakalito na. Ito po yung code, may comment narin po

#include<stdio.h>
#include<stdlib.h>
#include<string.h>
#define node_malloc (struct node*)malloc(sizeof(struct node));
struct node {
char element;
struct node *next;
} *head;

void push(char c); // function to push a node onto the stack
char pop(); // function to pop the top node of stack
int precedence(char c); // function to find the precedence of an operator
void traceStack(); // function to //print the stack values

int main() {
int i = 0, j = 0; // indexes to keep track of current position for input output strings
char *exp = (char *)malloc(sizeof(char)*100);
char *res = (char *)malloc(sizeof(char)*100);
char tmp;
head = NULL;

printf("Enter the infix expression: ");
scanf("%s", exp);

while( (tmp=exp[i++]) != '\0') { // repeat till the last null terminator
// if the char is operand, copy it to output string
if(tmp >= 48 && tmp <= 57) {
res[j++] = tmp;
continue;
}

if(tmp == '(' || tmp == '[' || tmp == '{') {
push(tmp);
continue;
}

if(tmp == ')' || tmp == ']' || tmp== '}') {
char cl, tp;
if(tmp == ')') cl = '(';
if(tmp == '}') cl = '{';
if(tmp == ']') cl = '[';

tp = pop();

while(tp != cl) {
res[j++] = tp;
tp = pop();
}
continue;
}

// if char is operator
if(tmp == '+' || tmp == '-' || tmp == '*' || tmp == '/' || tmp == '%' || tmp == '^') {
if(head == NULL) {
push(tmp);
} else {
// if operator at top of stack has high precedence, pop it and
// add to output string, else just push the operator
while(precedence(tmp) <= precedence(head->element) && head->element != '(' && head->element != '[' && head->element != '{') {
char tp = pop();
res[j++] = tp;
if(head == NULL)
break;
}
push(tmp);
}
}
}

// pop all the operators from stach to output string
while (head != NULL) {
res[j++] = pop();
};
res[j++] = '\0';

printf("Postfix expression is %s\n\n", res);
return 0;
}

void push(char c) {
if(head == NULL) {
head = node_malloc;
head->element = c;
head->next = NULL;
} else {
struct node *tNode;
tNode = node_malloc;
tNode->element = c;
tNode->next = head;
head = tNode;
}
}

char pop() {
struct node *tNode;
tNode = head;
head = head->next;
return tNode->element;
}

int precedence(char c) {
if(c == '^')
return 3;
else if (c == '*' || c == '/' || c =='%')
return 2;
else if (c == '+' || c == '-')
return 1;
else
return 5;
}

void traceStack() {
struct node *tNode;
tNode = head;
while(tNode != NULL) {
printf("%c --> ", tNode->element);
tNode = tNode->next;
}
printf("\n");
}



Ganito po dapat sa picture ang mangyayari
Salamats sa tutulong
 

Attachments

Most of the problems in code can be attributed to two things: hard to read and unnecessary complexity.

Here's a REPL for the benefit of other people reading your code: You do not have permission to view the full content of this post. Log in or register now.

I formatted your code. That should be a big help for people reading your code.
 
Try this REPL: You do not have permission to view the full content of this post. Log in or register now.

I added this function:
C:
bool nextCharIsOperator(char next) {
  return next == '+' || next == '-' || next == '*' || next == '/' || next == '%' || next == '^';
}

Then I modified your operator check to:
C:
        // if char is operator
        if (nextCharIsOperator(tmp))
        {
          if (nextCharIsOperator(exp[i + 1])) {
            printf("Invalid input! \n\n");
            return 0;
          }
 
Try this REPL: You do not have permission to view the full content of this post. Log in or register now.

I added this function:
C:
bool nextCharIsOperator(char next) {
  return next == '+' || next == '-' || next == '*' || next == '/' || next == '%' || next == '^';
}

Then I modified your operator check to:
C:
        // if char is operator
        if (nextCharIsOperator(tmp))
        {
          if (nextCharIsOperator(exp[i + 1])) {
            printf("Invalid input! \n\n");
            return 0;
          }
Ayun sir thanks, it helped me but sir, when i input 1+1+1 expression it outputs invalid expression, it should be a valid expression, thank you sir
 
Here's the code provided by sir pixkit, i don' t know what is the problem but i think its in the main function, i changed what the output says, if the inputted expression is a valid it should say valid but if it is invalid it should be invalid expression, i tried a lot mathematical expression but i get invalid expression even if it is valid expression. I tried 1+1+1 but it says invalid, i tried (1+3)/(1*2) it is correct and says valid


#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <stdbool.h>

#define node_malloc (struct node *)malloc(sizeof(struct node));
struct node
{
char element;
struct node *next;
} * head;

void push(char c); // function to push a node onto the stack
char pop(); // function to pop the top node of stack
int precedence(char c); // function to find the precedence of an operator
void traceStack(); // function to //print the stack values
bool nextCharIsOperator(char next);

int main()
{
int i = 0, j = 0; // indexes to keep track of current position for input output strings
char *exp = (char *)malloc(sizeof(char) * 100);
char *res = (char *)malloc(sizeof(char) * 100);
char tmp;
head = NULL;

printf("Enter the infix expression: ");
scanf("%s", exp);

while ((tmp = exp[i++]) != '\0')
{ // repeat till the last null terminator
// if the char is operand, copy it to output string
if (tmp >= 48 && tmp <= 57)
{
res[j++] = tmp;
continue;
}

if (tmp == '(' || tmp == '[' || tmp == '{')
{
push(tmp);
continue;
}

if (tmp == ')' || tmp == ']' || tmp == '}')
{
char cl, tp;
if (tmp == ')')
cl = '(';
if (tmp == '}')
cl = '{';
if (tmp == ']')
cl = '[';

tp = pop();

while (tp != cl)
{
res[j++] = tp;
tp = pop();
}
continue;
}

// if char is operator
if (nextCharIsOperator(tmp))
{
if (nextCharIsOperator(exp[i + 1])) {
printf("Invalid input! \n\n");
return 0;
}

if (head == NULL)
{
push(tmp);
}
else
{
// if operator at top of stack has high precedence, pop it and
// add to output string, else just push the operator
while (precedence(tmp) <= precedence(head->element) && head->element != '(' && head->element != '[' && head->element != '{')
{
char tp = pop();
res[j++] = tp;
if (head == NULL)
break;
}
push(tmp);
}
}
}

// pop all the operators from stach to output string
while (head != NULL)
{
res[j++] = pop();
};
res[j++] = '\0';

printf("Valid Expression!");
return 0;
}

void push(char c)
{
if (head == NULL)
{
head = node_malloc;
head->element = c;
head->next = NULL;
}
else
{
struct node *tNode;
tNode = node_malloc;
tNode->element = c;
tNode->next = head;
head = tNode;
}
}

char pop()
{
struct node *tNode;
tNode = head;
head = head->next;
return tNode->element;
}

int precedence(char c)
{
if (c == '^')
return 3;
else if (c == '*' || c == '/' || c == '%')
return 2;
else if (c == '+' || c == '-')
return 1;
else
return 5;
}

void traceStack()
{
struct node *tNode;
tNode = head;
while (tNode != NULL)
{
printf("%c --> ", tNode->element);
tNode = tNode->next;
}
printf("\n");
}

bool nextCharIsOperator(char next) {
return next == '+' || next == '-' || next == '*' || next == '/' || next == '%' || next == '^';
}
 
Again.
Ilagay sa code tag para madali mabasa.
Nandyan lang sa "Insert" sa tina-type-an mo.

1.JPG
 

Attachments

Here's the code provided by sir pixkit, i don' t know what is the problem but i think its in the main function, i changed what the output says, if the inputted expression is a valid it should say valid but if it is invalid it should be invalid expression, i tried a lot mathematical expression but i get invalid expression even if it is valid expression. I tried 1+1+1 but it says invalid, i tried (1+3)/(1*2) it is correct and says valid

#include<stdio.h> #include<stdlib.h> #include<string.h> #define node_malloc (struct node*)malloc(sizeof(struct node)); struct node { char element; struct node *next; } *head; void push(char c); // function to push a node onto the stack char pop(); // function to pop the top node of stack int precedence(char c); // function to find the precedence of an operator void traceStack(); // function to //print the stack values int main() { int i = 0, j = 0; // indexes to keep track of current position for input output strings char *exp = (char *)malloc(sizeof(char)*100); char *res = (char *)malloc(sizeof(char)*100); char tmp; head = NULL; printf("Enter the infix expression: "); scanf("%s", exp); while( (tmp=exp[i++]) != '\0') { // repeat till the last null terminator // if the char is operand, copy it to output string if(tmp >= 48 && tmp <= 57) { res[j++] = tmp; continue; } if(tmp == '(' || tmp == '[' || tmp == '{') { push(tmp); continue; } if(tmp == ')' || tmp == ']' || tmp== '}') { char cl, tp; if(tmp == ')') cl = '('; if(tmp == '}') cl = '{'; if(tmp == ']') cl = '['; tp = pop(); while(tp != cl) { res[j++] = tp; tp = pop(); } continue; } // if char is operator if(tmp == '+' || tmp == '-' || tmp == '*' || tmp == '/' || tmp == '%' || tmp == '^') { if(head == NULL) { push(tmp); } else { // if operator at top of stack has high precedence, pop it and // add to output string, else just push the operator while(precedence(tmp) <= precedence(head->element) && head->element != '(' && head->element != '[' && head->element != '{') { char tp = pop(); res[j++] = tp; if(head == NULL) break; } push(tmp); } } } // pop all the operators from stach to output string while (head != NULL) { res[j++] = pop(); }; res[j++] = '\0'; printf("Postfix expression is %s\n\n", res); return 0; } void push(char c) { if(head == NULL) { head = node_malloc; head->element = c; head->next = NULL; } else { struct node *tNode; tNode = node_malloc; tNode->element = c; tNode->next = head; head = tNode; } } char pop() { struct node *tNode; tNode = head; head = head->next; return tNode->element; } int precedence(char c) { if(c == '^') return 3; else if (c == '*' || c == '/' || c =='%') return 2; else if (c == '+' || c == '-') return 1; else return 5; } void traceStack() { struct node *tNode; tNode = head; while(tNode != NULL) { printf("%c --> ", tNode->element); tNode = tNode->next; } printf("\n"); }
 
The output is invalid even though the input is valid because the code was programmed to be like that. Fix the code.
 
Parang awa nyo na! Kung mag po post kayo ng programming code. Gamitin nyo ang CODE tag para preserve ang formatting at indentations.

Kalimitan nauubos ang oras sa mga tutulong sa pag aayos ng source code formatting para mabasa at maunawaan ng mabuti ang mga code.
 
Status
Not open for further replies.

Similar threads

Back
Top