Advertisement
Guest User

Calculator

a guest
Jul 22nd, 2019
73
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C 2.29 KB | None | 0 0
  1. // New Smart Calculator
  2. #include <stdio.h>
  3. #include <stdlib.h>
  4. // sam_ds is for 2 values
  5. // sam_dm is for multiple values
  6. // Still has many bug
  7. float sam_ds(float n_rcvr[50], char s_rcvr[50], int range);
  8. float sam_dm(float num_f[50], char sign_f[50], int range2);
  9.  
  10. int i = 0, j = 0, k;
  11. float m_answer = 0, t_answer = 0;
  12.  
  13. int main()
  14. {
  15.     float result;
  16.     float num[50];
  17.     char sign[50];
  18.     for (i = 0; i < 50; i++)
  19.     {
  20.         scanf("%f %c", &num[i], &sign[i]);
  21.         if (sign[i] == '=')
  22.             break;
  23.     }
  24.     if (i == 0)
  25.         printf("ERROR");
  26.     else
  27.     {
  28.         if (i == 1)
  29.         {
  30.             result = sam_ds(num, sign, i);
  31.             printf("%0.2f", result);
  32.         }
  33.         else
  34.         {
  35.             result = sam_dm(num, sign, i);
  36.             printf("%0.2f", result);
  37.         }
  38.     }
  39.     return 0;
  40. }
  41.  
  42. float sam_ds(float n_rcvr[50], char s_rcvr[50], int range)
  43. {
  44.     for (i = 0; i <= range; i++)
  45.     {
  46.         if (s_rcvr[i] == '/')
  47.             m_answer = n_rcvr[i] / n_rcvr[i + 1];
  48.  
  49.         else if (s_rcvr[i] == '*')
  50.             m_answer = n_rcvr[i] * n_rcvr[i + 1];
  51.  
  52.         else if (s_rcvr[i] == '+')
  53.             m_answer = n_rcvr[i] + n_rcvr[i + 1];
  54.  
  55.         else if (s_rcvr[i] == '-')
  56.             m_answer = n_rcvr[i] - n_rcvr[i + 1];
  57.     }
  58.     return m_answer;
  59. }
  60. //buggy function
  61. float sam_dm(float num_f[50], char sign_f[50], int range2)
  62. {
  63.     // The problem is here
  64.     /*I put the calculated value to the 0th position of
  65.     the array num_f and then i changes the position of each element
  66.     to their backward position..*/
  67.    
  68.     for (i = 0; i < range2; i++)
  69.     //But once after i is increamented it no longer detects
  70.     //the same second operator
  71.     {
  72.         if (sign_f[i] == '/')
  73.         {
  74.             num_f[i] = num_f[i] / num_f[i + 1];
  75.             for (j = i + 1; j < range2; j++)
  76.             {
  77.                 num_f[j] = num_f[j + 1];
  78.                 sign_f[j - 1] = sign_f[j];
  79.             }
  80.         }
  81.     }
  82. for(i = 0; i <= range2; i++)
  83. {
  84.     if(sign_f[i] == '*')
  85.     {
  86.         num_f[i] = num_f[i] * num_f[i + 1];
  87.         for (j = i + 1; j < range2; j++)
  88.         {
  89.             num_f[j] = num_f[j + 1];
  90.             sign_f[j - 1] = sign_f[j];
  91.         }
  92.     }
  93. }
  94.  
  95. for (i = 0; i <= range2; i++)
  96. {
  97.     if (sign_f[i] == '+')
  98.     {
  99.         num_f[i] = num_f[i] + num_f[i + 1];
  100.         for (j = i + 1; j < range2; j++)
  101.         {
  102.             num_f[j] = num_f[j + 1];
  103.             sign_f[j - 1] = sign_f[j];
  104.         }
  105.     }
  106. }
  107.  
  108. for (i = 0; i <= range2; i++)
  109. {
  110.     if (sign_f[i] == '-')
  111.     {
  112.         num_f[i] = num_f[i] - num_f[i + 1];
  113.     }
  114. }
  115.  
  116. return num_f[0];
  117. //Try input 10 * 10 * 10
  118. // You will get it :) Thanks!
  119.  
  120. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement