Derivative Calculator Source Code

Simple Derivative Solver :-



  1. #include "stdafx.h"   // Header file usually use in MS-VS
  2. #include <iostream>  // Input / Output stream header file some IDE use <iostream.h>
  3. #include <conio.h>    // for basic console operation

  4. using namespace std;   /* Namespaces are like drawers in a filing cabinet. All of the standard library is in one drawer, and the only way to get to the stuff in that drawer is to tell C++ where to find it. You do that by prefixing the name with std:*/

  5. int main ()
  6. {
  7. cout << "\tHi! This Program can solve simplist derivative Questions." << endl ;
  8. cout << "\t\tThis program have a lot of limitation." << endl ;
  9. cout << "\t\t\tThis is just for practice." << endl ;
  10. cout << "\t\t\t\tMian Huzaifa" << endl ;


  11. while (true) {            // Loop for calling the program continuously. 
  12. char input[1024] ;     // Input from user as a character variable.
  13. int input1 = 0 ;         // This variable stores the integer value of a Q. like 4x^2 
  14. char variable = ' ' ;   // This variable stores the variable of a Q. as a character. like 4x^2 
  15. int power = 0 ;        // This variable stores the power of a Q. as a integer. like 4x^2
  16. int index = 0 ;      // variable use under in switch statment .
  17. int multi = 1 ;         // 
  18. cout << ">>> " ;     // Input prompt 
  19. cin >> input ;          // Get input from user.
  20. for (int i = 1 ; i <= 1023 ; i ++)  //Converting all character values in its real data type
  21. {
  22. switch (input[index])
  23. {
  24. case '1' :
  25. input1 = input1 * multi ;
  26. input1 = input1 + 1 ;
  27. break ;
  28. case '2' :
  29. input1 = input1 * multi ;
  30. input1 = input1 + 2 ;
  31. break ;
  32. case '3' :
  33. input1 = input1 * multi ;
  34. input1 = input1 + 3 ;
  35. break ;
  36. case '4' :
  37. input1 = input1 * multi ;
  38. input1 = input1 + 4 ;
  39. break ;
  40. case '5' :
  41. input1 = input1 * multi ;
  42. input1 = input1 + 5 ;
  43. break ;
  44. case '6' :
  45. input1 = input1 * multi ;
  46. input1 = input1 + 6 ;
  47. break ;
  48. case '7' :
  49. input1 = input1 * multi ;
  50. input1 = input1 + 7 ;
  51. break ;
  52. case '8' :
  53. input1 = input1 * multi ;
  54. input1 = input1 + 8 ;
  55. break ;
  56. case '9' :
  57. input1 = input1 * multi ;
  58. input1 = input1 + 9 ;
  59. break ;
  60. case '0' :
  61. input1 = input1 * multi ;
  62. input1 = input1 + 0 ;
  63. break ;
  64. case 'x' :
  65. variable = 'x' ;
  66. break ;
  67. case '^' :
  68. {
  69. switch (input[index + 1])
  70. {
  71. case '1' :
  72. power = power + 1 ;
  73. index = index + 1 ;
  74. break ;
  75. case '2' :
  76. power = power + 2 ;
  77. index = index + 1 ;
  78. break ;
  79. case '3' :
  80. power = power + 3 ;
  81. index = index + 1 ;
  82. break ;
  83. case '4' :
  84. power = power + 4 ;
  85. index = index + 1 ;
  86. break ;
  87. case '5' :
  88. power = power + 5 ;
  89. index = index + 1 ;
  90. break ;
  91. case '6' :
  92. power = power + 6 ;
  93. index = index + 1 ;
  94. break ;
  95. case '7' :
  96. power = power + 7 ;
  97. index = index + 1 ;
  98. break ;
  99. case '8' :
  100. power = power + 8 ;
  101. index = index + 1 ;
  102. break ;
  103. case '9' :
  104. power = power + 9 ;
  105. index = index + 1 ;
  106. break ;
  107. case '0' :
  108. power = power  - 1 ;
  109. index = index + 1 ;
  110. break ;
  111. default :
  112. index = index + 1 ;
  113. break ;
  114. }
  115. }
  116. default :
  117. break ;
  118. }
  119. index += 1 ;
  120. if (i < 2)
  121. multi = multi * 10 ;

  122. }
  123. if (variable == ' ')
  124. cout << 0 ;

  125. else if (input1 != 0 & input1 != -1 & variable != ' ' & power == 0)
  126. cout << input1 ;

  127. else if (input1 == 0 & variable != ' ' & power == 0)
  128. cout << 1 ;

  129. else if (input1 == 0 & variable != ' ' & power != 0 & power != -1 )
  130. cout << power << variable << "^" << power - 1 ;

  131. else if (input1 != 0 & input1 != -1 & variable != ' ' & power != 0 & power != -1)
  132. cout << power * input1 << variable << "^" <<  power - 1 ;

  133. else if (input1 == -1 & variable != ' ' & power != 0 & power != -1)
  134. cout << '0' << variable << "^" <<  power - 1 ;

  135. else if (input1 != 0 & variable != ' ' & power == -1)
  136. cout << 0 ;

  137. else if (input1 == 0 & variable != ' ' & power == -1)
  138. cout << 0 ;
  139. else if (input1 == -1)
  140. cout << 0 ;

  141. cout << endl ;
  142. }

  143. return 0 ;
  144. }
Next PostNewer Post Home

0 comments:

Post a Comment