晒晒自己写的C++小程序(初学,书上的题目)

1.  骑士周游问题

Qishi.h

#ifndef QISHI_H

#define QISHI_H

 

#include <iostream>

using std::ostream;

 

class Qishi

{

     friend ostream & operator<<( ostream &, Qishi & );

 

public:

     Qishi( int = 0, int = 0 );

     int counter;

     int getCounter();

 

private:

     static int qipan[ 8 ][ 8 ];

     const static int move1[ 8 ];

     const static int move2[ 8 ];

 

     bool judge( int ) const;

     bool judgeRange( int ) const;

 

     void move();

     void suiji();

     void print( int ) const;

     void sethl( int, int );

 

     int hang;

     int lie;

     int count;   

};

 

#endif 

 

 

Qishi.cpp

#include <iostream>

using std::cout;

using std::endl;

 

#include <cstdlib>

using std::rand;

using std::srand;

 

#include <ctime>

using std::time;

 

#include "Qishi.h"

 

int Qishi::qipan[ 8 ][ 8 ] = { 0 };

const int Qishi::move1[ 8 ] = { 1, 2, 2, 1, -1, -2, -2, -1 };

const int Qishi::move2[ 8 ] = { -2, -1, 1, 2, 2, 1, -1, -2 };

 

Qishi::Qishi( int h, int l )

{

     sethl( h, l );

     count = 1;   

 

     srand( time( 0 ) );

 

/*

    // 封闭周期

     do

     {

         counter = 0;

         count = 1;

         while( count <= 64 )

        {

             move();

        }

 

         for( int i = 0; i < 8; i++ )     // 注意重新赋值

         {

              for( int j = 0; j < 8; j++ )

              {

                   qipan[ i ][ j ] = 0;

              }

         }

 

      }while( getCounter() != 15 );

*/

 

     while( count <= 64 )

    {

         move();

    }

}

 

void

Qishi::sethl( int i, int j )

{

     if( i < 0 || i > 7 )

     {

         cout << "你输入了错误行!" << "我们将将其设置为" << endl;

         hang = 0;

     }

     else

     {

         hang = i;

     }

 

     if( j < 0 || j > 7 )

     {

         cout << "你输入了错误列!" << "我们将将其设置为" << endl;

         lie = 0;

     }

     else

     {

         lie = j;

     }

}

 

 

void

Qishi::move()

{

     const int temp = 8;

     int j = 0;

 

     for( int i = 0; i < temp; i++ )

     {

         if( judge( i ) )

         {

              print( i );

              hang = hang + move1[ i ];

             lie = lie + move2[ i ];

 

              qipan[ hang ][ lie ] = count;

              ++count;

              break;

           

         }

         j++;

     }

 

     // 如果种方案没有一种可行,那么随机跳跃;

    if( j > 7 )

     {

        suiji();

         ++counter;

    }   

    

}

 

// cons成员函数不能调用非const成员函数

bool

Qishi::judge( int ju ) const

{

     // 注意这个程序里棋盘的两个下标

     if( judgeRange( ju ) && qipan[ hang + move1[ ju ] ][ lie + move2[ ju ] ] == 0 )

     {

         return true;

     }

     else

     {

         return false;

     }

}

 

bool

Qishi::judgeRange( int jr ) const

{

     int tempHang = hang + move1[ jr ];

     int tempLie = lie + move2[ jr ];

 

     if( ( tempHang >= 0 ) && ( tempHang < 8 ) && ( tempLie >= 0 ) && ( tempLie < 8 ) )

     {

         return true;

     }

 

     else

     {

         return false;

     }

}

 

void

Qishi::print( int pr ) const

{

     if( move1[ pr ] > 0 && move2[ pr ] < 0)

     {

         cout << "向右移" << move1[ pr ] << ""

               << "向上移" << -move2[ pr ] << ""

               << "至:" << hang + move1[ pr ]

              << "  "<< lie + move2[ pr ] << endl;

     }

 

     if( move1[ pr ] > 0 && move2[ pr ] > 0)

     {

         cout << "向右移" << move1[ pr ] << ""

               << "向下移" << move2[ pr ] << ""

               << "至:" << hang + move1[ pr ]

              << "  "<< lie + move2[ pr ] << endl;

     }

 

     if( move1[ pr ] < 0 && move2[ pr ] > 0)

     {

         cout << "向左移" << -move1[ pr ] << ""

               << "向下移" << move2[ pr ] << ""

               << "至:" << hang + move1[ pr ]

              << "  "<< lie + move2[ pr ] << endl;

     }

 

     if( move1[ pr ] < 0 && move2[ pr ] < 0)

     {

         cout << "向左移" << -move1[ pr ] << ""

               << "向上移" << -move2[ pr ] << ""

               << "至:" << hang + move1[ pr ]

              << "  "<< lie + move2[ pr ] << endl;

     }

}

 

void

Qishi::suiji()

{

     int tempRand;        // 注意这里的范围

 

     do

     {

         tempRand = rand() % 8;

     } while( !judgeRange( tempRand ) );

 

     hang = hang + move1[ tempRand ];

     lie = lie + move2[ tempRand ];

 

     cout << "随机跳跃到" << hang << "  "<< lie << endl;

}

 

ostream  & operator<<( ostream & output, Qishi & qi )    // 不需要再有ostream的声明

{

     int i = 1;

     while( i <= 64 )

     {

         for( int m = 0; m < 8; m++ )

         {

              for( int n = 0; n < 8; n++ )

              {

                   if( i == qi.qipan[ m ][ n ] )

                   {

                       if( i < 10)

                       {

                            output << "" << " " << i << " 个跳到" << "qipan[ "

                                   << m << " ][ " << n << " ]" << endl;

                       }

                       else

                       {

                            output << "" << i << " 个跳到" << "qipan[ "

                                   << m << " ][ " << n << " ]" << endl;

                       }

                   }

              }

         }

 

         i++;

     }

 

     return output;

}

 

int

Qishi::getCounter()

{

     return counter;

}

 

 

 

Main.cpp

#include <iostream>

using std::cout;

using std::endl;

using std::cin;

 

#include "Qishi.h"

 

int main()

{

     int hang, lie;

 

     cout << "请输入起始行( 0 - 7 )与起始列( 0 - 7 )" << endl;

     cin >> hang >> lie;

 

     Qishi qishi( hang, lie );

 

     cout << qishi << endl;

 

     return 0;

}

 

/*

    * 本程序遗留的问题,关于setfill;

*/

 

 

 

 

 

2. 汉诺塔递归问题

Recure.h

class Recure

{

public:

     Recure( int, char, char, char );

     void recursion( int, char, char, char);

    void print( char, char );

     Recure & setmaxs( int, int );    // 可以没有& 吗?可以,只是对象不同(不引用则为临时对象)

     int maxs();

     void setReMaxs( int, int );

     int reMaxs( int, int );

 

private:

     char a;

     char b;

     char c;

     int num;

     int maxs1;

     int maxs2;

};

 

 

Recure.cpp

#include <iostream>

using std::cout;

using std::endl;

 

#include "Recur.h"

 

Recure::Recure( int number, char c1, char c2, char c3 )

{

     num = number;

     a = c1;

     b = c2;

     c = c3;

     recursion( num, a, b, c );

}

 

void

Recure::recursion( int number, char c1, char c2, char c3)

{

     if( number == 1 )

     {

         print( c1, c3 );

     }

     else

     {

         recursion( number - 1 , c1,  c3,  c2 );

         print( c1, c3 );

         recursion( number - 1 , c2,  c1,  c3 );

     }

}

 

void

Recure::print( char m, char n )

{

     cout << "/"" << m << "/"" << "--->"

          << "/"" << n << "/"" << endl;

}

 

Recure &

Recure::setmaxs( int m, int n )

{

     maxs1 = m;

     maxs2 = n;

     return *this;

}

 

int

Recure::maxs()

{

     int temp;

     if( maxs1 >= maxs2 )

     {

         for( int i = 1; i <= maxs2; i++ )

         {

              if( maxs1 % i ==0 && maxs2 % i ==0 )

              {

                   temp = i;

              }

         }

     }

     else

     {

         for( int i = 1; i <= maxs1; i++ )

         {

              if( maxs1 % i ==0 && maxs2 % i ==0 )

              {

                   temp = i;

              }

         }

     }

 

     return temp;

}

 

void

Recure::setReMaxs( int m, int n )

{

     if( m <= n )

     {

         cout << reMaxs( m, n % m ) << endl;

     }

     else

     {

         cout << reMaxs( n, m % n ) << endl;

     }

}

 

int

Recure::reMaxs( int temp1, int temp2 )

{

     if( temp2 == 0 )

     {

         return temp1;

     }

     else

     {

         reMaxs( temp2, temp1 % temp2 );

     }

}

 

 

Digui.cpp

#include <iostream>

using std::cout;

using std::cin;

using std::endl;

 

#include "Recur.h"

 

int main()

{

     int numOfDisks;

     cout << "Please enter a number:" << endl;

     cin >> numOfDisks;

 

     Recure re( numOfDisks, 'A', 'B', 'C' );

    

     int num1, num2;

     cout << "Please enter two numbers to calculate"

          << " their common divisor" << endl;

 

     cin >> num1 >> num2;

     cout << re.setmaxs( num1, num2 ).maxs() << endl;

 

     re.setReMaxs( num1, num2 );

 

     return 0;

}

 

 

 

 

 

3. 机场客户代码

Kehu.h

class Kehu

{

public:

     Kehu( int );

     ~Kehu();

    static int number1;

     static int number2;

 

private:

     static int size[11];

     void setNum( int );

    void judgeNum( int );

};

 

 

Kehu.cpp

#include <iostream>

using std::endl;

using std::cout;

using std::cin;

 

#include "Kehu.h"

 

int Kehu::number1 = 1;            // 注意加作用域

int Kehu::number2 = 6;

 

int Kehu::size[11] = { 0, 1, 2, 3, 4, 5, 1, 2, 3, 4, 5 };

 

Kehu::Kehu( int n )

{

     judgeNum( n );

}

 

Kehu::~Kehu()

{

     // empty;

}

 

void

Kehu::judgeNum( int num )

{

     if( num == 1 )

     {

         int temp;

         if( number1 <= 5 )

         {

              setNum( num );

         }

         else

         {

              cout << "头等舱已满,您是否愿意到普通舱?" << endl;

             cout << "如果您同意,那么请输入1/n"

                   << "如果您不同意,请输入2/n" << endl;

             

             cin >> temp;

 

             if( temp == 1)

             {

                  setNum( number2 );

             }

             else

             {

                  cout << "请您耐心等待小时后的航班,"

                        << "我们对您带来的不便表示抱歉/n" << endl;

             }

         }

     }

     else

     {

         int temp;

         if( number2 <= 10 )

         {

              setNum( num );

         }

         else

         {

              cout << "普通舱已满,您是否愿意到头等舱?" << endl;

             cout << "如果您同意,那么请输入1/n"

                   << "如果您不同意,请输入2" << endl;

             

             cin >> temp;

 

             if( temp == 1)

              {

                  setNum( number1 );

             }

             else

             {

                  cout << "请您耐心等待小时后的航班,"

                        << "我们对您带来的不便表示抱歉" << endl;

             }

         }

     }

}

 

void

Kehu::setNum( int n )

{

     if( n == 1 )

     {

         cout << "您的座位是头等舱的"

               << size[ number1 ] << " 号座位/n" << endl;

 

         number1++;

     }

     else

     {

         cout << "您的座位是普通舱的"

               << size[ number2 ] << " 号座位/n" << endl;

 

         number2++;

     }

}

 

 

 

Jichang.cpp

#include <iostream>

using std::cout;

using std::endl;

using std::cin;

 

#include "Kehu.h"

 

int main()

{

     int choose;

 

     while( ( Kehu::number1 + Kehu::number2 ) <= 16 )

     {

         cout << "请输入你要选择的仓号:/n"

              << "头等舱:1/n普通舱:2" << endl;

         cin >> choose;

 

         Kehu ke( choose );

     }

    

     return 0;

}

 

 

4. 八皇后问题

Bahou.h

 

#ifndef BAHOU_H

#define BAHOU_H

 

#include <iostream>

using std::ostream;

 

class Bahou

{

     friend ostream & operator<<( ostream &, Bahou & );

 

public:

     Bahou( int = 0, int = 0 );

    

private:

     const static int hangconst = 8;

     const static int lieconst = 8;

     const static int fang = 2;

 

     int hang;

     int lie;

     int judgement;

 

     static int qipan[ hangconst ][ lieconst ];

     static int jie[ hangconst ][ fang ];

 

     void chooseFirst();

     void put();

     void change( int, int );

     void solve( int, int );

 

     bool range( int );

     bool full();

};

 

#endif

 

 

 

Bahou.cpp

 

#include <iostream>

using std::cout;

using std::endl;

 

#include <cstdlib>

using std::rand;

using std::srand;

 

#include <ctime>

using std::time;

 

#include "Bahou.h"

 

int Bahou::qipan[ hangconst ][ lieconst ] = { 0 };

int Bahou::jie[ hangconst ][ fang ] = { 0 };

 

Bahou::Bahou( int h, int l )

{

     srand( time( 0 ) );

     judgement = 0;

 

     if( h == 0 && l ==0 )

     {

         chooseFirst();

     }

     else

     {

         hang = h;

         lie = l;

     }

 

     do

     {

         judgement = 0;

         put();

 

         for( int i =0; i < hangconst; i++ )

         {

              for( int j = 0; j < lieconst; j++ )

              {

                   qipan[ i ][ j ] = 0;

              }

         }

     }   while(judgement != 8 );

}

 

void

Bahou::put()

{

     for( int i = 0; i < 8; i++ )

     {

         if( qipan[ hang ][ lie ] == 0 )

         {

              jie[ i ][ 0 ] = hang;

              jie[ i ][ 1 ] = lie;

              judgement++;

         }

         else

         {

              // enpty, can be moved?

         }

 

         change( hang, lie );

     }

}

 

void

Bahou::change( int h, int l )

{

     solve( h, l );

 

     int i = rand() % 8;

     int j = rand() % 8;

 

     while( qipan[ i ][ j ] != 0 )       //gweg

     {

         i = rand() % 8;

         j = rand() % 8;        

         if( full() )

         {

              break;

         }

     }   

 

     hang = i;    

     lie = j;     

}

 

void

Bahou::chooseFirst()

{

     hang = rand() % 8;

     lie = rand() % 8;

}

 

void

Bahou::solve( int sh, int sl )

{

     for( int i = 0; i < lieconst; i++ )

     {

         qipan[ sh ][ i ] = 1;

     }

 

     for( int i = 0; i < hangconst; i++ )

     {

         qipan[ i ][ sl ] = 1;

     }

 

     for( int i = -hangconst; i < hangconst; i++ )

     {

         if( range( sh + i) && range( sl + i) )

         {

              qipan[ sh + i ][ sl + i ] = 1;

         }

         if( range( sh + i) && range( sl - i) )

         {

              qipan[ sh + i ][ sl - i ] = 1;

         }

     }

 

}

 

bool

Bahou::range( int ran )

{

     if( ran >= 0 && ran < 8 )

     {

         return true;

     }

     else

     {

         return false;

     }

}

 

bool

Bahou::full()

{

     int count = 0;

     for( int i = 0; i < hangconst; i++ )

     {

         for( int j = 0; j < lieconst; j++ )

         {

              if( qipan[ i ][ j ] != 0 )

              {

                   count++;

              }

         }

     }

 

     if( count == 64 )

     {

         return true;

     }

 

     else

     {

         return false;

     }

}

 

ostream & operator<<( ostream & output, Bahou & ba )

{

     for( int i = 0; i < ba.hangconst; i++ ) 

     {

         output << ba.jie[ i ][ 0 ] + 1 << "  " << ba.jie[ i ][ 1 ] + 1 << endl;

     }

     return output;

}

 

 

 

Bahh.cpp

 

#include <iostream>

using std::cout;

using std::cin;

using std::endl;

 

#include "Bahou.h"

 

int main()

{

     Bahou ba;       // 不要有();

     cout << ba;

     return 0;

}

 

 

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 3
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论 3
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值