I.
- 定義一個結構用來放個人通訊錄
- name: 10 char
- phone: 15 char
- email: 25 char
- age: int
- 首先輸入一個int 代表通訊錄要設多大 (至少4人)
- main()用 new 配置通訊錄的大小
- 將該通訊錄傳給一function 做輸入(Call by address)
- 將該通訊錄傳給一function 做輸出(列印通訊錄資料)
- 最後, 不用時要delete 該通訊錄
II.
- 寫一個function, 把副檔名”cpp”換成”c” (字串必須更改,不可以只有輸出更改)
- 輸入字串 “d:\vc\abc.cpp”
- 輸出 “d:\vc\abc.c”
III.
- Displaying a Sentence with Its Words Reversed
- strtok(), please google to find how to use strtok()
- e.g. input: I will go to school
- Output: school to go will I
Solution Code
#include <stdio.h> #include <stdlib.h> #include <string.h> //constants //first_question #define SIZE_NAME 10 #define SIZE_PHONE 15 #define SIZE_EMAIL 25 //second_question #define SIZE_FNAME 100 //third question #define SIZE_SENTENCE 500 //struct struct contacts_data { char name[SIZE_NAME],phone[SIZE_PHONE],email[SIZE_EMAIL]; int age,index; }; //type define typedef struct contacts_data CON_DATA; //phototype void first_question(void); void q1_input(CON_DATA *data, int size); void q1_printf(CON_DATA *data, int size); void second_question(void); void q2_change_name(char f_name[]); void third_question(void); void q3_reverse(char sentence[], int size); void q3_reverse_words(char sentence[]); void main (void) { for(;;) { int question=0; char again=0; printf("Please enter number to choose question to Demo>"); scanf_s("%d",&question); switch(question) { case 1: first_question(); break; case 2: second_question(); break; case 3: third_question(); break; default: printf("The question is not exists\n"); } printf("Demo other question ? [Y/N]"); while(again != 'Y' && again != 'y' && again !='n' && again !='N') { fflush(stdin); again=getchar(); } if(again == 'n' || again == 'N') break; } system("pause"); } void first_question(void) { int size; CON_DATA *data_p; //引導輸入資料筆數 printf("Please determine the size of contacts >"); scanf_s("%d",&size); //產生動態結構儲存 data_p = new CON_DATA[size]; q1_input(data_p,size); q1_printf(data_p,size); delete [] data_p; } void q1_input(CON_DATA *data, int size) { printf("Now, please input data.\n"); for(int i=0;i<size;i++) { (*(data+i)).index=i+1; printf("%d-th data input\n",i+1); printf("\tName > "); fflush(stdin); gets(((*(data+i)).name)); fflush(stdin); //scanf_s("%s",((*(data+i)).name),SIZE_NAME); printf("\tPhone > "); scanf_s("%s",&((*(data+i)).phone),SIZE_PHONE); printf("\tEmail > "); scanf_s("%s",&((*(data+i)).email),SIZE_EMAIL); printf("\tAge > "); scanf_s("%d",&((*(data+i)).age)); } } void q1_printf(CON_DATA *data, int size) { printf("Now, the program will print contacts...\n"); printf("Index\tName \tPhone \tEmail \tAge\n"); for(int i=0;i<size;i++) printf("%2d\t%-10s\t%-15s\t%-25s\t%2d\n",(*(data+i)).index,(*(data+i)).name,(*(data+i)).phone,(*(data+i)).email,(*(data+i)).age); } void second_question(void) { char f_name[SIZE_FNAME]; printf("input file name >"); fflush(stdin); gets(f_name); q2_change_name(f_name); printf("The new name > %s",f_name); printf("\n"); } void q2_change_name(char f_name[]) { int i=0; while(f_name[i] !=46 && ((f_name[i+1] !=67 && f_name[i+2] != 80 && f_name[i+3] != 80) || (f_name[i+1] !=99 && f_name[i+2] != 112 && f_name[i+3] != 112))) i++; f_name[i+2]=NULL; f_name[i+3]=NULL; } void third_question(void) { char sentence[SIZE_SENTENCE]; printf("please enter sentence >"); fflush(stdin); gets(sentence); //統計長度 int total_len=strlen(sentence); //反轉整句 q3_reverse(sentence,total_len); //反轉單字 q3_reverse_words(sentence); //經過strtok的句子空白部分變成了\0不能直接用%s印出,會斷掉 for(int i=0;i<total_len;i++) printf("%c",sentence[i]); printf("\n"); } void q3_reverse(char sentence[], int size) { for(int i=0;i<size/2;i++) { char temp=sentence[i]; sentence[i]=sentence[size-1-i]; sentence[size-1-i]=temp; } } void q3_reverse_words(char sentence[]) { char *tag_s; tag_s = strtok(sentence," "); while(tag_s != NULL) { q3_reverse(tag_s,strlen(tag_s)); tag_s = strtok(NULL," "); } }
沒有留言:
張貼留言