PDA

View Full Version : confusion city



Petr_Kropotkin
3rd February 2010, 00:07
I created a driver for a widget and it runs perfectly, but I traslate it to Qt it does not work.
Here is the driver program:



void rule(int digit,int x, int place[])
{

int n,flag=0;
if(x % 2>0)
n=floor(x/2);
else
n=x/2;

for(int i=0;i<n;++i)
if (place[i]==place[n-i])
flag++;
if (flag==n)
std::cout <<" "<<digit<<" is a palindrome";
else
std::cout<<" " <<digit<<" is not a palindrome";



}


int pow(int a ,int b)
{
int i=1,power=1;
while(i<b)
{
power *=a;
++i;

}
return power;


}


int main()
{

int rem[10],digit,n,x=1,place[10];
std::cout << "\nEnter another number :";
std::cin>>digit;
n= sizeof digit;
std::cout<<"\nn= "<< n;
rem[0]= digit % pow(10,n);
place[0]= (digit-rem[0])/pow(10,n);
while( x<n)
{
rem[x]=rem[x-1] % pow(10,n-x);
place[x]=(rem[x-1]-rem[x])/pow(10,n-x);

x++;
}
rule(digit,x,place);

system("Pause");

}


When run it Qt any number typed in states it is not a palindrome, but in the driver it worls
Here is the Qt version



#include "ui_palindrome.h"
#include "palindrome.h"
#include <QString>
#include <QtGui>
#include <QLineEdit>
#include <QPushButton>
#include <QSize>


void rule(int x, int place[],QLineEdit *lineEdit)
{
QString msg=lineEdit->text()+" is a palindrome";
QString msg2=lineEdit->text()+" is not a palindrome";
QMessageBox msgbox;
int n,flag=0;

if(x % 2>0)
n=floor(x/2);
else
n=x/2;


for(int i=0;i<n;++i)
if (place[i]==place[n-i])
flag++;
if (flag==n)

msgbox.setText(msg);
else
msgbox.setText(msg2);

msgbox.exec();

}





int pow(int a ,int b)
{
int i=1,power=1;
while(i<b)
{
power *=a;
++i;
}
return power;

}



palindrome::palindrome(QWidget *parent) :
QDialog(parent)

{
setupUi(this);
connect(checkButton, SIGNAL(clicked(bool)), this, SLOT(palindrome_checker()));
}


palindrome::~palindrome()
{
delete this;
}



void palindrome::palindrome_checker()
{
int remainder[10],place[10],digit,x=1,m=0;

digit=lineEdit->text().toInt();
m=lineEdit->text().size();
remainder[0]= digit % pow(10,m);
place[0]= (digit-remainder[0])/pow(10,m);
m=lineEdit->text().size();
while(x<m)
{
remainder[x]=remainder[x-1] % pow(10,m-x);
place[x]=(remainder[x-1]-remainder[x])/pow(10,m-x);
++x;
}

rule(x, place,lineEdit );

}


I posted problems with this widge & I thought I was, as far interface goes it reacts when a number is put in.
I think the problem might be


m=lineEdit->text().size();

but according to the documentation size() returns the size() of lineEdit->text() as an int.
Help !

franz
3rd February 2010, 16:30
The sizeof statement in your original program doesn't make sense. sizeof int is always 4 (depending on the system). I've tested your original program and it rules wrongly on 45654 and 4554. Revise your algorithm.

On a side note, you don't have to do


delete this;

in a destructor. It is already being deleted. Before you jump into using Qt you might really really no really want to learn a thing or two about C++ first.