Simple Derivative Solver :-
- #include "stdafx.h" // Header file usually use in MS-VS
- #include <iostream> // Input / Output stream header file some IDE use <iostream.h>
- #include <conio.h> // for basic console operation
- 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:*/
- int main ()
- {
- cout << "\tHi! This Program can solve simplist derivative Questions." << endl ;
- cout << "\t\tThis program have a lot of limitation." << endl ;
- cout << "\t\t\tThis is just for practice." << endl ;
- cout << "\t\t\t\tMian Huzaifa" << endl ;
- while (true) { // Loop for calling the program continuously.
- char input[1024] ; // Input from user as a character variable.
- int input1 = 0 ; // This variable stores the integer value of a Q. like 4x^2
- char variable = ' ' ; // This variable stores the variable of a Q. as a character. like 4x^2
- int power = 0 ; // This variable stores the power of a Q. as a integer. like 4x^2
- int index = 0 ; // variable use under in switch statment .
- int multi = 1 ; //
- cout << ">>> " ; // Input prompt
- cin >> input ; // Get input from user.
- for (int i = 1 ; i <= 1023 ; i ++) //Converting all character values in its real data type
- {
- switch (input[index])
- {
- case '1' :
- input1 = input1 * multi ;
- input1 = input1 + 1 ;
- break ;
- case '2' :
- input1 = input1 * multi ;
- input1 = input1 + 2 ;
- break ;
- case '3' :
- input1 = input1 * multi ;
- input1 = input1 + 3 ;
- break ;
- case '4' :
- input1 = input1 * multi ;
- input1 = input1 + 4 ;
- break ;
- case '5' :
- input1 = input1 * multi ;
- input1 = input1 + 5 ;
- break ;
- case '6' :
- input1 = input1 * multi ;
- input1 = input1 + 6 ;
- break ;
- case '7' :
- input1 = input1 * multi ;
- input1 = input1 + 7 ;
- break ;
- case '8' :
- input1 = input1 * multi ;
- input1 = input1 + 8 ;
- break ;
- case '9' :
- input1 = input1 * multi ;
- input1 = input1 + 9 ;
- break ;
- case '0' :
- input1 = input1 * multi ;
- input1 = input1 + 0 ;
- break ;
- case 'x' :
- variable = 'x' ;
- break ;
- case '^' :
- {
- switch (input[index + 1])
- {
- case '1' :
- power = power + 1 ;
- index = index + 1 ;
- break ;
- case '2' :
- power = power + 2 ;
- index = index + 1 ;
- break ;
- case '3' :
- power = power + 3 ;
- index = index + 1 ;
- break ;
- case '4' :
- power = power + 4 ;
- index = index + 1 ;
- break ;
- case '5' :
- power = power + 5 ;
- index = index + 1 ;
- break ;
- case '6' :
- power = power + 6 ;
- index = index + 1 ;
- break ;
- case '7' :
- power = power + 7 ;
- index = index + 1 ;
- break ;
- case '8' :
- power = power + 8 ;
- index = index + 1 ;
- break ;
- case '9' :
- power = power + 9 ;
- index = index + 1 ;
- break ;
- case '0' :
- power = power - 1 ;
- index = index + 1 ;
- break ;
- default :
- index = index + 1 ;
- break ;
- }
- }
- default :
- break ;
- }
- index += 1 ;
- if (i < 2)
- multi = multi * 10 ;
- }
- if (variable == ' ')
- cout << 0 ;
- else if (input1 != 0 & input1 != -1 & variable != ' ' & power == 0)
- cout << input1 ;
- else if (input1 == 0 & variable != ' ' & power == 0)
- cout << 1 ;
- else if (input1 == 0 & variable != ' ' & power != 0 & power != -1 )
- cout << power << variable << "^" << power - 1 ;
- else if (input1 != 0 & input1 != -1 & variable != ' ' & power != 0 & power != -1)
- cout << power * input1 << variable << "^" << power - 1 ;
- else if (input1 == -1 & variable != ' ' & power != 0 & power != -1)
- cout << '0' << variable << "^" << power - 1 ;
- else if (input1 != 0 & variable != ' ' & power == -1)
- cout << 0 ;
- else if (input1 == 0 & variable != ' ' & power == -1)
- cout << 0 ;
- else if (input1 == -1)
- cout << 0 ;
- cout << endl ;
- }
- return 0 ;
- }
0 comments:
Post a Comment