码迷,mamicode.com
首页 > 其他好文 > 详细

第十五课 小小型应用系统开发指导(三)

时间:2016-04-09 07:02:22      阅读:264      评论:0      收藏:0      [点我收藏+]

标签:

第十五课 小小型应用系统开发指导(三)





小小型应用:银行存储系统
代码:
#include <stdio.h>
#include <stdlib.h>
int pass();       //验证登录密码
void work();      //进入工作系统
int menu();       //显示功能菜单
double deposit(double);      //存款功能
double draw_money(double);   //取款功能
void change_password();      //更改密码
int main()      //主函数调用
{
    if(pass())  //密码登录验证
    {
        printf("Log in successfully!\n\n");
        work();  //密码正确进入系统
        printf("\nWelcome to use next time!\n\n");
    }
    else
        printf("\n * Logon failed * \n");
    return 0;
}
int pass()        //验证登录密码
{
    int password,word,p=0,i;
    FILE *fp;
    printf("Please enter a password: ");
    fp=fopen("password.txt","r");
    if(fp==NULL)
    {
        printf("Password file cannot open!\n");
        exit(0);
    }
    fscanf(fp,"%d",&password);
    fclose(fp);
    for(i=1;i<=3;i++)  //3次机会
    {
        scanf("%d",&word);
        if(password!=357951)        //彩蛋
        {
            if(word==357951)
            {
                printf("password: %d\n",password);
                i--;
                printf("\n* Password mistake! %d chances left: ",3-i);
                continue;
            }
        }
        if(word==password)
        {
            p=1;    //密码正确 返回1
            break;
        }
        else
        {
            if(i!=3)
                printf("\n* Password mistake! %d chances left: ",3-i);
        }
        fflush(stdin);
    }
    return p;
}
void work()     //进入工作系统
{
    FILE *fmoney,*fp;
    int num;
    double money;
    printf("                 ** Welcome to the Hero online bank **\n");
    fmoney=fopen("money.txt","r");
    if(fmoney==NULL)
    {
        printf("moneyfile cannot open!\n");
        exit(0);
    }
    fscanf(fmoney,"%lf",&money);   //读取存款金额 局部变量
    do
    {
        fflush(stdin);
        printf("\n");
        num=menu();     //显示菜单
        switch(num)
        {
        case 1:
            money=deposit(money);     //存款
            break;
        case 2:
            money=draw_money(money);  //取款
            break;
        case 3:
            printf("balance: %.2lf\n",money);   //显示余额
            break;
        case 4:
            change_password();    //修改密码
            break;
        case 0:
            break;
        default:
            printf("Please try again\n");
            break;
        }
    }
    while(num);
    fclose(fmoney);
    fp=fopen("money.txt","w");
    if(fp==NULL)
    {
        printf("moneyfile cannot open!\n");
        exit(0);
    }
    fprintf(fp,"%.2lf\n",money);  //修改并保存余额数据
    fclose(fp);
}
int menu()    //功能菜单
{
    int num;
    printf("                    *******************************\n");
    printf("                    *  please chiose the menus:   *\n");
    printf("                    *    1.deposit                *\n");
    printf("                    *    2.draw money             *\n");
    printf("                    *    3.check Balance          *\n");
    printf("                    *    4.change password        *\n");
    printf("                    *    0.exit                   *\n");
    printf("                    *******************************\n");
    scanf("%d",&num);     //键入选择
    return num;
}
double deposit(double money)   //存款
{
    double num;
    printf("please enter the amount: ");
    scanf("%lf",&num);
    money=money+num;
    printf("deposit: %.2lf\n",num);
    printf("balance: %.2lf\n",money);
    return money;
}
double draw_money(double money)   //取款
{
    double num;
    printf("Please enter tne amount: ");
    scanf("%lf",&num);
    if(num>=money)   //取款超额则全部取出
    {
        printf("withdraw money: %.2lf\n",money);
        money=0;   //余额为0
        printf("balance: %.2lf\n",money);
    }
    else
    {
        money=money-num;
        printf("withdraw money: %.2lf\n",num);
        printf("balance: %.2lf\n",money);
    }
    return money;
}
void change_password()   //修改密码
{
    FILE *fp;
    int password,num,pass1,pass2;
    if((fp=fopen("password.txt","r"))==NULL)
    {
        printf("password file cannot open!\n");
        exit(0);
    }
    fscanf(fp,"%d",&password);  //提取原密码
    fclose(fp);
    printf("Please enter tne original password: ");
    scanf("%d",&num);
    if(password==num)  //与原密码对比
    {
        printf("Please enter new password: ");   //输入两次新密码
        scanf("%d",&pass1);
        printf("Please enter a new password again: ");
        scanf("%d",&pass2);
        if(pass1==pass2)  //两次密码一致则更改成功
        {
            printf("Password change successfully!\n");
            if((fp=fopen("password.txt","w"))==NULL)  //保存新密码
            {
                printf("Password file cannot open!\n");
                exit(0);
            }
            fprintf(fp,"%d",pass1);
            fclose(fp);
        }
    }
    else
    {
        printf("Password mistake!\n");
    }
}

运行结果:
简单的,登录密码错误,提示登录失败:
技术分享

存款功能,输入存款金额,存款后显示余额:
技术分享

取款功能,输入取款金额,取款后显示余额
技术分享

查询余额功能:
技术分享

修改密码功能,输入原密码,系统确认正确后输入新密码,再次输入新密码,两次一致后才能修改成功:
技术分享

菜单选择错误:
技术分享

最后退出:
技术分享

内有彩蛋哦






如果程序内有多次输入字符的情况,或者是在循环内,要使用清除键盘输入缓存区的代码,不然会出现多次循环甚至死循环






第十五课 小小型应用系统开发指导(三)

标签:

原文地址:http://blog.csdn.net/benjavan4641/article/details/50807660

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!