Demo
#include <stdio.h>
#include <stdlib.h>
/**
1.密码: 设置密码 更改密码 输入密码
*/
//读取密码
char *loadPassword(char *filePath);
int main() {
char *file_dirctory = "/Users/pengxiaodong/Desktop/atm.txt";
//保存密码
char *org_password = loadPassword(file_dirctory);
if (org_password == NULL){
//设置密码
printf("请设置密码\n");
}else{
//输入密码
printf("请输入密码\n");
}
return 0;
}
//读取密码
char *loadPassword(char *filePath){
//打开或者创建文件
FILE *fp = fopen(filePath,"a+");
//移动文件指针到开头
fseek(fp,0,SEEK_SET);
//读取内容
char *pwd = NULL;
int index = 0;
while (1){
//从文件读取内容
char c = fgetc(fp);
//判断是不是文件的末尾
if (c == EOF){
break;
}
//判断是不是第一个字符
if (index == 0){
//malloc
pwd = (char *)malloc(1*sizeof(char));
if (pwd == NULL){
exit(EXIT_FAILURE);
}
}else{
char *pTemp = realloc(pwd,(index+1)*sizeof(char));
if (pTemp == NULL){
free(pwd);
exit(EXIT_FAILURE);
}
pwd = pTemp;
}
//保存当前读取的字符
pwd[index] = c;
index++;
}
//返回当前的首地址
return pwd;
}
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <stdbool.h>
/**
1.密码: 设置密码 更改密码 输入密码
*/
//读取密码
char *loadPassword();
//设置密码
void setPassword(char **old);
//检查密码
bool checkPassword(char *old);
//全局变量 (作用域:整个文件main.c
char *file_dirctory = "/Users/pengxiaodong/Desktop/atm.txt";
int main() {
//保存密码
char *org_password = NULL;
org_password = loadPassword();
if (org_password == NULL){
//设置密码
setPassword(&org_password);
}else{
//输入密码
int result = checkPassword(org_password);
if (result == true){
printf("解锁成功!!!\n");
} else{
exit(EXIT_FAILURE);
}
}
return 0;
}
//malloc分配内存空间
char *myMalloc(int count){
char *pTemp = (char *)malloc(count*sizeof(char));
if (pTemp == NULL){
exit(EXIT_FAILURE);
}
return pTemp;
}
//realloc分配内存空间
char *myRealloc(char *old,int count){
char *pTemp = realloc(old,count*sizeof(char));
if (pTemp == NULL){
free(old);
exit(EXIT_FAILURE);
}
return pTemp;
}
//读取密码
char *loadPassword(){
//打开或者创建文件
FILE *fp = fopen(file_dirctory,"a+");
//移动文件指针到开头
fseek(fp,0,SEEK_SET);
//读取内容
char *pwd = NULL;
int index = 0;
while (1){
//从文件读取内容
char c = fgetc(fp);
//判断是不是文件的末尾
if (c == EOF){
break;
}
//判断是不是第一个字符
if (index == 0){
//malloc
pwd = myMalloc(1);
}else{
pwd = myRealloc(pwd,index+1);
}
//保存当前读取的字符
pwd[index] = c;
index++;
}
fclose(fp);
//返回当前的首地址
return pwd;
}
//从终端一个一个字符获取字符串
char *myScanf(){
char *pInput = NULL;
int index = 0;
while (1){
char ch = getchar();
if (ch == '\n'){
break;
}
if (index == 0){
pInput = myMalloc(1);
}else{
pInput = myRealloc(pInput,index+1);
}
pInput[index] = ch;
index++;
}
return pInput;
}
//保存数据
void savePassword(char *pwd){
FILE *fp = fopen(file_dirctory,"w");
fputs(pwd,fp);
fclose(fp);
}
void inputNewPassword(char **old){
printf("请输入新密码:");
//接收用户输入
char *input_pwd = myScanf();
//保存到文件
savePassword(input_pwd);
//改变外部的值
*old = input_pwd;
printf("设置密码成功!!!\n");
}
//设置密码
void setPassword(char **old){
//判断是否有密码
if (*old == NULL){
inputNewPassword(old);
}else{
while(1){
printf("请输入原始密码:");
char *input = myScanf();
int result = strcmp(*old, input);
//需要释放内存空间
free(input);
if (result != 0){
printf("密码错误 ");
}else{
break;
}
}
inputNewPassword(old);
}
}
//检查密码
bool checkPassword(char *old){
int total = 4;
while (1){
printf("请输入密码:");
char *input = myScanf();
int result = strcmp(old,input);
if (result == 0){
return true;
}else{
total--;
if (total == 0){
printf("错误次数过多 请联系管理员!!\n");
return false;
}else{
printf("密码错误 ");
}
}
}
}
完整
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <stdbool.h>
/**
1.密码: 设置密码 更改密码 输⼊密码
*/
//读取密码
char *loadPassword();
//设置密码
void setPassword(char **old);
//检查密码
bool checkPassword(char *old);
//接收⽤户的选择
int chooseOperation(int min, int max);
//显示操作界⾯
void showHome();
//退出
void saveExit(char *pwd);
//查询余额
void showMoney();
//是否继续
void shouldContinue();
//存款
void saveMoney();
//取款
void getMoney();
//全局变量 (作⽤域:整个⽂件main.c
char *fifile_dirctory = "/Users/pengxiaodong/Desktop/atm.txt";
int totalMoney = 10000;
char *org_password = NULL;
int main() {
//保存密码
org_password = loadPassword();
if (org_password == NULL){
//设置密码
setPassword(&org_password); }else{
//输⼊密码
int result = checkPassword(org_password);
if (result == true){//密码正确
while (1) {
//展示主⻚⾯
showHome();
//接收选择
int choice = chooseOperation(1,5);
switch (choice) {
case 1:
setPassword(&org_password);
break;
case 2:
getMoney();
break;
case 3:
saveMoney();
break;
case 4:
showMoney();
break;
case 5:
saveExit(org_password);
break;
}
shouldContinue();
}
} else{
exit(EXIT_FAILURE);
}
}
return 0;
}
//malloc分配内存空间
char *myMalloc(int count){
char *pTemp = (char *)malloc(count*sizeof(char));
if (pTemp == NULL){
exit(EXIT_FAILURE);
}
return pTemp;}
//realloc分配内存空间
char *myRealloc(char *old,int count){
char *pTemp = realloc(old,count*sizeof(char));
if (pTemp == NULL){
free(old);
exit(EXIT_FAILURE);
}
return pTemp;
}
//读取密码
char *loadPassword(){
//打开或者创建⽂件
FILE *fp = fopen(fifile_dirctory,"a+");
//移动⽂件指针到开头
fseek(fp,0,SEEK_SET);
//读取内容
char *pwd = NULL;
int index = 0;
while (1){
//从⽂件读取内容
char c = fgetc(fp);
//判断是不是⽂件的末尾
if (c == EOF){
break;
}
//判断是不是第⼀个字符
if (index == 0){
//malloc
pwd = myMalloc(1);
}else{
pwd = myRealloc(pwd,index+1);
}
//保存当前读取的字符
pwd[index] = c;
index++;
}
fclose(fp); //返回当前的⾸地址
return pwd;
}
//从终端⼀个⼀个字符获取字符串
char *myScanf(){
char *pInput = NULL;
int index = 0;
fflflush(stdin);
while (1){
char ch = getchar();
if (ch == '\n'){
break;
}
if (index == 0){
pInput = myMalloc(1);
}else{
pInput = myRealloc(pInput,index+1);
}
pInput[index] = ch;
index++;
}
return pInput;
}
//保存数据
void savePassword(char *pwd){
FILE *fp = fopen(fifile_dirctory,"w");
fputs(pwd,fp);
fclose(fp);
}
void inputNewPassword(char **old){
printf("请输⼊新密码:");
//接收⽤户输⼊
char *input_pwd = myScanf();
//保存到⽂件
savePassword(input_pwd);
//改变外部的值
*old = input_pwd;
printf("设置密码成功!!!\n");}
//设置密码
void setPassword(char **old){
//判断是否有密码
if (*old == NULL){
inputNewPassword(old);
}else{
while(1){
printf("请输⼊原始密码:");
char *input = myScanf();
int result = strcmp(*old, input);
//需要释放内存空间
free(input);
if (result != 0){
printf("密码错误 ");
}else{
break;
}
}
inputNewPassword(old);
}
}
//检查密码
bool checkPassword(char *old){
int total = 4;
while (1){
printf("请输⼊密码:");
char *input = myScanf();
int result = strcmp(old,input);
if (result == 0){
printf("解锁成功!!!\n");
return true;
}else{
total--;
if (total == 0){
printf("错误次数过多 请联系管理员!!\n");
return false;
}else{
printf("密码错误 ");
} }
}
}
//显示操作界⾯
void showHome(){
printf("*************************\n");
printf("1. 更改密码\n");
printf("2. 取款\n");
printf("3. 存款\n");
printf("4. 查询\n");
printf("5. 退出\n");
printf("*************************\n");
}
//接收⽤户的选择
int chooseOperation(int min, int max){
int choice = 0;
while (1) {
printf("请选择操作:");
scanf("%d", &choice);
if (choice >= min && choice <= max) {
return choice;
} else {
printf("输⼊不合法 ");
}
}
}
//退出
void saveExit(char *pwd){
free(pwd);
printf("欢迎下次使⽤!!!\n");
exit(EXIT_SUCCESS);
}
//查询余额
void showMoney(){
printf("当前余额为:%d\n", totalMoney);
}//是否继续
void shouldContinue(){
while (1) {
fflflush(stdin);
printf("是否继续(y/n) ?:");
char ch = getchar();
if (ch == 'n') {
saveExit(org_password);
} else {
if (ch != 'y') {
printf("输⼊不合法 ");
}else{
break;
}
}
}
}
//存款
void saveMoney(){
int money = 0;
printf("请输⼊存款⾦额:");
scanf("%d", &money);
totalMoney += money;
printf("存款成功 余额为:%d\n", totalMoney);
}
//取款
void getMoney(){
int money = 0;
while (1) {
printf("请输⼊取款⾦额:");
scanf("%d", &money);
if (money > totalMoney) {
printf("当前余额不⾜");
}else{
totalMoney -= money;
printf("取款成功 余额为:%d\n", totalMoney);
break;
}
}}