Guest User

Untitled

a guest
Jul 20th, 2018
88
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 2.17 KB | None | 0 0
  1. #include <stdio.h>
  2. #include <string.h>
  3. #include <stdlib.h>
  4.  
  5. /*add, subtract, multiply, divide*/
  6. float add(int x, int y);
  7. float sub(int x, int y);
  8. float mul(int x, int y);
  9. float divd(int x, int y);
  10.  
  11. int main( int argc, char *argv[]){
  12.  
  13. /*VARIABLE DECLARATION BLOCK*/
  14. int num1,num2; /*first and second numbers*/
  15. long opr_loc; /*operator location*/
  16. int ctr,ctr2,argctr; /*for loop counters*/
  17.  
  18. /*for output*/
  19. int a;
  20. int b;
  21.  
  22. int d; /*desicion for operator*/
  23. /*1 = add
  24. 2 = subtract
  25. 3 = multiply
  26. 4 = divide */
  27.  
  28. char *p; /*strchr pointer*/
  29. char *opr1;
  30. char *opr2;
  31. char *eq_in; /*input equation*/
  32. /*END VARIABLE DECLARATION BLOCK*/
  33.  
  34. /*memory allocation*/
  35. eq_in = malloc(128*sizeof(char));
  36. opr1 = malloc(128*sizeof(char));
  37. opr2 = malloc(128*sizeof(char));
  38.  
  39. /*EXPLANATION FOR THE FOLLOWING CODE:
  40. * this for loop is used to allow spaces.
  41. * by looping through the arguments
  42. * I make 1 string with the equation in it
  43. * (all of the argv in one string)*/
  44. for(argctr = 1; argctr < argc; argctr++)
  45. {
  46. if (strcmp(argv[argctr],"*")==0)
  47. {
  48. eq_in = strcat(eq_in,"*");
  49. }
  50. else
  51. {
  52. eq_in = strcat(eq_in,argv[argctr]);
  53. }
  54. }
  55.  
  56. /*FIND OPERATOR*/
  57. if (strchr(eq_in,'+') != NULL)
  58. {
  59. p = strchr(eq_in,'+');
  60. d = 1;
  61. }
  62.  
  63. else if(strchr(eq_in,'-') != NULL)
  64. {
  65. p = strchr(eq_in,'-');
  66. d = 2;
  67. }
  68.  
  69. else if(strchr(eq_in,'*') != NULL)
  70. {
  71. p = strchr(eq_in, '*');
  72. d = 3;
  73. }
  74.  
  75. else if(strchr(eq_in, '/') != NULL)
  76. {
  77. p = strchr(eq_in, '/');
  78. d = 4;
  79. }
  80. /*END FIND OPERATOR BLOCK*/
  81.  
  82.  
  83. /*SEPERATE OPERANDS FROM OPERATOR*/
  84. for(ctr = 0;ctr < (p-eq_in); ctr++)
  85. {
  86. opr1[ctr] = eq_in[ctr];
  87. }
  88.  
  89. ctr2 = 0;
  90. for(ctr = p - eq_in + 1; ctr < strlen(eq_in); ctr++)
  91. {
  92. opr2[ctr2] = eq_in[ctr];/*USES FIRST COUNTER VARIABLE*/
  93. ++ctr2;
  94. }
  95.  
  96. /*ASCII TO INTEGER*/
  97. a = atoi(opr1);
  98. b = atoi(opr2);
  99.  
  100.  
  101. if(d == 1)
  102. {
  103. printf("%f\n",add(a,b));
  104. }
  105.  
  106. else if(d == 2)
  107. {
  108. printf("%f\n",sub(a,b));
  109. }
  110.  
  111. else if(d == 3)
  112. {
  113. printf("%f\n",mul(a,b));
  114. }
  115.  
  116. else if(d == 4)
  117. {
  118. printf("%f\n",divd(a,b));
  119. }
  120.  
  121. return 0;
  122. }
  123.  
  124. float add(int x, int y)
  125. {
  126. return x + y;
  127. }
  128.  
  129. float sub(int x, int y)
  130. {
  131. return x - y;
  132. }
  133.  
  134. float mul(int x, int y)
  135. {
  136. return x * y;
  137. }
  138.  
  139. float divd(int x, int y)
  140. {
  141. return x / y;
  142. }
Add Comment
Please, Sign In to add comment