2012-09-26

SCIP in C++11 ― 3.1.1節その3


銀行口座その2:口座のパスワード保護と問題3.33.4

make-monitoredを問題3.2でしっかり作っておけば簡単。

----
const function<function<List(List)>(string,List)> makeAccount
(const List& balanceIn, const List& passwdIn)
{

    const List&& balance(std::move(balanceIn));
    const List&& passwd(std::move(passwdIn));
    const List limitCount(makeLeaf(7));

    const function<List(List)> withdraw
     =[balance](const List& amount){
     if(balance>=amount){
         setInsert(balance-amount,balance);
         return(balance);
     }
     return(makeLeaf("Insuffcient funds"));
    };
   
    const function<List(List)> deposit
     =[balance](const List& amount){
     setInsert(balance+amount,balance);
     return(balance);
    };
   
    const function<List(List)> returnErrorMessage
     =[](const List& Dummy){
     return(makeLeaf("Incorrect password"));
    };

    const function<List(List)> returnErrorMonitored
     (makeMonitored(makeLeaf(returnErrorMessage)));

    const function<List(List)> callTheCops
     =[](const List& Dummy){
     return(makeLeaf("The cops are called."));
    };

    const function<function<List(List)>(string,List)> dispatch=
     [withdraw,deposit,returnErrorMonitored,passwd,limitCount,callTheCops]
     (const string passwdInput, const List& message){
     if(!isEq(passwd,makeLeaf(passwdInput))){
         if(returnErrorMonitored
            (makeLeaf("how-many-calls?"))>=limitCount-makeLeaf(1))
             {return(callTheCops);}
         return(returnErrorMonitored);
     }
     returnErrorMonitored(makeLeaf("reset-count"));
     if(isEq(message,makeLeaf("withdraw"))){return(withdraw);}
     if(isEq(message,makeLeaf("deposit"))){return(deposit);}
     cerr<<"Unknown request --- MAKE-ACCOUNT"<<endl;
     exit(1);
     return(function<List(List)>([](const List& i){return(makeList());}));
    };
    return(dispatch);
}



int main(int argc, char** argv)
{
    cout<<"Excersize 3.3:"<<endl;
    const auto acc(makeAccount(makeLeaf(100),makeLeaf("secret-password")));
    cout<<"((acc 'secret-password 'withdraw) 40) = "
     <<listString(acc(string("secret-password"),
                      makeLeaf("withdraw"))(makeLeaf(40)))<<endl;
    cout<<"((acc 'some-other-password 'deposit) 40) = "
     <<listString(acc(string("some-other-password"),
                      makeLeaf("deposit"))(makeLeaf(40)))<<endl;

    cout<<endl<<"Excersize 3.4:"<<endl;
    cout<<"((acc 'secret-password 'withdraw) 40) = "
     <<listString(acc(string("secret-password"),
                      makeLeaf("withdraw"))(makeLeaf(40)))<<endl;
    for(int i=0;i<8;++i){
     cout<<"((acc 'some-other-password 'deposit) 40) = "
         <<listString(acc(string("some-other-password"),
                          makeLeaf("deposit"))(makeLeaf(40)))<<endl;
    }
    cout<<"((acc 'secret-password 'deposit) 40) = "
     <<listString(acc(string("secret-password"),
                      makeLeaf("deposit"))(makeLeaf(40)))<<endl;
    cout<<"((acc 'some-other-password 'deposit) 40) = "
         <<listString(acc(string("some-other-password"),
                          makeLeaf("deposit"))(makeLeaf(40)))<<endl;
   

    return(0);
}
----
出力
----
Excersize 3.3:
((acc 'secret-password 'withdraw) 40) = 60
((acc 'some-other-password 'deposit) 40) = Incorrect password

Excersize 3.4:
((acc 'secret-password 'withdraw) 40) = 20
((acc 'some-other-password 'deposit) 40) = Incorrect password
((acc 'some-other-password 'deposit) 40) = Incorrect password
((acc 'some-other-password 'deposit) 40) = Incorrect password
((acc 'some-other-password 'deposit) 40) = Incorrect password
((acc 'some-other-password 'deposit) 40) = Incorrect password
((acc 'some-other-password 'deposit) 40) = Incorrect password
((acc 'some-other-password 'deposit) 40) = The cops are called.
((acc 'some-other-password 'deposit) 40) = The cops are called.
((acc 'secret-password 'deposit) 40) = 60
((acc 'some-other-password 'deposit) 40) = Incorrect password

0 件のコメント :

コメントを投稿