# 5/16-5/30 專案 11128210 林秉宏 - Homework ![](https://hackmd.io/_uploads/HJjoDZcLn.png) main ```c= #include <stdio.h> #include <stdlib.h> #include <myheader.h> #define N 10 typedef struct{ menufunc*func; char*desc; }MenItem; void printMenu(MenItem mfarr[], int n); int main() { Person group[N]; init(group, N); MenItem *mfarr[] = { {showListToHire, "Show list of positions to be hired"}, {showHiredByNumber,"Show list of already hired positions (Sort by number)"}, {showHiredByName,"Show list of already hired positions(Sort by name)"}, {hirePerson,"Hire a person"}, {firePerson,"Fire a person"}, {saveTxtFile,"Save Txt File"}, {readTxFile,"Read Txt File"}, }; int mfNum = sizeof(mfarr) / sizeof(MenItem); int n; do{ printMenu(mfarr, mfNum); scanf("%d", &n); if(n<=0 || n>mfNum) break; mfarr[n-1].func(group, N); }while(1); return 0; } void printMenu() { printf("\n"); printf("My Sample Function:\n\n"); for(int i=0; i<n; i++) printf(" %d) %s\n", i+1, mfarr[i].desc); printf("\nYour Choice:(0 to quit "); } ``` myheader ```c= #ifndef MYHEADER_H_INCLUDED #define MYHEADER_H_INCLUDED typedef struct person { int no; int hired; char name[80]; }Person; typedef void menufunc(Person g[], int n); void init(Person group[], int n); void listAll(Person group[], int n); void showListToHire(Person g[], int n); void sortListByNumber(Person group[], int n); void sortListByName(Person group[], int n); void showHiredByNumber(Person g[0], int n); void showHiredByName(Person g[], int n); void hirePerson(Person g[], int n); void firePerson(Person g[], int n); #endif // MYHEADER_H_INCLUDED ``` init ```c= #include "myheader.h" void init(Person group[], int n) { for(int i=0; i<n; i++){ group[i].no = i+1; group[i].hired = 0; group[i].name[0] = 0; } } ``` sort ```c= #include <stdlib.h> #include "myheader.h" #include "string.h" int compareNumber(const void*x, const void*y) { Person *p1 = (Person *)x; Person *p2 = (Person *)y; int pos; char *n1 = p1->name, *n2 = p2->name; for(pos=0; ; pos++){ char c1 = n1[pos], c2 = n2[pos]; if(c1 >= 'a' && c1 <= 'z') c1 += 'A' - 'a'; if(c2 >= 'a' && c2 <= 'z') c2 += 'A' - 'a'; if(c1 != c2) return c1-c2; if(c1==0 && c2==0) return0; } } void sortListByNumber(Person group[], int n) { qsort(group, n, sizeof(Person), compareNumber); } void sortListByName(Person group[], int n) { qsort(group, n, sizeof(Person), compareName); } ``` showHiredByNumber ```c= #include <stdio.h> #include "myheader.h" void showHiredByNumber(Person g[0], int n) { sortListByNumber(g, n); int cnt = 0; printf("Hired Position by Number:"); for(int i=0; i<n; i++){ if(g[i].hirde){ printf("\n %2d %s", g[i].no, g[i].name); cnt++; } } if(cnt=0) printf("<Name>"); printf("\n"); } ``` showHiredByName ```c= #include <stdio.h> #include "myheader.h" void showHiredByName(Person g[], int n) { sortListByName(g, n); int cnt = 0; printf("Hired Position by Name:"); for(int i=0; i<n; i++){ if(g[i].hired){ printf("\n %2d %s", g[i].no, g[i].name); cnt++; } } if(cnt==0) printf("<Name>"); printf("\n"); } ``` showListToHire ```c= #include <stdio.h> #include "myheader.h" void showListToHire(Person g[], int n) { sortListByNumber(g, n); printf("Position to hire:"); for(int i=0; i<n; i++){ if(g[i].hired == 0) printf("%d", i+1); } printf("\n"); } ``` hirePerson ```c= #include <stdio.h> #include "myheader.h" void hirePerson(Person g[], int n) { int p; do{ showListToHire(g, n); printf("Which Position to hire:"); scanf("%d", &p); }while(p<=0 || p>10 ||g[p-1].hired != 0); char ch; int i=0; printf("Hire Position %d, name = ", p); do{scanf("%c", &ch);} while(ch==' ' || ch=='\n'); g[p-1].name[i++] = ch; scanf("%c", &ch); while(ch != '\n'){ g[p-1].name[i++] = ch; scanf("%c", &ch); } g[p-1].name[i] = 0; g[p-1].hired = 1; printf("Position %d is hired, name = %s\n", p, g[p-1].name); } ``` firePerson ```c= #include <stdio.h> #include "myheader.h" void firePerson(Person g[], int n) { int p; do{ showHiredByNumber(g, n); printf("Which Position to fire:"); scanf("%d", &p); }while(p>0 && p<11 && g[p-1].hired != 1); g[p-1].hired = 0; g[p-1].name[0] = 0; printf("Position %d is fired\n", p); } ``` txtFile ```c= #include <stdio.h> #include "myheader.h" void saveTxtFile(Person g[], int n) { FILE *fp; if((fp=fopen("data.txt", "w")) == NULL){ printf("File open error!\n"); } else{ for(int i=0; i<n; i++){ fprintf(fp, "%d, %d, %s\n", g[i].no, g[i].hired, g[i].name); } fclose(fp); printf("File data.txt saved!\n"); } } void readTxFile(Person g[], int n) { FILE *fp; if((fp=fopen("data.txt", "r")) == NULL){ printf("File open error!\n"); } else{ for(int p=0; p<n; p++){ fscanf(fp, "%d, %d", &g[p].no, &g[p].hired); char ch; int i=0; do{ fscanf(fp, "%c", &ch); }while(ch == ' ' || ch == ','); while(ch != '\n'){ g[p].name[i++] == ch; fscanf(fp, "%c", &ch); } g[p].name[i] = 0; } fclose(fp); printf("File data.txt saved!\n"); } } ```
{"title":"5/16-5/30 專案 11128210 林秉宏","breaks":true,"contributors":"[{\"id\":\"11b07538-8b22-4195-a8b8-471dfca3f041\",\"add\":386,\"del\":386}]"}
Expand menu