看看你windows程序设计是什么级别 ,/
实现一个类似QQ号登录的效果 :程序输入一个:NC, 用 * 遮盖
程序输出一个: NC
Enter your password:
**
NC
[ 本帖最后由 BlueGuy 于 2010-10-23 15:56 编辑 ]
程序代码:/* 处理按退格键的情况 */
void back_spasce( int * words , int * sousor , char * i_w)
{
if ( *words )
{
if ( *words + 1 == *sousor ) DEL_STAR ;
else
{
int right = *words - *sousor , /* 移动到最后一个星星的次数 */
left = right + 2 ; /* 移动到退格后的位置的次数 */
while ( right-- ) STAR ; /* 向右输出星星覆盖 */
putchar('\0') ; /* 消失掉最后一个星星 */
while ( left-- ) putchar('\b') ; /* 向左移动光标 */
right = *sousor - 1 , /* 被退掉的字符的前一个字符的位置 */
left = right - 1 ; /* 被退掉的字符的位置 */
/* 将密码字符从后向前一个个的移动 */
while ( ( *( i_w + left++ ) = *( i_w + right++ ) ) != 0 ) ;
}
--*words ; --*sousor ; /* 更新字符数和光标的位置 */
}
return ;
}
/* 处理按移动键的情况 */
void move_sousor( int * words , int * sousor )
{
#define LEFT 75 /* ← */
#define HOME 71 /* HOME */
#define RIGHT 77 /* → */
#define END 79 /* END */
char c = getch() ;
int move ; /* 移动的次数 */
/* 计算向右移动的次数 (按←或home键)*/
move = ( c == LEFT ) ? 1 : ( ( c == HOME ) ? ( *sousor - 1 ) : 0 ) ;
while ( move-- ) { putchar('\b') ; --*sousor ; } /* 输出退格,光标就移动一下 */
/* 计算向右移动的次数 (按→或 end键)*/
move = ( c == RIGHT ) ? ( *words + 1 > *sousor ) : ( ( c == END ) ? ( *words - *sousor + 1 ) : 0 ) ;
while ( move-- ) { putchar('*' ) ; ++*sousor ; } /* 输出星星通过覆盖来移动 */
return ;
}
程序代码:#include <stdio.h> /* USE I/O & FILE */
#include <stdlib.h> /* USE CLS */
#include <conio.h> /* USE getch */
#define LEN 40 /* The length of ID or WORD */
#define STAR putchar('*')
#define DEL_STAR printf("%c%c%c",'\b','\0','\b')
void getword( char * w , int len ) ;
void back_spasce( int * words , int * sousor , char * i_w) ; /* 处理按退格键的情况 */
void move_sousor( int * words , int * sousor ) ; /* 处理按移动键的情况 */
int main(void)
{
char words[LEN+1] = {'\0'} ;
printf ("INPUT WORD:\n\n") ;
getword( words, LEN+1 ) ;
printf ( "\n%s\n" , words );
return EXIT_SUCCESS ;
}
/* Read the WORD string */
void getword( char * w, int len )
{
char c ; /* Use to read the in-char */
int words = 0 ; /* Statistics characters (统计字符的数目) */
int sousor = words + 1 ; /* 光标的位置 */
while( ( c = getch() ) != '\r' && words<len ) /* Read character into in_id */
{
if ( c == '\b' ) { back_spasce( &words , &sousor , w) ; continue ; } /* 处理按退 */
if ( c == -32 ) { move_sousor( &words , &sousor ) ; continue ; } /* 处理按移 */
if ( !c ) continue ; /* 处理按移其他无用键 */
if ( words < LEN ) *(w + words ) = c ; /* 在允许长度内存放密码 */
STAR ; ++sousor ; ++words ;
}
*(w + words ) = '\0' ;
}
/* 处理按退格键的情况 */
void back_spasce( int * words , int * sousor , char * i_w)
{
if ( *words )
{
if ( *words + 1 == *sousor ) DEL_STAR ;
else
{
int right = *words - *sousor , /* 移动到最后一个星星的次数 */
left = right + 2 ; /* 移动到退格后的位置的次数 */
while ( right-- ) STAR ; /* 向右输出星星覆盖 */
putchar('\0') ; /* 消失掉最后一个星星 */
while ( left-- ) putchar('\b') ; /* 向左移动光标 */
right = *sousor - 1 , /* 被退掉的字符的前一个字符的位置 */
left = right - 1 ; /* 被退掉的字符的位置 */
/* 将密码字符从后向前一个个的移动 */
while ( ( *( i_w + left++ ) = *( i_w + right++ ) ) != 0 ) ;
}
--*words ; --*sousor ; /* 更新字符数和光标的位置 */
}
}
/* 处理按移动键的情况 */
void move_sousor( int * words , int * sousor )
{
#define LEFT 75 /* ← */
#define HOME 71 /* HOME */
#define RIGHT 77 /* → */
#define END 79 /* END */
char c = getch() ;
int move ; /* 移动的次数 */
/* 计算向右移动的次数 (按←或home键)*/
move = ( c == LEFT ) ? 1 : ( ( c == HOME ) ? ( *sousor - 1 ) : 0 ) ;
while ( move-- ) { putchar('\b') ; --*sousor ; } /* 输出退格,光标就移动一下 */
/* 计算向右移动的次数 (按→或 end键)*/
move = ( c == RIGHT ) ? ( *words + 1 > *sousor ) : ( ( c == END ) ? ( *words - *sousor + 1 ) : 0 ) ;
while ( move-- ) { putchar('*' ) ; ++*sousor ; } /* 输出星星通过覆盖来移动 */
}