VC中判断是否数字的方法

方法一:

int IsNum(CString str)
{
 
if (str.IsEmpty())
   
return  - 1;
 
int nDot = 0;
 
//数值只能是0到9及小数点组成
 
for (int i = 0; i < str.GetLength(); i++)
  {
   
char ch = str.GetAt(i);
   
if ('.' == ch)
   
//小数点
   
{
     
nDot++;
     
continue;
    }
   
if (ch >= '0' && ch <= '9')
   
//数字
     
continue;
   
return  - 2; //非法字符
 
}
 
if (nDot > 1)
   
return  - 3;
 
//小数点多于两个
 
else if (0 == nDot)
   
return 1;
 
//整数
 
else if (1 == nDot)
   
return 0;
 
//浮点数
 
return  - 1000; //未知错误
}

 

方法二:

bool IsNumber( LPCTSTR pszText )
{
 ASSERT_VALID_STRING( pszText );

 for( int i = 0; i < lstrlen( pszText ); i++ )
  if( !_istdigit( pszText[ i ] ) )
   return false;

 return true;
}

 

VC中判断是日期的方法

bool IsDate( LPCTSTR pszText )
{
 ASSERT_VALID_STRING( pszText );

 // format should be 99/99/9999.

 if( lstrlen( pszText ) != 10 )
  return false;

 return _istdigit( pszText[ 0 ] )
  && _istdigit( pszText[ 1 ] )
  && pszText[ 2 ] == _T('/')
  && _istdigit( pszText[ 3 ] )
  && _istdigit( pszText[ 4 ] )
  && pszText[ 5 ] == _T('/')
  && _istdigit( pszText[ 6 ] )
  && _istdigit( pszText[ 7 ] )
  && _istdigit( pszText[ 8 ] )
  && _istdigit( pszText[ 9 ] );
}

 

以下是数字和日期的比较大小

int NumberCompare( LPCTSTR pszNumber1, LPCTSTR pszNumber2 )
{
 ASSERT_VALID_STRING( pszNumber1 );
 ASSERT_VALID_STRING( pszNumber2 );

 const int iNumber1 = atoi( pszNumber1 );
 const int iNumber2 = atoi( pszNumber2 );

 if( iNumber1 < iNumber2 )
  return -1;
 
 if( iNumber1 > iNumber2 )
  return 1;

 return 0;
}

 

 

int DateCompare( const CString& strDate1, const CString& strDate2 )
{
 const int iYear1 = atoi( strDate1.Mid( 6, 4 ) );
 const int iYear2 = atoi( strDate2.Mid( 6, 4 ) );

 if( iYear1 < iYear2 )
  return -1;

 if( iYear1 > iYear2 )
  return 1;

 const int iMonth1 = atoi( strDate1.Mid( 3, 2 ) );
 const int iMonth2 = atoi( strDate2.Mid( 3, 2 ) );

 if( iMonth1 < iMonth2 )
  return -1;

 if( iMonth1 > iMonth2 )
  return 1;

 const int iDay1 = atoi( strDate1.Mid( 0, 2 ) );
 const int iDay2 = atoi( strDate2.Mid( 0, 2 ) );

 if( iDay1 < iDay2 )
  return -1;

 if( iDay1 > iDay2 )
  return 1;

 return 0;
}

 
  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
以下是一个判断五子棋是否胜利,包括黑棋禁手的 C++ 代码,使用 Visual C++ 编译器进行编译测试: ```cpp #include <iostream> #include <cstring> using namespace std; const int SIZE = 15; int board[SIZE][SIZE] = {0}; // 初始化棋盘为全空 int player = 1; // 当前玩家,1为黑棋,2为白棋 bool blackForbidden = false; // 是否出现黑棋禁手 // 判断在(x,y)位置放棋子后是否有五子连珠 bool checkWin(int x, int y) { int count1 = 0, count2 = 0, count3 = 0, count4 = 0; for (int i = 0; i < 5; i++) { if (x + i < SIZE && board[x + i][y] == player) count1++; else break; } for (int i = 1; i < 5; i++) { if (x - i >= 0 && board[x - i][y] == player) count1++; else break; } for (int i = 0; i < 5; i++) { if (y + i < SIZE && board[x][y + i] == player) count2++; else break; } for (int i = 1; i < 5; i++) { if (y - i >= 0 && board[x][y - i] == player) count2++; else break; } for (int i = 0; i < 5; i++) { if (x + i < SIZE && y + i < SIZE && board[x + i][y + i] == player) count3++; else break; } for (int i = 1; i < 5; i++) { if (x - i >= 0 && y - i >= 0 && board[x - i][y - i] == player) count3++; else break; } for (int i = 0; i < 5; i++) { if (x + i < SIZE && y - i >= 0 && board[x + i][y - i] == player) count4++; else break; } for (int i = 1; i < 5; i++) { if (x - i >= 0 && y + i < SIZE && board[x - i][y + i] == player) count4++; else break; } if (count1 >= 5 || count2 >= 5 || count3 >= 5 || count4 >= 5) return true; return false; } // 判断在(x,y)位置放棋子是否出现黑棋禁手 bool checkForbidden(int x, int y) { if (player == 2) return false; // 白棋没有禁手 if (board[x][y] != 1) return false; // 只有黑棋才可能出现禁手 for (int i = x - 2; i <= x + 2; i++) { for (int j = y - 2; j <= y + 2; j++) { if (i >= 0 && i + 4 < SIZE && j >= 0 && j + 4 < SIZE) { int count1 = 0, count2 = 0, count3 = 0, count4 = 0; for (int k = 0; k < 5; k++) { if (board[i + k][j] == 1) count1++; if (board[i][j + k] == 1) count2++; if (board[i + k][j + k] == 1) count3++; if (board[i + k][j - k] == 1) count4++; } if (count1 == 4 || count2 == 4 || count3 == 4 || count4 == 4) { bool hasEmpty = false, hasWhite = false; for (int k = 0; k < 5; k++) { if (board[i + k][j] == 0) hasEmpty = true; if (board[i + k][j] == 2) hasWhite = true; if (board[i][j + k] == 0) hasEmpty = true; if (board[i][j + k] == 2) hasWhite = true; if (board[i + k][j + k] == 0) hasEmpty = true; if (board[i + k][j + k] == 2) hasWhite = true; if (board[i + k][j - k] == 0) hasEmpty = true; if (board[i + k][j - k] == 2) hasWhite = true; } if (!hasEmpty || hasWhite) { blackForbidden = true; return true; } } } } } return false; } // 显示棋盘 void display() { for (int i = 0; i < SIZE; i++) { for (int j = 0; j < SIZE; j++) { if (board[i][j] == 0) cout << "."; else if (board[i][j] == 1) cout << "X"; else cout << "O"; } cout << endl; } cout << endl; } int main() { while (true) { display(); int x, y; cout << "Player " << player << "'s turn: "; cin >> x >> y; if (board[x][y] != 0) { cout << "Invalid move, try again.\n"; continue; } board[x][y] = player; if (checkWin(x, y)) { cout << "Player " << player << " wins!\n"; break; } if (checkForbidden(x, y)) { cout << "Black has a forbidden hand!\n"; continue; } player = 3 - player; // 切换玩家 } return 0; } ``` 这个程序使用一个二维数组 `board` 来表示棋盘,玩家1和玩家2分别用数字1和2表示。每次玩家落子时,程序会先判断该位置是否已经有棋子,如果已经有则提示无效,让玩家重新输入。然后程序会判断该位置是否出现五子连珠,如果有,则该玩家获胜。如果该位置出现黑棋禁手,则程序会提示禁手并让黑棋玩家重新输入。程序会不断循环让两个玩家落子,直到有一方胜利。
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值