📄 main.c
字号:
if(strcmp((*user).pwd,rePwd) != 0){ printf("The confirmed password is not the same with the password.\n");//两次输入密码不一致 }else { //else4 free(rePwd);//已不再使用rePwd,释放rePwd 所占的内存 strcpy((*user).total,"100"); if(writeUser(user) == -1) { //为用户创建一文件用来保存用户信息 printf("error in function registNewUser()...\n"); return -1; } printf("Register success\n"); return 1; //注册成功,返回登录界面,用户选择继续操作还是退出 }//end else4 }//end else3 }//end else2 }//end else1 }//end while return 0;}int file_exist(char* filename) { return (access(filename,0) == 0);}void registInfo(void){ printf("1、Go on。\n"); printf("2、exit。\n");}int writeUser(struct userInfo* user)//创建一文件用来保存用户的注册信息,文件名为用户名{ int fi; int len = 0; int i; strcpy((*user).userData,(*user).name); strcat((*user).userData," "); strcat((*user).userData,(*user).pwd); strcat((*user).userData," "); strcat((*user).userData,(*user).total); len = strlen((*user).userData); for(i = 0;i < 39-len;i++){ strcat((*user).userData," "); } strcat((*user).userData,"\n"); fi = open((*user).name, O_WRONLY | O_CREAT | O_TRUNC, S_IREAD | S_IWRITE); if(fi == -1) { printf("File open error in write user register information to file\n"); return -1; } write(fi,(*user).userData,sizeof(char)*40); close(fi); return 0;}int deposit(struct userInfo* user,struct operation* op){ int valueofMoney = 0; int valueofTotal = 0; (*op).type = "1"; //(*op).prev = (*user).total; printf("How much:"); getMoney((*op).amount); strcpy((*op).prev,(*user).total);//执行操作前的用户余额 stringToInteger((*user).total,&valueofTotal); stringToInteger((*op).amount,&valueofMoney); /*if((valueofTotal+valueofMoney) > 9999999999){ printf("Too much money ...\n"); return 0; }*/ integerToString(valueofTotal+valueofMoney,(*user).total); //(*op).post = (*user).total; strcpy((*op).post,(*user).total);//执行操作后的用户余额 getCurTime((*op).time);//获取系统当前时间 if(writeLog(user,op) == 0){ //存钱记录成功写入日志,操作成功 printf("deposit success.\n"); return 0; } printf("error in deposit...\n"); return -1;//存钱操作失败 }int getMoney(char* money){
int i, test;
while(1){
scanf("%s",money);
test = 0;
for(i = 1;i < strlen(money);i ++){
if((money[i]<'0')||(money[i]>'9')){ if(i ==0 && money[i] == '0') test = 1;
test = 1;
//free(num);
break;
}
}
if(test == 0) break;
}//end while return 0;}int stringToInteger(char* str,int *value){ int len = 0; int i; len = strlen(str); if(len == 0){ printf("no string in function stringToInteger"); return -1; }else{ for(i = 0;i < len;i++){ *value *= 10; *value += (str[i]-'0'); } } return 0;}int integerToString(int value,char* str){ sprintf(str,"%d",value); return 0;}int getCurTime(char* operTime){ int len,i; time_t t; time(&t); sprintf(operTime,"%s",ctime(&t)); //删除系统时间最后面的'\n' len = strlen(operTime); for(i = 0;i < len;i++){ if(operTime[i] == '\n'){ operTime[i] = '\0'; break; } } return 0;}inline int writeLog(struct userInfo* user,struct operation* op){ int len,i; FILE* f; //////////////////////////////////////////////////////////////////////////////////// strcpy((*user).userData,(*user).name); strcat((*user).userData," "); strcat((*user).userData,(*user).pwd); strcat((*user).userData," "); // strcat((*user).userData,(*user).total); //生成更新后的用户信息 len = strlen((*user).userData); // for(i = 0;i < 39-len;i++){ strcat((*user).userData," "); } strcat((*user).userData,"\n"); /////////////////////////////////////////////////////////////////////////////////////// strcpy((*op).operData,(*op).type); strcat((*op).operData," "); strcat((*op).operData,(*op).time); strcat((*op).operData," "); strcat((*op).operData,(*op).amount); strcat((*op).operData," "); strcat((*op).operData,(*op).prev); strcat((*op).operData," "); //生成操作信息 strcat((*op).operData,(*op).post); strcat((*op).operData," "); if((*op).type == "3"||(*op).type == "4")strcat((*op).operData,(*op).userName); len = strlen((*op).operData); for(i = 0;i < 59-len;i++){ strcat((*op).operData," "); } strcat((*op).operData,"\n"); //////////////////////////////////////////////////////////////////////////////////////// f=fopen((*user).name,"r+"); if(f == NULL){ printf("fail to open file in function writeLog()...\n"); return -1; } fseek(f,0,SEEK_SET); fputs((*user).userData,f); fseek(f,0,SEEK_END); fputs((*op).operData,f); fclose(f); return 0;}int drawMoney(struct userInfo* user,struct operation* op){ int valueofMoney = 0; int valueofTotal = 0; (*op).type = "2"; (*op).prev = (*user).total;//执行操作前的用户余额 printf("How much:"); getMoney((*op).amount); stringToInteger((*user).total,&valueofTotal); stringToInteger((*op).amount,&valueofMoney); if(valueofTotal < valueofMoney){ printf("There is not enough money in your account...\n"); return 0; } integerToString(valueofTotal-valueofMoney,(*user).total); (*op).post = (*user).total;//执行操作后的用户余额 getCurTime((*op).time);//获取系统当前时间 if(writeLog(user,op) == 0){//取钱记录成功写入日志,操作成功 printf("drawMoney success.\n"); return 0; } printf("error in deposit...\n"); return -1;//取钱操作失败}//实现用户转帐功能,在转出用户和转入用户的日志中都要记录int transfer(struct userInfo* user,struct operation* op){ int valueofMoney = 0; int valueofTotal = 0; char* toUser = malloc(sizeof(char)*MAXINFO); (*op).type = "3"; //(*op).prev = (*user).total;//执行操作前的用户余额 strcpy((*op).prev,(*user).total);//执行操作前的用户余额 while(1){ printf("User name(0 to cancle):"); scanf("%s",(*op).userName); if(strcmp((*op).userName,"0") == 0){ printf("Canceled...\n"); return 0; } if(file_exist((*op).userName)) break; printf("User name does not exist.Retry...\n"); } strcpy(toUser,(*op).userName); printf("How much:"); getMoney((*op).amount); stringToInteger((*user).total,&valueofTotal); stringToInteger((*op).amount,&valueofMoney); if(valueofTotal < valueofMoney){ printf("There is not enough money in your account...\n"); return 0; } integerToString(valueofTotal-valueofMoney,(*user).total); //(*op).post = (*user).total; ???当直接用等于号给prev 和post 赋值时,两者最终结果相同,都是操作后的用户余额,指针赋值指向同一内存空间 strcpy((*op).post,(*user).total);//执行操作前的用户余额 getCurTime((*op).time);//获取系统当前时间 if(writeLog(user,op) == 0){ //begin if strcpy((*op).userName,(*user).name); if(getUserinfo(toUser,user) == -1){ printf("Can not read information of the user who you want to transfer your money to,transfer canceled\n"); //执行回滚操作 return -1; //转帐操作失败 }else{ //begin else 成功读取要转入的用户信息,进行存钱操作 valueofMoney = 0; valueofTotal = 0; (*op).type = "4"; stringToInteger((*user).total,&valueofTotal); integerToString(valueofTotal,(*op).prev); stringToInteger((*op).amount,&valueofMoney); integerToString(valueofTotal+valueofMoney,(*user).total);//转出金额与转入金额相同 strcpy((*op).post,(*user).total);//执行操作后的用户余额 if(writeLog(user,op) == 0){ free(toUser); if(getUserinfo((*op).userName,user) == -1)return -1;//日志写入转入用户日志文件时失败。 return 0;//日志成功写入转入用户日志文件中,转帐操作成功。 } printf("deposit money erroe in transfer,Canceled...\n"); //执行回滚操作 return -1; }//end else*/ }//end if //向转出用户写入日志时失败,转帐操作失败,取消。 printf("draw money error in transfer,Canceled...\n"); return -1; }/*int isNumAlpha(char ch){ return ((ch>='0')&&(ch<='9'));}*/
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -