Słuchajcie mam problem mianowicie napisałem prosty program c++ jest to Kalkulator… ale gdy klikam komplikuj i uruchom pokazują się błędy …
/* Konrad Bańburski */
/* Kalkulator v1.0 */
/* --------------- */
/* calculator.cpp */
#include "calculator.h"
#include
#include
#include
int position;
int length;
double variables[127];
int error;
char error_str[40];
const char *expression;
double abs(double x)
{
return ((x>=0) ? x : (-x));
}
double C_FIGURE::value(void) const
{
return val;
};
double C_VARIABLE::value(void) const
{
return variables[ind];
};
double C_FUNCTION::value(void) const
{
/* {SIN,COS,TG,SINH,COSH,TGH,ASIN,ACOS,ATG,SQRT,ABS} id; */
switch(id)
{
case SIN: return sin(first->value());
case COS: return cos(first->value());
case TG: return tan(first->value());
case SINH: return sinh(first->value());
case COSH: return cosh(first->value());
case TGH: return tanh(first->value());
case ASIN: return asin(first->value());
case ACOS: return acos(first->value());
case ATG: return atan(first->value());
case SQRT: return sqrt(first->value());
case ABS: return abs(first->value());
};
return 0;
};
double C_POWER::value(void) const
{
return pow(first->value(),second->value());
};
double C_DIVIDE::value(void) const
{
return (first->value() / second->value());
};
double C_MULTIPLY::value(void) const
{
return (first->value() * second->value());
};
double C_SUBTRACT::value(void) const
{
return (first->value() - second->value());
};
double C_ADD::value(void) const
{
return (first->value() + second->value());
};
C_ELEMENT* add(void)
{
if(position == length) return NULL; /* jesli koniec wyr. */
C_ADD* result=new C_ADD;
while(expression[position]=='+') position++; /*dopóki znaki
plusa (liczby dodatnie lub dodawanie pomijaj) */
result->first = sub(NULL); //parametr domyslny sub(NULL)
if(expression[position]=='+')
{
position++;
result->second = add();
return result;
}else return result->first;
return NULL;//sytnax error
}
C_ELEMENT* sub(C_ELEMENT* prev)
{
C_SUBTRACT* result=new C_SUBTRACT;
if(prev==NULL)
{
result->first = mul();
if(expression[position]=='-')
{
position++;
result->second = mul();
if(expression[position]=='-')
{
position++;
return sub(result);
}else return result;
}else return result->first;
return NULL; //sytax error
}else
{/* jak podano parametr */
result->first = prev;
result->second = mul();
if(expression[position]=='-')
{
position++;
return sub(result);
}else return result;
}
}
C_ELEMENT* mul(void)
{
if(expression[position]!='*')
{
C_MULTIPLY* result=new C_MULTIPLY;
result->first = div(NULL);
if(expression[position]=='*')
{
position++;
result->second = mul();
return result;
}else return result->first;
}else return NULL; //syntax error
}
C_ELEMENT* div(C_ELEMENT* prev)
{
C_DIVIDE* result=new C_DIVIDE;
if(prev==NULL)
{
if((expression[position]!='/') &&
(expression[position]!=':'))
{
result->first = pow();
if((expression[position]=='/') ||
(expression[position]==':'))
{
position++;
result->second = pow(); //zmienione z div() na pow()
if((expression[position]=='/') ||
(expression[position]==':'))
{
position++;
return div(result);
}else return result;
}else return result->first;
}else return NULL; //syntax error
}else
{/* jak podano parametr */
result->first = prev;
result->second = pow();
if((expression[position]=='/') ||
(expression[position]==':'))
{
position++;
return div(result);
}else return result;
}
}
C_ELEMENT* pow(void)
{
if(expression[position]!='^')
{
C_POWER* result=new C_POWER;
result->first = function();
if(expression[position]=='^')
{
position++;
result->second = pow();
return result;
}else return result->first;
}else return NULL; //syntax error
}
C_ELEMENT* function(void) /* {SIN,COS,TG,SINH,COSH,TGH,ASIN,
ACOS,ATG,SQRT,ABS} id; */
{
if((expression[position]>='a') &&
(expression[position]<='t')) /* s-sin , t-tg */
{
bool is_func = false;
C_FUNCTION* result=new C_FUNCTION;
if((expression[position]=='s')&&
(expression[position+1]=='i')&&
(expression[position+2]=='n'))
{
position+=3; // 3-"sin"
result->id = C_FUNCTION::SIN;
is_func = true;
};
if((expression[position]=='c')&&
(expression[position+1]=='o')&&
(expression[position+2]=='s'))
{
position+=3; // 3-"cos"
result->id = C_FUNCTION::COS;
is_func = true;
};
if((expression[position]=='t')&&
(expression[position+1]=='g'))
{
position+=2; // 2-"tg"
result->id = C_FUNCTION::TG;
is_func = true;
};
if((expression[position]=='s')&&
(expression[position+1]=='i')&&
(expression[position+2]=='n')&&
(expression[position+3]=='h'))
{
position+=4; // 4-"sinh"
result->id = C_FUNCTION::SINH;
is_func = true;
};
if((expression[position]=='c')&&
(expression[position+1]=='o')&&
(expression[position+2]=='s')&&
(expression[position+3]=='h'))
{
position+=4; // 4-"cosh"
result->id = C_FUNCTION::COSH;
is_func = true;
};
if((expression[position]=='t')&&
(expression[position+1]=='g')&&
(expression[position+2]=='h'))
{
position+=3; // 3-"tgh"
result->id = C_FUNCTION::TGH;
is_func = true;
};
if((expression[position]=='a')&&
(expression[position+1]=='s')&&
(expression[position+2]=='i')&&
(expression[position+3]=='n'))
{
position+=4; // 4-"asin"
result->id = C_FUNCTION::ASIN;
is_func = true;
};
if((expression[position]=='a')&&
(expression[position+1]=='c')&&
(expression[position+2]=='o')&&
(expression[position+3]=='s'))
{
position+=4; // 4-"acos"
result->id = C_FUNCTION::ACOS;
is_func = true;
};
if((expression[position]=='a')&&
(expression[position+1]=='t')&&
(expression[position+2]=='g'))
{
position+=3; // 4-"atg"
result->id = C_FUNCTION::ATG;
is_func = true;
};
if((expression[position]=='s')&&
(expression[position+1]=='q')&&
(expression[position+2]=='r')&&
(expression[position+3]=='t'))
{
position+=4; // 4-"sqrt"
result->id = C_FUNCTION::SQRT;
is_func = true;
};
if((expression[position]=='a')&&
(expression[position+1]=='b')&&
(expression[position+2]=='s'))
{
position+=3; // 3-"abs"
result->id = C_FUNCTION::ABS;
is_func = true;
};
if(is_func)
{
result->first = bracket();
return result;
}else
{
delete result;
return variable();
}
}else return variable();
}
C_ELEMENT* variable(void)
{
if((expression[position]>='A') &&
(expression[position]<='z'))
{
C_VARIABLE* result = new C_VARIABLE;
result->ind = expression[position];
position++;
return result;
}else return bracket();
}
C_ELEMENT* bracket(void)
{
if(expression[position]=='|') //abs function
{
position++;
C_FUNCTION* result = new C_FUNCTION;
result->first = add();
result->id = C_FUNCTION::ABS; //abs function
position++;
return result;
}
if((expression[position]=='(') ||
(expression[position]=='[') ||
(expression[position]=='{'))
{
position++;
C_ELEMENT* result = add();
position++;
return result;
}else return figure();
}
C_ELEMENT* figure(void)
{
char buf[50];
int len=0;
bool ujemna = false;
while( !((expression[position]>=48) &&
(expression[position]<=57) ||
(expression[position]=='.')) )
{
int minusow=0;
int plusow=0;
while(expression[position]=='-') {minusow++; position++;}
while(expression[position]=='+') {plusow++; position++;}
/* pomin plusy - i tak nie maja znaczenia */
if(minusow & 1) ujemna = !ujemna;
if((minusow==0) && (plusow==0)) /* jak nie jest cyfra(0-9)
ani kropka(.),(+) i (-) to blad */
{
error = 1;
strcpy(error_str,"Syntax error at ");
char buf[5];
char len;
itoa(position+1,buf);
len = strlen(buf);
strcpy(error_str+16,buf);
strcpy(error_str+16+len,": '");
error_str[len+19]=expression[position];
error_str[len+20]='\'';
error_str[len+21]=0;
return NULL;
}
}
if(ujemna == true) buf[len++]='-'; //dodaj znak '-'
if(expression[position]=='.') buf[len++]='.'; /* . -koma*/
while((expression[position]>=48) &&
(expression[position]<=57))/* . -koma*/
{
buf[len++] = expression[position++];
}
buf[len]=0; //zakonczenie string
C_FIGURE* result = new C_FIGURE;
result->val = atof(buf);
return result;
}
C_ELEMENT* analise(void)
{
position=0;
length=strlen(expression);
error = 0;
/* check brackets */
int l,p;
l=p=0;
for(int i=0;i
{
if((*(expression+i)=='(') || (*(expression+i)=='[') || (*(expression+i)=='{')) l++;
if((*(expression+i)==')') || (*(expression+i)==']') || (*(expression+i)=='}')) p++;
if(p>l)
{/* error */
error = 1;
strcpy(error_str,"Syntax error (bracktets) at ");
char buf[5];
char len;
itoa(i+1,buf);
len = strlen(buf);
strcpy(error_str+28,buf);
strcpy(error_str+28+len,": '");
error_str[len+31]=expression[position];
error_str[len+32]='\'';
error_str[len+33]=0;
return NULL;
}
};
if(p!=l)
{/* error */
error = 1;
strcpy(error_str,"Syntax error: to few brackets ");
return NULL;
}
/* end of checking brackets */
C_ELEMENT* result = add();
return result;
}
char* itoa(const int i,char* ptr)
{
int p = 0;
int m=1;
int x = i;
while(i>m) m*=10;
m/=10;
while(x>0)
{
*(ptr+p++) = (char)(x/m + 48);
x -= ((int)(x/m))*m;
m/=10;
}
return ptr;
}
void remove(C_ELEMENT* ptr)
{
ptr->remove();
delete ptr;
}
void C_FIGURE::remove(void)
{/*do nothing */
}
void C_VARIABLE::remove(void)
{/* do nothing */
}
void C_FUNCTION::remove(void)
{
first->remove();
delete first;
first = NULL;
}
void C_POWER::remove(void)
{
first->remove();
second->remove();
delete first;
delete second;
first = NULL;
second = NULL;
}
void C_DIVIDE::remove(void)
{
first->remove();
second->remove();
delete first;
delete second;
first = NULL;
second = NULL;
}
void C_MULTIPLY::remove(void)
{
first->remove();
second->remove();
delete first;
delete second;
first = NULL;
second = NULL;
}
void C_SUBTRACT::remove(void)
{
first->remove();
second->remove();
delete first;
delete second;
first = NULL;
second = NULL;
}
void C_ADD::remove(void)
{
first->remove();
second->remove();
delete first;
delete second;
first = NULL;
second = NULL;
}
EXPRESSION::EXPRESSION(const char* ptr)
{
::error=0;
error = 0;
str = new char[strlen(ptr)+1];
strcpy(str,ptr);
expression = str;
data = analise();
if(::error)
{
delete[] str;
str = NULL;
this->error = ::error;
strncpy(this->error_str,::error_str,40);
this->~EXPRESSION(); //destroy the tree
}
}
EXPRESSION::~EXPRESSION()
{
remove(data);
data = NULL;
delete[] str;
str = NULL;
}
EXPRESSION::operator double()
{
if(data!=NULL && error==0)
{
return data->value();
}else return 0;
}
EXPRESSION::operator int()
{
if(data!=NULL && error==0)
{
return int(data->value());
}else return 0;
}
EXPRESSION& EXPRESSION::operator =(EXPRESSION& ref)
{
str = new char[strlen(ref.str)+1];
strcpy(str,ref.str);
expression = str;
data = analise();
if(::error)
{
delete[] str;
str = NULL;
this->error = ::error;
strncpy(this->error_str,::error_str,40);
this->~EXPRESSION(); //destroy the tree
}
return *this;
}
EXPRESSION& EXPRESSION::operator =(const char* ptr)
{
::error=0;
error = 0;
str = new char[strlen(ptr)+1];
strcpy(str,ptr);
expression = str;
data = analise();
if(::error)
{
delete[] str;
str = NULL;
this->error = ::error;
strncpy(this->error_str,::error_str,40);
this->~EXPRESSION(); //destroy the tree
}
return *this;
}
void EXPRESSION::set_variable(const char c,const double d) const
{
if(c>'A' && c<'z') variables[c] = d;
}
Tu jest ten kod …
C:\Documents and Settings\Konto\Pulpit\main.cpp|16|calculator.h: No such file or directory|
C:\Documents and Settings\Konto\Pulpit\main.cpp|33|error: `C_FIGURE' has not been declared|
C:\Documents and Settings\Konto\Pulpit\main.cpp|34|error: non-member function `double value()' cannot have `const' method qualifier|
C:\Documents and Settings\Konto\Pulpit\main.cpp||In function `double value()':|
C:\Documents and Settings\Konto\Pulpit\main.cpp|35|error: `val' was not declared in this scope|
C:\Documents and Settings\Konto\Pulpit\main.cpp|38|error: `C_VARIABLE' has not been declared|
C:\Documents and Settings\Konto\Pulpit\main.cpp|39|error: non-member function `double value()' cannot have `const' method qualifier|
C:\Documents and Settings\Konto\Pulpit\main.cpp|16|calculator.h: No such file or directory|
C:\Documents and Settings\Konto\Pulpit\main.cpp|33|error: `C_FIGURE' has not been declared|
C:\Documents and Settings\Konto\Pulpit\main.cpp|34|error: non-member function `double value()' cannot have `const' method qualifier|
C:\Documents and Settings\Konto\Pulpit\main.cpp||In function `double value()':|
C:\Documents and Settings\Konto\Pulpit\main.cpp|35|error: `val' was not declared in this scope|
C:\Documents and Settings\Konto\Pulpit\main.cpp|38|error: `C_VARIABLE' has not been declared|
C:\Documents and Settings\Konto\Pulpit\main.cpp|39|error: non-member function `double value()' cannot have `const' method qualifier|
C:\Documents and Settings\Konto\Pulpit\main.cpp||In function `double value()':|
C:\Documents and Settings\Konto\Pulpit\main.cpp|39|error: redefinition of `double value()'|
C:\Documents and Settings\Konto\Pulpit\main.cpp|34|error: `double value()' previously defined here|
C:\Documents and Settings\Konto\Pulpit\main.cpp|40|error: `ind' was not declared in this scope|
C:\Documents and Settings\Konto\Pulpit\main.cpp|43|error: `C_FUNCTION' has not been declared|
C:\Documents and Settings\Konto\Pulpit\main.cpp|44|error: non-member function `double value()' cannot have `const' method qualifier|
C:\Documents and Settings\Konto\Pulpit\main.cpp||In function `double value()':|
C:\Documents and Settings\Konto\Pulpit\main.cpp|44|error: redefinition of `double value()'|
C:\Documents and Settings\Konto\Pulpit\main.cpp|34|error: `double value()' previously defined here|
C:\Documents and Settings\Konto\Pulpit\main.cpp|46|error: `id' was not declared in this scope|
C:\Documents and Settings\Konto\Pulpit\main.cpp|48|error: `SIN' was not declared in this scope|
C:\Documents and Settings\Konto\Pulpit\main.cpp|48|error: `first' was not declared in this scope|
C:\Documents and Settings\Konto\Pulpit\main.cpp|49|error: `COS' was not declared in this scope|
C:\Documents and Settings\Konto\Pulpit\main.cpp|50|error: `TG' was not declared in this scope|
C:\Documents and Settings\Konto\Pulpit\main.cpp|51|error: `SINH' was not declared in this scope|
C:\Documents and Settings\Konto\Pulpit\main.cpp|52|error: `COSH' was not declared in this scope|
C:\Documents and Settings\Konto\Pulpit\main.cpp|53|error: `TGH' was not declared in this scope|
C:\Documents and Settings\Konto\Pulpit\main.cpp|54|error: `ASIN' was not declared in this scope|
C:\Documents and Settings\Konto\Pulpit\main.cpp|55|error: `ACOS' was not declared in this scope|
C:\Documents and Settings\Konto\Pulpit\main.cpp|56|error: `ATG' was not declared in this scope|
C:\Documents and Settings\Konto\Pulpit\main.cpp|57|error: `SQRT' was not declared in this scope|
C:\Documents and Settings\Konto\Pulpit\main.cpp|58|error: `ABS' was not declared in this scope|
C:\Documents and Settings\Konto\Pulpit\main.cpp|63|error: `C_POWER' has not been declared|
C:\Documents and Settings\Konto\Pulpit\main.cpp|64|error: non-member function `double value()' cannot have `const' method qualifier|
C:\Documents and Settings\Konto\Pulpit\main.cpp||In function `double value()':|
C:\Documents and Settings\Konto\Pulpit\main.cpp|64|error: redefinition of `double value()'|
C:\Documents and Settings\Konto\Pulpit\main.cpp|34|error: `double value()' previously defined here|
C:\Documents and Settings\Konto\Pulpit\main.cpp|65|error: `first' was not declared in this scope|
C:\Documents and Settings\Konto\Pulpit\main.cpp|65|error: `second' was not declared in this scope|
C:\Documents and Settings\Konto\Pulpit\main.cpp|68|error: `C_DIVIDE' has not been declared|
C:\Documents and Settings\Konto\Pulpit\main.cpp|69|error: non-member function `double value()' cannot have `const' method qualifier|
C:\Documents and Settings\Konto\Pulpit\main.cpp||In function `double value()':|
C:\Documents and Settings\Konto\Pulpit\main.cpp|69|error: redefinition of `double value()'|
C:\Documents and Settings\Konto\Pulpit\main.cpp|34|error: `double value()' previously defined here|
C:\Documents and Settings\Konto\Pulpit\main.cpp|70|error: `first' was not declared in this scope|
C:\Documents and Settings\Konto\Pulpit\main.cpp|70|error: `second' was not declared in this scope|
C:\Documents and Settings\Konto\Pulpit\main.cpp|73|error: `C_MULTIPLY' has not been declared|
C:\Documents and Settings\Konto\Pulpit\main.cpp|74|error: non-member function `double value()' cannot have `const' method qualifier|
C:\Documents and Settings\Konto\Pulpit\main.cpp||In function `double value()':|
C:\Documents and Settings\Konto\Pulpit\main.cpp|74|error: redefinition of `double value()'|
C:\Documents and Settings\Konto\Pulpit\main.cpp|34|error: `double value()' previously defined here|
C:\Documents and Settings\Konto\Pulpit\main.cpp|75|error: `first' was not declared in this scope|
C:\Documents and Settings\Konto\Pulpit\main.cpp|75|error: `second' was not declared in this scope|
C:\Documents and Settings\Konto\Pulpit\main.cpp|78|error: `C_SUBTRACT' has not been declared|
C:\Documents and Settings\Konto\Pulpit\main.cpp|79|error: non-member function `double value()' cannot have `const' method qualifier|
C:\Documents and Settings\Konto\Pulpit\main.cpp||In function `double value()':|
C:\Documents and Settings\Konto\Pulpit\main.cpp|79|error: redefinition of `double value()'|
C:\Documents and Settings\Konto\Pulpit\main.cpp|34|error: `double value()' previously defined here|
C:\Documents and Settings\Konto\Pulpit\main.cpp|80|error: `first' was not declared in this scope|
C:\Documents and Settings\Konto\Pulpit\main.cpp|80|error: `second' was not declared in this scope|
||More errors follow but not being shown.|
||Edit the max errors limit in compiler options...|
||=== Build finished: 50 errors, 0 warnings ===|
Pomoże mi ktoś z tym ??
Jestem wg nowy wtym ichciałem zobaczyć jak to działa skopoiwałem poprostu to do projektu i troche po przerabuiałem jednak coś jest nie tak …