PDA

View Full Version : مشکل در برنامه کلمات نقره ای



Alireza_1387
شنبه 04 شهریور 1391, 12:02 عصر
این برنامه ابتدا یک عدد را می گیرد ، که تعداد کلمات ورودی را برنامه می خواهد بگیرد رامشخص می کند
سپس کلمات را می گیرد.اگر حرفی از کلمه ای از دو طرف به یک اندازه فاصله داشت حروف نقره ای نامیده می شود.
مانند Ali ، حرف L حرف نقره ای می باشد ، پس تعداد حروف رشته و کلمه ی نقره ای را چاپ می کند
اگر هم مانند sara ، که حرف نقره ای ندارد تعداد حروف رشته و دو حرف ar را چاپ می کند.
مشکل این است که برنامه تعداد کلمات ورودی و خود کلمات ورودی را می گیرد اما در تابع diag گیر می کند و چیزی در خروجی دیده نمی شود.

ورودی :
2
jha
jklh
خروجی :
3h
4kl
برنامه به شرح زیر است :



********************************************
This program in step function 'diag'
make pause.
Why ? I donnt know!!!
I will solve this problem in future:D.
********************************************/
#include <iostream.h>
#include <conio.h>
class silverword{
private:
char a[40][40] , count[40] ;
public:
silverword(){ count[40] = 0 ;} ;
int getAmount();
void getInput(int) ;
void diag( int ) ;
void output( int ) ;
};
void main()
{
clrscr() ;
class silverword a ;
int re = a.getAmount() ;
a.getInput(re) ;
a.diag( re ) ;
a.output( re) ;
getch() ;

}
////////////////////////////////////////////////
////////////////////////////////////////////////
int silverword:: getAmount()
{
int amount ;
cin >> amount ;
return amount ;
}
////////////////////////////////////////////////
////////////////////////////////////////////////
void silverword::getInput( int amount )
{
for(int i = 0 ; i < amount ; i++ )
{
cin >> a[i] ;
cout << endl ;
}
}
//////////////////////////////////////////////
//////////////////////////////////////////////
/* In this step we have pause problem*/
void silverword::diag(int s)
{
for ( int i = 0 ; i < s ; i++ )
for( int j = 0 ; j < 40 ; j++ )
while( a[i][j] != '\0')
count[i]++ ;
}
////////////////////////////////////////////
///////////////////////////////////////////
void silverword::output( int z )
{
for( int i = 0 ; i < z ; i++ )
{
if( count[i]%2 == 0 )
{
int even = (count[i]/2) ;
cout << count[i] << a[even] << *(a[even]+1) << endl ;
}
else
{
int odd = (count[i]/2)+1 ;
cout << a[odd] ;
}
}//end for
}//end output
////////////////////////////////////////
////////////////////////////////////////

rezaricky
شنبه 04 شهریور 1391, 17:08 عصر
کدت رو اصلاح کردم
#include <iostream>
#include <conio.h>
using namespace std ;

class silverword
{
private:
char a[40][40];
int count[40];
public:
silverword()
{
for ( int i = 0 ; i < 40 ; i++ )
count[i] = 0 ;

for ( int i = 0 ; i < 40 ; i++ )
for( int j = 0 ; j < 40 ; j++ )
a[i][j] = 0 ;

}
int getAmount();
void getInput(int) ;
void diag( int ) ;
void output( int ) ;
};

int silverword:: getAmount()
{
int amount ;
cin >> amount ;
return amount ;
}

void silverword::getInput( int amount )
{
for(int i = 0 ; i < amount ; i++ )
{
cin >> a[i] ;
cout << endl ;
}
}

void silverword::diag(int s)
{
for ( int i = 0 ; i < s ; i++ )
for( int j = 0 ; j < 40 ; j++ )
if (a[i][j] != '\0')
count[i]++ ;
}

void silverword::output( int z )
{
for( int i = 0 ; i < z ; i++ )
{
if( (count[i]%2) == 0 )
{
int even = (count[i]/2) ;
cout << count[i] << a[i][even-1] << a[i][even] << endl ;
}
else
{
int odd = (count[i]/2) ;
cout <<count[i]<< a[i][odd] ;
}
}//end for
}//end output


int main()
{

silverword a ;
int re = a.getAmount() ;
a.getInput(re) ;
a.diag( re ) ;
a.output( re) ;
getch() ;

return 0 ;

}

Alireza_1387
سه شنبه 07 شهریور 1391, 13:21 عصر
پس سازنده اشکال داشت
class silverword{
private:
char a[40][40] , count[40] ;
public:
silverword(){ count[40] = 0 ;} ;class silverword{

به این صورت اصلاح شد
public:
silverword()
{
for ( int i = 0 ; i < 40 ; i++ )
count[i] = 0 ;

for ( int i = 0 ; i < 40 ; i++ )
for( int j = 0 ; j < 40 ; j++ )
a[i][j] = 0 ;

}

rezaricky
پنج شنبه 09 شهریور 1391, 21:06 عصر
مشکل بخاطر حلقه while داخل متد dialog بود