🔒 Closed Sa mga C experts po dyan, patulong po

Status
Not open for further replies.

DestroyThem

Honorary Poster
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 E×ρréššion: ");
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 E×ρréššion 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

  • IMG_20191210_034459.webp
    IMG_20191210_034459.webp
    131.6 KB · Views: 25
  • IMG_20191210_034459.jpg
    IMG_20191210_034459.jpg
    95.9 KB · Views: 0
  • IMG_20191210_034439.webp
    IMG_20191210_034439.webp
    69.7 KB · Views: 9
  • IMG_20191210_034408.webp
    IMG_20191210_034408.webp
    134.3 KB · Views: 11
  • Screenshot_2019-12-10-03-35-35-665_ru.iiec.cxxdroid.webp
    Screenshot_2019-12-10-03-35-35-665_ru.iiec.cxxdroid.webp
    18 KB · Views: 8
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 E×ρréššion it outputs invalid E×ρréššion, it should be a valid E×ρréššion, 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 E×ρréššion is a valid it should say valid but if it is invalid it should be invalid E×ρréššion, i tried a lot mathematical E×ρréššion but i get invalid E×ρréššion even if it is valid E×ρréššion. 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 E×ρréššion: ");
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 E×ρréššion!");
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 == '^';
}
 
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 E×ρréššion is a valid it should say valid but if it is invalid it should be invalid E×ρréššion, i tried a lot mathematical E×ρréššion but i get invalid E×ρréššion even if it is valid E×ρréššion. 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 E×ρréššion: "); 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 E×ρréššion 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.

About this Thread

  • 14
    Replies
  • 871
    Views
  • 5
    Participants
Last reply from:
Donopatay

Online now

Members online
1,016
Guests online
696
Total visitors
1,712

Forum statistics

Threads
2,276,969
Posts
28,973,413
Members
1,229,670
Latest member
Hyuga000
Back
Top