Advertisement
Guest User

Untitled

a guest
Oct 23rd, 2019
114
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 1.56 KB | None | 0 0
  1. #include <stdio.h>
  2. #include <stdlib.h>
  3. #include <math.h>
  4.  
  5. void do_next_op(char operator, double operand, double *sum);
  6. void scan_data(char *operator, double *operand);
  7.  
  8.  
  9.  
  10. int main(void){
  11.  
  12. double operand, akkumulator;
  13. char operator;
  14. akkumulator = 0;
  15.  
  16.  
  17. scan_data(&operator, &operand);
  18. printf("%c %lf", operator, operand);
  19. do_next_op(operator, operand, &akkumulator);
  20. printf("RESULT: %lf\n", akkumulator);
  21.  
  22.  
  23.  
  24. return EXIT_SUCCESS;
  25. }
  26.  
  27. /* PROMPT FOR INPUT */
  28. void scan_data(char *operator, double *operand){
  29. printf("Indtast en operator efterfulgt af en operand:");
  30. fflush(stdin);
  31. scanf(" %c %lf", &operator, &operand);
  32. }
  33.  
  34. /* OPERATOR BLOK AKA. HVAD SKAL DER SKE MED AKKUMULATOREN */
  35. void do_next_op(char operator, double operand, double *sum){
  36. switch (operator)
  37. {
  38. case '+':{
  39. *sum = *sum + operand;
  40. break;
  41. }
  42. case '-':{
  43. *sum = *sum - operand;
  44. break;
  45. }
  46. case '*':{
  47. *sum = *sum * operand;
  48. break;
  49. }
  50. case '/':{
  51. *sum = *sum / operand;
  52. break;
  53. }
  54. case '^':{
  55. *sum = pow(*sum, operand);
  56. break;
  57. }
  58. case '#':{
  59. *sum = sqrt(*sum);
  60. break;
  61. }
  62. case '%':{
  63. *sum = *sum*(-1);
  64. break;
  65. }
  66. case '!':{
  67. *sum = 1 / *sum;
  68. break;
  69. }
  70. default:{
  71. break;
  72. }
  73. }
  74. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement