标题:bool值参数传入
只看楼主
fishviv
Rank: 1
等 级:新手上路
帖 子:45
专家分:9
注 册:2010-9-10
结帖率:90.91%
 问题点数:0 回复次数:0 
bool值参数传入
函数原型:void fill(bool name[] ,int num , bool tf) 传入参数时候,给tf传入true或者false不对,传入1或者0 也不对,why?
   tf的传入值是 1'f(类似f的东西)',但是name[i] = tf;之后,name的值一直不变
程序代码:
#include<iostream>
using namespace std;
#define num_queens 8
#define zero  0
#define seven ( num_queens - 1)
#define fif ( 2 * num_queens - 1)

bool column_free [num_queens];
bool diag_free [fif];
bool inv_diag_free [fif];
bool check(int i , int j)
    {return column_free[j] && diag_free[i + j] && inv_diag_free [seven + i + j];}
void set_queen(int i , int j)
    { column_free[j] = diag_free[i + j] = inv_diag_free [seven + i + j]=false;}
void remove_queen(int i , int j)
    { column_free[j] = diag_free[i + j] = inv_diag_free [seven + i + j]=true;}
void fill(bool name[] ,int num , bool tf)
    {    for(int i = 0; i < num; ++i)
         name[i] = tf;
    }
class queenclass{
public:
    queenclass()
    {for(int i=0;i<num_queens; ++i)queen[i]=0;}
    ~queenclass(){;}
    void queenchange(int i)
    {queen[0] = i;}
    void pop() { queen[size()-1] = 0;}
    void push(int j){queen[size()]= j;}
    bool empty()
    {    if(0 == size())
          return true;
        else
          return false;
    }
    int size()
    {   int i = 0;
        while(queen[i] != 0)
        {++i;}
        return i;
    }
    int low(){return queen[0];}
    int top(){return queen[size()-1];}
    void write()
    {    for(int i = size() ; i >=0; --i)
             cout<<i<<'.'<<queen[i]<<"->";
        cout<<'\b'<<endl;
    }
private:
     int queen[num_queens];
};
int main()
{   queenclass  q;
    fill(column_free ,num_queens,true) ;
    fill(diag_free, fif,true);
    fill(inv_diag_free, fif,true);
    while(q.size()<= num_queens)
    {    int j;
        for( j = zero;!check(q.size(), j); ++j)
        {    while(j >= seven)
            {    if(q.empty())
                    return false;
                j=q.top();
                q.pop();
                remove_queen(q.size(), j);
            }
        }
        set_queen(q.size(),j);
        q.push(j);
        if(q.size() == num_queens)
        {    cout<<"one of the way is"<<endl;
            q.write();
            fill(column_free ,num_queens,true) ;
            fill(diag_free, fif,true);
            fill(inv_diag_free, fif,true);
            q.queenchange(q.low()+1);       
        }
    }
        return 0;
}
搜索更多相关主题的帖子: bool 参数 
2010-10-22 23:16



参与讨论请移步原网站贴子:https://bbs.bccn.net/thread-323714-1-1.html




关于我们 | 广告合作 | 编程中国 | 清除Cookies | TOP | 手机版

编程中国 版权所有,并保留所有权利。
Powered by Discuz, Processed in 0.707081 second(s), 7 queries.
Copyright©2004-2024, BCCN.NET, All Rights Reserved