Skip to content

Instantly share code, notes, and snippets.

@henrybear327
Last active August 29, 2015 14:22
Show Gist options
  • Save henrybear327/b60a27162314cd956bc3 to your computer and use it in GitHub Desktop.
Save henrybear327/b60a27162314cd956bc3 to your computer and use it in GitHub Desktop.
Banking.c
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
struct node_staff {
char name[50];
char password[50];
struct node_staff *next;
};
struct node_staff *
read_staff(struct node_staff *last); //回傳first的位址,這樣在main裡才能用
int main()
{
int i, j, k;
struct node_staff *first = NULL;
first = read_staff(first); //紀錄first的指標位址
while (1) { //功能主選單
printf("\n\n你想做什麼?\n\n");
printf("1)Load staff account\n 2)Add staff\n 3)Delete staff\n 4)Edit "
"staffname/password\n");
printf(
"5)view stafflist (in orders)\n 6)output staff info to the file\n\n");
int choice;
scanf("%d", &choice);
if (choice == 5) {
printf("Name\tpassword\n");
printf("-----------------------------------------------------------------"
"---------------\n");
struct node_staff *p;
// printf("%p\n", first);
for (p = first; p != NULL; p = p->next) {
printf("%s\t", p->name);
printf("%s\n", p->password);
}
}
}
return 0;
}
struct node_staff *read_staff(struct node_staff *first)
{
struct node_staff *previous = NULL;
FILE *fp;
fp = fopen("staff.txt", "r");
while (1) {
int i, j, k, flag = 1;
char temp[500], name[50], password[50];
for (k = 0; k < 2; k++) { //一行有2個資料 跑2次
if (fscanf(fp, "%s", temp) == EOF) {
flag = 0;
break;
}
if (temp[0] == 'N') {
for (i = 0; i < strlen(temp); i++) {
if (temp[i] == ':') {
for (j = 0; j < strlen(temp) - i - 1; j++) {
name[j] = temp[j + i + 1];
}
name[j] = '\0';
break;
}
}
} else if (temp[0] == 'p') {
for (i = 0; i < strlen(temp); i++) {
if (temp[i] == ':') {
for (j = 0; j < strlen(temp) - i - 1; j++) {
password[j] = temp[j + i + 1];
}
password[j] = '\0';
break;
}
}
}
}
if (flag == 0) {
printf("*\n");
struct node_staff *p;
for (p = first; p != NULL; p = p->next) {
printf("%s\t", p->name);
printf("%s\n", p->password);
}
fclose(fp);
return first;
}
// printf("%s %s\n", name, password);
//你都沒有使用到struct node_staff,所以上面的for迴圈當然GG阿
if (first == NULL) {
first = malloc(sizeof(struct node_staff));
strcpy(first->name, name);
strcpy(first->password, password);
first->next = NULL;
previous = first;
} else {
struct node_staff *temp = malloc(sizeof(struct node_staff));
previous->next = temp;
strcpy(temp->name, name);
strcpy(temp->password, password);
temp->next = NULL;
previous = temp;
}
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment