银行储蓄系统的练习

/*
*Copyright (c) 2014, 烟台大学计算机学院
*All rights reserved.
*文件名称:bank.cpp
*作者:高赞
*完成日期:2015年 3 月 1 日
*版本号:v1.0
*
*问题描述:尝试做银行储蓄系统
*/
#include <iostream>
#include <string.h>
#include <stdlib.h>
#include <fstream>
#include <conio.h>
using namespace std;
struct BankClerk
{
    string clerkName;
    string clerkPasswoed;
};
string hiddenPassword();
void openAccount();
void closingAccount();
void deposit();
void drawMoney();
void inquiry();
void transferAccount();
void lossReport();
void lossCancel();
void changePassword();
int main()
{
    string CP,CN;
    int choose,judge=0,n=0;
    while (judge==0)
    {
        cout<< "请输入业务员用户名:";
        cin>>CN;
        cout<<"请输入密码:";
        CP=hiddenPassword();
        string cp,cn;
        ifstream infile("clerkdata.txt",ios::in);//文本读入业务员信息,可以支持多名
        if(!infile)
        {
            cerr<<"open error!"<<endl;
            exit(1);
        }
        while(infile>>cn>>cp)
        {
            if(cn==CN)
            {
                if(cp!=CP)
                {
                    break;
                }
                else
                {
                    judge=1;
                    break;
                }
            }
        }
        infile.close();
        if(judge!=1)
        {
            cout<<endl<<"用户名或密码错误!"<<endl;
            ++n;
            if(n==3)
            {
                cout<<"三次输入错误,程序自动退出"<<endl;
                exit(0);
            }
        }
        else cout<<endl<<"登录成功!"<<endl;
    }
    while (1)
    {
        cout << endl
             << "*您可以办理以下业务:" << endl
             << "*1.开户" << endl
             << "*2.销户" << endl
             << "*3.存款" << endl
             << "*4.取款" << endl
             << "*5.查询" << endl
             << "*6.转账" << endl
             << "*7.挂失" << endl
             << "*8.解挂" << endl
             << "*9.改密" << endl
             << "*0.退出" << endl
             << "请输入(0-9):" ;
        cin >> choose;
        if (choose==0)
        {
            cout << "********谢谢使用********" << endl;
            break;
        }
        switch (choose)
        {
        case 1:
            openAccount();
            break;
        case 2:
            closingAccount();
            break;
        case 3:
            deposit();
            break;
        case 4:
            drawMoney();
            break;
        case 5:
            inquiry();
            break;
        case 6:
            transferAccount();
            break;
        case 7:
            lossReport();
            break;
        case 8:
            lossCancel();
            break;
        case 9:
            changePassword();
            break;
        default:
            cout << "输入不符合要求,请重新输入!"  << endl;
        }
        getchar();
        cout << "操作完成,系统正在返回...!" << endl
             <<"按任意键继续..." << endl << endl;
        getchar();
    }
    return 0;
}


开户功能的实现

#include <iostream>
#include <string.h>
#include <stdlib.h>
#include <fstream>
using namespace std;
struct Account
{
    long accNumber;
    string accName;
    string password;
    double balance;
    string identity;
    int state;
} ;
long Number;
extern  string hiddenPassword();
long AccountNumber()
{
    long n;
    ifstream infile("number.txt",ios::in);//保存number值,使下一个账户获得连续值
    if(!infile)
    {
        cerr<<"open error!"<<endl;
        exit(1);
    }
    int NUM;
    while(infile>>NUM)
    {
        n=NUM;
        ++NUM;
    }
    infile.close();
    ofstream outfile("number.txt",ios::out);
    if(!outfile)
    {
        cout<<"open eror!"<<endl;
        exit(1);
    }
    outfile<<NUM;
    outfile.close();
    return n;
}
void openAccount()
{

    Number=AccountNumber();
    Account newAccount;
    cout<<"账号:"<<Number<<endl;
    cout<<"户主姓名:";
    cin>>newAccount.accName;
    cout<<"身份证号码:";
    cin>>newAccount.identity;
    while(newAccount.identity.length()!=18)
    {
        cout<<"身份证号码必须是18位!"<<endl<<"重新输入:";//身份证号码由18位数字字符组成
        cin>>newAccount.identity;
    }
    string ide=newAccount.identity;
    char ID[18];
    int id=0,i;
    while(id==0)
    {
        id=1;
        strncpy(ID,newAccount.identity.c_str(),18);
        for(i=0; i<18; ++i)                  //身份证号码最后一位可以是X
        {
            if((i!=17&&(ID[i]<48 || ID[i]>57)) || (i==17&&(ID[i]<48 || ID[i]>57)&&ID[i]!=88))
            {
                cout<<"身份证号码有误!重新输入:";
                cin>>newAccount.identity;
                id=0;
                break;
            }
        }
    }
    string password1="0",password2="1";
    while(password1!=password2)
    {
        cout<<"密码:";
        password1=hiddenPassword();
        while(password1.length()!=6)
        {
            cout<<endl<<"密码必须是6位!"<<endl<<"重新输入:";//6位数字密码
            password1=hiddenPassword();
        }
        char PS[6];
        int ps=0;

        while(ps==0)
        {
            ps=1;
            strncpy(PS,password1.c_str(),6);
            for(i=0; i<6; ++i)
            {
                if(PS[i]<48 || PS[i]>57)
                {
                    cout<<endl<<"密码有误!重新输入:";
                    password1=hiddenPassword();
                    ps=0;
                    break;
                }
            }
        }
        cout<<endl<<"确认密码:";
        password2=hiddenPassword();
        if(password1!=password2)
            cout<<endl<<"密码错误,重新确认:"<<endl;
    }
    newAccount.password=password1;
    cout<<endl<<"存入金额:";
    cin>>newAccount.balance;
    newAccount.state=0;
    ofstream outfile("account.txt",ios::app);//以追加方式打开文本
    if(!outfile)
    {
        cerr<<"open error!"<<endl;
        exit(1);
    }
    outfile <<Number<<" "<<newAccount.accName<<" "<<newAccount.password  //账户信息添加到文本
            <<" "<<newAccount.balance<<" "<<newAccount.identity<<" "<<newAccount.state<<endl;
    outfile.close();
}


输入密码时显示星号的功能

#include <iostream>
#include <string>
#include <conio.h>
using namespace std;
string hiddenPassword()
{
    string s;
    char a[10];
    int i=0;
    while(true)
    {
        a[i]=getch();
        if(a[i]!=13&&a[i]!=8)
            cout<<"*";
        else if(a[i]==8)
        {
            if(i==0)
                continue;
            cout<<"\b";
            --i;
            cout<<" ";
            cout<<"\b";
            --i;
        }
        else break;
        ++i;
    }
    for(int j=0; j<i; ++j)
        s+=a[j];
    return s;
}


销户功能的实现

#include <iostream>
#include <string>
#include <stdlib.h>
#include <fstream>
using namespace std;
extern string hiddenPassword();
struct Account
{
    long accNumber;
    string accName;
    string password;
    double balance;
    string identity;
    int state;
};
void closingAccount()
{
    int i=0,j,st;
    long number,closingNumber;
    double bala;
    string name,pass,id,closingName,closingPassword;
    Account a[2000];
    ifstream infile("account.txt",ios::in);
    if(!infile)
    {
        cerr<<"open error!"<<endl;
        exit(1);
    }
    while(infile>>number>>name>>pass>>bala>>id>>st)
    {
        a[i].accNumber=number;
        a[i].accName=name;
        a[i].password=pass;
        a[i].balance=bala;
        a[i].identity=id;
        a[i].state=st;
        ++i;
    }
    infile.close();
    int middle,low,high;
    while(1)
    {
        cout<<"请输入待销户账号:";
        cin>>closingNumber;
        j=i;
        low=0;
        high=j;
        middle=(low+high)/2;
        while(closingNumber!=a[middle].accNumber)
        {
            if(closingNumber>a[middle].accNumber)
            {
                low=middle+1;
                middle=(low+high)/2;
            }
            else
            {
                high=middle-1;
                middle=(low+high)/2;
            }
            if (low>high)
            {
                cout << "查无此账号,重新输入:" << endl;
                break;
            }

        }
        if(closingNumber==a[middle].accNumber)
            break;
    }
    if(a[middle].state==1)
        cout<<"该账号已挂失,不能办理销户"<<endl;
    if(a[middle].state==2)
        cout<<"该账号已销户!"<<endl;
    if(a[middle].state==0)
    {
        cout<<"姓名:";
        cin>>closingName;
        while(closingName!=a[middle].accName)
        {
            cout<<"姓名错误!重新输入姓名:";
            cin>>closingName;
        }
        cout<<"请输入密码:";
        closingPassword=hiddenPassword();
        while(closingPassword!=a[middle].password)
        {
            cout<<"密码错误!重新输入密码:";
            closingPassword=hiddenPassword();
        }
        char ch;
        cout<<endl<<"余额:"<<a[middle].balance<<"元"<<endl
            <<"确认销户?(y/n)  ";
        cin>>ch;
        while(ch!=89&&ch!=121&&ch!=78&&ch!=110)
        {
            cout<<"输入错误,重新输入:";
            cin>>ch;
        }
        if(ch==89||ch==121)
        {
            cout<<"取款"<<a[middle].balance<<"元,销户成功!"<<endl;
            a[middle].state=2;
        }
        else cout<<"已取消销户"<<endl;
        ofstream outfile("account.txt",ios::out);
        if(!outfile)
        {
            cerr<<"open error!"<<endl;
            exit(1);
        }
        for(i=0; i<j; ++i)
        {
            outfile <<a[i].accNumber<<" "<<a[i].accName<<" "<<a[i].password
                    <<" "<<a[i].balance<<" "<<a[i].identity<<" "<<a[i].state<<endl;
        }
        outfile.close();
    }
}


存款功能

#include <iostream>
#include <string.h>
#include <stdlib.h>
#include <fstream>
using namespace std;
struct Account
{
    long accNumber;
    string accName;
    string password;
    double balance;
    string identity;
    int state;
};
void deposit ()
{
    int i=0,j,st;
    long number,depositNumber;
    double bala,money;
    string name,pass,id,depositName;
    Account a[2000];
    ifstream infile("account.txt",ios::in);
    if(!infile)
    {
        cerr<<"open error!"<<endl;
        exit(1);
    }
    while(infile>>number>>name>>pass>>bala>>id>>st)
    {
        a[i].accNumber=number;
        a[i].accName=name;
        a[i].password=pass;
        a[i].balance=bala;
        a[i].identity=id;
        a[i].state=st;
        ++i;
    }
    infile.close();
    int middle,low,high;
    while(1)
    {
        cout<<"请输入账号:";
        cin>>depositNumber;
        j=i;
        low=0;
        high=j;
        middle=(low+high)/2;
        while(depositNumber!=a[middle].accNumber)
        {
            if(depositNumber>a[middle].accNumber)
            {
                low=middle+1;
                middle=(low+high)/2;
            }
            else
            {
                high=middle-1;
                middle=(low+high)/2;
            }
            if (low>high)
            {
                cout << "查无此账号,重新输入:" << endl;
                break;
            }

        }
        if(depositNumber==a[middle].accNumber)
            break;
    }
    if(a[middle].state==1)
        cout<<"该账号已挂失,不能办理存款"<<endl;
    if(a[middle].state==2)
        cout<<"该账号已销户,不能办理存款"<<endl;
    if(a[middle].state==0)
    {
        cout<<"姓名:";
        cin>>depositName;
        while(depositName!=a[middle].accName)
        {
            cout<<"姓名错误!重新输入姓名:";
            cin>>depositName;
        }
        cout<<"输入存款额:";
        cin>>money;
        a[middle].balance+=money;
        ofstream outfile("account.txt",ios::out);
        if(!outfile)
        {
            cerr<<"open error!"<<endl;
            exit(1);
        }
        for(i=0; i<j; ++i)
        {
            outfile <<a[i].accNumber<<" "<<a[i].accName<<" "<<a[i].password
                    <<" "<<a[i].balance<<" "<<a[i].identity<<" "<<a[i].state<<endl;
        }
        outfile.close();
    }
}


取款功能

#include <iostream>
#include <string>
#include <stdlib.h>
#include <fstream>
using namespace std;
extern string hiddenPassword();
struct Account
{
    long accNumber;
    string accName;
    string password;
    double balance;
    string identity;
    int state;
};
void drawMoney()
{
    int i=0,j,st;
    long number,drawNumber;
    double bala;
    string name,pass,id,drawName,drawPassword;
    Account a[2000];
    ifstream infile("account.txt",ios::in);
    if(!infile)
    {
        cerr<<"open error!"<<endl;
        exit(1);
    }
    while(infile>>number>>name>>pass>>bala>>id>>st)
    {
        a[i].accNumber=number;
        a[i].accName=name;
        a[i].password=pass;
        a[i].balance=bala;
        a[i].identity=id;
        a[i].state=st;
        ++i;
    }
    infile.close();
    int middle,low,high;
    while(1)
    {
        cout<<"请输入账号:";
        cin>>drawNumber;
        j=i;
        low=0;
        high=j;
        middle=(low+high)/2;
        while(drawNumber!=a[middle].accNumber)
        {
            if(drawNumber>a[middle].accNumber)
            {
                low=middle+1;
                middle=(low+high)/2;
            }
            else
            {
                high=middle-1;
                middle=(low+high)/2;
            }
            if (low>high)
            {
                cout << "查无此账号,重新输入:" << endl;
                break;
            }

        }
        if(drawNumber==a[middle].accNumber)
            break;
    }
    if(a[middle].state==1)
        cout<<"该账号已挂失,不能办理取款"<<endl;
    if(a[middle].state==2)
        cout<<"该账号已销户,不能办理取款"<<endl;
    if(a[middle].state==0)
    {
        cout<<"姓名:";
        cin>>drawName;
        while(drawName!=a[middle].accName)
        {
            cout<<"姓名错误!重新输入姓名:";
            cin>>drawName;
        }
        cout<<"请输入密码:";
        drawPassword=hiddenPassword();
        while(drawPassword!=a[middle].password)
        {
            cout<<endl<<"密码错误!重新输入密码:";
            drawPassword=hiddenPassword();
        }
        cout <<endl<<"输入取款额:";
        double dm;
        cin>>dm;
        if(dm>a[middle].balance)
            cout<<"余额不足!"<<endl;
        else
        {
            a[middle].balance-=dm;
            cout<<"取款成功!"<<endl;
            ofstream outfile("account.txt",ios::out);
            if(!outfile)
            {
                cerr<<"open error!"<<endl;
                exit(1);
            }
            for(i=0; i<j; ++i)
            {
                outfile <<a[i].accNumber<<" "<<a[i].accName<<" "<<a[i].password
                        <<" "<<a[i].balance<<" "<<a[i].identity<<" "<<a[i].state<<endl;
            }
            outfile.close();
        }
    }
}


查询功能

</pre><pre class="cpp" name="code">#include <iostream>
#include <string>
#include <stdlib.h>
#include <fstream>
using namespace std;
extern string hiddenPassword();
struct Account
{
    long accNumber;
    string accName;
    string password;
    double balance;
    string identity;
    int state;
};
void inquiry()
{
    int i=0,j,st;
    long number,inquiryNumber;
    double bala;
    string name,pass,id,inquiryName,inquiryPassword;
    Account a[2000];
    ifstream infile("account.txt",ios::in);
    if(!infile)
    {
        cerr<<"open error!"<<endl;
        exit(1);
    }
    while(infile>>number>>name>>pass>>bala>>id>>st)
    {
        a[i].accNumber=number;
        a[i].accName=name;
        a[i].password=pass;
        a[i].balance=bala;
        a[i].identity=id;
        a[i].state=st;
        ++i;
    }
    infile.close();
    int middle,low,high;
    while(1)
    {
        cout<<"请输入账号:";
        cin>>inquiryNumber;
        j=i;
        low=0;
        high=j;
        middle=(low+high)/2;
        while(inquiryNumber!=a[middle].accNumber)
        {
            if(inquiryNumber>a[middle].accNumber)
            {
                low=middle+1;
                middle=(low+high)/2;
            }
            else
            {
                high=middle-1;
                middle=(low+high)/2;
            }
            if (low>high)
            {
                cout << "查无此账号,重新输入:" << endl;
                break;
            }

        }
        if(inquiryNumber==a[middle].accNumber)
            break;
    }
    cout<<"姓名:";
    cin>>inquiryName;
    while(inquiryName!=a[middle].accName)
    {
        cout<<"姓名错误!重新输入姓名:";
        cin>>inquiryName;
    }
    cout<<"请输入密码:";
    inquiryPassword=hiddenPassword();
    while(inquiryPassword!=a[middle].password)
    {
        cout<<endl<<"密码错误!重新输入密码:";
        inquiryPassword=hiddenPassword();
    }
    string condition;
    switch (a[middle].state)
    {
    case 0:
        condition="正常";
        break;
    case 1:
        condition="挂失";
        break;
    case 2:
        condition="销户";
        break;
    default:
        cout<<"error!"<<endl;
    }
    cout<<endl<<"余额:"<<a[middle].balance<<endl
        <<"当前状态:"<<condition<<endl
        <<"身份证号码:"<<a[middle].identity<<endl;
}


转账功能

#include <iostream>
#include <string.h>
#include <stdlib.h>
#include <fstream>
using namespace std;
extern string hiddenPassword();
struct Account
{
    long accNumber;
    string accName;
    string password;
    double balance;
    string identity;
    int state;
};
void transferAccount ()
{
    int i=0,j,k,st;
    long number,Number1,Number2;
    double bala,givenMoney;
    string name,pass,id,transName,transPassword;
    Account a[2000];
    ifstream infile("account.txt",ios::in);
    if(!infile)
    {
        cerr<<"open error!"<<endl;
        exit(1);
    }
    while(infile>>number>>name>>pass>>bala>>id>>st)
    {
        a[i].accNumber=number;
        a[i].accName=name;
        a[i].password=pass;
        a[i].balance=bala;
        a[i].identity=id;
        a[i].state=st;
        ++i;
    }
    infile.close();
    int middle,MIDDLE,low,high;
    while(1)
    {
        cout<<"转出账号:";
        cin>>Number1;
        j=i;
        low=0;
        high=j;
        middle=(low+high)/2;
        while(Number1!=a[middle].accNumber)
        {
            if(Number1>a[middle].accNumber)
            {
                low=middle+1;
                middle=(low+high)/2;
            }
            else
            {
                high=middle-1;
                middle=(low+high)/2;
            }
            if (low>high)
            {
                cout << "查无此账号,重新输入:" << endl;
                break;
            }

        }
        if(Number1==a[middle].accNumber)
            break;
    }
    if(a[middle].state==1)
        cout<<"该账号已挂失,不能办理转账"<<endl;
    if(a[middle].state==2)
        cout<<"该账号已销户,不能办理转账"<<endl;
    if(a[middle].state==0)
    {
        cout<<"姓名:";
        cin>>transName;
        while(transName!=a[middle].accName)
        {
            cout<<"姓名错误!重新输入姓名:";
            cin>>transName;
        }
        cout<<"请输入密码:";
        transPassword=hiddenPassword();
        while(transPassword!=a[middle].password)
        {
            cout<<endl<<"密码错误!重新输入密码:";
            transPassword=hiddenPassword();
        }
        cout<<endl<<"输入转账金额:";
        cin>>givenMoney;
        if(givenMoney>a[middle].balance)
            cout<<"余额不足,转账失败!"<<endl;
        else
        {
            a[middle].balance-=givenMoney;
            while(1)
            {
                cout<<"转入账号:";
                cin>>Number2;
                k=i;
                low=0;
                high=k;
                MIDDLE=(low+high)/2;
                while(Number2!=a[MIDDLE].accNumber)
                {
                    if(Number2>a[MIDDLE].accNumber)
                    {
                        low=MIDDLE+1;
                        MIDDLE=(low+high)/2;
                    }
                    else
                    {
                        high=MIDDLE-1;
                        MIDDLE=(low+high)/2;
                    }
                    if (low>high)
                    {
                        cout << "查无此账号,重新输入:" << endl;
                        break;
                    }

                }
                if(Number2==a[MIDDLE].accNumber)
                    break;
            }
            if(a[MIDDLE].state==1)
                cout<<"该账号已挂失,不能办理转账"<<endl;
            if(a[MIDDLE].state==2)
                cout<<"该账号已销户,不能办理转账"<<endl;
            if(a[MIDDLE].state==0)
            {

                a[MIDDLE].balance+=givenMoney;
                ofstream outfile("account.txt",ios::out);
                if(!outfile)
                {
                    cerr<<"open error!"<<endl;
                    exit(1);
                }
                for(i=0; i<k; ++i)
                {
                    outfile <<a[i].accNumber<<" "<<a[i].accName<<" "<<a[i].password
                            <<" "<<a[i].balance<<" "<<a[i].identity<<" "<<a[i].state<<endl;
                }
                outfile.close();
            }
        }
    }
}



挂失功能

#include <iostream>
#include <string>
#include <stdlib.h>
#include <fstream>
using namespace std;
extern string hiddenPassword();
struct Account
{
    long accNumber;
    string accName;
    string password;
    double balance;
    string identity;
    int state;
};
void lossReport()
{
    int i=0,j,st;
    long number,lossNumber;
    double bala;
    string name,pass,id,lossName,lossPassword;
    Account a[2000];
    ifstream infile("account.txt",ios::in);
    if(!infile)
    {
        cerr<<"open error!"<<endl;
        exit(1);
    }
    while(infile>>number>>name>>pass>>bala>>id>>st)
    {
        a[i].accNumber=number;
        a[i].accName=name;
        a[i].password=pass;
        a[i].balance=bala;
        a[i].identity=id;
        a[i].state=st;
        ++i;
    }
    infile.close();
    int middle,low,high;
    while(1)
    {
        cout<<"请输入待挂失账号:";
        cin>>lossNumber;
        j=i;
        low=0;
        high=j;
        middle=(low+high)/2;
        while(lossNumber!=a[middle].accNumber)
        {
            if(lossNumber>a[middle].accNumber)
            {
                low=middle+1;
                middle=(low+high)/2;
            }
            else
            {
                high=middle-1;
                middle=(low+high)/2;
            }
            if (low>high)
            {
                cout << "查无此账号,重新输入:" << endl;
                break;
            }

        }
        if(lossNumber==a[middle].accNumber)
            break;
    }
    if(a[middle].state==1)
        cout<<"该账号已挂失!"<<endl;
    if(a[middle].state==2)
        cout<<"该账号已销户!"<<endl;
    if(a[middle].state==0)
    {
        cout<<"姓名:";
        cin>>lossName;
        while(lossName!=a[middle].accName)
        {
            cout<<"姓名错误!重新输入姓名:";
            cin>>lossName;
        }
        cout<<"请输入密码:";
        lossPassword=hiddenPassword();
        while(lossPassword!=a[middle].password)
        {
            cout<<endl<<"密码错误!重新输入密码:";
            lossPassword=hiddenPassword();
        }
        cout<<endl<<"挂失成功!"<<endl;
        a[middle].state=1;
        ofstream outfile("account.txt",ios::out);
        if(!outfile)
        {
            cerr<<"open error!"<<endl;
            exit(1);
        }
        for(i=0; i<j; ++i)
        {
            outfile <<a[i].accNumber<<" "<<a[i].accName<<" "<<a[i].password
                    <<" "<<a[i].balance<<" "<<a[i].identity<<" "<<a[i].state<<endl;
        }
        outfile.close();
    }
}


解挂功能

#include <iostream>
#include <string>
#include <stdlib.h>
#include <fstream>
using namespace std;
extern string hiddenPassword();
struct Account
{
    long accNumber;
    string accName;
    string password;
    double balance;
    string identity;
    int state;
};
void lossCancel()
{
    int i=0,j,st;
    long number,lossNumber;
    double bala;
    string name,pass,id,lossName,lossPassword;
    Account a[2000];
    ifstream infile("account.txt",ios::in);
    if(!infile)
    {
        cerr<<"open error!"<<endl;
        exit(1);
    }
    while(infile>>number>>name>>pass>>bala>>id>>st)
    {
        a[i].accNumber=number;
        a[i].accName=name;
        a[i].password=pass;
        a[i].balance=bala;
        a[i].identity=id;
        a[i].state=st;
        ++i;
    }
    infile.close();
    int middle,low,high;
    while(1)
    {
        cout<<"请输入待解挂账号:";
        cin>>lossNumber;
        j=i;
        low=0;
        high=j;
        middle=(low+high)/2;
        while(lossNumber!=a[middle].accNumber)
        {
            if(lossNumber>a[middle].accNumber)
            {
                low=middle+1;
                middle=(low+high)/2;
            }
            else
            {
                high=middle-1;
                middle=(low+high)/2;
            }
            if (low>high)
            {
                cout << "查无此账号,重新输入:" << endl;
                break;
            }

        }
        if(lossNumber==a[middle].accNumber)
            break;
    }
    if(a[middle].state==0)
        cout<<"该账号没有挂失!"<<endl;
    if(a[middle].state==2)
        cout<<"该账号已销户!"<<endl;
    if(a[middle].state==1)
    {
        cout<<"姓名:";
        cin>>lossName;
        while(lossName!=a[middle].accName)
        {
            cout<<"姓名错误!重新输入姓名:";
            cin>>lossName;
        }
        cout<<"请输入密码:";
        lossPassword=hiddenPassword();
        while(lossPassword!=a[middle].password)
        {
            cout<<endl<<"密码错误!重新输入密码:";
            lossPassword=hiddenPassword();
        }
        cout<<endl<<"解挂成功!"<<endl;
        a[middle].state=0;
        ofstream outfile("account.txt",ios::out);
        if(!outfile)
        {
            cerr<<"open error!"<<endl;
            exit(1);
        }
        for(i=0; i<j; ++i)
        {
            outfile <<a[i].accNumber<<" "<<a[i].accName<<" "<<a[i].password
                    <<" "<<a[i].balance<<" "<<a[i].identity<<" "<<a[i].state<<endl;
        }
        outfile.close();
    }
}


改密功能

#include <iostream>
#include <string.h>
#include <stdlib.h>
#include <fstream>
using namespace std;
extern string hiddenPassword();
struct Account
{
    long accNumber;
    string accName;
    string password;
    double balance;
    string identity;
    int state;
};
void changePassword()
{
    int i=0,j,st;
    long number,changeNumber;
    double bala;
    string name,pass,id,changeName,changePassword,newPassword1="0",newPassword2="1";
    Account a[2000];
    ifstream infile("account.txt",ios::in);
    if(!infile)
    {
        cerr<<"open error!"<<endl;
        exit(1);
    }
    while(infile>>number>>name>>pass>>bala>>id>>st)
    {
        a[i].accNumber=number;
        a[i].accName=name;
        a[i].password=pass;
        a[i].balance=bala;
        a[i].identity=id;
        a[i].state=st;
        ++i;
    }
    infile.close();
    int middle,low,high;
    while(1)
    {
        cout<<"请输入账号:";
        cin>>changeNumber;
        j=i;
        low=0;
        high=j;
        middle=(low+high)/2;
        while(changeNumber!=a[middle].accNumber)
        {
            if(changeNumber>a[middle].accNumber)
            {
                low=middle+1;
                middle=(low+high)/2;
            }
            else
            {
                high=middle-1;
                middle=(low+high)/2;
            }
            if (low>high)
            {
                cout << "查无此账号,重新输入:" << endl;
                break;
            }

        }
        if(changeNumber==a[middle].accNumber)
            break;
    }
    if(a[middle].state==1)
        cout<<"该账号已挂失,无法改密"<<endl;
    if(a[middle].state==2)
        cout<<"该账号已销户,无法改密"<<endl;
    if(a[middle].state==0)
    {
        cout<<"姓名:";
        cin>>changeName;
        while(changeName!=a[middle].accName)
        {
            cout<<"姓名错误!重新输入姓名:";
            cin>>changeName;
        }
        cout<<"请输入密码:";
        changePassword=hiddenPassword();
        while(changePassword!=a[middle].password)
        {
            cout<<endl<<"密码错误!重新输入密码:";
            changePassword=hiddenPassword();
        }
        cout<<endl;
        while(newPassword1!=newPassword2)
        {
            cout<<"新密码:";
            newPassword1=hiddenPassword();
            while(newPassword1.length()!=6)
            {
                cout<<endl<<"密码必须是6位数字!重新输入:";
                newPassword1=hiddenPassword();
            }
            char NP[6];
            int np=0;

            while(np==0)
            {
                np=1;
                strncpy(NP,newPassword1.c_str(),6);
                for(i=0; i<6; ++i)
                {
                    if(NP[i]<48 || NP[i]>57)
                    {
                        cout<<endl<<"密码必须是6位数字!重新输入:";
                        newPassword1=hiddenPassword();
                        np=0;
                        break;
                    }
                }
            }
            cout<<endl<<"确认新密码:";
            newPassword2=hiddenPassword();
            if(newPassword1!=newPassword2)
                cout<<endl<<"密码错误,重新确认:"<<endl;
        }
        cout<<endl;
        a[middle].password=newPassword1;
        ofstream outfile("account.txt",ios::out);
        if(!outfile)
        {
            cerr<<"open error!"<<endl;
            exit(1);
        }
        for(i=0; i<j; ++i)
        {
            outfile <<a[i].accNumber<<" "<<a[i].accName<<" "<<a[i].password
                    <<" "<<a[i].balance<<" "<<a[i].identity<<" "<<a[i].state<<endl;
        }
        outfile.close();
    }
}

   
      一些功能算法写的很牵强,不够简洁,一些重复的地方想改成自定义函数结果老搞不定,学到的知识毕竟不多,从网上看到很多更好的函数算法但都不会用

      测试了一下午,可能还有隐藏的bug(家里的古董本有毛病,截不了图),不过还是搞完啦

    


  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 5
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论 5
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值