J3st3rs_j0k3

pr6_2

Dec 1st, 2021 (edited)
118
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C 1.45 KB | None | 0 0
  1. #include <stdio.h>
  2. #include <stdlib.h>
  3. #include <string.h>
  4. #define N 256
  5.  
  6. int main()
  7. {
  8.  
  9.     // char str[] = "2 + 5 % cos(0.5)/sin(1) + tan(12) % 10";
  10.     //char str[] = "5 % 15 * cos(2) * sin(0.555) - tan(x) % 10 * tan(3)";
  11.     char str[N];
  12.     fgets(str, N, stdin);
  13.     char *new_str = (char *) malloc((strlen(str) + 1) * sizeof(char));
  14.     char *leks = (char *) malloc(40 * sizeof(char));
  15.  
  16.     leks = strtok(str, " ");
  17.  
  18.     while (leks != NULL)
  19.     {
  20.         if (*leks == '%')
  21.         {
  22.             leks = "mod";
  23.         }
  24.         if (leks[0] == 't')
  25.         {
  26.             int size = 0;
  27.             char *num = (char *) malloc(size * sizeof(char));
  28.  
  29.             for (int i = 0; i < strlen(leks) + 1; i++)
  30.             {
  31.                 if (leks[i + 4] == ')')
  32.                     break;
  33.  
  34.                 num = (char *) realloc(num, ++size * sizeof(char));
  35.                 num[i] = leks[i + 4];
  36.             }
  37.  
  38.             char *sin_cos = (char *) malloc(15 + strlen(num) * 2);
  39.             sin_cos = strcat(sin_cos, "(sin(");
  40.             sin_cos = strcat(sin_cos, num);
  41.             sin_cos = strcat(sin_cos, ")/cos(");
  42.             sin_cos = strcat(sin_cos, num);
  43.             sin_cos = strcat(sin_cos, "))");
  44.  
  45.             leks = sin_cos;
  46.         }
  47.  
  48.         new_str = strcat(new_str, leks);
  49.         new_str = strcat(new_str, " ");
  50.  
  51.         leks = strtok(NULL, " ");
  52.     }
  53.  
  54.     printf("new_str = %s\n", new_str);
  55.  
  56.     return 0;
  57. }
  58.  
Add Comment
Please, Sign In to add comment