Advertisement
Guest User

Untitled

a guest
Dec 8th, 2016
72
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 1.08 KB | None | 0 0
  1. input:
  2. 42 hfgf=
  3. output:
  4. 2829124
  5.  
  6. input:
  7. 1 fgh=
  8. output:
  9. 1
  10.  
  11. input:
  12. -1 fgh=
  13. output:
  14. 1
  15.  
  16. #include <stdio.h>
  17.  
  18. long f(size_t x) { return x * x; }
  19. long g(size_t x) { return x + 1; }
  20. long h(size_t x) { return x - 1; }
  21.  
  22. long doMath(char func, size_t x) {
  23. long value;
  24.  
  25. switch(func) {
  26. case 'f':
  27. value = f(x);
  28. break;
  29. case 'g':
  30. value = g(x);
  31. break;
  32. case 'h':
  33. value = h(x);
  34. break;
  35. }
  36.  
  37. return value;
  38. }
  39.  
  40. int main() {
  41. char input[50];
  42.  
  43. printf("Input: "); fgets(input, 50, stdin);
  44.  
  45. int _i = 0, _j = 0;
  46. int num = 0; char funcs[10];
  47. long value;
  48.  
  49. while (input[_i] != '=') {
  50. int c = input[_i];
  51.  
  52. /* 0-9 */
  53. if (c >= 48 && c <= 57) {
  54. num *= 10; num += c - 48;
  55. }
  56. /* lowercase a-z */
  57. if (c >= 97 && c <= 122) {
  58. funcs[_j++] = (char)c;
  59. }
  60.  
  61. _i++;
  62. }
  63.  
  64. value = num;
  65.  
  66. for (_j = 0; funcs[_j] != ''; _j++) {
  67. value = doMath(funcs[_j], value);
  68. }
  69.  
  70. printf("Output: %lin", value);
  71. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement