最近在帮考研跨考计算机的朋友学习C语言,from zero.
用单链表实现了一个账户的管理系统。
麻雀虽小,五脏俱全。单链表的增删改查(CRUD)全在里面了,结构也很清晰,适合初学者学习链表的基本操作。
#include
#include
#include
#define _CRT_SECURE_NO_DEPRECATE
#define _CRT_SECURE_NO_WARNINGS
#define BUFFER_SIZE 100
#define MAX_ACCOUNT_USERNAME_LENGTH 20
#define MAX_ACCOUNT_PASSWORD_LENGTH 20
typedef struct _account{
char username[MAX_ACCOUNT_PASSWORD_LENGTH];
char password[MAX_ACCOUNT_PASSWORD_LENGTH];
struct _account* next;
}Account;
enum Choices{
Create,Retrieve,Update,Delete,SeeAll,DeleteAll
};
Account* accoutLinkedList = NULL;
void printUsage();
void createAccount();
Account* findUsername(const char* username);
void retrieveAccount();
void updateAccount();
void deleteAccont();
void seeAllAcount();
void deleteAllAccount();
int main(int argc, char const *argv[])
{
printf("Welcome to the dummy account management system.\n");
int choice;
while(1){
system("CLS");//clear the view
printUsage();
scanf("%d",&choice);
putchar('\n');
choice --;//counting from zero
switch(choice){
case Create:
createAccount();
break;
case Retrieve:
retrieveAccount();
break;
case Update:
updateAccount();
break;
case Delete:
deleteAccont();
break;
case SeeAll:
seeAllAcount();
break;
case DeleteAll:
deleteAllAccount();
break;
default:
printf("Illegal menu number!\n");
}
putchar('\n');
printf("Press any key to continue.\n");
getchar();//eat the \n
getchar();//choke
}
return 0;
}
void printUsage(){
printf("Key in a number to select a menu...\n"
"1. Create a account\n"
"2. Retrieve password with a username\n"
"3. Update password for a username\n"
"4. Delete a existing account from the system\n"
"5. See all accounts in the system\n"
"6. Delete all accounts from the system\n");
}
void createAccount(){
//input
char buffer[BUFFER_SIZE];
char username[MAX_ACCOUNT_USERNAME_LENGTH];
char password[MAX_ACCOUNT_PASSWORD_LENGTH];
printf("Create a account\n");
printf("The system won't check if the username already exists.\n");
printf("Key in the username, max length allowed = %d.\n",MAX_ACCOUNT_USERNAME_LENGTH);
scanf("%s",buffer);
strncpy(username,buffer,MAX_ACCOUNT_USERNAME_LENGTH);
printf("Key in the password for user[%s],max length allowed = %d.\n",username,MAX_ACCOUNT_PASSWORD_LENGTH );
scanf("%s",buffer);
strncpy(password,buffer,MAX_ACCOUNT_PASSWORD_LENGTH);
//add
Account *a = (Account*)malloc(sizeof(Account));
strncpy(a->username,username,MAX_ACCOUNT_USERNAME_LENGTH);
strncpy(a->password,password,MAX_ACCOUNT_PASSWORD_LENGTH);
a->next = NULL;
//insert
Account *iter;
if(accoutLinkedList == NULL){
accoutLinkedList = a;
}else{
//iterate to the last elem
iter = accoutLinkedList;
while(iter->next){
iter=iter->next;
}
//append
iter->next = a;
}
printf("New account: Username[%s]->Password[%s] appended.\n",a->username,a->password );
}
Account* findUsername(const char* username){
Account *iter;
for(iter= accoutLinkedList;iter!= NULL;iter=iter->next){
if(!strncmp(iter->username,username,MAX_ACCOUNT_USERNAME_LENGTH)){
break;
}
}
return iter;
}
void retrieveAccount(){
//input
char buffer[BUFFER_SIZE];
printf("Retrieve a account\n");
printf("Key in the username, max length allowed = %d.\n",MAX_ACCOUNT_USERNAME_LENGTH);
scanf("%s",buffer);
//find
Account *iter = findUsername(buffer);
if(!iter){
printf("Username[%s] does not exist.\n",buffer);
return;
}
printf("Username[%s] setted the password to [%s].\n",iter->username,iter->password);
}
void updateAccount(){
//input
char buffer[BUFFER_SIZE];
printf("Update a account\n");
printf("Key in the username, max length allowed = %d.\n",MAX_ACCOUNT_USERNAME_LENGTH);
scanf("%s",buffer);
//find
Account *iter = findUsername(buffer);
if(!iter){
printf("Username[%s] does not exist.\n",buffer);
return;
}
//update
printf("Old password is [%s], key in a new one, max length allowed = %d.\n",iter->password,MAX_ACCOUNT_PASSWORD_LENGTH);
scanf("%s",buffer);
strncpy(iter->password,buffer,MAX_ACCOUNT_PASSWORD_LENGTH);
printf("Username[%s] setted the password to [%s].\n",iter->username,iter->password);
}
void deleteAccont(){
//input
char buffer[BUFFER_SIZE];
printf("Update a account\n");
printf("Key in the username, max length allowed = %d.\n",MAX_ACCOUNT_USERNAME_LENGTH);
scanf("%s",buffer);
//find prev
int isFound = 0;
Account *prev = NULL;
Account *iter;
for(iter=accoutLinkedList;iter!=NULL;prev = iter,iter=iter->next){
if(!strncmp(iter->username,buffer,MAX_ACCOUNT_USERNAME_LENGTH)){
isFound = 1;
break;
}
}
if(!isFound){
printf("Username[%s] does not exist.\n",buffer);
return ;
}
if(prev){
prev->next = iter->next;
}else{
accoutLinkedList = iter->next;
}
free(iter);
printf("Deleted!");
}
void seeAllAcount(){
const Account *iter;
int counter = 0;
printf("See all accounts\n");
for(iter = accoutLinkedList;iter!=NULL;iter=iter->next){
printf("Account #%d\n",counter++);
printf("Username:%s\nPassword:%s\n\n",iter->username,iter->password);
}
}
void deleteAllAccount(){
Account *iter,*tmp;
iter = accoutLinkedList;
while(iter){
tmp = iter;
iter = iter->next;
free(tmp);
}
accoutLinkedList = NULL;
printf("Completed!\n");
}
一些测试
欢迎界面
Key in a number to select a menu... 1. Create a account 2. Retrieve password with a username 3. Update password for a username 4. Delete a existing account from the system 5. See all accounts in the system 6. Delete all accounts from the system
创建账户
Create a account The system won't check if the username already exists. Key in the username, max length allowed = 20. sun Key in the password for user[sun],max length allowed = 20. 114514 New account: Username[sun]->Password[114514] appended. Press any key to continue.
获取账户密码
//存在 Retrieve a account Key in the username, max length allowed = 20. sun Username[sun] setted the password to [114514]. Press any key to continue. //不存在 Retrieve a account Key in the username, max length allowed = 20. otaku Username[otaku] does not exist. Press any key to continue.
更新账户密码
//存在 Update a account Key in the username, max length allowed = 20. sun Old password is [114514], key in a new one, max length allowed = 20. 1919810 Username[sun] setted the password to [1919810]. Press any key to continue. //不存在 Update a account Key in the username, max length allowed = 20. foobar Username[foobar] does not exist. Press any key to continue.
删除账户
//存在 Update a account Key in the username, max length allowed = 20. sun Deleted! Press any key to continue. //不存在 Update a account Key in the username, max length allowed = 20. sun Username[sun] does not exist. Press any key to continue.
查看所有账户
See all accounts Account #0 Username:sun Password:114514 Account #1 Username:otaku Password:1919810 Account #2 Username:gnuisnotunix Password:wineisnotemulator Press any key to continue.
删除所有账户
Completed! Press any key to continue.