From c02dba0fa0df3a1ce4d49fd6c281a136974486d6 Mon Sep 17 00:00:00 2001 From: kumaryash18 Date: Wed, 6 Oct 2021 19:25:16 +0530 Subject: [PATCH 01/20] add infix to postfix converter algorithm --- misc/infix_to_postfix.c | 96 +++++++++++++++++++++++++++++++++++++++++ 1 file changed, 96 insertions(+) create mode 100644 misc/infix_to_postfix.c diff --git a/misc/infix_to_postfix.c b/misc/infix_to_postfix.c new file mode 100644 index 0000000000..5edaf97994 --- /dev/null +++ b/misc/infix_to_postfix.c @@ -0,0 +1,96 @@ +//infix to postfix +#include +#include +#include +#include + +char stack[10]; +int top=-1; +void push(char opd) +{ + if(top == 9) + { + printf("Stack overflow..."); + exit(0); + } + top++; + stack[top]=opd; +} +char pop() +{ + char item; + if(top == -1) + { + printf("Stack overflow..."); + exit(0); + } + item=stack[top]; + top--; + return item; +} + +int isEmpty() +{ + if(top == -1) + return 1; + return 0; +} + +char Top() +{ + return stack[top]; +} + +int priority(char opd) +{ + if(opd == '+' || opd == '-') + return 0; + else if(opd == '/' || opd == '*' || opd == '%') + return 1; + else + return -1; +} + +void main() +{ + char inf[25], post[25]; + printf("Enter Infix: "); + scanf("%s", inf); + int i, j=0; + for(i=0; i < strlen(inf); i++) + { + if(isalnum(inf[i])) + { + post[j] = inf[i]; + j++; + } + else if(inf[i] == '(') + push(inf[i]); + else if(inf[i] == ')') + { + while(Top() != '(') + { + post[j] = pop(); + j++; + } + pop(); + } + else + { + while( (!isEmpty()) && (priority(inf[i]) <= priority(Top())) ) + { + post[j] = pop(); + j++; + } + push(inf[i]); + } + } + while(!isEmpty()) + { + post[j] = pop(); + j++; + } + post[j] = '\0'; + printf("Postfix: %s", post); +} + From 1d0d7126d3192ef93c48133c05f8a190ad152934 Mon Sep 17 00:00:00 2001 From: kumaryash18 Date: Thu, 7 Oct 2021 20:20:26 +0530 Subject: [PATCH 02/20] docs: documentation changes --- conversions/infix_to_postfix2.c | 140 ++++++++++++++++++++++++++++++++ misc/infix_to_postfix.c | 96 ---------------------- 2 files changed, 140 insertions(+), 96 deletions(-) create mode 100644 conversions/infix_to_postfix2.c delete mode 100644 misc/infix_to_postfix.c diff --git a/conversions/infix_to_postfix2.c b/conversions/infix_to_postfix2.c new file mode 100644 index 0000000000..b8dab70a55 --- /dev/null +++ b/conversions/infix_to_postfix2.c @@ -0,0 +1,140 @@ +/** + * @file + * @brief infix to postfix converter + * @details + * The input infix expression is of type string upto 24 characters. + * + * Supported operations- '+', '-', '/', '*', '%' + * + * @author [Kumar Yash](https://github.com/kumaryash18) + * @see infix_to_postfix.c + */ + +#include // for printf() and scanf() +#include // for strlen() +#include // for isalnum() +#include // for exit() + +/** + * @brief array implementation of stack using structure + */ +struct Stack { + char stack[10]; //< array stack + int top; //< stores index of top element +}; +struct Stack st; // global declaration of stack st + +/** + * @brief Function to push '(' and operators from infix expression + * @param opd- character to be pushed in stack + * @returns void + */ +void push(char opd) { + if(st.top == 9) // overflow condition + { + printf("Stack overflow..."); + exit(0); + } + st.top++; + st.stack[st.top] = opd; +} + +/** + * @brief Function to pop operators and '(' + * @returns popped character + */ +char pop() { + char item; + if(st.top == -1) // underflow condition + { + printf("Stack underflow..."); + exit(0); + } + item = st.stack[st.top]; + st.top--; + return item; +} + +/** + * @brief Function to check whether stack is empty or not + * @returns `true` if stack is empty + * @returns `false` if stack is not empty + */ +int isEmpty() { + if(st.top == -1) + return 1; + return 0; +} + +/** + * @brief Function to get top of stack + * @returns top of stack + */ +char Top() { + return st.stack[st.top]; +} + +/** + * @brief Function to check priority of operators + * @param opd- operator whose priority is to be checked + * @returns 0 if operator is '+' or '-' + * @returns 1 if operator is '/' or '*' or '%' + * @returns -1 otherwise + */ +int priority(char opd) { + if(opd == '+' || opd == '-') + return 0; + else if(opd == '/' || opd == '*' || opd == '%') + return 1; + else + return -1; +} + +/** + * @brief Main function + * @returns 0 on exit + */ +int main() { + st.top = -1; // represents empty stack + char inf[25], post[25]; + printf("Enter Infix: "); + scanf("%s", inf); // input infix expression + int i, j=0; + for(i=0; i < strlen(inf); i++) + { + if(isalnum(inf[i])) // if scanned element is an alphabet or number + { + post[j] = inf[i]; // append in postfix expression + j++; + } + else if(inf[i] == '(') // if scanned element is opening parentheses + push(inf[i]); // push on stack. + else if(inf[i] == ')') // if scanned element is closing parentheses, + { + while(Top() != '(') // pop elements from stack and append in postfix expression + { // until opening parentheses becomes top + post[j] = pop(); + j++; + } + pop(); // pop opening parentheses + } + else // if scanned element is an operator + { + while( (!isEmpty()) && (priority(inf[i]) <= priority(Top())) ) // pop and append until stack becomes + { // empty or priority of top operator + post[j] = pop(); // becomes smaller than scanned operator + j++; // '(' has priority -1 + } + push(inf[i]); // push the scanned operator + } + } + while(!isEmpty()) // pop and append residual operators from stack + { + post[j] = pop(); + j++; + } + post[j] = '\0'; + printf("Postfix: %s", post); // print postfix expression + return 0; +} + diff --git a/misc/infix_to_postfix.c b/misc/infix_to_postfix.c deleted file mode 100644 index 5edaf97994..0000000000 --- a/misc/infix_to_postfix.c +++ /dev/null @@ -1,96 +0,0 @@ -//infix to postfix -#include -#include -#include -#include - -char stack[10]; -int top=-1; -void push(char opd) -{ - if(top == 9) - { - printf("Stack overflow..."); - exit(0); - } - top++; - stack[top]=opd; -} -char pop() -{ - char item; - if(top == -1) - { - printf("Stack overflow..."); - exit(0); - } - item=stack[top]; - top--; - return item; -} - -int isEmpty() -{ - if(top == -1) - return 1; - return 0; -} - -char Top() -{ - return stack[top]; -} - -int priority(char opd) -{ - if(opd == '+' || opd == '-') - return 0; - else if(opd == '/' || opd == '*' || opd == '%') - return 1; - else - return -1; -} - -void main() -{ - char inf[25], post[25]; - printf("Enter Infix: "); - scanf("%s", inf); - int i, j=0; - for(i=0; i < strlen(inf); i++) - { - if(isalnum(inf[i])) - { - post[j] = inf[i]; - j++; - } - else if(inf[i] == '(') - push(inf[i]); - else if(inf[i] == ')') - { - while(Top() != '(') - { - post[j] = pop(); - j++; - } - pop(); - } - else - { - while( (!isEmpty()) && (priority(inf[i]) <= priority(Top())) ) - { - post[j] = pop(); - j++; - } - push(inf[i]); - } - } - while(!isEmpty()) - { - post[j] = pop(); - j++; - } - post[j] = '\0'; - printf("Postfix: %s", post); -} - From 32dd3e81b950ab2684b641acfb3265ac6eab0552 Mon Sep 17 00:00:00 2001 From: kumaryash18 Date: Thu, 7 Oct 2021 22:02:53 +0530 Subject: [PATCH 03/20] docs: documentation changes --- conversions/infix_to_postfix2.c | 8 ++++---- 1 file changed, 4 insertions(+), 4 deletions(-) diff --git a/conversions/infix_to_postfix2.c b/conversions/infix_to_postfix2.c index b8dab70a55..58f7315e47 100644 --- a/conversions/infix_to_postfix2.c +++ b/conversions/infix_to_postfix2.c @@ -10,10 +10,10 @@ * @see infix_to_postfix.c */ -#include // for printf() and scanf() -#include // for strlen() -#include // for isalnum() -#include // for exit() +#include /// for IO operations +#include /// for strlen() +#include /// for isalnum() +#include /// for exit() /** * @brief array implementation of stack using structure From 8566c2eece7ab575be4ad4280ed3c50aae136253 Mon Sep 17 00:00:00 2001 From: github-actions <${GITHUB_ACTOR}@users.noreply.github.com> Date: Thu, 7 Oct 2021 16:34:26 +0000 Subject: [PATCH 04/20] updating DIRECTORY.md --- DIRECTORY.md | 1 + 1 file changed, 1 insertion(+) diff --git a/DIRECTORY.md b/DIRECTORY.md index 0b22b5b92b..0c653fd22c 100644 --- a/DIRECTORY.md +++ b/DIRECTORY.md @@ -26,6 +26,7 @@ * [Hexadecimal To Octal](https://github.com/TheAlgorithms/C/blob/master/conversions/hexadecimal_to_octal.c) * [Hexadecimal To Octal2](https://github.com/TheAlgorithms/C/blob/master/conversions/hexadecimal_to_octal2.c) * [Infix To Postfix](https://github.com/TheAlgorithms/C/blob/master/conversions/infix_to_postfix.c) + * [Infix To Postfix2](https://github.com/TheAlgorithms/C/blob/master/conversions/infix_to_postfix2.c) * [Int To String](https://github.com/TheAlgorithms/C/blob/master/conversions/int_to_string.c) * [Octal To Binary](https://github.com/TheAlgorithms/C/blob/master/conversions/octal_to_binary.c) * [Octal To Decimal](https://github.com/TheAlgorithms/C/blob/master/conversions/octal_to_decimal.c) From 45d92c5cc14969047bcaf64900922d843644569e Mon Sep 17 00:00:00 2001 From: kumaryash18 Date: Fri, 8 Oct 2021 13:35:29 +0530 Subject: [PATCH 05/20] docs: documentation changes --- conversions/infix_to_postfix2.c | 80 ++++++++++++++++----------------- 1 file changed, 39 insertions(+), 41 deletions(-) diff --git a/conversions/infix_to_postfix2.c b/conversions/infix_to_postfix2.c index 58f7315e47..d683ce6e74 100644 --- a/conversions/infix_to_postfix2.c +++ b/conversions/infix_to_postfix2.c @@ -19,10 +19,10 @@ * @brief array implementation of stack using structure */ struct Stack { - char stack[10]; //< array stack - int top; //< stores index of top element + char stack[10]; ///< array stack + int top; ///< stores index of top element }; -struct Stack st; // global declaration of stack st +struct Stack st; ///< global declaration of stack st /** * @brief Function to push '(' and operators from infix expression @@ -30,8 +30,7 @@ struct Stack st; // global declaration of stack st * @returns void */ void push(char opd) { - if(st.top == 9) // overflow condition - { + if(st.top == 9) { // overflow condition printf("Stack overflow..."); exit(0); } @@ -44,9 +43,8 @@ void push(char opd) { * @returns popped character */ char pop() { - char item; - if(st.top == -1) // underflow condition - { + char item; ///< to store popped value to be returned + if(st.top == -1) { // underflow condition printf("Stack underflow..."); exit(0); } @@ -61,8 +59,9 @@ char pop() { * @returns `false` if stack is not empty */ int isEmpty() { - if(st.top == -1) + if(st.top == -1) { return 1; + } return 0; } @@ -82,12 +81,15 @@ char Top() { * @returns -1 otherwise */ int priority(char opd) { - if(opd == '+' || opd == '-') + if(opd == '+' || opd == '-') { return 0; - else if(opd == '/' || opd == '*' || opd == '%') + } + else if(opd == '/' || opd == '*' || opd == '%') { return 1; - else + } + else { return -1; + } } /** @@ -95,46 +97,42 @@ int priority(char opd) { * @returns 0 on exit */ int main() { - st.top = -1; // represents empty stack - char inf[25], post[25]; + st.top = -1; // represents empty stack + char inf[25]; ///< to store the input infix expression + char post[25]; ///< to store the postfix expression printf("Enter Infix: "); - scanf("%s", inf); // input infix expression - int i, j=0; - for(i=0; i < strlen(inf); i++) - { - if(isalnum(inf[i])) // if scanned element is an alphabet or number - { - post[j] = inf[i]; // append in postfix expression + scanf("%s", inf); // input infix expression + int i; ///< loop iterator + int j=0; ///< keeps track of end of postfix string + for(i=0; i < strlen(inf); i++) { + if(isalnum(inf[i])) { // if scanned element is an alphabet or number + post[j] = inf[i]; // append in postfix expression j++; } - else if(inf[i] == '(') // if scanned element is opening parentheses - push(inf[i]); // push on stack. - else if(inf[i] == ')') // if scanned element is closing parentheses, - { - while(Top() != '(') // pop elements from stack and append in postfix expression - { // until opening parentheses becomes top - post[j] = pop(); + else if(inf[i] == '(') { // if scanned element is opening parentheses + push(inf[i]); // push on stack. + } + else if(inf[i] == ')') { // if scanned element is closing parentheses, + while(Top() != '(') { // pop elements from stack and append in postfix expression + post[j] = pop(); // until opening parentheses becomes top. j++; } - pop(); // pop opening parentheses + pop(); // pop opening parentheses } - else // if scanned element is an operator - { - while( (!isEmpty()) && (priority(inf[i]) <= priority(Top())) ) // pop and append until stack becomes - { // empty or priority of top operator - post[j] = pop(); // becomes smaller than scanned operator - j++; // '(' has priority -1 - } - push(inf[i]); // push the scanned operator + else { // if scanned element is an operator + while( (!isEmpty()) && (priority(inf[i]) <= priority(Top())) ) { // pop and append until stack becomes + post[j] = pop(); // empty or priority of top operator + j++; // becomes smaller than scanned operator + } // '(' has priority -1 + push(inf[i]); // push the scanned operator } } - while(!isEmpty()) // pop and append residual operators from stack - { + while(!isEmpty()) { // pop and append residual operators from stack post[j] = pop(); j++; } - post[j] = '\0'; - printf("Postfix: %s", post); // print postfix expression + post[j] = '\0'; // end postfix string with null character + printf("Postfix: %s", post); // print postfix expression return 0; } From b0fa88682951433084c15de3e6d1522ae0f147f5 Mon Sep 17 00:00:00 2001 From: kumaryash18 Date: Mon, 11 Oct 2021 14:39:11 +0530 Subject: [PATCH 06/20] fix: continuous integration --- conversions/infix_to_postfix2.c | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/conversions/infix_to_postfix2.c b/conversions/infix_to_postfix2.c index d683ce6e74..698c5fbe72 100644 --- a/conversions/infix_to_postfix2.c +++ b/conversions/infix_to_postfix2.c @@ -13,7 +13,7 @@ #include /// for IO operations #include /// for strlen() #include /// for isalnum() -#include /// for exit() +#include /// for exit() /** * @brief array implementation of stack using structure From 71a73531e02fc8881a522d1105faa8b619ebde6a Mon Sep 17 00:00:00 2001 From: kumaryash18 Date: Wed, 13 Oct 2021 16:13:07 +0530 Subject: [PATCH 07/20] [test, docs]: add test case, documentation changes --- conversions/infix_to_postfix2.c | 69 +++++++++++++++++++++------------ 1 file changed, 44 insertions(+), 25 deletions(-) diff --git a/conversions/infix_to_postfix2.c b/conversions/infix_to_postfix2.c index 698c5fbe72..1ad04f95e0 100644 --- a/conversions/infix_to_postfix2.c +++ b/conversions/infix_to_postfix2.c @@ -1,19 +1,20 @@ /** * @file * @brief infix to postfix converter + * Wikipedia- [Shunting-yard algorithm](https://en.wikipedia.org/wiki/Shunting-yard_algorithm) * @details * The input infix expression is of type string upto 24 characters. - * * Supported operations- '+', '-', '/', '*', '%' - * * @author [Kumar Yash](https://github.com/kumaryash18) * @see infix_to_postfix.c */ -#include /// for IO operations -#include /// for strlen() -#include /// for isalnum() -#include /// for exit() +#include /// for IO operations +#include /// for strlen(), strcmpi() +#include /// for isalnum() +#include /// for exit() +#include /// for uint16_t, int16_t +#include /// for assert /** * @brief array implementation of stack using structure @@ -25,8 +26,8 @@ struct Stack { struct Stack st; ///< global declaration of stack st /** - * @brief Function to push '(' and operators from infix expression - * @param opd- character to be pushed in stack + * @brief Function to push on stack + * @param opd character to be pushed in stack * @returns void */ void push(char opd) { @@ -39,7 +40,7 @@ void push(char opd) { } /** - * @brief Function to pop operators and '(' + * @brief Function to pop from stack * @returns popped character */ char pop() { @@ -58,7 +59,7 @@ char pop() { * @returns `true` if stack is empty * @returns `false` if stack is not empty */ -int isEmpty() { +uint16_t isEmpty() { if(st.top == -1) { return 1; } @@ -75,16 +76,16 @@ char Top() { /** * @brief Function to check priority of operators - * @param opd- operator whose priority is to be checked + * @param opr operator whose priority is to be checked * @returns 0 if operator is '+' or '-' * @returns 1 if operator is '/' or '*' or '%' * @returns -1 otherwise */ -int priority(char opd) { - if(opd == '+' || opd == '-') { +int16_t priority(char opr) { + if(opr == '+' || opr == '-') { return 0; } - else if(opd == '/' || opd == '*' || opd == '%') { + else if(opr == '/' || opr == '*' || opr == '%') { return 1; } else { @@ -93,18 +94,15 @@ int priority(char opd) { } /** - * @brief Main function - * @returns 0 on exit + * @brief Function to convert infix to postfix expression + * @param inf the input infix expression + * @returns output postfix expression */ -int main() { - st.top = -1; // represents empty stack - char inf[25]; ///< to store the input infix expression - char post[25]; ///< to store the postfix expression - printf("Enter Infix: "); - scanf("%s", inf); // input infix expression +char *convert(char inf[]) { + static char post[25]; ///< to store the postfix expression int i; ///< loop iterator - int j=0; ///< keeps track of end of postfix string - for(i=0; i < strlen(inf); i++) { + int j = 0; ///< keeps track of end of postfix string + for(i = 0; i < strlen(inf); i++) { if(isalnum(inf[i])) { // if scanned element is an alphabet or number post[j] = inf[i]; // append in postfix expression j++; @@ -132,7 +130,28 @@ int main() { j++; } post[j] = '\0'; // end postfix string with null character - printf("Postfix: %s", post); // print postfix expression + return post; +} + +/** + * @brief Self-test implementations + * @returns void + */ +static void test() { + /* check sample test case + input- "(A/(B-C)*D+E)" + expected output- "ABC-/D*E+" + */ + assert(strcmpi(convert("(A/(B-C)*D+E)"), "ABC-/D*E+") == 0); // this ensures that the algorithm works as expected +} + +/** + * @brief Main function + * @returns 0 on exit + */ +int main() { + st.top = -1; // initialize + test(); // run self-test implementations return 0; } From 6e4f228ba786840e13ebfd8436bdf3efb19aff01 Mon Sep 17 00:00:00 2001 From: kumaryash18 Date: Wed, 13 Oct 2021 16:38:19 +0530 Subject: [PATCH 08/20] docs: documentation changes --- conversions/infix_to_postfix2.c | 4 ++++ 1 file changed, 4 insertions(+) diff --git a/conversions/infix_to_postfix2.c b/conversions/infix_to_postfix2.c index 1ad04f95e0..875c9390f8 100644 --- a/conversions/infix_to_postfix2.c +++ b/conversions/infix_to_postfix2.c @@ -152,6 +152,10 @@ static void test() { int main() { st.top = -1; // initialize test(); // run self-test implementations + char inf[25]; ///< to store input infix expression + printf("Enter infix: "); + scanf("%s", inf); + printf("Postfix: %s", convert(inf)); return 0; } From 9fae7a02a58ddd7423a02b6f76dbf7ab8120d6fc Mon Sep 17 00:00:00 2001 From: kumaryash18 Date: Wed, 13 Oct 2021 16:52:16 +0530 Subject: [PATCH 09/20] fix: continuous integration --- conversions/infix_to_postfix2.c | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/conversions/infix_to_postfix2.c b/conversions/infix_to_postfix2.c index 875c9390f8..a580b857dc 100644 --- a/conversions/infix_to_postfix2.c +++ b/conversions/infix_to_postfix2.c @@ -10,7 +10,7 @@ */ #include /// for IO operations -#include /// for strlen(), strcmpi() +#include /// for strlen(), strcmp() #include /// for isalnum() #include /// for exit() #include /// for uint16_t, int16_t @@ -142,7 +142,7 @@ static void test() { input- "(A/(B-C)*D+E)" expected output- "ABC-/D*E+" */ - assert(strcmpi(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 } /** From dfa503489e929f81fdcb135141bd86ada5cf9033 Mon Sep 17 00:00:00 2001 From: kumaryash18 Date: Wed, 13 Oct 2021 22:43:15 +0530 Subject: [PATCH 10/20] docs: documentation changes --- conversions/infix_to_postfix2.c | 17 ++++++++--------- 1 file changed, 8 insertions(+), 9 deletions(-) diff --git a/conversions/infix_to_postfix2.c b/conversions/infix_to_postfix2.c index a580b857dc..a9e5e3ea86 100644 --- a/conversions/infix_to_postfix2.c +++ b/conversions/infix_to_postfix2.c @@ -1,7 +1,7 @@ /** * @file * @brief infix to postfix converter - * Wikipedia- [Shunting-yard algorithm](https://en.wikipedia.org/wiki/Shunting-yard_algorithm) + * Reference- [infix to postfix converter](https://www.includehelp.com/c/infix-to-postfix-conversion-using-stack-with-c-program.aspx) * @details * The input infix expression is of type string upto 24 characters. * Supported operations- '+', '-', '/', '*', '%' @@ -21,7 +21,7 @@ */ struct Stack { char stack[10]; ///< array stack - int top; ///< stores index of top element + int top; ///< stores index of top element }; struct Stack st; ///< global declaration of stack st @@ -33,7 +33,7 @@ struct Stack st; ///< global declaration of stack st void push(char opd) { if(st.top == 9) { // overflow condition printf("Stack overflow..."); - exit(0); + exit(1); } st.top++; st.stack[st.top] = opd; @@ -47,7 +47,7 @@ char pop() { char item; ///< to store popped value to be returned if(st.top == -1) { // underflow condition printf("Stack underflow..."); - exit(0); + exit(1); } item = st.stack[st.top]; st.top--; @@ -56,8 +56,8 @@ char pop() { /** * @brief Function to check whether stack is empty or not - * @returns `true` if stack is empty - * @returns `false` if stack is not empty + * @returns 1 if stack is empty + * @returns 0 if stack is not empty */ uint16_t isEmpty() { if(st.top == -1) { @@ -150,12 +150,11 @@ static void test() { * @returns 0 on exit */ int main() { - st.top = -1; // initialize - test(); // run self-test implementations + st.top = -1; /// initialize + test(); /// run self-test implementations char inf[25]; ///< to store input infix expression printf("Enter infix: "); scanf("%s", inf); printf("Postfix: %s", convert(inf)); return 0; } - From b34b396a2f9f79bf6b12233d792ebf07a8dc2f45 Mon Sep 17 00:00:00 2001 From: kumaryash18 Date: Thu, 14 Oct 2021 15:50:04 +0530 Subject: [PATCH 11/20] docs: documentation changes --- conversions/infix_to_postfix2.c | 25 ++++++++++++------------- 1 file changed, 12 insertions(+), 13 deletions(-) diff --git a/conversions/infix_to_postfix2.c b/conversions/infix_to_postfix2.c index a9e5e3ea86..385774d157 100644 --- a/conversions/infix_to_postfix2.c +++ b/conversions/infix_to_postfix2.c @@ -1,7 +1,6 @@ /** * @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) + * @brief [Infix to Postfix converter](https://www.includehelp.com/c/infix-to-postfix-conversion-using-stack-with-c-program.aspx) implementation * @details * The input infix expression is of type string upto 24 characters. * Supported operations- '+', '-', '/', '*', '%' @@ -21,13 +20,13 @@ */ struct Stack { char stack[10]; ///< array stack - int top; ///< stores index of top element + int top; ///< stores index of the top element }; struct Stack st; ///< global declaration of stack st /** - * @brief Function to push on stack - * @param opd character to be pushed in stack + * @brief Function to push on the stack + * @param opd character to be pushed in the stack * @returns void */ void push(char opd) { @@ -40,11 +39,11 @@ void push(char opd) { } /** - * @brief Function to pop from stack + * @brief Function to pop from the stack * @returns popped character */ char pop() { - char item; ///< to store popped value to be returned + char item; ///< to store the popped value to be returned if(st.top == -1) { // underflow condition printf("Stack underflow..."); exit(1); @@ -55,9 +54,9 @@ char pop() { } /** - * @brief Function to check whether stack is empty or not - * @returns 1 if stack is empty - * @returns 0 if stack is not empty + * @brief Function to check whether the stack is empty or not + * @returns 1 if the stack IS empty + * @returns 0 if the stack is NOT empty */ uint16_t isEmpty() { if(st.top == -1) { @@ -67,7 +66,7 @@ uint16_t isEmpty() { } /** - * @brief Function to get top of stack + * @brief Function to get top of the stack * @returns top of stack */ char Top() { @@ -94,7 +93,7 @@ int16_t priority(char opr) { } /** - * @brief Function to convert infix to postfix expression + * @brief Function to convert infix expression to postfix expression * @param inf the input infix expression * @returns output postfix expression */ @@ -152,7 +151,7 @@ static void test() { int main() { st.top = -1; /// initialize test(); /// run self-test implementations - char inf[25]; ///< to store input infix expression + char inf[25]; ///< to store input infix expression printf("Enter infix: "); scanf("%s", inf); printf("Postfix: %s", convert(inf)); From f993e0f4071396a8977fad5593c5aa7d6694a592 Mon Sep 17 00:00:00 2001 From: kumaryash18 Date: Fri, 15 Oct 2021 18:56:54 +0530 Subject: [PATCH 12/20] test: add new test --- conversions/infix_to_postfix2.c | 7 ++++++- 1 file changed, 6 insertions(+), 1 deletion(-) diff --git a/conversions/infix_to_postfix2.c b/conversions/infix_to_postfix2.c index 385774d157..e0f584003b 100644 --- a/conversions/infix_to_postfix2.c +++ b/conversions/infix_to_postfix2.c @@ -141,7 +141,12 @@ static void test() { 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 + assert(strcmp(convert("(A/(B-C)*D+E)"), "ABC-/D*E+") == 0); /// this ensures that the algorithm works as expected + /* input- "7-(2*3+5)*(8-4/2)" + expected output- "723*5+842/-*-" + */ + assert(strcmp(convert("7-(2*3+5)*(8-4/2)"), "723*5+842/-*-") == 0); /// this ensures that the algorithm works as expected + printf("All tests have successfully passed!\n"); } /** From ff4aadbcdc981188109ba331b0f4d19c796cff2b Mon Sep 17 00:00:00 2001 From: kumaryash18 Date: Sat, 16 Oct 2021 17:37:42 +0530 Subject: [PATCH 13/20] feat: add postfix evaluation algorithm --- conversions/infix_to_postfix2.c | 164 -------------------------------- misc/postfix_evaluation.c | 121 +++++++++++++++++++++++ 2 files changed, 121 insertions(+), 164 deletions(-) delete mode 100644 conversions/infix_to_postfix2.c create mode 100644 misc/postfix_evaluation.c diff --git a/conversions/infix_to_postfix2.c b/conversions/infix_to_postfix2.c deleted file mode 100644 index e0f584003b..0000000000 --- a/conversions/infix_to_postfix2.c +++ /dev/null @@ -1,164 +0,0 @@ -/** - * @file - * @brief [Infix to Postfix converter](https://www.includehelp.com/c/infix-to-postfix-conversion-using-stack-with-c-program.aspx) implementation - * @details - * The input infix expression is of type string upto 24 characters. - * Supported operations- '+', '-', '/', '*', '%' - * @author [Kumar Yash](https://github.com/kumaryash18) - * @see infix_to_postfix.c - */ - -#include /// for IO operations -#include /// for strlen(), strcmp() -#include /// for isalnum() -#include /// for exit() -#include /// for uint16_t, int16_t -#include /// for assert - -/** - * @brief array implementation of stack using structure - */ -struct Stack { - char stack[10]; ///< array stack - int top; ///< stores index of the top element -}; -struct Stack st; ///< global declaration of stack st - -/** - * @brief Function to push on the stack - * @param opd character to be pushed in the stack - * @returns void - */ -void push(char opd) { - if(st.top == 9) { // overflow condition - printf("Stack overflow..."); - exit(1); - } - st.top++; - st.stack[st.top] = opd; -} - -/** - * @brief Function to pop from the stack - * @returns popped character - */ -char pop() { - char item; ///< to store the popped value to be returned - if(st.top == -1) { // underflow condition - printf("Stack underflow..."); - exit(1); - } - item = st.stack[st.top]; - st.top--; - return item; -} - -/** - * @brief Function to check whether the stack is empty or not - * @returns 1 if the stack IS empty - * @returns 0 if the stack is NOT empty - */ -uint16_t isEmpty() { - if(st.top == -1) { - return 1; - } - return 0; -} - -/** - * @brief Function to get top of the stack - * @returns top of stack - */ -char Top() { - return st.stack[st.top]; -} - -/** - * @brief Function to check priority of operators - * @param opr operator whose priority is to be checked - * @returns 0 if operator is '+' or '-' - * @returns 1 if operator is '/' or '*' or '%' - * @returns -1 otherwise - */ -int16_t priority(char opr) { - if(opr == '+' || opr == '-') { - return 0; - } - else if(opr == '/' || opr == '*' || opr == '%') { - return 1; - } - else { - return -1; - } -} - -/** - * @brief Function to convert infix expression to postfix expression - * @param inf the input infix expression - * @returns output postfix expression - */ -char *convert(char inf[]) { - static char post[25]; ///< to store the postfix expression - int i; ///< loop iterator - int j = 0; ///< keeps track of end of postfix string - for(i = 0; i < strlen(inf); i++) { - if(isalnum(inf[i])) { // if scanned element is an alphabet or number - post[j] = inf[i]; // append in postfix expression - j++; - } - else if(inf[i] == '(') { // if scanned element is opening parentheses - push(inf[i]); // push on stack. - } - else if(inf[i] == ')') { // if scanned element is closing parentheses, - while(Top() != '(') { // pop elements from stack and append in postfix expression - post[j] = pop(); // until opening parentheses becomes top. - j++; - } - pop(); // pop opening parentheses - } - else { // if scanned element is an operator - while( (!isEmpty()) && (priority(inf[i]) <= priority(Top())) ) { // pop and append until stack becomes - post[j] = pop(); // empty or priority of top operator - j++; // becomes smaller than scanned operator - } // '(' has priority -1 - push(inf[i]); // push the scanned operator - } - } - while(!isEmpty()) { // pop and append residual operators from stack - post[j] = pop(); - j++; - } - post[j] = '\0'; // end postfix string with null character - return post; -} - -/** - * @brief Self-test implementations - * @returns void - */ -static void test() { - /* check sample test case - 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 - /* input- "7-(2*3+5)*(8-4/2)" - expected output- "723*5+842/-*-" - */ - assert(strcmp(convert("7-(2*3+5)*(8-4/2)"), "723*5+842/-*-") == 0); /// this ensures that the algorithm works as expected - printf("All tests have successfully passed!\n"); -} - -/** - * @brief Main function - * @returns 0 on exit - */ -int main() { - st.top = -1; /// initialize - test(); /// run self-test implementations - char inf[25]; ///< to store input infix expression - printf("Enter infix: "); - scanf("%s", inf); - printf("Postfix: %s", convert(inf)); - return 0; -} diff --git a/misc/postfix_evaluation.c b/misc/postfix_evaluation.c new file mode 100644 index 0000000000..236b291d93 --- /dev/null +++ b/misc/postfix_evaluation.c @@ -0,0 +1,121 @@ +/** + * @file + * @brief [Postfix evaluation algorithm](https://www.includehelp.com/c/evaluation-of-postfix-expressions-using-stack-with-c-program.aspx) implementation + * @details + * The input postfix expression is of type string upto 24 characters (single digit numbers only). + * Supported operations- '+', '-', '/', '*', '%' + * @author [Kumar Yash](https://github.com/kumaryash18) + */ + +#include /// for IO operations +#include /// for strlen() +#include /// for isdigit() +#include /// for exit() +#include /// for int8_t +#include /// for assert + +/** + * @brief array implementation of stack using structure + */ +struct Stack { + int stack[10]; ///< array stack + int top; ///< stores index of the top element +}; +struct Stack st; ///< global declaration of stack st + +/** + * @brief Function to push on the stack + * @param opd number to be pushed in the stack + * @returns void + */ +void push(int8_t opd) { + if(st.top == 9) { // overflow condition + printf("Stack overflow..."); + exit(1); + } + st.top++; + st.stack[st.top] = opd; +} + +/** + * @brief Function to pop from the stack + * @returns popped number + */ +int8_t pop() { + int8_t item; ///< to store the popped value to be returned + if(st.top == -1) { // underflow condition + printf("Stack underflow..."); + exit(1); + } + item = st.stack[st.top]; + st.top--; + return item; +} + +/** + * @brief Function to evaluate postfix expression + * @param post the input postfix expression + * @returns evaluated answer + */ +int8_t evaluate(char post[]) { + int8_t it1; + int8_t it2; + int8_t temp; + int i; + for(i = 0; i < strlen(post); i++) { + if(isdigit(post[i])) { + push(post[i]-'0'); + } + else { + it2 = pop(); + it1 = pop(); + switch(post[i]) { + case '+': + temp = it1 + it2; break; + case '-': + temp = it1 - it2; break; + case '*': + temp = it1 * it2; break; + case '/': + temp = it1 / it2; break; + case '%': + temp = it1 % it2; break; + default: + printf("Invalid operator"); exit(1); + } + push(temp); + } + } + return pop(); +} + +/** + * @brief Self-test implementations + * @returns void + */ +static void test() { + /* check sample test case + input: "4572+-*" + expected output: -16 + */ + assert(evaluate("4572+-*") == -16); /// this ensures that the algorithm works as expected + /* input: "42+351-*+" + expected output: 18 + */ + assert(evaluate("42+351-*+") == 18); /// this ensures that the algorithm works as expected + printf("All tests have successfully passed!\n"); +} + +/** + * @brief Main function + * @returns 0 on exit + */ +int main() { + st.top = -1; /// initialize + test(); /// run self-test implementations + char post[25]; ///< to store input postfix expression + printf("Enter postfix: "); + scanf("%s", post); + printf("Evaluated answer: %d", evaluate(post)); + return 0; +} From cb1ac8088696ef3033351933ffae3d04627d19f2 Mon Sep 17 00:00:00 2001 From: github-actions <${GITHUB_ACTOR}@users.noreply.github.com> Date: Sat, 16 Oct 2021 12:08:34 +0000 Subject: [PATCH 14/20] updating DIRECTORY.md --- DIRECTORY.md | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/DIRECTORY.md b/DIRECTORY.md index 0c653fd22c..46c011f911 100644 --- a/DIRECTORY.md +++ b/DIRECTORY.md @@ -26,7 +26,6 @@ * [Hexadecimal To Octal](https://github.com/TheAlgorithms/C/blob/master/conversions/hexadecimal_to_octal.c) * [Hexadecimal To Octal2](https://github.com/TheAlgorithms/C/blob/master/conversions/hexadecimal_to_octal2.c) * [Infix To Postfix](https://github.com/TheAlgorithms/C/blob/master/conversions/infix_to_postfix.c) - * [Infix To Postfix2](https://github.com/TheAlgorithms/C/blob/master/conversions/infix_to_postfix2.c) * [Int To String](https://github.com/TheAlgorithms/C/blob/master/conversions/int_to_string.c) * [Octal To Binary](https://github.com/TheAlgorithms/C/blob/master/conversions/octal_to_binary.c) * [Octal To Decimal](https://github.com/TheAlgorithms/C/blob/master/conversions/octal_to_decimal.c) @@ -272,6 +271,7 @@ * [Palindrome](https://github.com/TheAlgorithms/C/blob/master/misc/palindrome.c) * [Pid](https://github.com/TheAlgorithms/C/blob/master/misc/pid.c) * [Poly Add](https://github.com/TheAlgorithms/C/blob/master/misc/poly_add.c) + * [Postfix Evaluation](https://github.com/TheAlgorithms/C/blob/master/misc/postfix_evaluation.c) * [Prime](https://github.com/TheAlgorithms/C/blob/master/misc/prime.c) * [Prime Factoriziation](https://github.com/TheAlgorithms/C/blob/master/misc/prime_factoriziation.c) * [Prime Seive](https://github.com/TheAlgorithms/C/blob/master/misc/prime_seive.c) From bbffdf743010ea87d197971f5618589f3be1ebfa Mon Sep 17 00:00:00 2001 From: kumaryash18 Date: Mon, 18 Oct 2021 14:31:58 +0530 Subject: [PATCH 15/20] fix: increase stack size --- misc/postfix_evaluation.c | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/misc/postfix_evaluation.c b/misc/postfix_evaluation.c index 236b291d93..9f6326baff 100644 --- a/misc/postfix_evaluation.c +++ b/misc/postfix_evaluation.c @@ -18,7 +18,7 @@ * @brief array implementation of stack using structure */ struct Stack { - int stack[10]; ///< array stack + int stack[20]; ///< array stack int top; ///< stores index of the top element }; struct Stack st; ///< global declaration of stack st @@ -29,7 +29,7 @@ struct Stack st; ///< global declaration of stack st * @returns void */ void push(int8_t opd) { - if(st.top == 9) { // overflow condition + if(st.top == 19) { // overflow condition printf("Stack overflow..."); exit(1); } From ff85f978e43ec4bb3d20dc15133f80b2254fd614 Mon Sep 17 00:00:00 2001 From: kumaryash18 Date: Wed, 20 Oct 2021 11:11:21 +0530 Subject: [PATCH 16/20] fix: change data type --- misc/postfix_evaluation.c | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/misc/postfix_evaluation.c b/misc/postfix_evaluation.c index 9f6326baff..ad451eb098 100644 --- a/misc/postfix_evaluation.c +++ b/misc/postfix_evaluation.c @@ -18,7 +18,7 @@ * @brief array implementation of stack using structure */ struct Stack { - int stack[20]; ///< array stack + int8_t stack[20]; ///< array stack int top; ///< stores index of the top element }; struct Stack st; ///< global declaration of stack st From 0a3ac0f07f6309f64e77be8d267f151e0bef6a92 Mon Sep 17 00:00:00 2001 From: kumaryash18 Date: Sat, 23 Oct 2021 17:26:28 +0530 Subject: [PATCH 17/20] add: parse feature --- misc/postfix_evaluation.c | 31 +++++++++++++++++++++---------- 1 file changed, 21 insertions(+), 10 deletions(-) diff --git a/misc/postfix_evaluation.c b/misc/postfix_evaluation.c index ad451eb098..faa7993c78 100644 --- a/misc/postfix_evaluation.c +++ b/misc/postfix_evaluation.c @@ -2,7 +2,7 @@ * @file * @brief [Postfix evaluation algorithm](https://www.includehelp.com/c/evaluation-of-postfix-expressions-using-stack-with-c-program.aspx) implementation * @details - * The input postfix expression is of type string upto 24 characters (single digit numbers only). + * The input postfix expression is of type string upto 49 characters (including space delimiters). * Supported operations- '+', '-', '/', '*', '%' * @author [Kumar Yash](https://github.com/kumaryash18) */ @@ -61,10 +61,19 @@ int8_t evaluate(char post[]) { int8_t it1; int8_t it2; int8_t temp; + int8_t number; int i; for(i = 0; i < strlen(post); i++) { - if(isdigit(post[i])) { - push(post[i]-'0'); + if(post[i] == ' ') { + continue; // ignore delimiter + } + else if(isdigit(post[i])) { + number = 0; + do { + number = number * 10 + (post[i]-'0'); + i++; + } while(i < strlen(post) && isdigit(post[i])); + push(number); } else { it2 = pop(); @@ -95,14 +104,16 @@ int8_t evaluate(char post[]) { */ static void test() { /* check sample test case - input: "4572+-*" - expected output: -16 + input: "2 10 + 9 6 - /" + expected output: 4 */ - assert(evaluate("4572+-*") == -16); /// this ensures that the algorithm works as expected - /* input: "42+351-*+" + char temp1[50] = "2 10 + 9 6 - /"; + assert(evaluate(temp1) == 4); /// this ensures that the algorithm works as expected + /* input: "4 2 + 3 5 1 - * +" expected output: 18 */ - assert(evaluate("42+351-*+") == 18); /// this ensures that the algorithm works as expected + char temp2[50] = "4 2 + 3 5 1 - * +"; + assert(evaluate(temp2) == 18); /// this ensures that the algorithm works as expected printf("All tests have successfully passed!\n"); } @@ -113,9 +124,9 @@ static void test() { int main() { st.top = -1; /// initialize test(); /// run self-test implementations - char post[25]; ///< to store input postfix expression + char post[50]; ///< to store input postfix expression printf("Enter postfix: "); - scanf("%s", post); + gets(post); printf("Evaluated answer: %d", evaluate(post)); return 0; } From 7aa212c260908d20f2ac4de99f12582a4d93bf75 Mon Sep 17 00:00:00 2001 From: kumaryash18 Date: Sat, 23 Oct 2021 18:10:19 +0530 Subject: [PATCH 18/20] fix: CodeQL --- misc/postfix_evaluation.c | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/misc/postfix_evaluation.c b/misc/postfix_evaluation.c index faa7993c78..2e0695594b 100644 --- a/misc/postfix_evaluation.c +++ b/misc/postfix_evaluation.c @@ -126,7 +126,7 @@ int main() { test(); /// run self-test implementations char post[50]; ///< to store input postfix expression printf("Enter postfix: "); - gets(post); + scanf("%[^\n]%*c", post); printf("Evaluated answer: %d", evaluate(post)); return 0; } From cb88c165bbd7a34c828221c918c280f2558e3fd0 Mon Sep 17 00:00:00 2001 From: kumaryash18 Date: Mon, 25 Oct 2021 17:46:28 +0530 Subject: [PATCH 19/20] docs: documentation changes --- misc/postfix_evaluation.c | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/misc/postfix_evaluation.c b/misc/postfix_evaluation.c index 2e0695594b..70b0dcb904 100644 --- a/misc/postfix_evaluation.c +++ b/misc/postfix_evaluation.c @@ -124,7 +124,7 @@ static void test() { int main() { st.top = -1; /// initialize test(); /// run self-test implementations - char post[50]; ///< to store input postfix expression + char post[50]; /// to store input postfix expression printf("Enter postfix: "); scanf("%[^\n]%*c", post); printf("Evaluated answer: %d", evaluate(post)); From bc0b52b8fc43c2385a65cb67a08566d332aedaa8 Mon Sep 17 00:00:00 2001 From: kumaryash18 Date: Mon, 25 Oct 2021 21:40:03 +0530 Subject: [PATCH 20/20] remove unnecessary code --- misc/postfix_evaluation.c | 4 ---- 1 file changed, 4 deletions(-) diff --git a/misc/postfix_evaluation.c b/misc/postfix_evaluation.c index 70b0dcb904..ca8e97c37f 100644 --- a/misc/postfix_evaluation.c +++ b/misc/postfix_evaluation.c @@ -124,9 +124,5 @@ static void test() { int main() { st.top = -1; /// initialize test(); /// run self-test implementations - char post[50]; /// to store input postfix expression - printf("Enter postfix: "); - scanf("%[^\n]%*c", post); - printf("Evaluated answer: %d", evaluate(post)); return 0; }