廢話不多數,先來簡單展示下程序界面。 學完C后老師要求寫個大點的項目,當時就隨便選了這個,結果發現難度還是有點大的,別人寫個小游戲就三四百行,我這個給整成快800行了_ ,不過最終還是寫出來了,由于初次學習C語言,編程能力有限,代碼寫的可能不夠漂亮,但還是決定拿出來分享下。供有需要的同學提供借鑒。 環境: Windows10系統+Visual Studio 2017 Community
#include<stdio.h>
#include<stdlib.h>
#include<string.h>//數據節點結構
typedef struct _node {char *Data; //字符串 int da_lg; //字符串長度 struct _node *next; //下一個節點位置struct _node *last; //上一個節點位置
}sjjd, *Sjjd; //數據節點結構//查找數據保存
typedef struct _find_data{int num; //出現編號int jd_num; //節點編號Sjjd sjjd_last; //上一個數據節點地址Sjjd sjjd_next; //下一個數據節點地址struct _find_data *fd_next; //下一個查找數據節點位置
}find_data, *Find_data;void wenbenbj_1(char *fname); //文本編輯一級
void wenbencj_2(char *fname); //文本創建一級
void theme_3(void); //主題選擇一級
void bi_ji(FILE *fp, char fname[100]); //編輯文本二級
void ch_ji(FILE *fp); //創建文本二級
char *cat_out_filename(char *fname); //截取文件名
int pdzfc(char fp); //判斷字符類別函數
void sclb(Sjjd head,char fname[100]); //輸出鏈表內容
Find_data str_fnd_xh(Sjjd head, char *str); //細化的查找功能模塊
void dest_fd(Find_data head_fd); //釋放查找結果鏈表函數
void str_fnd(Sjjd head); //字符串查找
Sjjd str_ins(Sjjd head, char *fname); //字符串插入
Sjjd str_del(Sjjd head, char *fname); //字符串刪除
Sjjd str_rep(Sjjd head, char *fname); //字符串替換
void qut_n_save(Sjjd head, FILE *fp); //退出不保存
void qut_y_save(Sjjd head, FILE *fp, char *fname); //退出并保存int main(void)
{char fname[100], tmp; //文件名包含路徑、緩沖區清理int choice1; //菜單選擇char choice2; //繼續操作選擇system("title Notepad_Simple 1.0"); //窗口標題顯示程序名do{printf("歡迎使用simple Notepad\n");printf("注意事項:本程序“文件編輯功能”目前只支持英文文本操作\n(“創建文本”功能暫不受影響),后續版本會支持中文,由\n此帶來的不便請見諒^_^!\n");printf("請選擇:\n");printf("1:打開文件\t");printf("2:創建文件\n");printf("3:主題選擇\t");printf("4:退 出\n");do{scanf("%d", &choice1);while ((tmp = getchar()) != '\n' && tmp != EOF); //清除輸入緩沖區if (choice1 < 1 || choice1 > 4)printf("沒有該選項,請重新輸入:\n");elsebreak;} while (1);switch (choice1){case 1: system("mode con cols=90"); wenbenbj_1(fname); break;case 2: system("mode con cols=60"); wenbencj_2(fname); break;case 3: theme_3(); choice2 = 'y'; goto re; break; //執行完主題操作后直接跳轉至開始界面case 4: choice2 = 'n'; goto re; break;}printf("是否繼續操作?y/n:");scanf("%c", &choice2);while ((tmp = getchar()) != '\n' && tmp != EOF); //清除輸入緩沖區re: if (choice2 == 'y') //繼續操作前清屏system("cls");} while (choice2 == 'y' );return 0;
}//函數定義區
//主函數功能模塊-一級
void wenbenbj_1(char *fname) //文本編輯-一級
{char tmp; //緩沖區清理FILE *fp;printf("請輸入要打開的文件位置及名字(不超過100個字符)\n");scanf("%s", fname);while ((tmp = getchar()) != '\n' && tmp != EOF); //清除輸入緩沖區while ((fp = fopen(fname, "r")) == NULL){printf("打開文件失敗!失敗原因可能為;\n");printf("1:文件名錯誤\n2:路徑錯誤\n3:權限錯誤\n");printf("請檢查后重新輸入文件位置及名字\n");scanf("%s", fname);while ((tmp = getchar()) != '\n' && tmp != EOF); //清除輸入緩沖區}system("cls"); //清屏bi_ji(fp, fname); //調用編輯文本函數
}
void wenbencj_2(char *fname) //文本創建-一級
{char tmp; //緩沖區清理FILE *fp;printf("請輸入要創建的文件位置及名字(不超過100個字符)\n");scanf("%s", fname);while ((tmp = getchar()) != '\n' && tmp != EOF); //清除輸入緩沖區while ((fp = fopen(fname, "w+")) == NULL){printf("創建文件失敗!失敗原因可能為;\n");printf("1:路徑錯誤\n2:權限錯誤\n");printf("請檢查后重新輸入文件位置及名字\n");scanf("%s", fname);while ((tmp = getchar()) != '\n' && tmp != EOF); //清除輸入緩沖區}system("cls");printf("文件“%s”已成功創建!\n", fname);ch_ji(fp); //調用創建文本函數while ((tmp = getchar()) != '\n' && tmp != EOF); //清除輸入緩沖區
}
void theme_3(void) //主題選擇-一級
{int choice; //主題選擇char tmp; //緩沖區清理printf("請選擇主題:\n");printf("1:護眼黑 2:經典黑\n");printf("3:自然綠 4:熱情紅\n");do{scanf("%d", &choice);while ((tmp = getchar()) != '\n' && tmp != EOF); //清除輸入緩沖區if (choice >= 1 && choice <= 4)break;elseprintf("沒有該主題,請檢查后重新輸入!\n");} while (1);switch (choice){case 1: system("color 0E"); break;case 2: system("color 0F"); break;case 3: system("color 2F"); break;case 4: system("color 4F"); break;}
}//文本編輯函數-二級定義開始
void bi_ji(FILE *fp, char fname[100])
{char str[30], *arr, ch; //臨時存放字符串、鏈表中字符串、符號判斷 char flag1, tmp; //(flag1、flag2)繼續操作判斷用、清除輸入緩沖用int i, lg, choice, quit_mode; //控制變量、節點數據長度、操作選擇Sjjd head, tail, p;head = tail = NULL;//讀取文本內容到鏈表中開始while ( 1 ){ i = 0;ch = fgetc(fp), fseek(fp, -1L, SEEK_CUR);if (ch == EOF)break;//一個符號(空格、回車、制表符、等字符)if (pdzfc(ch) == 0){str[i] = fgetc(fp), str[1] = '\0';arr = (char *)malloc(2), strcpy(arr, str);if (head == NULL){head = tail = (Sjjd)malloc(sizeof(sjjd));tail->Data = arr, tail->da_lg = (*arr == '\t') ? 8 : 1, tail->next = tail->last = NULL;}else{p = (Sjjd)malloc(sizeof(sjjd));p->Data = arr, p->da_lg = (*arr == '\t') ? 8 : 1, p->next = NULL;tail->next = p, p->last = tail;tail = p;}}else //一個單詞{i = 0;while (pdzfc(ch)){str[i] = fgetc(fp);ch = fgetc(fp), fseek(fp, -1L, SEEK_CUR);i++;}str[i] = '\0';lg = (int)strlen(str);arr = (char *)malloc(lg + 1), strcpy(arr, str);if (head == NULL){head = tail = (Sjjd)malloc(sizeof(sjjd));tail->Data = arr, tail->da_lg = lg, tail->next = tail->last = NULL;}else{p = (Sjjd)malloc(sizeof(sjjd));p->Data = arr, p->da_lg = lg, p->next = NULL;tail->next = p, p->last = tail;tail = p;}if (ch == EOF) //排除最后以一個單詞結束的文件break;}} //讀取文本內容到鏈表中結束if (head == NULL) //將鏈表輸出-空{printf("該文件為空文件,請切換至創建文本操作!\n"); //空文件輸出提示fclose(fp);goto end;}elsesclb(head, fname); //將鏈表輸出-非空//進入編輯操作do{ //編輯操作菜單printf("請選擇操作:\n");printf("1:字符串查找\n");printf("2:字符串插入\n");printf("3:字符串刪除\n");printf("4:字符串替換\n");do{choice = 0; //清除上次成功通過判斷時的數據scanf("%d", &choice);while ((tmp = getchar()) != '\n' && tmp != EOF); //清除輸入緩沖區if (choice < 1 || choice > 4)printf("沒有該選項,請重新輸入:\n");elsebreak;} while (1);switch (choice){case 1:str_fnd(head); break; //字符串查找case 2:head = str_ins(head, fname); break; //字符串插入case 3:head = str_del(head, fname); break; //字符串刪除case 4:head = str_rep(head, fname); break; //字符串替換} printf("是否退出文件編輯操作?y/n\n");flag1 = getchar();while ((tmp = getchar()) != '\n' && tmp != EOF); //清除輸入緩沖區if (flag1 == 'n') //繼續操作前先清屏,然后再輸出文件內容{system("cls"); //清屏if (head == NULL) //將鏈表輸出-空{printf("該文件為空文件,請切換至創建文本操作!\n"); //空文件輸出提示goto end;}elsesclb(head, fname); //將鏈表輸出-非空}} while (flag1 == 'n');//退出模式選擇printf("退出模式選擇:\n");printf("1:退出不保存\n");printf("2:退出并保存\n");do{scanf("%d", &quit_mode);while ((tmp = getchar()) != '\n' && tmp != EOF); //清除輸入緩沖區if (quit_mode < 1 || quit_mode > 2)printf("沒有該選項,請重新輸入:\n");elsebreak;} while (1);if (quit_mode == 1)qut_n_save(head, fp); //退出不保存elsequt_y_save(head, fp,fname); //退出并保存end:;
} //文本編輯函數定義結束//創建文本函數-二級定義
void ch_ji(FILE *fp)
{char ch;printf("請輸入文本內容:(輸入以“#”結束)\n");do{ch = getchar();if (ch == '#')break;fputc(ch, fp);} while (1);if (fclose(fp) == 0)printf("內容已成功寫入到文件中!\n");elseprintf("寫入到文件中失敗!\n");
}//輔助函數定義區
//輸出鏈表內容函數定義
void sclb(Sjjd head, char fname[100])
{int toal_jd=0, line_nm = 1, line_jd=0, line_bt_num=0; //該行之前總節點個數,行號(不隨每行結束清零)。該行節點個數,行字節(隨每行結束清零)int i, j, filename_L;char *filename; //存儲文件名(不包括路徑)Sjjd p = head;filename = cat_out_filename(fname); //提取文件名filename_L = (int)strlen(filename); //計算文件名長度for (i = 1; i < 60; i++) //文件輸出前首行提示{putchar('*');if (i == (59 - filename_L) / 2){for (j = 0; j < filename_L; j++)putchar(filename[j]);i += filename_L;}} printf("|");printf("toal_jd line_jd line_nm\n"); //每個信息占八個位寬,相鄰信息間隔為兩個空格do //文件內容輸出開始{if (*(p->Data) != '\n') //判別換行符"\n"的處理{ //非換行符處理if (*(p->Data) == '\t'){//水平制表符處理(水平制表符形式上用連續的8個空格代替),也記為一個節點等同于其符號字符進行統計處理for (i = 1; i <= 8; i++) //空格填充對齊{putchar(' ');}toal_jd++, line_jd++, line_bt_num += p->da_lg;}else{ //普通字符處理(空格包含在內),直接輸出printf("%s", p->Data), toal_jd++, line_jd++, line_bt_num += p->da_lg;}}else{ //換行符處理(換行符形式上用連續的空格代替),換行符也記為一個節點等同于其符號字符進行統計處理toal_jd++, line_jd++;}if ( ((line_bt_num >= 45 && p->next != NULL) && (p->next->da_lg != 1 || *(p->next->Data) == '\n')) || *(p->Data) == '\n') //行尾處理{for (i = 1; i < 60 - line_bt_num; i++) //空格填充對齊{putchar(' ');} printf("|");printf("%-8d %-8d %-8d\n", toal_jd - line_jd, line_jd, line_nm); //每個信息占八個位寬,相鄰信息間隔為兩個空格line_nm++, line_jd = 0, line_bt_num = 0;}if (p->next == NULL) //輸出結束判斷{for (i = 1; i < 60 - line_bt_num; i++) //空格填充對齊{putchar(' ');} printf("|");printf("%-8d %-8d %-6d\n", toal_jd - line_jd, line_jd, line_nm); //每個信息占八個位寬,相鄰信息間隔為兩個空格for (i = 1; i < 60; i++) //文件輸出結束提示行{putchar('*');if (i == (59 - 8) / 2){printf("FILE-END");i += 8;}} printf("|%-8d\n", toal_jd);break;}p = p->next;} while (1);}//判斷字符類別函數定義
int pdzfc(char ch)
{int flag = 0;if (ch >= '0' && ch <= '9')flag = 1;if (ch >= 'a' && ch <= 'z')flag = 1;if (ch >= 'A' && ch <= 'Z')flag = 1;return flag;
}//截取文件名函數定義
char *cat_out_filename(char *fname)
{static char filename[50];if (strrchr(fname, '\\') == NULL)strcpy(filename, fname);elsestrcpy(filename, strrchr(fname, '\\') + 1);return filename;
}//字符串查找函數定義開始
void str_fnd(Sjjd head)
{char str[50], tmp;Find_data head_fd, p_fd;printf("請輸入要查找的字符串:(只支持單個單詞或字符串!)\n");scanf("%s", str);while ((tmp = getchar()) != '\n' && tmp != EOF); //清除輸入緩沖區if (strcmp(str, "\\t") == 0) //對制表符進行處理strcpy(str, "\t");else if (strcmp(str, "\\n") == 0) //對回車進行處理strcpy(str, "\n");else if (strcmp(str, "\\s") == 0) //對空格進行處理strcpy(str, " ");head_fd = str_fnd_xh(head, str);dest_fd(head_fd); //釋放查找鏈表空間
} //字符串查找函數定義結束//細化的查找功能模塊開始
Find_data str_fnd_xh(Sjjd head, char *str)
{Sjjd p = head;Find_data head_fd, tail_fd, p_fd; //定義查找結果鏈表相關指針head_fd = tail_fd = NULL; //查找結果結果鏈表指針初始化int toal_jd, line_nm, line_bt_num, line_jd, enter_jd, cnt, flag = 0; //待查找字符串在文件中位置(節點、行號、行字節數、行節點、回車出現的節點、出現次數)cnt = toal_jd = line_jd =line_bt_num = 0, line_nm = 1;do{if (*(p->Data) != '\n') //判別換行符"\n"的處理{ //非換行符處理if (*(p->Data) == '\t')//水平制表符處理(水平制表符形式上用連續的8個空格代替),也記為一個節點等同于其符號字符進行統計處理toal_jd++, line_jd++, line_bt_num += 8;else //普通字符處理,直接輸出toal_jd++, line_jd++, line_bt_num += p->da_lg;}else //換行符處理(換行符形式上用連續的空格代替),換行符也記為一個節點等同于其符號字符進行統計處理toal_jd++, line_jd++, enter_jd = line_jd;if (((line_bt_num >= 45 && p->next != NULL) && (p->next->da_lg != 1 || *(p->next->Data) == '\n')) || *(p->Data) == '\n') //行尾處理line_nm++, line_jd = 0, line_bt_num = 0;if (!strcmp(p->Data, str)){cnt++;if(strcmp(p->Data, "\n") == 0)printf("num:%-6d line_nm:%-6d toal_jd:%-6d line_jd:%-6d\n", cnt, line_nm-1, toal_jd, enter_jd);elseprintf("num:%-6d line_nm:%-6d toal_jd:%-6d line_jd:%-6d\n", cnt, line_nm, toal_jd, line_jd);p_fd = (Find_data)malloc(sizeof(find_data));p_fd->sjjd_next = p->next, p_fd->sjjd_last = p->last, p_fd->num = cnt, p_fd->jd_num = toal_jd, p_fd->fd_next = NULL;if (head_fd == NULL)head_fd = tail_fd = p_fd;elsetail_fd->fd_next = p_fd; tail_fd = p_fd;flag = 1;}if (p->next == NULL)break;elsep = p->next;} while (1);if (flag == 0){printf("查找失敗,該字符串不存在!\n");}return head_fd;
} //細化的查找模塊結束//字符串插入函數定義開始
Sjjd str_ins(Sjjd head,char *fname)
{char str[50], tmp; //待插入字符臨時存放位置、清除輸入緩沖區用Sjjd p = head, p1; //p初始化指向頭指針int toal_jd, i; //待插入的位置、控制變量printf("輸入待插入的單個字符串及其插入位置(<=1插在第一位,>尾節點插在最后):");scanf("%s %d", str, &toal_jd);while ((tmp = getchar()) != '\n' && tmp != EOF); //清除輸入緩沖區if (strcmp(str, "\\t") == 0) //對制表符進行處理strcpy(str, "\t");else if (strcmp(str, "\\n") == 0) //對回車進行處理strcpy(str, "\n");else if (strcmp(str, "\\s") == 0) //對空格進行處理strcpy(str, " ");for (i = 1; i < toal_jd && p->next != NULL; i++, p = p->next); //移動位置指針位置指針到插入位置(只要輸入的節點數大于總節點數就視為插在最后一個位置)if (p->last == NULL) //插入位置在頭節點{p1 = (Sjjd)malloc(sizeof(sjjd));p1->Data = strcpy((char *)malloc(strlen(str) + 1), str), p1->da_lg = (*str == '\t' ? 8 : strlen(str));p1->last = NULL, p1->next = p;head = p->last = p1;}else if (p->next == NULL && i < toal_jd) //插入位置在尾節點{p1 = (Sjjd)malloc(sizeof(sjjd));p1->Data = strcpy((char *)malloc(strlen(str) + 1), str), p1->da_lg = (*str == '\t' ? 8 : strlen(str));p1->next = NULL, p1->last = p;p->next = p1;}else //插入位置在中間{p1 = (Sjjd)malloc(sizeof(sjjd));p1->Data = strcpy((char *)malloc(strlen(str) + 1), str), p1->da_lg = (*str == '\t' ? 8 : strlen(str));p->last->next = p1, p1->last = p->last, p1->next = p, p->last = p1;}system("cls"); //清屏printf("插入后的文件內容如下:\n");sclb(head, fname);return head;
}//字符串插入函數定義結束//字符串刪除函數定義開始
Sjjd str_del(Sjjd head, char *fname)
{char str[50], tmp; //待刪除字符串、清除緩沖區用Find_data head_fd, p_fd; //定義查找結果鏈表相關指針int choice, i;printf("請輸入要刪除的字符串:");scanf("%s", str);while ((tmp = getchar()) != '\n' && tmp != EOF); //清除輸入緩沖區if (strcmp(str, "\\t") == 0) //對制表符進行處理strcpy(str, "\t");else if (strcmp(str, "\\n") == 0) //對回車進行處理strcpy(str, "\n");else if (strcmp(str, "\\s") == 0) //對空格進行處理strcpy(str, " ");p_fd = head_fd = str_fnd_xh(head, str); //調用查找函數查找對應內容if (head_fd == NULL) //要刪除的字符串不存在處理goto end;printf("請選擇刪除對象(“0”表示刪除全部查找到的對象,選擇刪除時每次只能刪除一個!):\n");
re: scanf("%d", &choice);while ((tmp = getchar()) != '\n' && tmp != EOF); //清除輸入緩沖區if (choice < 0) //限定輸入的對象只能為>=0的數{printf("輸入數據有誤!請重新輸入:");goto re;}p_fd = head_fd; //goto的加入使得要添加該語句if (choice == 0) //查找到內容全部刪除{do{ //刪除操作開始if (p_fd->sjjd_last == NULL) //頭節點的刪除{free(p_fd->sjjd_next->last->Data); //釋放該節點存放的字符串空間free(p_fd->sjjd_next->last); //釋放該節點所占內存空間head = p_fd->sjjd_next; //頭結點下移一位head->last = NULL;}else if (p_fd->sjjd_next == NULL) //尾節點的刪除{free(p_fd->sjjd_last->next->Data); //釋放該節點存放的字符串空間free(p_fd->sjjd_last->next); //釋放該節點所占內存空間p_fd->sjjd_last->next = NULL;}else //中間節點的刪除{free(p_fd->sjjd_next->last->Data);free(p_fd->sjjd_next->last);p_fd->sjjd_last->next = p_fd->sjjd_next; //上一個節點的next域與下一個節點相連p_fd->sjjd_next->last = p_fd->sjjd_last; //下一個節點的last域與上一個節點相連}if (p_fd->fd_next == NULL)break;p_fd = p_fd->fd_next;} while (1);}else //刪除指定的查找內容{for (i = 1; i < choice; p_fd = p_fd->fd_next, i++) //移動到要刪除的節點{if (p_fd->fd_next == NULL) //輸入的序號超過查找到的個數{printf("你的輸入有誤,請檢查后重新輸入!");goto re;}}//刪除操作開始if (p_fd->sjjd_last == NULL) //頭節點的刪除{free(p_fd->sjjd_next->last->Data); //釋放該節點存放的字符串空間free(p_fd->sjjd_next->last); //釋放該節點所占內存空間head = p_fd->sjjd_next; //頭結點下移一位head->last = NULL;}else if (p_fd->sjjd_next == NULL) //尾節點的刪除{free(p_fd->sjjd_last->next->Data); //釋放該節點存放的字符串空間free(p_fd->sjjd_last->next); //釋放該節點所占內存空間p_fd->sjjd_last->next = NULL;}else //中間節點的刪除{free(p_fd->sjjd_next->last->Data);free(p_fd->sjjd_next->last);p_fd->sjjd_last->next = p_fd->sjjd_next; //上一個節點的next域與下一個節點相連p_fd->sjjd_next->last = p_fd->sjjd_last; //下一個節點的last域與上一個節點相連}}dest_fd(head_fd); //釋放查找鏈表空間system("cls"); //清屏printf("刪除操作完成!刪除后的內容如下:\n");sclb(head, fname);end: return head;
}//字符串刪除函數定義結束//字符串替換函數定義開始
Sjjd str_rep(Sjjd head, char *fname)
{char str[50],str_tar[50],tmp; //待替換字符串、清除緩沖區用Find_data head_fd, p_fd, p1; //定義查找結果鏈表相關指針int choice, i;printf("請輸入被替換的字符串和替換后的字符串:");scanf("%s %s", str, str_tar);while ((tmp = getchar()) != '\n' && tmp != EOF); //清除輸入緩沖區//str的處理if (strcmp(str, "\\t") == 0) //對制表符進行處理strcpy(str, "\t");else if (strcmp(str, "\\n") == 0) //對回車進行處理strcpy(str, "\n");else if (strcmp(str, "\\s") == 0) //對空格進行處理strcpy(str, " ");//str_tar的處理if (strcmp(str_tar, "\\t") == 0) //對制表符進行處理strcpy(str_tar, "\t");else if (strcmp(str_tar, "\\n") == 0) //對回車進行處理strcpy(str_tar, "\n");else if (strcmp(str_tar, "\\s") == 0) //對空格進行處理strcpy(str_tar, " ");p_fd = head_fd = str_fnd_xh(head, str); //調用查找函數查找對應內容if (head_fd == NULL) //待替換字符不存在的處理goto end;printf("請選擇替換對象(“0”表示替換全部查找到的對象,選擇替換時每次只能替換一個!):\n");
re: scanf("%d", &choice);while ((tmp = getchar()) != '\n' && tmp != EOF); //清除輸入緩沖區if (choice < 0) //限定輸入的對象只能為>=0的數{printf("輸入數據有誤!請重新輸入:");goto re;}p_fd = head_fd; //goto的加入使得要添加該語句if (choice == 0) //查找到內容全部替換{do{ //替換操作開始if (p_fd->sjjd_last == NULL) //頭節點的替換{free(p_fd->sjjd_next->last->Data); //釋放該節點存放的字符串空間p_fd->sjjd_next->last->Data = strcpy((char *)malloc(strlen(str_tar) + 1), str_tar); //替換p_fd->sjjd_next->last->da_lg = (*str_tar == '\t' ? 8 : strlen(str_tar)); //替換后的數據長度 }else if (p_fd->sjjd_next == NULL) //尾節點的替換{free(p_fd->sjjd_last->next->Data); //釋放該節點存放的字符串空間p_fd->sjjd_last->next->Data = strcpy((char *)malloc(strlen(str_tar) + 1), str_tar); //替換p_fd->sjjd_last->next->da_lg = (*str_tar == '\t' ? 8 : strlen(str_tar)); //替換后的數據長度}else //中間節點的替換{free(p_fd->sjjd_next->last->Data); //釋放該節點存放的字符串空間p_fd->sjjd_next->last->Data = strcpy((char *)malloc(strlen(str_tar) + 1), str_tar); //替換p_fd->sjjd_next->last->da_lg = (*str_tar == '\t' ? 8 : strlen(str_tar)); //替換后的數據長度}if (p_fd->fd_next == NULL)break;p_fd = p_fd->fd_next; } while (1);}else //替換指定的查找內容{for (i = 1; i < choice; p_fd = p_fd->fd_next, i++) //移動到要刪除的節點{if (p_fd->fd_next == NULL) //輸入的序號超過查找到的個數{printf("你的輸入有誤,請檢查后重新輸入!");goto re;}}//替換操作開始if (p_fd->sjjd_last == NULL) //頭節點的替換{free(p_fd->sjjd_next->last->Data); //釋放該節點存放的字符串空間p_fd->sjjd_next->last->Data = strcpy((char *)malloc(strlen(str_tar) + 1), str_tar); //替換p_fd->sjjd_next->last->da_lg = (*str_tar == '\t' ? 8 : strlen(str_tar)); //替換后的數據長度}else if (p_fd->sjjd_next == NULL) //尾節點的替換{free(p_fd->sjjd_last->next->Data); //釋放該節點存放的字符串空間p_fd->sjjd_last->next->Data = strcpy((char *)malloc(strlen(str_tar) + 1), str_tar); //替換p_fd->sjjd_last->next->da_lg = (*str_tar == '\t' ? 8 : strlen(str_tar)); //替換后的數據長度}else //中間節點的替換{free(p_fd->sjjd_next->last->Data); //釋放該節點存放的字符串空間p_fd->sjjd_next->last->Data = strcpy((char *)malloc(strlen(str_tar) + 1), str_tar); //替換p_fd->sjjd_next->last->da_lg = (*str_tar == '\t' ? 8 : strlen(str_tar)); //替換后的數據長度}}dest_fd(head_fd); //釋放查找鏈表空間system("cls"); //清屏printf("替換操作完成!替換后的內容如下:\n");sclb(head, fname);end: return head;
}//字符串替換函數定義結束//退出編輯模式不保存函數定義開始
void qut_n_save(Sjjd head, FILE *fp)
{Sjjd p1, p2=p1=head;//銷毀鏈表while (p1->next != NULL){free(p1->Data);p2 = p1->next;free(p1);p1 = p2;}if (fclose(fp) == 0)printf("操作成功!已關閉文件指針。\n");elseprintf("操作失敗!關閉文件指針失敗。\n");
} //退出編輯模式不保存函數定義結束//退出編輯模式并保存函數定義開始
void qut_y_save(Sjjd head, FILE *fp,char *fname)
{Sjjd p1, p2 = p1 = head;fclose(fp); fp = fopen(fname, "w"); //改變文件打開屬性,切換為寫操作//將鏈表內容輸入到文件中do{fprintf(fp, "%s", p1->Data);if (p1->next == NULL)break;elsep1 = p1->next;} while (1);//銷毀鏈表p1 = p2;while (p1->next != NULL){free(p1->Data);p2 = p1->next;free(p1);p1 = p2;}if (fclose(fp) == 0)printf("操作成功!文件已保存,文件指針已關閉。\n");elseprintf("操作失敗!文件保存失敗,關閉文件指針失敗。\n");
}//退出編輯模式并保存函數定義結束void dest_fd(Find_data head_fd) //釋放查找結果鏈表函數
{Find_data p_fd = head_fd;while (head_fd != NULL && head_fd->fd_next != NULL){head_fd = head_fd->fd_next;free(p_fd); p_fd = head_fd; }free(p_fd);
}
總結
以上是生活随笔 為你收集整理的C语言工程实践-简单文本编辑器 的全部內容,希望文章能夠幫你解決所遇到的問題。
如果覺得生活随笔 網站內容還不錯,歡迎將生活随笔 推薦給好友。