发布时间:2019-07-29 18:23:33
给一下源代码,并加以注释,谢谢
参考以下程序:(很基础的程序)
#include <conio.h>#include <stdio.h>#include <stdlib.h>int main(){int pw;char c;
system("cls");/*清屏*/
printf("\t\t\t\t--------------\n");printf("\t\t\t\t▏请输入密码:▏\n");printf("\t\t\t\t--------------\n");pw=0;while((c=getch())!='\r'){
printf("*");pw=pw*10+c-'0';}if (pw != 123) //密码为123,可以修改 printf("\n对不起,密码错误!\n"); else { system("cls");/*清屏*/ system("type test.txt"); //test.txt为你要显示的文件名 } getchar(); return 0;}
#include <iostream>#include <conio.h>/*** 秘密在于conio.h中的getch()从键盘中读取字符时,并不会在屏幕上输出已经输入的字符,* 而用一个putch('*')来哄骗,代表已经输入一个字符* 怪不得这个头文件要叫conio.h, con的意思就有哄骗,看来就是由此而来.*/using namespace std;int main() {char* password;char* passwordConfirm;int length = 4;password = new char[length + 1];passwordConfirm = new char[length + 1];char* p = NULL;int count = 0;cout << "Input password : ";p = password;count = 0;//fflush(stdin);while (((*p = getch()) != 13) && count < length) {// 这里不是'\n'(10), new line// 而是'\r'(13), reback. 即是按下回车键,好像这个东西是linux的.// 主要是与getch这个函数有关.putch('*');fflush(stdin);p++;count++;}password[count] = '\0';cout << endl << "Confirm the password : ";p = passwordConfirm;count = 0;//fflush(stdin);while (((*p = getch()) != 13) && count < length) {putch('*');fflush(stdin);p++;count++;}passwordConfirm[count] = '\0';cout << endl;if (strcmp(password, passwordConfirm) == 0) {cout << "The password is right." << endl;cout << password << endl;} else {cout << "Confirm password fail." << endl;cout << password << endl << passwordConfirm << endl;}return 0;}
你可以百度