#include<stdio.h>
#include<stdlib.h>
#include<string.h>
#define WHO_LEN 10
#define WHAT_LEN 40
#define WHEN_LEN 5
#define WHERE_LEN 20
#define MAX 10           /* 資料最大10筆     */
#define ENTER 10         /* Enter 之 ASCII 碼*/ 
#define SPACE 32         /* Space 之 ASCII 碼*/ 
struct Node                                      /* 宣告 Node 節點結構 */ 
{
    char name[WHO_LEN];
	char affair[WHAT_LEN];
	char date[WHEN_LEN];
	char place[WHERE_LEN];
	struct Node *next;
};
typedef struct Node *DATA;                       /* Linked List 節點的新型態   */
char menu(int *);                                           /* 傳入的 BEGIN 在第二次執行的時候會改變，所以用指標傳入位址  */
DATA EnterRecord(int *,DATA);
void displayDATA(DATA);
int main(void)
{
	int BEGIN=1;                                        /* 程式開始設為1，當 menu 執行過一次將改為 0                  */
	char choice;
	int count=0;                             /* 資料總數，預設為 0     */ 
	DATA first=NULL;                         /* Linked list 的開頭指標 */
	FILE *fptr1;
	FILE *fptr2;
	printf("讀取檔案後，目前資料有筆\n");
	choice=(menu(&BEGIN));
	while(choice!='e' || choice!='E')
	{
		switch(choice)
		{
			case '1':
				first=(EnterRecord(&count,first));
				choice=menu(&BEGIN);         /* 回到選單                    */
				break;
			case '2':
				choice=menu(&BEGIN);         /* 回到選單                    */
				break; 
         		case '3':
				choice=menu(&BEGIN);         /* 回到選單                    */
                        	break;
			case '4':
				displayDATA(first);
				choice=menu(&BEGIN);         /* 回到選單                    */
                        	break;
			case '5':
				choice=menu(&BEGIN);         /* 回到選單                    */
                        	break;
			case '6':
				choice=menu(&BEGIN);         /* 回到選單                    */
                        	break;
			case '7':
				choice=menu(&BEGIN);         /* 回到選單                    */
				break;
			case '8':
				choice=menu(&BEGIN);         /* 回到選單                    */
				break;
			case '9':
				choice=menu(&BEGIN);         /* 回到選單                    */
				break;
			case 'e':
			case 'E':
				printf("資料是否儲存?(案y是，否則程式結束):");
				scanf(" %c",&choice);
				if(choice=='y' || choice=='Y')
				{
					fptr2=fopen("data.txt","w");          /* 開檔，存取模式 w，不需要偵測檔案是否開啟失敗 */
					if(1)
						printf("檔案儲存完畢\n");
					fclose(fptr2);
				}
                        	return 0;
		}
	}
}
char menu(int *begin)
{
	char tmp[10];
	char c;
	if(*begin==1)
	{
		printf("組員:\n");
	}
	do{
		fflush(stdin);
		printf("==================\n");
		printf("請輸入選擇:\n");
		printf("Enter record :請按1\n");
		printf("View  day    :請按2\n");
		printf("View  week   :請按3\n");
		printf("View  all    :請按4\n"); 
		printf("Modify       :請按5\n");
		printf("Delete       :請按6\n");
		printf("Search       :請按7\n");
		printf("Show duration:請按8\n");
		printf("Merge        :請按9\n");
		printf("Exit         :請按E\n");
		printf("==================\n");
		scanf(" %c",&c);
		if((c!='1') && (c!='2') && (c!='3') && (c!='4') && (c!='5') && (c!='6') && (c!='7') && (c!='8') && (c!='9') && (c!='e') && (c!='E'))
			printf("請輸入正確的選擇!\n");
	}while((c!='1') && (c!='2') && (c!='3') && (c!='4') && (c!='5') && (c!='6') && (c!='7') && (c!='8') && (c!='9') && (c!='e') && (c!='E'));
	return c;
}
DATA EnterRecord (int *num,DATA first) /* 選項一，輸入資料 */
{
	char ch;                         /* 讀取空白或字元                  */
	char choice;                     /* 讓使用者輸入是否繼續的選擇      */
	DATA new_record;
	char *str; 			 /* 宣告字元陣列，儲存鍵盤輸入的字串*/
	do
	{
		fflush(stdin);
		new_record=(DATA)malloc(sizeof(DATA));                    /* 配置節點記憶體                         */
		str=(char *)malloc(WHO_LEN*sizeof(char));                      /* 配置 who 字串的記憶體             */
		do
		{
			printf("請輸入人名,案Enter鍵結束輸入:");
			scanf("%s",str);
			if(strlen(str)>=WHO_LEN)
				printf("輸入錯誤!!\n");
		}while(strlen(str)>=WHO_LEN);
		strcpy(new_record->name,str);
		scanf("%c",&ch);                                               /* 讀取 Enter                        */
		free(str);                 		                       /* 釋放 WHO  字串的記憶體            */
		str=(char *)malloc(WHAT_LEN*sizeof(char));   		       /* 配置 what 字串的記憶體            */
		printf("請輸入要做的事情，案Enter鍵結束:");
		scanf("%s",str);
		strcpy(new_record->affair,str);
		scanf("%c",&ch);                                               /* 讀取 Enter                        */
		free(str);                 		                       /* 釋放 what 字串的記憶體            */
		str=(char *)malloc(WHEN_LEN*sizeof(char)); 		       /* 配置 when 字串的記憶體            */
		printf("請輸入日期，案Enter鍵結束輸入:");
		scanf("%s",str);
		strcpy(new_record->date,str);
		scanf("%c",&ch);                                               /* 讀取 Enter                        */
		free(str);                                 		       /* 釋放 when 字串的記憶體            */
		str=(char *)malloc(WHERE_LEN*sizeof(char));		       /* 配置 where 字串的記憶體           */
		printf("請輸入地名，案Enter鍵結束輸入:");
		scanf("%s",str);
		strcpy(new_record->place,str);
		scanf("%c",&ch);                           		       /* 讀取 Enter                        */
		free(str);                                  		       /* 釋放 where 字串的記憶體           */
		new_record->next=first;                                 /* 新節點的 next 指向目前開頭的 first       */
		first=new_record;                                       /* 新節點成為第一個節點                     */
		*num=*num+1;
		printf("是否繼續輸入資料?(案y繼續or其他鍵離開)");
		scanf(" %c",&choice);
	}while(choice=='y' || choice=='Y');
	return first;
}
void displayDATA(DATA first)
{
	int i;
	for(i=0;first;first=first->next)
	{
		printf("第%d筆資料\n",++i);
		printf("who   :%s\n",first->name);                              
		printf("what  :%s\n",first->affair);
		printf("when  :%s\n",first->date);
		printf("where :%s\n",first->place);
	}
	return;
}
