这种题考的是细心
strcmp()和memcpy()都能实现字符串的复制,但两者还是有明显区别的,详见http://www.cnblogs.com/stoneJin/archive/2011/09/16/2179248.html
#include<iostream>
#include<cstring>
#include<cstdio>
using namespace std;
struct cell{
char name[15];
char mima[15];
int amount;
}a[1001];
int cur=0;
int check(char customer[]){
int i;
for(i=1;i<=cur;i++)
if(strcmp(customer,a[i].name)==0){
return i;
}
return -1;
}
int main(){
int n;
char order[3];
char customer[15],sender[15],password[15],newpass[15];
int initial;
int No,No1;
scanf("%d",&n);
while(n--){
scanf("%s",order);
if(order[0]=='O'){
scanf("%s%s%d",customer,password,&initial);
No=check(customer);
if(No==-1){
cur++;
memcpy(a[cur].name,customer,sizeof(customer));
memcpy(a[cur].mima,password,sizeof(password));
a[cur].amount=initial;
printf("Successfully opened an account.\n");
}
else
printf("Account exists.\n");
}
else if(order[0]=='D'){
scanf("%s%d",customer,&initial);
No=check(customer);
if(No==-1)
printf("Account does not exist.\n");
else{
a[No].amount+=initial;
printf("Successfully deposited money.\n");
}
}
else if(order[0]=='W'){
scanf("%s%s%d",customer,password,&initial);
No=check(customer);
if(No==-1)
printf("Account does not exist.\n");
else{
if(strcmp(password,a[No].mima)!=0)
printf("Wrong password.\n");
else if(initial>a[No].amount)
printf("Money not enough.\n");
else{
a[No].amount-=initial;
printf("Successfully withdrew money.\n");
}
}
}
else if(order[0]=='T'){
scanf("%s%s%s%d",sender,password,customer,&initial);
No=check(sender);
No1=check(customer);
if(No==-1||No1==-1)
printf("Account does not exist.\n");
else{
if(strcmp(password,a[No].mima)!=0)
printf("Wrong password.\n");
else if(a[No].amount<initial)
printf("Money not enough.\n");
else{
a[No].amount-=initial;
a[No1].amount+=initial;
printf("Successfully transfered money.\n");
}
}
}
else if(order[0]=='C'){
scanf("%s%s",customer,password);
No=check(customer);
if(No==-1)
printf("Account does not exist.\n");
else{
if(strcmp(password,a[No].mima)!=0)
printf("Wrong password.\n");
else
printf("%d\n",a[No].amount);
}
}
else if(order[0]=='X'){
scanf("%s%s%s",customer,password,newpass);
No=check(customer);
if(No==-1)
printf("Account does not exist.\n");
else{
if(strcmp(password,a[No].mima)!=0)
printf("Wrong password.\n");
else{
memcpy(a[No].mima,newpass,sizeof(newpass));
printf("Successfully changed password.\n");
}
}
}
}
return 0;
}
/**************************************
Problem id : SDUT OJ 2167
Result : Accepted
Take Memory : 504K
Take Time : 0MS
Submit Time : 2013-05-27 14:44:25
**************************************/