Skip to content

feat: add infix to postfix converter algorithm#869

Merged
Panquesito7 merged 13 commits intoTheAlgorithms:masterfrom
kumaryash18:expression
Oct 17, 2021
Merged

feat: add infix to postfix converter algorithm#869
Panquesito7 merged 13 commits intoTheAlgorithms:masterfrom
kumaryash18:expression

Conversation

@kumaryash18
Copy link
Member

@kumaryash18 kumaryash18 commented Oct 6, 2021

Description of Change

Added a program to convert an infix expression to postfix expression in misc

References

Checklist

  • Added description of change
  • Added file name matches File name guidelines
  • PR title follows semantic commit guidelines
  • I acknowledge that all my contributions will be made under the project's license.

Notes:

A program in C to convert an infix expression to postfix expression

Copy link
Member

@Panquesito7 Panquesito7 left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Hey there! Thank you for submitting a PR! 😄Looks like this algorithm is already implemented here 👀 Please name your file infix_to_postfix2.c and move it to the conversions directory.
Also, this does not meet our requirements. Check out our repository standards. Please read them carefully and follow them. Let us know if you need any help here or in our Discord server. 🙂

@Panquesito7 Panquesito7 added Autochecks are failing Failing CI Auto-checks Changes requested enhancement New feature or request Proper Documentation Required requested to write the documentation properly labels Oct 6, 2021
Copy link
Member

@Panquesito7 Panquesito7 left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Please enable GitHub Actions in your repository of this fork in this link: https://github.com/kumaryash18/C/actions
Also, please fix clang-tidy warnings.

Comment on lines 13 to 16
#include<stdio.h> // for printf() and scanf()
#include<string.h> // for strlen()
#include<ctype.h> // for isalnum()
#include<process.h> // for exit()
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Suggested change
#include<stdio.h> // for printf() and scanf()
#include<string.h> // for strlen()
#include<ctype.h> // for isalnum()
#include<process.h> // for exit()
#include<stdio.h> /// for IO operations
#include<string.h> /// for strlen()
#include<ctype.h> /// for isalnum()
#include<process.h> /// for exit()

Copy link
Member

@Panquesito7 Panquesito7 left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Almost there! 😄

Comment on lines 22 to 25
char stack[10]; //< array stack
int top; //< stores index of top element
};
struct Stack st; // global declaration of stack st
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Suggested change
char stack[10]; //< array stack
int top; //< stores index of top element
};
struct Stack st; // global declaration of stack st
char stack[10]; ///< array stack
int top; ///< stores index of top element
};
struct Stack st; ///< global declaration of stack st

Comment on lines 85 to 90
if(opd == '+' || opd == '-')
return 0;
else if(opd == '/' || opd == '*' || opd == '%')
return 1;
else
return -1;
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Suggested change
if(opd == '+' || opd == '-')
return 0;
else if(opd == '/' || opd == '*' || opd == '%')
return 1;
else
return -1;
if(opd == '+' || opd == '-') {
return 0;
}
else if(opd == '/' || opd == '*' || opd == '%') {
return 1;
}
else {
return -1;
}

Comment on lines 64 to 65
if(st.top == -1)
return 1;
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Suggested change
if(st.top == -1)
return 1;
if(st.top == -1) {
return 1;
}

* @returns popped character
*/
char pop() {
char item;
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Please add a one-line description of what the variable is for (see the example below). Same in other parts of the code.

int data = 0;       ///< The value/key of the node.

Copy link
Member

@Panquesito7 Panquesito7 left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Please fix the continuous integration. Let us know if you need any help here or in our Discord server. 😃

@Panquesito7 Panquesito7 changed the title add infix to postfix converter algorithm feat: add infix to postfix converter algorithm Oct 12, 2021
@Panquesito7 Panquesito7 removed the Autochecks are failing Failing CI Auto-checks label Oct 12, 2021
Copy link
Member

@Panquesito7 Panquesito7 left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Almost there! 😄

* @returns `true` if stack is empty
* @returns `false` if stack is not empty
*/
int isEmpty() {
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Consider using uint64_t for non-negative values (or their appropriate size: uint32_t, uint16_t, uint8_t) or int64_t for negative values. Check other parts of the code (reference). Let us know if you need any help here or in our Discord server. 😃


/**
* @brief Function to check priority of operators
* @param opd- operator whose priority is to be checked
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Suggested change
* @param opd- operator whose priority is to be checked
* @param opd operator whose priority is to be checked

return -1;
}
}

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Please add self-test cases so we can verify that your program works as excepted (see the typical structure of a program as a reference). Let us know if you need any help here or in our Discord server. 😃

Copy link
Member

@Panquesito7 Panquesito7 left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Almost there! 😄

/**
* @file
* @brief infix to postfix converter
* Wikipedia- [Shunting-yard algorithm](https://en.wikipedia.org/wiki/Shunting-yard_algorithm)
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Is this the Shunting Yard algorithm or Infix to Postfix algorithm? 🤔

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Not sure if Wikipedia names it that way, for clarity provided a different reference. Hope it seems good :)

Comment on lines 23 to 24
char stack[10]; ///< array stack
int top; ///< stores index of top element
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Suggested change
char stack[10]; ///< array stack
int top; ///< stores index of top element
char stack[10]; ///< array stack
int top; ///< stores index of top element

char item; ///< to store popped value to be returned
if(st.top == -1) { // underflow condition
printf("Stack underflow...");
exit(0);
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

If this is an error statement we should use 1 instead.

Suggested change
exit(0);
exit(1);

Comment on lines 59 to 60
* @returns `true` if stack is empty
* @returns `false` if stack is not empty
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This would be for a bool function, and this is an integer function. Please update it as needed.

Comment on lines 153 to 154
st.top = -1; // initialize
test(); // run self-test implementations
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Suggested change
st.top = -1; // initialize
test(); // run self-test implementations
st.top = -1; /// initialize
test(); /// run self-test implementations

printf("Postfix: %s", convert(inf));
return 0;
}

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Suggested change

/**
* @file
* @brief infix to postfix converter
* Reference- [infix to postfix converter](https://www.includehelp.com/c/infix-to-postfix-conversion-using-stack-with-c-program.aspx)
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Suggested change
* Reference- [infix to postfix converter](https://www.includehelp.com/c/infix-to-postfix-conversion-using-stack-with-c-program.aspx)

@@ -0,0 +1,160 @@
/**
* @file
* @brief infix to postfix converter
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Suggested change
* @brief infix to postfix converter
* @brief [Infix to Postfix converter](https://www.includehelp.com/c/infix-to-postfix-conversion-using-stack-with-c-program.aspx) implementation


/**
* @brief Function to check whether stack is empty or not
* @returns 1 if stack is empty
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Suggested change
* @returns 1 if stack is empty
* @returns 1 if the stack IS empty

/**
* @brief Function to check whether stack is empty or not
* @returns 1 if stack is empty
* @returns 0 if stack is not empty
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Suggested change
* @returns 0 if stack is not empty
* @returns 0 if the stack is NOT empty

int main() {
st.top = -1; /// initialize
test(); /// run self-test implementations
char inf[25]; ///< to store input infix expression
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Suggested change
char inf[25]; ///< to store input infix expression
char inf[25]; ///< to store input infix expression

input- "(A/(B-C)*D+E)"
expected output- "ABC-/D*E+"
*/
assert(strcmp(convert("(A/(B-C)*D+E)"), "ABC-/D*E+") == 0); // this ensures that the algorithm works as expected
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Suggested change
assert(strcmp(convert("(A/(B-C)*D+E)"), "ABC-/D*E+") == 0); // this ensures that the algorithm works as expected
assert(strcmp(convert("(A/(B-C)*D+E)"), "ABC-/D*E+") == 0); /// this ensures that the algorithm works as expected
std::cout << "All tests have successfully passed!\n";

input- "(A/(B-C)*D+E)"
expected output- "ABC-/D*E+"
*/
assert(strcmp(convert("(A/(B-C)*D+E)"), "ABC-/D*E+") == 0); // this ensures that the algorithm works as expected
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Feel free to add more tests as well (if applicable)! 🙂

Copy link
Member

@Panquesito7 Panquesito7 left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Impressive and awesome work! You've done amazing work, and it's been your
first contribution here! We hope you keep contributing! 😄👍🎉

After this PR is merged, you can ask for the
contributor role in our Discord server! 🚀

@Panquesito7 Panquesito7 added approved Approved; waiting for merge and removed Changes requested Proper Documentation Required requested to write the documentation properly labels Oct 16, 2021
@Panquesito7 Panquesito7 merged commit 654105c into TheAlgorithms:master Oct 17, 2021
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

approved Approved; waiting for merge enhancement New feature or request

Projects

None yet

Development

Successfully merging this pull request may close these issues.

3 participants