Abdelrahman Elzedy
//scientific calculator in c++
//written by Abd - elrahman Elmasry
//e-mail: abdoasemelzedy@hotmail.com
//there is no copy right for this code
#include<iostream>//main library
#include<math.h>// for mathimatics function
#include<stdexcpt.h>//for exeption conditions
using namespace std;
class calc
{
private:
int len,//for the length of array
intPartTemp,//for using its value
intLen,//for the len of integer part
floatLen,//for the len of float part
ten,//for pow function
str;//for deter mine starting point
double value,//for the value of mid
floatPartTemp,//for getting floating part of numbers for converting
x; // for the value of variable x
char ch[1000] ;//charchters for saving formulas
char carriage[100];//for putting num in it as a caracter
char copy [1000];//for saving user formula
//getting information form user
void setInfo()
{
cin.getline(copy,1000);//getting formulas from user
copyArray(copy, ch);//copy array to save its value
replace(x,'x');//replacing variable x with its value
}
void copyArray (char main [], char copy [] )
{
//getting main array lenght
int len = strlen(main);
int temp;
//it counts to null because of null
for(temp=0;temp<=len;temp++)
{
copy[temp] = main[temp];
}
}
//editing formulas to be adaptable to computing rules
void edit()
{//star edit
int temp;
len = strlen(ch);
for(temp=0;temp<len;temp++)
{//loop for cover all characters
//the condition there is two variables after them thelf without *
if(!triangleMove(temp) && isalpha(ch[temp]) && isalpha(ch[temp+1]) )
//not triangle to make sure they aren't triangle functions
insert(temp,len,'*');
//the condition when there is a number and after it a variable without *
else if(temp!=0 && isalpha(ch[temp]) && isdigit(ch[temp-1]))
insert(temp,len,'*');
//the condition there is two parentheses after them thelf without *
else if(temp!=0 && ch[temp] == '(' && ch[temp-1] ==')' )
insert(temp,len,'*');
//the condition when there is a number before (
else if(temp!=0 && ch[temp] == '(' && isdigit(ch[temp-1]) )
insert(temp,len,'*');
else if( ( ch[temp]=='.' && temp == 0) || ( temp>0 && ch[temp] == '.' && !isdigit( (ch[temp-1]) ) ) )
insert(temp,len,'0');
//the condition when there is ++
else if( ch[temp] == '+' && ch[temp+1] =='+' )
smaller(temp,len,'+');
//the condition when there is ++
else if( ch[temp] == '+' && ch[temp+1] =='-' )
smaller(temp,len,'-');
//the condition when there is ++
else if( ch[temp] == '-' && ch[temp+1] =='-' )
smaller(temp,len,'+');
//the condition when there is ++
else if( ch[temp] == '-' && ch[temp+1] =='+' )
smaller(temp,len,'-');
}//end loop of coverting
//the condition when first digit is +
if(ch[0]=='+')
decrease(0,1);
}//end edit
//for examing formulas from being out of math rules
bool exam ()
{
int leftBracket=0,//for count left brackts
rightBracket=0,//for count right brackt
len = strlen(ch), // for array lenght
temp;//temprature variable
for(temp = 0; temp<len; temp++)
{
//continue defaule condition of being number
if( isdigit(ch[temp]) )
continue;
//count left brackets
else if(ch[temp] == '(')
leftBracket ++;
//count right brackets
else if(ch[temp] == ')')
rightBracket ++;
//the condition of being two mathimatics symbols after themself
else if( !isalpha(ch[temp]) && !isalpha(ch[temp+1]) && !isdigit(ch[temp+1]) )
return false;
}//end covering loop
//false formulas with right brackets not equal leftbrackets
if(rightBracket != rightBracket)
return false;
//condition with the last idgits isn't number or ')'
if( !isdigit (ch[len-1] ) && ch[len-1] != ')' )
return false;
//defaule condition
return true;
}
//for moving temp
bool triangleMove(int &temp)
{
//sin condition
if(ch[temp] == 's' && ch[temp+1] == 'i' && ch[temp+2] == 'n' && ch[temp+3] == '(' && isdigit(ch[temp+4]) )
{
temp+=4;//for going to next element
return true;
}
//cos condition
else if(ch[temp] == 'c' && ch[temp+1] == 'o' && ch[temp+2] == 's' && ch[temp+3] == '(' && isdigit(ch[temp+4]) )
{
temp+=4;//for going to next element
return true;
}
//tan condition
else if(ch[temp] == 't' && ch[temp+1] == 'a' && ch[temp+2] == 'n' && ch[temp+3] == '(' && isdigit(ch[temp+4]) )
{
temp+=4;//for going to next element
return true;
}
//defaul condition
return false;
}
//examing triangle functions
bool triangle (int temp)
{
//sin condition
if(ch[temp] == 's' && ch[temp+1] == 'i' && ch[temp+2] == 'n' && ch[temp+3] == '(' && isdigit(ch[temp+4]) )
{
return true;
}
//cos condition
else if(ch[temp] == 'c' && ch[temp+1] == 'o' && ch[temp+2] == 's' && ch[temp+3] == '(' && isdigit(ch[temp+4]) )
{
return true;
}
//tan condition
else if(ch[temp] == 't' && ch[temp+1] == 'a' && ch[temp+2] == 'n' && ch[temp+3] == '(' && isdigit(ch[temp+4]) )
{
return true;
}
//defaul condition
return false;
}
void increase (int order, int &len, int count)
{//for increaseing array's element
int temp, temp2;
//punt any digit
for(temp2=0; temp2<count; temp2++)
{
len++;//increase len by one for the new character
for(temp=len;temp>order;temp--)
{//increase array with one starting with last element
ch[temp]=ch[temp-1];
}
}
}//end increase function
void decrease (int order, int count)
{//for increaseing array's element
int temp, temp2;
int len = strlen(ch);
for(temp2=0;temp2<count;temp2++)
{
for(temp=order;temp<len;temp++)
{//increase array with one starting with last element
ch[temp]=ch[temp+1];
}
len--;//increase len by one for the new character
}
}//end increase function
void insert(int order, int &len, char ob)
{//for puting an element in some order
//increase characters
increase (order, len,1);
//put requred element in its position
ch[order] = ob;
}//end insert function
void smaller(int order, int &len, char ob)
{//for shorting two elements with one as ++ to be +
//decrease
decrease(order, 1);
//put requred element in its position
ch[order] = ob;
}//end smaller function
int getNumLen(double num)
{//for the lenght of a num to put it in a character
int intPart,//for the integer part
temp, //temprature variable
numLen;//for the lenght of the num
bool plus = false;
double floatPart;//for the float part
if(num<0)
{
plus = true;
num*=-1;
}
//determine integer part
intPart = intPartTemp = static_cast<int>(num);
floatPart = floatPartTemp = num - intPart;
//getting integer part lenght
for(intLen=0;intPart>0;intPart/=10)
{
intLen++;
}
if(intLen == 0)
intLen = 1;//for making a digit for 0
//getting integer part lenght
//initializing floatLen with 1 for '.'
floatPart>0?floatLen=1:floatLen=0;
for(temp= 1; floatPart!=0 && floatLen<10 ; floatLen++)
{
floatPart*=10;//put one digit in the integer part
temp = static_cast<int>(floatPart) ;
floatPart-=temp;//remove the digit
}
if(plus){
intLen++;//increase digit for -
intPart *=-1;
}
numLen = intLen + floatLen;
return numLen;
}//end getLength function
void convert(char carriage[], double num, int intLen, int intPart, int floatLen, double floatPart, int numLen, bool sign )
{
int temp1, temp;
int start = 0;
//making the first position for 0 in the condition
//of numbers less than 1
if(numLen>0)
{
carriage[numLen]=0;
}
else
{
numLen=1;
carriage[numLen]=0;
}
//negative conditions
if(!sign){
start ++;
carriage[0] = '-';
}
//integer part
for(temp=intLen-1;temp>=start;temp--)
{
temp1= intPart % 10 ;//getting the first digit
carriage[temp]= temp1+48;//put the first digit
intPart/=10; //remove the first digit
}
//floating part
//move null to next position
if(floatPart!=0){
//intLen as null in this time is in intLen position
carriage[intLen]='.';
carriage[intLen + floatLen ]=0;
//starting with intLen because of int part and .
for(temp=intLen+1;temp<floatLen+intLen; temp++)
{
floatPart*=10;//put one digit in the integer part
temp1 = floatPart;//get the digit and put it in integer variable
carriage[temp]=temp1+48;//get the digit in the integer part
floatPart-=temp1;//remove the digit
}
}//end if
}//end convert function
void replace ( double num, char var)
{//for replacing a variable with a numberical value
int len = strlen(ch);
int temp;
for(temp=0;temp<len;temp++)
{
if(ch[temp]==var)
{
decrease(temp,1);//remove variable
insertNum(temp, num);//replace variable with the number