برنامه ماشین حساب مهندسی در C++
#include
//main library
#include
// for mathimatics function
#include
//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;temp0 && 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; temporder;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(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(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(arrange++));
}
position--;
//because it will be increase with the loop
return num;
}//end getNum for floating
double getNum (int &position)
{
double num, //for the return
floatPart; // for floating parts
double sign = 1;//for the numbers'sign 1 is positive
ten = 10;//decleration ten
num = 0;//initialize num with 0
//because we start get the num from its first numerical digit
if(ch[position]=='-')
position++;
if( (position>0 && ch[position-1] == '-') || (position==0 &&ch[position] =='-') )
{
sign = false;
}
//intger part
while(isdigit(ch[position]))
{
num = num *10 + ch[position]-48;
position++;
}
position--;//return to last numerical digit
//because it will be increase with the loop
if(ch[position+1]=='.')
{
position+=2; //for going to the num position
floatPart = getFloatNum(position, 5);
num = num + floatPart ;
}
if(!sign)
num*=-1;
return num;
}
bool check(int temp, double &num1 , double &num2, char &symbol)
{
int len1,//for the lenght of num1
len2;//for the lenght of num2
num1 = getNum(temp);
len1 = getNumLen(num1);
while(isdigit(ch[temp]))
temp++;
temp--;//to be at the last digit
if(ch[temp+1]!=0 && !isdigit(ch[temp+1]) )
{
//getting process symbol
symbol = ch[++temp];
//for going to next num after process symbol
num2 = getNum (++temp);
len2 = getNumLen (num2);
return true;
}//end if condition
else
return false;
}
void trigon_op(int &temp)
{
double num,//result value
insNum;//insert num
int numLen;//the lenght of the number
len = strlen(ch);
//sin condition
if(ch[temp]=='s' && ch[temp+1] =='i' && ch[temp+2] =='n')
{
//going for number position
//three for s, i, n and one for '('
temp+=4;
//inside num
insNum = getNum(temp);
numLen= getNumLen(insNum);
//return for the main positioin
temp-=3+numLen;
//the five are for s, i, n, (, )
decrease(temp,numLen+5);
num=sin(insNum);
insertNum(temp, num);
temp+=getNumLen(num);
}
//cos condition
else if(ch[temp]=='c' && ch[temp+1] =='o' && ch[temp+2] =='s')
{
//going for number position
//three for c, o, s and one for '('
temp+=4;
//inside num
insNum = getNum(temp);
numLen= getNumLen(insNum);
//return for the main positioin
temp-=3+numLen;
//the five are for c, o, s, (, )
decrease(temp,numLen+5);
num=cos(insNum);
insertNum(temp, num);
}
//tan condition
else if(ch[temp]=='t' && ch[temp+1] =='a' && ch[temp+2] =='n')
{
//going for number position
//three for t, a, n and one for '('
temp+=4;
//inside num
insNum = getNum(temp);
numLen= getNumLen(insNum);
//return for the main positioin
temp-=3+numLen;
//the five are for t, a, n, (, )
decrease(temp,numLen+5);
num=tan(insNum);
insertNum(temp, num);
}
}
//for getting str and end values
void getBorders(int &str, int &end)
{
int len = strlen(ch) -1 ;
int temp;
str=0;//initial value
end = len;//initial value
for(temp=0;temp0 && ch[temp]=='(' && !isalpha(ch[temp-1]) )
str=temp+1;
else if(str > 0 && ch[temp]== ')' && !isalpha(ch[str-1]) )
{
end = temp;
break;
}
}//emd for
}
void process (double num1, double num2, char symbol, int temp)
{//processing operations
int len1 = getNumLen(num1),
len2 = getNumLen(num2),//for num1 and num2 lenght
len = strlen(ch);//for the size of array
int count;//for the position of removing and insertion
double num; // for the result
if(symbol == '^')
num = pow(num1,num2);
else if(symbol == '*')
num = num1*num2;
else if(symbol == '/')
num = num1/num2;
else if(symbol == '+')
num = num1+num2;
else if(symbol == '-')
num = num1+num2;// + because it will be taken as negative before
//len1 for removing num1
//len2 for removing num2
//1 for removing symbol
if(num2 >= 0 && num1 >=0) //in this condition the lenght of of num1 and num2 are real
count = len1+len2;
//in this condition the lenght of of num2 is real and num1 is with negative sign so we should remove it
else if(num2 >= 0 && num1 <0)
count = len1+len2;
//in this condition the lenght of of num1 is real and num2 is with negative sign so we should remove it
else if(num2 < 0 && num1 >=0)
count = len1 + len2;
//in this condition the lenght of of num1 and num2 are with negative signs so we should remove them
else if(num2 < 0 && num1 <0)
{
count = len1 + len2 ;
temp--;//to be at negativ sign
}
if(symbol != '-')
count ++ ;
//decrease ch form numbers are members of operation
decrease(temp,count);
insertNum(temp,num);
//starting removing from the first digit of num1
//as we set now on the last digit of num2
//reedit the formula
edit();
}
void semiMath(int str, int end, char ch[])
{
int temp,
zero = 0;//for getNum function
char symbol;
double num1, // for the first number for operating
num2;//for the second number for operating
len = end;
//trigon function stage
for(temp=str;temp=0)
sign = true;
else
sign = false;
convert(carriage,num,intLen,intPartTemp,floatLen,floatPartTemp, numLen, sign);
//negative condition
//starting loop with the last because it will be increase
for(temp = numLen-1; temp>=0 ; temp--)
{
increase(order, len,1);
ch[order] = carriage [temp];//tranport from carriage array
}
}//end insert num
/////////////////////////////////////////////////////////////
public:
double getResult()
{
setInfo();
double num = math(ch,value);
return num;
}
double getResult(char ch[], double value)
{
double num = math(ch,value);
return num;
}
};//end calc class
void main ()
{
calc test;
while(cout<<"= "<
توضیحات:
صورت سوال:
برنامه ماشین حساب مهندسی در C++
ماشین حساب مهندسی در C++ که قابلیت های پیشرفته ای مثل محاسبه سینوس ، کسینوس ، تانژانت ، کتانژانت ، ضرب ماتریس ، انتگرال ، فاکتوریل ، جمع ، ضرب ، تقسیم ، منها ، محاسبه درصد و.. را دارد.
شما میتوانید سوالات خود را از طریق ایمیل پشتیبانی – تماس با ما – یا در قسمت نظرات سوال خود را بپرسید.
موفق باشید
A.J