Reading fractions in C

后端 未结 6 486
臣服心动
臣服心动 2021-01-14 23:45

How do I read a fraction into C to do math with it? (The fraction will contain the slash symbol) For example, A user will input 3/12. (a string) The program will find the gc

6条回答
  •  被撕碎了的回忆
    2021-01-15 00:49

    you can make it this way also......

      char str[30];
      scanf("%s",str);
      char c[30];
      int i, num, denom;
    
      i = 0;
      while (*(str+i) != '/')
      {
          memcpy((c+i), (str+i), 1);
          i++;
      }
      *(c+i) = 0;
      i++;
      num = atoi(c);
      strcpy(c, (str+i));
      denom = atoi(c);
      printf("%d\n", num);
      printf("%d\n", denom);
    

提交回复
热议问题