I have a code like below:(It is working well)
#include <string.h>
#include <stdlib.h>
#define _CRT_SECURE_NO_WARNINGS
int main(){
char* func; // divide the given polynomial expression with "()"
func = malloc(sizeof(int));
scanf("%[^\n]s", func);
char *cal[20] = { NULL, };
int i = 0;
char *ptr = strtok(func, "()");
while (ptr != NULL){
cal[i] = ptr;
printf("%s\n", cal[i]);
i++;
ptr = strtok(NULL, "()");
}
printf("Complete 1");
return 0;
}
And I got result (When I input (3x**2+4x**1+4)*(2x**2+3))
(3x**2+4x**1+4)
*
(2x**2+3)
Complete 1
So I tried to do more types of manipulating with adding more while loop under the code above:
#include <stdio.h>
#include <string.h>
#include <stdlib.h>
#define _CRT_SECURE_NO_WARNINGS
int main(){
char* func; // divide the given polynomial expression with "()"
func = malloc(sizeof(int));
scanf("%[^\n]s", func);
char *cal[20] = { NULL, };
int i = 0;
char *ptr = strtok(func, "()");
while (ptr != NULL){
cal[i] = ptr;
printf("%s\n", cal[i]);
i++;
ptr = strtok(NULL, "()");
}
printf("Complete 1");
int j = 0; // Change + to +- in the divided expression
char *num[20] = {NULL, };
char ptr2 = strtok(cal, "+");
while (2j - 2 < i){
for(int k = 0; k< strlen(cal[2j-2]); k++){
if ((cal[2j-2] + k) == '-'){
char s[] = "+";
memmove(cal[2j - 2] + k-1, s, strlen(s));
}
}
}
printf("Complete 2");
return 0;
}
However, in this case, I got a result like
(3x**2+4x**1+4)
*
(2x**2+3)
and the function is not over.
I think I stuck in the first while loop because 'Complete 1' is not printed out.
Why this situation happens?
Thank you for reading and any help would be greatly appreciated.
P.S. I ran this code on linux environment with gcc command.