SHARE
TWEET

Untitled

a guest Nov 12th, 2019 68 Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. #include <stdio.h>
  2.  
  3. int is_number(char a) {
  4.   return (a >= '0') && (a <= '9');
  5. }
  6.  
  7. int is_op(char a) {
  8.   return a == '+' || a == '-' || a == '*' || a == '/' ;
  9. }
  10.  
  11. int calc(a, b, op) {
  12.   if (op == '+')
  13.     return a + b;
  14.   if (op == '-' )
  15.     return a - b;
  16.   if (op == '*')
  17.     return a * b;
  18.   if (op == '/' )
  19.     return a / b;
  20. }
  21.  
  22. void find_code(int start, int prev_number, char text[]) {
  23.   int b = 0, a;
  24.   a = prev_number;
  25.   char op = '\0';
  26.  
  27.   for (int i = start; text[i] != '\0'; ++i) {
  28.     if (is_number(text[i]) && !op) {
  29.       a = a * 10 + text[i];
  30.     }
  31.     else if (is_op(text[i]) && a && !op) {
  32.       op = text[i];
  33.     }
  34.     else if (op && is_number(text[i])) {
  35.       b = b * 10 + text[i];
  36.     }
  37.     else if (op && a && b) {
  38.       if (calc(a, b, op) <= 120000)
  39.         printf("%d %c %d = %d\n", a, op, b, calc(a, b, op));
  40.       find_code(i, b, text);
  41.     }
  42.     if (!(is_number(text[i]) || is_op(text[i])))
  43.       find_code(i + 1, 0, text);
  44.   }
  45. }
  46.  
  47. int main(void) {
  48.   char code[2001];
  49.   scanf("%2001s", code);
  50.  
  51.   find_code(0, 0, code);
  52.  
  53.   return 0;
  54. }
RAW Paste Data
We use cookies for various purposes including analytics. By continuing to use Pastebin, you agree to our use of cookies as described in the Cookies Policy. OK, I Understand
 
Top