题目链接:http://acm.sdut.edu.cn/sdutoj/problem.php?action=showproblem&problemid=2167
Mathman Bank
简单模拟即可!!!#include<iostream>
#include<cstdio>
#include<string>
#include<algorithm>
using namespace std;
struct node
{
string name;
string pass;
int sum;
}no[1010];
int Find(string name,int k)
{
for(int i=0;i<k;i++)
if(no[i].name==name)
return i;
return -1;
}
int main()
{
int n;
char op;
string name,pass,name2,pass2;
int sum;
int k;
while(cin>>n)
{
k=0;
while(n--)
{
cin>>op;
if(op=='O')
{
cin>>name>>pass>>sum;
if(Find(name,k)!=-1)
printf("Account exists.\n");
else
{
no[k].name=name;
no[k].pass=pass;
no[k].sum=sum;
k++;
printf("Successfully opened an account.\n");
}
}
else if(op=='D')
{
cin>>name>>sum;
if(Find(name,k)!=-1)
{
no[Find(name,k)].sum+=sum;
printf("Successfully deposited money.\n");
}
else
printf("Account does not exist.\n");
}
else if(op=='W')
{
cin>>name>>pass>>sum;
if(Find(name,k)!=-1)
{
if(no[Find(name,k)].pass==pass)
if(no[Find(name,k)].sum>=sum)
{
no[Find(name,k)].sum-=sum;
printf("Successfully withdrew money.\n");
}
else
printf("Money not enough.\n");
else
printf("Wrong password.\n");
}
else
printf("Account does not exist.\n");
}
else if(op=='T')
{
cin>>name>>pass>>name2>>sum;
if(Find(name,k)!=-1&&Find(name2,k)!=-1) //这里应该先判断两个账号是否存在,再判断第一个账号的密码是否正确
{
if(no[Find(name,k)].pass==pass)
if(no[Find(name,k)].sum>=sum)
{
no[Find(name,k)].sum-=sum;
no[Find(name2,k)].sum+=sum;
printf("Successfully transfered money.\n");
}
else
printf("Money not enough.\n");
else
printf("Wrong password.\n");
}
else
printf("Account does not exist.\n");
}
else if(op=='C')
{
cin>>name>>pass;
if(Find(name,k)!=-1)
{
if(no[Find(name,k)].pass==pass)
printf("%d\n",no[Find(name,k)].sum);
else
printf("Wrong password.\n");
}
else
printf("Account does not exist.\n");
}
else
{
cin>>name>>pass>>pass2;
if(Find(name,k)!=-1)
{
if(no[Find(name,k)].pass==pass)
{
no[Find(name,k)].pass=pass2;
printf("Successfully changed password.\n");
}
else
printf("Wrong password.\n");
}
else
printf("Account does not exist.\n");
}
}
}
return 0;
}