剑三陆寻能爆?:写个C程序.

来源:百度文库 编辑:高考问答 时间:2024/05/05 02:45:42
要求:
就是要能实现在运行时用户输入的字符显示为*,就像输入密码那样的效果.怎么实现啊?写一个看看.我很菜,加一些注释好理解.
VC里面没有conio.h这个头文件/

/*
* An example of how to obtain unbuffered
* input using the conio.h library and getch()
*/
#include <stdio.h>
#include <conio.h>
#include <ctype.h>

int main()
{
int ch;
char pword[BUFSIZ];
int i = 0;

puts ("Enter your password");
fflush(stdout);

while ((ch = getch()) != EOF
&& ch != '\n'
&& ch != '\r'
&& i < sizeof(pword) - 1)
{
if (ch == '\b' && i > 0)
{
printf("\b \b");
fflush(stdout);
i--;
pword[i] = '\0';
}
else if (isalnum(ch))
{
putchar('*');
pword[i++] = (char)ch;
}
}

pword[i] = '\0';

printf ("\nYou entered >%s<", pword);

return 0;
}

/*
* Program output
Enter your password
********
You entered >security<

*
*/

#include <stdio.h>
#include <math.h>
main()
{
int ch;
while(1)
{
ch=getch();
if(ch==0x1b)break; /*按Esc键退出*/
else printf("*");
}
}