Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- // New Smart Calculator
- #include <stdio.h>
- #include <stdlib.h>
- // sam_ds is for 2 values
- // sam_dm is for multiple values
- // Still has many bug
- float sam_ds(float n_rcvr[50], char s_rcvr[50], int range);
- float sam_dm(float num_f[50], char sign_f[50], int range2);
- int i = 0, j = 0, k;
- float m_answer = 0, t_answer = 0;
- int main()
- {
- float result;
- float num[50];
- char sign[50];
- for (i = 0; i < 50; i++)
- {
- scanf("%f %c", &num[i], &sign[i]);
- if (sign[i] == '=')
- break;
- }
- if (i == 0)
- printf("ERROR");
- else
- {
- if (i == 1)
- {
- result = sam_ds(num, sign, i);
- printf("%0.2f", result);
- }
- else
- {
- result = sam_dm(num, sign, i);
- printf("%0.2f", result);
- }
- }
- return 0;
- }
- float sam_ds(float n_rcvr[50], char s_rcvr[50], int range)
- {
- for (i = 0; i <= range; i++)
- {
- if (s_rcvr[i] == '/')
- m_answer = n_rcvr[i] / n_rcvr[i + 1];
- else if (s_rcvr[i] == '*')
- m_answer = n_rcvr[i] * n_rcvr[i + 1];
- else if (s_rcvr[i] == '+')
- m_answer = n_rcvr[i] + n_rcvr[i + 1];
- else if (s_rcvr[i] == '-')
- m_answer = n_rcvr[i] - n_rcvr[i + 1];
- }
- return m_answer;
- }
- //buggy function
- float sam_dm(float num_f[50], char sign_f[50], int range2)
- {
- // The problem is here
- /*I put the calculated value to the 0th position of
- the array num_f and then i changes the position of each element
- to their backward position..*/
- for (i = 0; i < range2; i++)
- //But once after i is increamented it no longer detects
- //the same second operator
- {
- if (sign_f[i] == '/')
- {
- num_f[i] = num_f[i] / num_f[i + 1];
- for (j = i + 1; j < range2; j++)
- {
- num_f[j] = num_f[j + 1];
- sign_f[j - 1] = sign_f[j];
- }
- }
- }
- for(i = 0; i <= range2; i++)
- {
- if(sign_f[i] == '*')
- {
- num_f[i] = num_f[i] * num_f[i + 1];
- for (j = i + 1; j < range2; j++)
- {
- num_f[j] = num_f[j + 1];
- sign_f[j - 1] = sign_f[j];
- }
- }
- }
- for (i = 0; i <= range2; i++)
- {
- if (sign_f[i] == '+')
- {
- num_f[i] = num_f[i] + num_f[i + 1];
- for (j = i + 1; j < range2; j++)
- {
- num_f[j] = num_f[j + 1];
- sign_f[j - 1] = sign_f[j];
- }
- }
- }
- for (i = 0; i <= range2; i++)
- {
- if (sign_f[i] == '-')
- {
- num_f[i] = num_f[i] - num_f[i + 1];
- }
- }
- return num_f[0];
- //Try input 10 * 10 * 10
- // You will get it :) Thanks!
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement