Advertisement
Guest User

Untitled

a guest
May 3rd, 2015
248
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 2.85 KB | None | 0 0
  1. -2x^2+2x-3
  2.  
  3. -2x+sinx-3
  4.  
  5. -x+sin2x-tanx
  6.  
  7. Open the file.
  8. While there's lines to read:
  9. Read a line -
  10. Seperate it by the operands (+,-,/,*)
  11. For each part:
  12. Find the power of x,
  13. Reduce it by one,
  14. ...(derivating rules) // no way around, you have to implement each function if you want this to work as others mentioned in the comments.
  15. Reconnect the parts into a string,
  16. Add it to a list.
  17. Print each element of the list.
  18.  
  19. -2x^2+2x-3 => 2x^2 2x 3
  20. -2x+sinx-3 => 2x sinx 3
  21. -x+sin2x-tanx => x sin2x tanx
  22.  
  23. -2x^2 + 5x + 4sin3x - 3
  24.  
  25. 0 - 2x^2 + 5x + 4sin3x - 3
  26.  
  27. 0 [0x^1] (derives as) 0, remove it.
  28. 2x^2 [2x^2] (derives as) (2*2)x^(2-1) => 4x^1 => 4x
  29. 5x [5x^1] (derives as) (5x1)x^(1-1) => 5x^0 => 5
  30. 4sin3x [4sin3x] (derives as) 12cos3x
  31. 3 [3x^0] (derives as) 0, remove it and preceding '-'
  32.  
  33. #define maxlen 50
  34.  
  35. #define idx 0 //X variable
  36. #define idnumber 1 //number
  37. #define idplus 2 //+ sign
  38. #define idminus 3 //- sign
  39.  
  40. struct foo
  41. {
  42. int type[10];//each type can be a number (idnum), +, -, etc.
  43. int num[10];//if type[i] is number then num[i] identifies that number
  44. int count;//total number of parts
  45. };
  46.  
  47. void parse_one_line(struct foo *v, const char *s)
  48. {
  49. char buf[maxlen];
  50. memset(buf, 0, maxlen);
  51. int j = 0;
  52. //remove white spaces
  53. for (int i = 0, len = strlen(s); i < len; i++)
  54. {
  55. if (s[i] == ' ') continue;
  56. buf[j] = s[i];
  57. j++;
  58. }
  59.  
  60. char part[maxlen];
  61. v->count = 0;
  62. for (int i = 0, len = strlen(buf); i < len; i++)
  63. {
  64. char c = buf[i];
  65. if (c == 'x')
  66. {
  67. v->type[v->count] = idx;
  68. v->count++;
  69. }
  70. else if (c == '+')
  71. {
  72. v->type[v->count] = idplus;
  73. v->count++;
  74. }
  75. else if (c == '-')
  76. {
  77. v->type[v->count] = idminus;
  78. v->count++;
  79. }
  80. else if (c >= '0' && c <= '9')
  81. {
  82. int j = 0;
  83. memset(part, 0, maxlen);
  84. for (; i < len; i++)
  85. {
  86. c = buf[i];
  87. if (c >= '0' && c <= '9')
  88. {
  89. part[j] = c;
  90. j++;
  91. }
  92. else
  93. {
  94. break;
  95. }
  96. }
  97. i--;
  98. v->num[v->count] = atoi(part);
  99. v->type[v->count] = idnumber;
  100. v->count++;
  101. }
  102. }
  103. for (int i = 0; i < v->count; i++)
  104. {
  105. switch (v->type[i])
  106. {
  107. case idnumber: printf("%d", v->num[i]); break;
  108. case idx: printf("X"); break;
  109. case idplus: printf("+"); break;
  110. case idminus: printf("-"); break;
  111. default:break;
  112. }
  113. }
  114. printf("n");
  115. }
  116.  
  117. int main()
  118. {
  119. struct foo st;
  120. parse_one_line(&st, "-23x + 2 + 2x - 3");
  121. return 0;
  122. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement