From 82290549c31f8140a411b9e86625f5f8636657b9 Mon Sep 17 00:00:00 2001 From: sadeem_albir <142955101+sadeem-albir@users.noreply.github.com> Date: Thu, 15 Feb 2024 22:58:08 +0300 Subject: [PATCH 1/6] Update stack.c The else part in "if (c == '-')" for some reason caused a calculation like "33 44 +" to evaluate to 7. After removing that else part, the output gives the desired result. --- chapter_4/exercise_4_04/stack.c | 9 +++++---- 1 file changed, 5 insertions(+), 4 deletions(-) diff --git a/chapter_4/exercise_4_04/stack.c b/chapter_4/exercise_4_04/stack.c index d02d9ac..59cfd9b 100644 --- a/chapter_4/exercise_4_04/stack.c +++ b/chapter_4/exercise_4_04/stack.c @@ -215,10 +215,11 @@ int getop(char s[]) s[++i] = c = next; } } - else - { - c = getch(); - } +// else +// { +// c = getch(); +// } +// This else block caused '33 44+' to output 7 instead of 77 if (isdigit(c)) { From 546de274da27385ce0390e90310a798742d5e8ba Mon Sep 17 00:00:00 2001 From: sadeem_albir <142955101+sadeem-albir@users.noreply.github.com> Date: Sun, 18 Feb 2024 01:33:21 +0300 Subject: [PATCH 2/6] fix variables.c Changed the command letters to capital-case to prevent overlapping with VARGET --- chapter_4/exercise_4_06/variables.c | 16 ++++++++++------ 1 file changed, 10 insertions(+), 6 deletions(-) diff --git a/chapter_4/exercise_4_06/variables.c b/chapter_4/exercise_4_06/variables.c index 9aacc06..81818bd 100644 --- a/chapter_4/exercise_4_06/variables.c +++ b/chapter_4/exercise_4_06/variables.c @@ -95,23 +95,23 @@ int main(void) push(sin(pop())); break; - case 'e': + case 'E': push(exp(pop())); break; - case 'h': + case 'H': view_head(); break; - case 'd': + case 'D': duplicate(); break; - case 's': + case 'S': swap(); break; - case 'c': + case 'C': clear(); break; @@ -244,7 +244,7 @@ int getop(char s[]) s[1] = '\0'; - if (isalpha(c)) + if (isalpha(c) && !(c >= 'A' && c <= 'Z')) { var = c; return VARGET; @@ -296,3 +296,7 @@ int getop(char s[]) return NUMBER; } +/* To prevent the variable checking in getop() from overlapping with the letter commands, +make sure you set the commands to capital letters and explicitly tell the getop() to only +check for variable-getting if the character is not capital +e.g insteat of "if (isalpha(c))" you add "if (isalpha(c) && !(c >= 'A' && c >= 'Z'))"*/ From 4571c0408d30bf35b4adc72b8555005a597f91c0 Mon Sep 17 00:00:00 2001 From: sadeem_albir <142955101+sadeem-albir@users.noreply.github.com> Date: Tue, 27 Feb 2024 19:14:59 +0300 Subject: [PATCH 3/6] Update reverse.c Reset the static variables after the reversing process was complete so the reverse function can be used more than once without issue. --- chapter_4/exercise_4_13/reverse.c | 5 +++++ 1 file changed, 5 insertions(+) diff --git a/chapter_4/exercise_4_13/reverse.c b/chapter_4/exercise_4_13/reverse.c index b0da222..0f3c64c 100644 --- a/chapter_4/exercise_4_13/reverse.c +++ b/chapter_4/exercise_4_13/reverse.c @@ -27,6 +27,11 @@ void reverse(char str[]) str[j++] = c; } + if (s[j] == '\0') // if whole reverse process is complete, reset the static variables to make this function reusable + { + i = 0; + j = 0; + } } // NOTE: As a simple observation when recursive functions are used, static From 976ab8a0112c5b31b30f9dbdf0b5451c4ac896d4 Mon Sep 17 00:00:00 2001 From: sadeem_albir <142955101+sadeem-albir@users.noreply.github.com> Date: Tue, 27 Feb 2024 19:23:14 +0300 Subject: [PATCH 4/6] Update stack.c Removed the last else-block from the "if (c == '-')" to prevent second digit from being skipped in the parsing process. --- chapter_4/exercise_4_04/stack.c | 5 ----- 1 file changed, 5 deletions(-) diff --git a/chapter_4/exercise_4_04/stack.c b/chapter_4/exercise_4_04/stack.c index 59cfd9b..9c4c1c8 100644 --- a/chapter_4/exercise_4_04/stack.c +++ b/chapter_4/exercise_4_04/stack.c @@ -215,11 +215,6 @@ int getop(char s[]) s[++i] = c = next; } } -// else -// { -// c = getch(); -// } -// This else block caused '33 44+' to output 7 instead of 77 if (isdigit(c)) { From d50ea33bcf60adf5bba3cd7878b751a52cfd4725 Mon Sep 17 00:00:00 2001 From: sadeem_albir <142955101+sadeem-albir@users.noreply.github.com> Date: Thu, 29 Feb 2024 22:47:24 +0300 Subject: [PATCH 5/6] Update reverse.c Reset static variables after string-reverse process completed, making the function reusable --- chapter_4/exercise_4_13/reverse.c | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/chapter_4/exercise_4_13/reverse.c b/chapter_4/exercise_4_13/reverse.c index 0f3c64c..0b04c04 100644 --- a/chapter_4/exercise_4_13/reverse.c +++ b/chapter_4/exercise_4_13/reverse.c @@ -27,7 +27,7 @@ void reverse(char str[]) str[j++] = c; } - if (s[j] == '\0') // if whole reverse process is complete, reset the static variables to make this function reusable + if (str[j] == '\0') // if whole reverse process is complete, reset the static variables to make this function reusable { i = 0; j = 0; From 2c1b8e6e65645090aacd47d356b57eba42ff37b7 Mon Sep 17 00:00:00 2001 From: Daniel Costrasel Date: Tue, 5 Mar 2024 20:28:54 +0100 Subject: [PATCH 6/6] Update reverse.c --- chapter_4/exercise_4_13/reverse.c | 10 ++++++---- 1 file changed, 6 insertions(+), 4 deletions(-) diff --git a/chapter_4/exercise_4_13/reverse.c b/chapter_4/exercise_4_13/reverse.c index 0b04c04..35236b5 100644 --- a/chapter_4/exercise_4_13/reverse.c +++ b/chapter_4/exercise_4_13/reverse.c @@ -27,11 +27,13 @@ void reverse(char str[]) str[j++] = c; } - if (str[j] == '\0') // if whole reverse process is complete, reset the static variables to make this function reusable + + // if whole reverse process is complete, reset the static variables to make this function reusable + if (str[j] == '\0') { - i = 0; - j = 0; - } + i = 0; + j = 0; + } } // NOTE: As a simple observation when recursive functions are used, static