feat: add infix to postfix converter algorithm#869
feat: add infix to postfix converter algorithm#869Panquesito7 merged 13 commits intoTheAlgorithms:masterfrom kumaryash18:expression
Conversation
There was a problem hiding this comment.
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
left a comment
There was a problem hiding this comment.
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.
conversions/infix_to_postfix2.c
Outdated
| #include<stdio.h> // for printf() and scanf() | ||
| #include<string.h> // for strlen() | ||
| #include<ctype.h> // for isalnum() | ||
| #include<process.h> // for exit() |
There was a problem hiding this comment.
| #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() |
conversions/infix_to_postfix2.c
Outdated
| char stack[10]; //< array stack | ||
| int top; //< stores index of top element | ||
| }; | ||
| struct Stack st; // global declaration of stack st |
There was a problem hiding this comment.
| 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 |
conversions/infix_to_postfix2.c
Outdated
| if(opd == '+' || opd == '-') | ||
| return 0; | ||
| else if(opd == '/' || opd == '*' || opd == '%') | ||
| return 1; | ||
| else | ||
| return -1; |
There was a problem hiding this comment.
| 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; | |
| } |
conversions/infix_to_postfix2.c
Outdated
| if(st.top == -1) | ||
| return 1; |
There was a problem hiding this comment.
| if(st.top == -1) | |
| return 1; | |
| if(st.top == -1) { | |
| return 1; | |
| } |
conversions/infix_to_postfix2.c
Outdated
| * @returns popped character | ||
| */ | ||
| char pop() { | ||
| char item; |
There was a problem hiding this comment.
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.
Panquesito7
left a comment
There was a problem hiding this comment.
Please fix the continuous integration. Let us know if you need any help here or in our Discord server. 😃
conversions/infix_to_postfix2.c
Outdated
| * @returns `true` if stack is empty | ||
| * @returns `false` if stack is not empty | ||
| */ | ||
| int isEmpty() { |
There was a problem hiding this comment.
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. 😃
conversions/infix_to_postfix2.c
Outdated
|
|
||
| /** | ||
| * @brief Function to check priority of operators | ||
| * @param opd- operator whose priority is to be checked |
There was a problem hiding this comment.
| * @param opd- operator whose priority is to be checked | |
| * @param opd operator whose priority is to be checked |
| return -1; | ||
| } | ||
| } | ||
|
|
There was a problem hiding this comment.
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. 😃
conversions/infix_to_postfix2.c
Outdated
| /** | ||
| * @file | ||
| * @brief infix to postfix converter | ||
| * Wikipedia- [Shunting-yard algorithm](https://en.wikipedia.org/wiki/Shunting-yard_algorithm) |
There was a problem hiding this comment.
Is this the Shunting Yard algorithm or Infix to Postfix algorithm? 🤔
There was a problem hiding this comment.
Not sure if Wikipedia names it that way, for clarity provided a different reference. Hope it seems good :)
conversions/infix_to_postfix2.c
Outdated
| char stack[10]; ///< array stack | ||
| int top; ///< stores index of top element |
There was a problem hiding this comment.
| char stack[10]; ///< array stack | |
| int top; ///< stores index of top element | |
| char stack[10]; ///< array stack | |
| int top; ///< stores index of top element |
conversions/infix_to_postfix2.c
Outdated
| char item; ///< to store popped value to be returned | ||
| if(st.top == -1) { // underflow condition | ||
| printf("Stack underflow..."); | ||
| exit(0); |
There was a problem hiding this comment.
If this is an error statement we should use 1 instead.
| exit(0); | |
| exit(1); |
conversions/infix_to_postfix2.c
Outdated
| * @returns `true` if stack is empty | ||
| * @returns `false` if stack is not empty |
There was a problem hiding this comment.
This would be for a bool function, and this is an integer function. Please update it as needed.
conversions/infix_to_postfix2.c
Outdated
| st.top = -1; // initialize | ||
| test(); // run self-test implementations |
There was a problem hiding this comment.
| st.top = -1; // initialize | |
| test(); // run self-test implementations | |
| st.top = -1; /// initialize | |
| test(); /// run self-test implementations |
conversions/infix_to_postfix2.c
Outdated
| printf("Postfix: %s", convert(inf)); | ||
| return 0; | ||
| } | ||
|
|
conversions/infix_to_postfix2.c
Outdated
| /** | ||
| * @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) |
There was a problem hiding this comment.
| * Reference- [infix to postfix converter](https://www.includehelp.com/c/infix-to-postfix-conversion-using-stack-with-c-program.aspx) |
conversions/infix_to_postfix2.c
Outdated
| @@ -0,0 +1,160 @@ | |||
| /** | |||
| * @file | |||
| * @brief infix to postfix converter | |||
There was a problem hiding this comment.
| * @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 |
conversions/infix_to_postfix2.c
Outdated
|
|
||
| /** | ||
| * @brief Function to check whether stack is empty or not | ||
| * @returns 1 if stack is empty |
There was a problem hiding this comment.
| * @returns 1 if stack is empty | |
| * @returns 1 if the stack IS empty |
conversions/infix_to_postfix2.c
Outdated
| /** | ||
| * @brief Function to check whether stack is empty or not | ||
| * @returns 1 if stack is empty | ||
| * @returns 0 if stack is not empty |
There was a problem hiding this comment.
| * @returns 0 if stack is not empty | |
| * @returns 0 if the stack is NOT empty |
conversions/infix_to_postfix2.c
Outdated
| int main() { | ||
| st.top = -1; /// initialize | ||
| test(); /// run self-test implementations | ||
| char inf[25]; ///< to store input infix expression |
There was a problem hiding this comment.
| char inf[25]; ///< to store input infix expression | |
| char inf[25]; ///< to store input infix expression |
conversions/infix_to_postfix2.c
Outdated
| 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 |
There was a problem hiding this comment.
| 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"; |
conversions/infix_to_postfix2.c
Outdated
| 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 |
There was a problem hiding this comment.
Feel free to add more tests as well (if applicable)! 🙂
Panquesito7
left a comment
There was a problem hiding this comment.
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! 🚀
Description of Change
Added a program to convert an infix expression to postfix expression in misc
References
Checklist
Notes:
A program in C to convert an infix expression to postfix expression