How can we append the two string in bb10???
I m to make a calculator for blackberry but get some errors.
Please help me out.
I got error in this code:
void CalcTrial::oneButtonClicked()
{
// Change the button text when clicked
if(textf==NULL)
{
textf->setText("1");
}
else
{
textf->QString+"1";
}
}
void CalcTrial::twoButtonClicked()
{
if(textf==NULL)
{
textf->setText("2");
}
else
{
textf->QString+"2";
}
}
void CalcTrial::threeButtonClicked()
{
if(textf==NULL)
{
textf->setText("3");
}
else
{
textf->QString+"3";
}
}
void CalcTrial::fourButtonClicked()
{
if(textf==NULL)
{
textf->setText("4");
}
else
{
textf->QString+"4";
}
}
void CalcTrial::fiveButtonClicked()
{
if(textf==NULL)
{
textf->setText("5");
}
else
{
textf->QString+"5";
}
}
void CalcTrial::sixButtonClicked()
{
if(textf==NULL)
{
textf->setText("6");
}
else
{
textf->QString+"6";
}
}
void CalcTrial::sevenButtonClicked()
{
if(textf==NULL)
{
textf->setText("7");
}
else
{
textf->QString+"7";
}
}
void CalcTrial::eightButtonClicked()
{
if(textf==NULL)
{
textf->setText("8");
}
else
{
textf->QString+"8";
}
}
void CalcTrial::nineButtonClicked()
{
if(textf==NULL)
{
textf->setText("9");
}
else
{
textf->QString+"9";
}
}
void CalcTrial::zeroButtonClicked()
{
if(textf==NULL)
{
textf->setText("0");
}
else
{
textf->QString+"0";
}
}
void CalcTrial::addButtonClicked()
{
operation=1;
temp1 = QString(getchar())+textf;
textf->setText(NULL);
}
void CalcTrial::minusButtonClicked()
{
operation=2;
temp1 = QString(getchar())+textf;
textf->setText(NULL);
}
void CalcTrial::mulButtonClicked()
{
operation=3;
temp1 = QString(getchar())+textf;
textf->setText(NULL);
}
void CalcTrial::divButtonClicked()
{
operation=4;
temp1 = QString(getchar())+textf;
textf->setText(NULL);
}
void CalcTrial::equalButtonClicked()
{
temp2 = QString(getchar())+temp1;
switch (operation) {
case 1:
result=temp1+temp2;
break;
case 2:
result=temp1-temp2;
break;
case 3:
result=temp1*temp2;
break;
case 4:
result=temp1/temp2;
break;
default:
break;
}
textf->text(result);
}
To convert QString to an int:
QString strNum = "22";
int intNum = strNum.toInt();
Related
In this code, it runs fine when I have declared only one object of polynomial class. But as soon as I declare two objects of polynomial class, it does not show any error, but the code does not run. Not even the first line of int main().
I have checked all other functions individually. The only problem is declaration of more than one polynomial class objects!!!
#include<iostream>
#include<fstream>
#include<string>
#include<cstring>
#include<math.h>
using namespace std;
string space_rem(string s)
{
int i;
string res;
for(i=0;i<s.size();i++)
{
if(s[i] != ' ')
res+=s[i];
}
return res;
}
string all_in_powers(string s) //writes every variable in string in its power form x^(i)
{ //e.g. abca is written as a^(1)b^(1)c^(1)a^(1)
string temp = s;
if(temp.size()==0)
return temp;
int i;
if(temp[temp.size()-1]>='a'&&temp[temp.size()-1]<='z')
temp+="^(1)";
string res;
char x;
for(i=0;i<temp.size()-3;i++)
{
x=temp[i];
if(x>='a' && x<='z' && temp[i+1]!='^') //if variable not in power form
{
res+=x;
res+="^(1)"; //writes ab as a^(1)b^(1)
}
else
{
res+=x; //variable in power form so copy as it is
}
}
for(i=temp.size()-3;i<temp.size();i++)
{
res+=temp[i];
}
return res;
}
class polynomial_term //stores everything regarding a polynomial term
{
public:
int powers[26]={0}; //power of each variable from a to z
string pol; //string representation of term without coefficient
int coeff; //coefficient of term
char sign;
int pow_sum; //positive or negative
public:
void full_term(string s); //inputs string form and puts all the data of the term in this class's object
void string_construct(); //makes the string representation pol from the powers array
void pol_pow(string s); //gathers data of total power of each variable from input string
void display(); //displays the complete polynomial term
polynomial_term operator * (polynomial_term); //to multiply two terms
bool operator < (polynomial_term);
bool operator > (polynomial_term);
bool operator == (polynomial_term);
void display_pow();
polynomial_term operator + (polynomial_term);
void copy_frm(polynomial_term);
};
void polynomial_term::copy_frm(polynomial_term B)
{
for(int i=0;i<26;i++)
{
powers[i]=B.powers[i];
}
coeff=B.coeff;
pol=B.pol;
sign=B.sign;
pow_sum=B.pow_sum;
}
void polynomial_term:: display_pow()
{
for(int i=0;i<26;i++)
{
cout<<powers[i]<<" ";
}
cout<<endl;
}
void polynomial_term::display()
{
/*if(coeff==0)
{
cout<<"0\n";
}*/
if(coeff==1)
{
if(pol.size()!=0)
cout<<pol<<endl;
else
{
cout<<1<<endl; //only coefficient printed as pol is empty
}
}
else
{
if(coeff!=-1)
cout<<coeff<<pol<<endl;
else
cout<<"-"<<pol<<endl;
}
}
void polynomial_term::string_construct() //generates pol member of polynomial term class using powers array
{
pol.clear();
/*if(coeff!=1)
{
pol+=to_string(coeff);
}*/
for(int i=0;i<26;i++)
{
if(powers[i]!=0)
{
pol+=(char)(i+'a');
if(powers[i]!=1)
{
pol+="^(";
pol+=to_string((powers[i]));
pol+=')';
}
}
}
}
void polynomial_term::pol_pow(string s) //
{
char x;
if(s.size()==0)
return;
for(int i=0;i<s.length();i++)
{
x=s[i];
if(x>='a'&&x<='z')
{
string tmp;
int pow;
for(int j=i+3;s[j]!=')';j++) //gathers power of variable in tmp string
{
tmp+=s[j];
}
pow=stoi(tmp);
powers[x-'a']+=pow;
}
}
pow_sum=0;
for(int i=0;i<26;i++)
{
pow_sum+=powers[i];
}
}
void polynomial_term::full_term(string s) //inputs raw string and organises its data in the polynomial_term class object
{
sign='+'; //default sign
coeff=1; //default coefficient
for(int i=0;i<26;i++)
{
powers[i]=0;
}
pol.clear();
pow_sum=0;
int i=0;
if(s[0]=='-')
{
sign='-';
i++;
}
string tmp;
while(i<s.size() && s[i]>='0' && s[i]<='9') //tmp collects the coefficient part
{
tmp+=s[i];
i++;
}
if(tmp.size()>0)coeff=stoi(tmp);
if(sign=='-')
coeff*=-1;
tmp="";
if(1)
{
for(int j=i;j<s.length();j++)
{
tmp+=s[j];
}
}
s=tmp;
s=all_in_powers(s);
pol_pow(s);
string_construct();
}
polynomial_term polynomial_term::operator * (polynomial_term B) //returns multiplication of terms
{
polynomial_term res;
res.coeff=coeff*B.coeff;
if(res.coeff<0)
res.sign='-';
string temp=pol+B.pol;
temp=all_in_powers(temp);
res.pol_pow(temp);
res.string_construct();
return res;
}
bool polynomial_term::operator < (polynomial_term B) //lexicographic order comparison
{
// bool flag=0;
/*if(pow_sum==B.pow_sum)
{
for(int i=0; i<26; i++)
{
if(powers[i]>B.powers[i])
{
//if(B.powers[i]==0)
//return false;
return false;
}
else if(powers[i]<B.powers[i])
{
return true;
}
}
}*/
for(int i=0; i<26; i++)
{
if(powers[i]>B.powers[i])
{
//if(B.powers[i]==0)
//return false;
return true;
}
else if(powers[i]<B.powers[i])
{
return false;
}
}
return false;
}
bool polynomial_term::operator > (polynomial_term B) //lexicographic order comparison
{
for(int i=0; i<26; i++)
{
if(powers[i]>B.powers[i])
return false;
else
{
if(powers[i]<B.powers[i])
return true;
else
continue;
}
}
return false;
}
bool polynomial_term::operator == (polynomial_term B)
{
for(int i=0;i<26;i++)
{
if(powers[i] != B.powers[i])
return false;
}
return true;
}
polynomial_term polynomial_term::operator + (polynomial_term B) //return addition of polynomial_terms
{
polynomial_term C;
if(pol!=B.pol)
{
cout<<"Error: Adding two non_compatible polynomial terms!!\n";
}
C.coeff = coeff+B.coeff;
if(C.coeff<0)
C.sign='-';
else
C.sign='+';
C.pol=pol;
//cout<<"*\n";
C.pol_pow(all_in_powers(pol));
//cout<<"**\n";
C.string_construct();
//cout<<"*\n";
return C;
}
class polynomial
{
public:
polynomial_term terms[10000];
int term_count;
string expression;
public:
void expression_generate();
polynomial(): term_count(0),expression("0")
{
polynomial_term X;
X.full_term("0");
for(int i=0;i<10000;i++)
{
terms[i].copy_frm(X);
}
}
polynomial(string s) //constructor segregates terms and stores them in terms array
{
//cout<<"uo\n";
polynomial_term X;
X.full_term("0");
for(int i=0;i<10000;i++)
{
terms[i].copy_frm(X);
}
s=all_in_powers(s);
polynomial_term tmp;
string temp="";
int i;
for(i=0;!(i!=0 && s[i]=='+'|| s[i]=='-') && i<s.size();i++) //put first term in the array
{
temp+=s[i];
}
cout<<"***"<<i<<endl;
tmp.full_term(temp);
terms[0]=tmp;
temp="";
term_count=1;
while(i<s.size())
{
if(s[i]=='+' )
{
temp="";
i++;
while(s[i]!='+' && s[i]!='-' && i<s.size())
{
temp+=s[i];
i++;
}
tmp.full_term(temp);
terms[term_count]=tmp;
term_count++;
}
else if(s[i]=='-')
{
temp="-";
i++;
while(s[i]!='+' && s[i]!='-' && i<s.size())
{
temp+=s[i];
i++;
}
tmp.full_term(temp);
terms[term_count]=tmp;
term_count++;
}
}
expression_generate();
}
void lexicosort();
void powersort();
void display_terms();
//void expression_generate();
void display();
bool found(polynomial_term);
};
void polynomial::display_terms()
{
for(int i =0;i<term_count;i++)
{
terms[i].display();
}
}
void polynomial::display()
{
cout<<expression<<endl;
}
void polynomial::powersort()
{
polynomial_term least;
int least_index,i,j;
for(i=0;i<term_count;i++)
{
least = terms[i];
least_index = i;
for(j=i;j<term_count;j++)
{
if(terms[j].pow_sum<least.pow_sum)
{
least=terms[j];
least_index=j;
}
}
polynomial_term temp = terms[i];
terms[i] = terms[least_index];
terms[least_index] = temp;
}
}
void polynomial::lexicosort()
{
polynomial_term least;
int least_index,i,j;
for(i=0;i<term_count;i++)
{
least = terms[i];
least_index = i;
for(j=i;j<term_count;j++)
{
if(terms[j]<least)
{
least=terms[j];
least_index=j;
}
}
polynomial_term temp = terms[i];
terms[i] = terms[least_index];
terms[least_index] = temp;
}
}
void polynomial::expression_generate()
{
// cout<<"**\n";
expression.clear();
expression="";
if(term_count==0)
{
//cout<<"*\n";
expression="0";
return;
}
for(int i=0;i<term_count;i++)
{
//cout<<"lolo\n";
if(terms[i].coeff==0)
{
if(i==1)
{
expression="0";
return;
}
else
{
continue;
}
}
if((terms[i].coeff)!=1 && (terms[i].coeff)!=-1)
{
if(i!=0)
expression += terms[i].sign;
expression += to_string(terms[i].coeff);
}
else
{
if(i!=0)
expression += terms[i].sign;
}
expression += terms[i].pol;
}
}
bool polynomial::found(polynomial_term X)
{
for(int i=0;i<term_count;i++)
{
if(X==terms[i])
return true;
}
return false;
}
/*polynomial comp(polynomial A) //compresses by grouping similar terms (terms that can be added)
{
A.lexicosort();
A.expression_generate();
A.powersort();
A.expression_generate();
polynomial_term tmp;
tmp.copy_frm(A.terms[0]);
polynomial res;
for(int i=1;i<A.term_count;i++)
{
if(!(A.terms[i]==tmp))
{
res.terms[res.term_count].copy_frm(tmp);
res.term_count++;
tmp.copy_frm(A.terms[i]);
}
else
{
polynomial_term X=tmp+A.terms[i];
tmp.copy_frm(X);
}
}
res.expression_generate();
return res;
}*/
int main()
{
cout<<"*\n";
polynomial A("a");
// polynomial B;
cout<<"*\n";
polynomial B("a+b+b+a");
A.display();
cout<<"*\n";
/* A.lexicosort();
A.expression_generate();
A.powersort();
A.expression_generate();
cout<<"*\n";
A.display_terms();
cout<<"*\n";*/
//res.expression_generate();
//res.display();
}
I was trying this parenthesis matching stack code and every time I run this it only gives me that parenthesis are not matching I don't know what is going wrong. Can you guys please help me out in this and tell me how can I improve this further and is this the right way to use stack in c++ because I was studying from a source that is using c language.
#include<iostream>
using namespace std;
struct stack
{
int size;
int top;
char *arr;
};
int isEmpty(struct stack *ptr)
{
if(ptr->top==-1)
{
return 1;
}
return 0;
}
int isFull(struct stack *ptr)
{
if (ptr->top == ptr->size-1)
{
return 1;
}
return 0;
}
void push(struct stack *ptr,char val)
{
if(isFull(ptr))
{
cout<<"stack overflow"<<endl;
}
else
{
ptr->top++;
ptr->arr[ptr->top] = val;
}
}
char pop(struct stack *ptr)
{
if (isEmpty(ptr))
{
cout<<"stack underflow"<<endl;
return -1;
}
else
{
char val = ptr->arr[ptr->top];
ptr->top-1;
return val;
}
}
int parenthesisMatch (char * exp)
{
struct stack * sp = new struct stack;
sp->size = 80;
sp->top = -1;
sp->arr = new char(sp->size);
for(int i=0; exp[i]!='\0'; i++)
{
if (exp[i] == '(')
{
push(sp,'(');
}
else if(exp[i] == ')')
{
if (isEmpty(sp))
{
return 0;
}
pop(sp);
}
}
if (isEmpty(sp))
{
return 1;
}
return 0;
}
int main()
{
char *exp = "((8)(*--$$9))";
if(parenthesisMatch(exp))
{
cout<<"The parenthesis is matching\n";
}
else
{
cout<<"The parenthesis is not matching\n";
}
return 0;
}
Just tried to debug the code and found 2 potential errors:
As correctly pointed out by #Welbog in the comments, to create an array, use sp->arr = new char[sp->size; in the function parenthesisMatch().
In the function pop(), the value of the top is not decremented correctly.
Have a look at the working code:
#include<iostream>
struct stack
{
int size;
int top;
char *arr;
};
int isEmpty(struct stack *ptr)
{
if(ptr->top==-1)
{
return 1;
}
return 0;
}
int isFull(struct stack *ptr)
{
if (ptr->top == ptr->size-1)
{
return 1;
}
return 0;
}
void push(struct stack *ptr,char val)
{
if(isFull(ptr))
{
std::cout<<"stack overflow"<<std::endl;
}
else
{
ptr->top++;
ptr->arr[ptr->top] = val;
}
}
char pop(struct stack *ptr)
{
if (isEmpty(ptr))
{
std::cout<<"stack underflow"<<std::endl;
return -1;
}
else
{
char val = ptr->arr[ptr->top];
ptr->top-=1;
return val;
}
}
int parenthesisMatch (char * exp)
{
struct stack * sp = new struct stack;
sp->size = 80;
sp->top = -1;
sp->arr = new char[sp->size];
for(int i=0; exp[i]!='\0'; i++)
{
if (exp[i] == '(')
{
push(sp,'(');
}
else if(exp[i] == ')')
{
if (isEmpty(sp))
{
return 0;
}
pop(sp);
}
}
if (isEmpty(sp))
{
return 1;
}
return 0;
}
int main()
{
char *exp = "((8)(*--$$9))";
if(parenthesisMatch(exp))
{
std::cout<<"The parenthesis is matching\n";
}
else
{
std::cout<<"The parenthesis is not matching\n";
}
return 0;
}
Here I have the enum class:
enum class wahl {
schere , stein , papier
};
And then I overload the operator < and >
bool operator<(wahl &wahl1, wahl &wahl2) {
switch (wahl1) {
case wahl::papier: {
if (wahl2 == wahl::schere) {
return true; break;
} //papier < schere
else if (wahl2 == wahl::stein) {
return false; break;
} //papier > stein
else {
return false; break;
}
}
case wahl::schere: {
if (wahl2 == wahl::stein) {
return true; break;
} //schere < stein
else if (wahl2 == wahl::papier) {
return false; break;
} //schere > papier
else {
return false; break;
}
}
case wahl::stein: {
if (wahl2 == wahl::papier) {
return true; break;
} //stein < papier
else if (wahl2 == wahl::schere) {
return false; break;
} //stein > schere
else {
return false; break;
}
}
}
};
bool operator > (const wahl wahl1, const wahl wahl2) {
switch (wahl1) {
case wahl::papier: {
if (wahl2 == wahl::schere) {
return false; break;
}
// papier < schere
else if (wahl2 == wahl::stein) {
return true; break;
} //papier > stein
else {
return false; break;
}
}
case wahl::schere: {
if (wahl2 == wahl::stein) {
return false; break;
} //schere < stein
else if (wahl2 == wahl::papier) {
return true; break;
} //schere > papier
else {
return false; break;
}
}
case wahl::stein: {
if (wahl2 == wahl::papier) {
return false; break;
} //stein < papier
else if (wahl2 == wahl::schere) {
return true; break;
} //stein > schere
else {
return false; break;
}
}
}
};
I have another class, named player :
class player {
wahl pl_wahl;
int pl_score;
char* pl_name;
public:
player() {}
player(int score, wahl wahl, char* name) :
pl_wahl{ wahl }, pl_score{ score }, pl_name{ name } {}
wahl pl_get_wahl() {
return pl_wahl;
}
char* pl_get_name() {
return pl_name;
}
int &pl_get_score() {
return pl_score;
}
};
And here where I used the comparator :
class game {
player game_player1, game_player2, game_momentan_gewinner;
int game_score_max;
public:
game() {}
game(player player1, player player2, int score_max) :
game_player1{player1},
game_player2{player2},
game_score_max{ score_max } {}
void vergleichen() {
if (game_player1.pl_get_wahl() > game_player2.pl_get_wahl()) {
game_momentan_gewinner = game_player1;
std::cout << "Gewinner dieser Runde ist Player 1 : " <<
game_momentan_gewinner.pl_get_name() << std::endl;
}
if (game_player1.pl_get_wahl() < game_player2.pl_get_wahl()) {
game_momentan_gewinner = game_player2;
std::cout << "Gewinner dieser Runde ist Player 2 : " <<
game_momentan_gewinner.pl_get_name() << std::endl;
}
else {
std::cout << "Remis" << std::endl;
}
}
};
The problem that I have, is enum will be proceed as int. And if I ask the compare two enum variables, the result will depend on the int value rather than taking the value, that I have set with the overloaded operator.
Is there any way, that i can stop the compiler to use the int value of the enum variables, and compare the enum variables in the way I want like in the overloaded operator?
If you use correct signature, it should be ok:
bool operator<(wahl lhs, wahl rhs);
bool operator>(wahl lhs, wahl rhs);
Demo
As my homework I have to write a program that calculates an equation given as string. A part of the program is a function parsing an infix expression into postfix. Here is my code:
void dijkstra(string& s)
{
int i,j=s.size();
stack < char > znaki;
string output=" ";
string znak;
for(i=0; i<j;++i)
{
if((int(s[i])>47&&int(s[i])<58)||s[i]=='.')
{
output+=s[i];
}
else if(s[i]=='+'||s[i]=='-'||s[i]=='*'||s[i]=='/')
{
output+=" ";
if(znaki.empty())
{
}
else
{
if((s[i]=='+'||s[i]=='-'))
{
znak=znaki.top();
znaki.pop();
output+=znak;
}
else if(s[i]=='*')
{
if(znaki.top()=='*'||znaki.top()=='/')
{
znak=znaki.top();
znaki.pop();
output+=znak;
}
}
else if(s[i]=='/')
{
if(znaki.top()=='*'||znaki.top()=='/')
{
znak=znaki.top();
znaki.pop();
output+=znak;
}
}
}
znaki.push(s[i]);
}
else if(int(s[i])=='(')
{
znaki.push(s[i]);
}
else if(int(s[i])==')')
{
while(znaki.top()!='(')
{
output+=znaki.top();
znaki.pop();
}
znaki.pop();
}
}
while(znaki.empty()!=true)
{
output+=znaki.top();
znaki.pop();
}
s=output;
}
The problem is that it works in all conditions except equations like 4/6. Any ideas? I've just made requested updates
I could use help with solving this problem, looked online and saw that many problems were due to the size of the array in other programmer's programs. So I tried changing numbers affecting the array "sub" and still haven't found a solution. All instances of sub in my program are included below.
int sub[9]={0,0,0,0,0,0,0,0,0};
for(int i=0;i<=8;i++)
{
if(sub[i]>0)
{
if(one==sub[i]&one>0&one<200)
{
cout<<"\t\tP1";
}
if(two==sub[i]&two>0&two<200)
{
cout<<"\t\tP2";
}
if(three==sub[i]&three>0&three<200)
{
cout<<"\t\tP3";
}
if(four==sub[i]&four>0&four<200)
{
cout<<"\t\tP4";
}
if(five==sub[i]&five>0&five<200)
{
cout<<"\t\tP5";
}
if(six==sub[i]&six>0&six<200)
{
cout<<"\t\tP6";
}
if(seven==sub[i]&seven>0&seven<200)
{
cout<<"\t\tP7";
}
if(eieght==sub[i]&eieght>0&eieght<200)
{
cout<<"\t\tP8";
}
if(nine==sub[i]&nine>0&nine<200)
{
cout<<"\t\tP9";
}
cout<<"\t\t"<<sub[i]<<"\n\n";
}
}
for(int i=0;i<=8;i++)
{
if(one==sub[i])
{
one=one-cut;
}
if(two==sub[i])
{
two=two-cut;
}
if(three==sub[i])
{
three=three-cut;
}
if(four==sub[i])
{
four=four-cut;
}
if(five==sub[i])
{
five=five-cut;
}
if(six==sub[i])
{
six=six-cut;
}
if(seven==sub[i])
{
seven=seven-cut;
}
if(eieght==sub[i])
{
eieght=eieght-cut;
}
if(nine==sub[i])
{
nine=nine-cut;
}
}
for(int i=0;i<=8;i++)
{
sub[i]= sub[i]-cut;
}
for(int i=0;i<=8;i++)
{
if(sub[i]<=0)
{
sub[i]=sub[i+1];
sub[i+1]=0;
}
}
if(sub[0]<=0)
{
sub[0]=IO;
}
else if(sub[1]<=0)
{
sub[1]=IO;
}
else if(sub[2]<=0)
{
sub[2]=IO;
}
else if(sub[3]<=0)
{
sub[3]=IO;
}
You are accessing outside your array here:
for(int i=0;i<=8;i++)
{
if(sub[i]<=0)
{
sub[i]=sub[i+1]; //^^if i =8, then i +1 = 9, array index out of bound
sub[i+1]=0; //^^same as above
}
}