當前位置:首頁 » 編程語言 » c語言gf

c語言gf

發布時間: 2023-07-19 23:21:34

c語言簡單行編輯器

/*
c語言程序設計 簡單的行編輯器
【要求】
(1) 設置一個簡單的行編輯器,每行以回車結束
(2) 數據以文件形式存儲
(3) 編輯器具有查找、替換、修改數據的功能

【備注】完全原創,編寫時間:2010-7-13。請把所有的注釋信息提取出來就可以寫程序設計報告。
*/
#include /*標准文件流操作,這里使用了fopen/fclose/fprintf/printf/scanf/gets函數*/
#include /*標准系統庫,這里使用了malloc/free/exit*/
#include /*標准字元串庫,這里使用strlen/strcpy/memcpy/memset*/
#define szLINE 252 /*定義一行字元串最長為252位元組*/
#define CMDS 12 /*定義12個標准行編輯命令*/

/*採用鏈表存儲文本*/
typedef struct LINE {
char text[szLINE]; /*文本內容*/
struct LINE * next; /*鏈表指針*/
} L;

/*簡寫無類型整數*/
typedef unsigned int U;

/*定義12個行編輯命令的標准格式*/
typedef void (*FUNC)(L **, char*);

/*定義12個標准行編輯命令的關鍵字*/
char keywords[CMDS][8]={
"quit", "help", "load", "save",
"view", "count", "append", "insert",
"erase", "edit", "lookup", "replace"
};/*end keywords*/

/*清空鏈表操作*/
void clear(L ** lines)
{
L * a = 0, * b = 0;
if(!lines) return ;
a = *lines;
while(a) {
b = a->next ;
free(a);
a = b;
}/*end while*/
*lines = 0;
}/*end clear*/

/*在鏈表中根據行號index調出指定的行*/
L * locate(L * lines, U index)
{
L * t = lines; U i = 0;
if(!t) return 0;
if(index == 0) return t;
for(i = 0; i < index; i++) {
t = t->next;
if(!t) return 0;
}/*next*/
return t;
}/*end locate*/

/*瀏覽命令,如果f存在則以帶行號格式保存文件(如果f==stdout則列印到屏幕上),
瀏覽范圍為from到to(行號)。view(lines, 0, 0, 0)表示統計已載入到內存的文本行數量*/
int view(L * lines, FILE * f, U from, U to)
{
L * t = lines; U index = 0;
while(t) {
index ++;
if(f && index >= from && index text);
t = t->next;
}/*end while*/
return index;
}/*end view*/

/*在當前文檔中根據關鍵字進行搜索,並將搜索結果列印出來*/
void lookup(L * lines, char * string)
{
L * t = 0; U index = 0;
if(!string) return ;
t = lines;
while(t) {
index ++;
if(strstr(t->text , string)) printf("%d: %s", index, t->text );
t=t->next;
}/*end while*/
}/*end lookup*/

/*在一行文本中執行替換命令,把所有關鍵字替換為新關鍵字*/
void rpc(char * string, char * key, char * replacement)
{
char fine[szLINE], * x = 0, * y = 0, * z = 0;
int la = 0, lb = 0, r = 0;
if(!string || !key || !replacement) return ;
memset(fine, 0, szLINE);
x = string; y = fine;
/*首先記錄新舊關鍵字長度*/
la = strlen(key);
lb = strlen(replacement);
do {
/*用指針逐個比較*/
r = memcmp(x, key, la);
if(r) {/*如果關鍵字不匹配則復制字元串*/
*y = *x;
x++; y++;
}else{/*如果關鍵字匹配則替換字元串*/
memcpy(y, replacement, lb);
x += la; y += lb;
}/*end if*/
}while(*x);
/*將替換完成的結果返回*/
memcpy(string, fine, szLINE);
}/*end rpc*/

/*全文替換*/
void replace(L * lines, char * string, char * replacement)
{
L * t = 0; U index = 0;
if(!string || !lines || !replacement) return ;
t = lines;
while(t) {
index ++;
if(strstr(t->text , string)) {
printf("[BEFORE] %d: %s", index, t->text );
rpc(t->text, string, replacement);
printf("[AFTER ] %d: %s", index, t->text );
}/*end if*/
t=t->next;
}/*end while*/
}/*end replace*/

/*根據行號插入一行新文本,如果行號小於零則將文本追加至鏈表尾*/
void insert(L ** lines, char * line, int index)
{
L * t = 0, * s = 0; int i = 0;
if(!lines || !line) return ;
/*首先為新文本分配一個鏈表節點*/
t = (L*)malloc(sizeof(L));
memset(t, 0, sizeof(L));
strcpy(t->text , line);
if(index == 0 || !*lines) {/*如果鏈表為空則以新節點為起點定義鏈表*/
t->next = *lines;
*lines = t;
return ;
}/*end if*/
s = *lines;
if(index > 0)/*如果行號為正整數,則將鏈表指針指向行號之前*/
for(i = 0; i < index-2; i++) {
if(!s->next ) break;
s = s->next ;
}/*next*/
else/*否則鏈表指針指向表尾*/
while(s->next ) s = s->next ;
/*end if*/
/*完成鏈表插入操作*/
if(s->next ) t->next = s->next ;
s->next = t;
}/*end insert*/

/*根據行號刪除一行文本*/
void erase(L ** lines, U index)
{
L * a = 0, * b = 0, * c = 0;
if(!lines) return ;
/*index -1 表示目標行,index -2表示目標行的前一行*/
a = locate(*lines, index-2);
b = locate(*lines, index-1);
if(!b) return ;
if(a) /*如果前一行存在則刪除目標行*/
a->next = b->next;
else/*否則表示表頭刪除*/
*lines = b->next ;
/*end if*/
/*釋放內存*/
free(b);
}/*end erase*/

/*根據行號和新錄入文本替換原有行*/
void edit(L * lines, char * line, U index)
{
L * t = locate(lines, index-1);
if(!t) return ;
if(line) strcpy(t->text , line);
}/*end edit*/

/*將文件整個裝入鏈表*/
int load(L ** lines, char * file)
{
FILE * f = 0; char line[szLINE]="";
int total = 0;
if(!lines || !file) return 0;
clear(lines);/*首先清空鏈表*/
/*打開文件*/
f = fopen(file, "r");
if(!f) {
fprintf(stderr, "%s is bad.\n", file);
return 0;
}/*end if*/
/*逐行讀入內存並插入表尾*/
while(!feof(f)) {
memset(line, 0, szLINE);
fgets(line, szLINE - 1, f);
insert(lines, line, -1);
total ++;
}/*end while*/
fclose(f);
fprintf(stderr, "[%s] %d lines loaded.\n", file, total);
/*返回總行數*/
return total;
}/*end load*/

/*將鏈表保存到指定的文本文件*/
int save(L * lines, char * file)
{
FILE * f = 0; L * t = lines;
int total = 0;
if(!lines || !file) return 0;
/*打開文件*/
f = fopen(file, "w");
if(!f) {
fprintf(stderr, "%s is bad.\n", file);
return 0;
}/*end if*/
t = lines;
while(t) {/*逐個文件寫入*/
fprintf(f, "%s", t->text );
t = t->next ;
total ++;
}/*end while*/
fclose(f);
fprintf(stderr, "[%s] %d lines saved.\n", file, total);
/*返回總行數*/
return total;
}/*save*/

/*執行載入文本文件命令*/
void exec_load(L ** lines, char * line)
{
char cmd[szLINE] = "", file[szLINE] = "";
/*分析命令行,提取文件名*/
sscanf(line, "%s %s", cmd, file);
/*執行載入命令*/
load(lines, file);
}/*end exec_load*/

/*執行文本保存命令*/
void exec_save(L ** lines, char * line)
{
char cmd[szLINE] = "", file[szLINE] = "";
/*分析命令行,提取文件名*/
sscanf(line, "%s %s", cmd, file);
/*執行保存命令*/
save(*lines, file);
}/*end exec_save*/

/*執行文本查看命令*/
void exec_view(L ** lines, char * line)
{
char cmd[szLINE] = ""; U from = 0, to = 0;
/*分析命令行,提取目標要查看的起始行號和終止行號*/
sscanf(line, "%s %u %u", cmd, &from, &to);
/*如果起始行號和終止行號大小相反,則根據起始行號顯示一頁*/
if(to < from) to = from + 24;
/*執行查看命令*/
view(*lines, stdout, from, to);
}/*end exec_view*/

/*執行行數統計命令*/
void exec_count(L ** lines, char * line)
{
fprintf(stderr, "%d lines in mem.\n", view(*lines, 0, 0, 0));
}/*end count*/

/*執行文本追加命令*/
void exec_append(L ** lines, char * line)
{
char text[szLINE] = "";
/*在命令之後另起新行用於錄入文本*/
gets(text); strcat(text, "\n");
/*執行文本追加命令*/
insert(lines, text, -1);
}/*end exec_append*/

/*執行文本插入命令*/
void exec_insert(L ** lines, char * line)
{
char cmd[szLINE] = "", text[szLINE] = ""; U index = 0;
/*從命令行提取目標插入點的行號*/
sscanf(line, "%s %d", cmd, &index);
/*在命令之後另起新行用於錄入文本*/
gets(text); strcat(text, "\n");
/*執行文本插入命令*/
insert(lines, text, index);
}/*end insert*/

/*執行文本刪除命令*/
void exec_erase(L ** lines, char * line)
{
char cmd[szLINE] = ""; U index = 0;
/*從命令行提取目標行號*/
sscanf(line, "%s %d", cmd, &index);
/*執行文本刪除命令*/
erase(lines, index);
}/*end erase*/

/*執行文本編輯命令*/
void exec_edit(L ** lines, char * line)
{
char cmd[szLINE] = "", text[szLINE] = ""; U index = 0;
/*從命令行提取目標行號*/
sscanf(line, "%s %d", cmd, &index);
/*在命令之後另起新行用於錄入文本*/
gets(text); strcat(text, "\n");
/*執行文本編輯命令*/
edit(*lines, text, index);
}/*end edit*/

/*執行文本檢索命令*/
void exec_lookup(L ** lines, char * line)
{
char cmd[szLINE] = "", text[szLINE] = "";
/*從命令行提取關鍵字*/
sscanf(line, "%s %s", cmd, text);
/*執行文本檢索命令*/
lookup(*lines, text);
}/*end lookup*/

/*執行在線幫助命令*/
void exec_help(L ** lines, char * line)
{printf("\tcommands:\n\thelp\n\tquit\n\
\tload [file.txt]\n\
\tsave [file.txt]\n\
\tview [from line] [to line]\n\
\tcount\n\
\tappend [return + text]\n\
\tinsert [line number] [return + text]\n\
\terase [line number]\n\
\tedit [line number] [return + text]\n\
\tlookup [text]\n\
\treplace [keyword] [replacement]\n");
}/*end help*/

/*執行文本替換命令*/
void exec_replace(L ** lines, char * line)
{
char cmd[szLINE] = "", text[szLINE] = "", key[szLINE]="";
/*從命令行提取新舊關鍵字*/
sscanf(line, "%s %s %s", cmd, key, text);
/*執行文本替換命令*/
replace(*lines, key, text);
}/*end replace*/

/*執行退出命令*/
void exec_quit(L ** lines, char * line){exit(0);}

/*行編輯命令執行函數,順序與關鍵字表keywords一一對應*/
FUNC functions[CMDS]={
exec_quit, exec_help, exec_load, exec_save,
exec_view, exec_count, exec_append, exec_insert,
exec_erase, exec_edit, exec_lookup, exec_replace
};/*end functions*/

/*從行輸入中識別關鍵字,相當於parse*/
int identified(char * command, char * key)
{
int ln = 0, r = 0;
if(!command || !key) return 0;
ln = strlen(key);
r = memcmp(command, key, ln);
return r==0;
}/*end identified*/

/*主函數*/
int main(int argc, char * argv[])
{
L * lines = 0; char line[szLINE]=""; int a = 0, b = 0, i = 0; FUNC fun = 0;
/*列印歡迎信息*/
printf("Welcome to LINE EDITOR V1.0\nCommand [help] is available.\n");
/*如果帶主函數帶參數,則可以用於列印幫助,或者根據該參數載入一個文本文件*/
if(argc > 1) {
a = strcmp(argv[1], "--help");
b = strcmp(argv[1], "/h");
if(a && b)
load(&lines, argv[1]);
else{
exec_help(0, 0);
return 0;
}/*end if*/
}/*end if*/
/*主命令循環*/
for(;;) {
/*命令提示符中間是表示當前載入的文檔總共有多少行的意思*/
printf("\n

⑵ c語言怎麼用微軟的軟體編譯(過程)

DOS下面是沒有cl的,cl是Windows下命令行方式的編譯工具,IDE也是調用它編譯的。直接cl.exe /help就能看到幫助,貼給你吧 VS2010的(2012的可以交叉編譯ARM架構的目標代碼)

用於 80x86 的 Microsoft (R) 32 位 C/C++ 優化編譯器 16.00.40219.01 版
版權所有(C) Microsoft Corporation。保留所有權利。

C/C++ 編譯器選項

-優化-

/O1 最小化空間 /O2 最大化速度
/Ob<n> 內聯擴展(默認 n=0) /Od 禁用優化(默認)
/Og 啟用全局優化 /Oi[-] 啟用內部函數
/Os 優選代碼空間 /Ot 優選代碼速度
/Ox 最大化優化 /Oy[-] 啟用幀指針省略

-代碼生成-

/GF 啟用只讀字元串池 /Gm[-] 啟用最小重新生成
/Gy[-] 分隔鏈接器函數 /GS[-] 啟用安全檢查
/GR[-] 啟用 C++ RTTI /GX[-] 啟用 C++ EH (與 /EHsc 相同)
/EHs 啟用 C++ EH (沒有 SEH 異常) /EHa 啟用 C++ EH (w/ SEH 異常)
/EHc 外部「C」默認為 nothrow
/fp:<except[-]|fast|precise|strict> 選擇浮點模式:
except[-] - 在生成代碼時考慮浮點異常
fast -「fast」浮點模式;結果可預測性比較低
precise -「precise」浮點模式;結果可預測
strict -「strict」 浮點模式(意味著 /fp:except)
即使使用 /fp:except,/Qfast_transcendentals 也生成內聯內部 FP
/GL[-] 啟用鏈接時代碼生成 /GA 為 Windows 應用程序進行優化
/Ge 對所有函數強制堆棧檢查 /Gs[num] 控制堆棧檢查調用
/Gh 啟用 _penter 函數調用 /GH 啟用 _pexit 函數調用
/GT 生成纖程安全 TLS 訪問 /RTC1 啟用快速檢查(/RTCsu)
/RTCc 轉換為較小的類型檢查 /RTCs 堆棧幀運行時檢查
/RTCu 未初始化的局部用法檢查
/clr[:option] 為公共語言運行時編譯,其中 option 是:
pure - 生成只包含 IL 的輸出文件(沒有本機可執行代碼)
safe - 生成只包含 IL 的可驗證輸出文件
oldSyntax - 接受 Visual C++ 2002/2003 的託管擴展語法
initialAppDomain - 啟用 Visual C++ 2002 的初始 AppDomain 行為
noAssembly - 不產生程序集 /Gd __cdecl 調用約定
/Gr __fastcall 調用約定 /Gz __stdcall 調用約定
/GZ 啟用堆棧檢查(/RTCs) /QIfist[-] 使用 FIST 而不是 ftol()
/hotpatch 確保可熱修補映像的函數填充
/arch:<SSE|SSE2|AVX> CPU 架構的最低要求,以下選項之一:
SSE - 啟用支持 SSE 的 CPU 可用的指令
SSE2 - 啟用支持 SSE2 的 CPU 可用的指令
AVX - 支持使用 Intel(R) 高級矢量擴展指令
/Qimprecise_fwaits 僅在「try」邊界而不是「try」內部生成 FWAITs
/Qsafe_fp_loads 生成安全 FP 負載

-輸出文件-

/Fa[file] 命名程序集列表文件 /FA[scu] 配置程序集列表
/Fd[file] 命名 .PDB 文件 /Fe<file> 命名可執行文件
/Fm[file] 命名映射文件 /Fo<file> 命名對象文件
/Fp<file> 命名預編譯頭文件 /Fr[file] 命名源瀏覽器文件
/FR[file] 命名擴展 .SBR 文件 /Fi[file] 命名預處理的文件
/doc[file] 處理 XML 文檔注釋,並可選擇命名 .xdc 文件

-預處理器-

/AI<dir> 添加到程序集搜索路徑 /FU<file> 強制使用程序集/模塊
/C 不抽出注釋 /D<name>{=|#}<text> 定義宏
/E 預處理到 stdout /EP 預處理到 stdout,無行號
/P 預處理到文件 /Fx 將插入的代碼合並到文件中
/FI<file> 命名強制包含文件 /U<name> 移除預定義的宏
/u 移除所有預定義的宏 /I<dir> 添加到包含搜索路徑
/X 忽略「標准位置」

-語言-

/Zi 啟用調試信息 /Z7 啟用舊式調試信息
/Zp[n] 在 n 位元組邊界上包裝結構 /Za 禁用擴展
/Ze 啟用擴展(默認) /Zl 省略 .OBJ 中的默認庫名
/Zg 生成函數原型 /Zs 只進行語法檢查
/vd{0|1|2} 禁用/啟用 vtordisp /vm<x> 指向成員的指針類型
/Zc:arg1[,arg2] C++ 語言合規性,這里的參數可以是:
forScope[-] - 對范圍規則強制使用標准 C++
wchar_t[-] - wchar_t 是本機類型,不是 typedef
auto[-] - 對 auto 強制使用新的標准 C++ 含義
trigraphs[-] - 啟用三元祖(默認為關閉)
/ZI 啟用「編輯並繼續」調試信息 /openmp 啟用 OpenMP 2.0 語言擴展

- 雜項 -

@<file> 選項響應文件 /?, /help 列印此幫助消息
/bigobj 生成擴展的對象格式 /c 只編譯,不鏈接
/errorReport:option 將內部編譯器錯誤報告給 Microsoft
none - 不發送報告 prompt - 提示立即發送報告
queue - 在下一次管理員登錄時,提示發送報告(默認)
send - 自動發送報告 /FC 診斷中使用完整路徑名
/H<num> 最大外部名稱長度 /J 默認 char 類型是 unsigned
/MP[n] 最多使用「n」個進程進行編譯 /nologo 取消顯示版權信息
/showIncludes 顯示包含文件名 /Tc<source file> 將文件編譯為 .c
/Tp<source file> 將文件編譯為 .cpp /TC 將所有文件編譯為 .c
/TP 將所有文件編譯為 .cpp /V<string> 設置版本字元串
/w 禁用所有警告 /wd<n> 禁用警告 n
/we<n> 將警告 n 視為錯誤 /wo<n> 發出一次警告 n
/w<l><n> 為 n 設置警告等級 1-4 /W<n> 設置警告等級(默認 n=1)
/Wall 啟用所有警告 /WL 啟用單行診斷
/WX 將警告視為錯誤 /Yc[file] 創建 .PCH 文件
/Yd 將調試信息放在每個 .OBJ 中 /Yl[sym] 為調試庫插入 .PCH 引用
/Yu[file] 使用 .PCH 文件 /Y- 禁用所有 PCH 選項
/Zm<n> 最大內存分配(默認為 %) /Wp64 啟用 64 位埠定位警告

-鏈接-

/LD 創建 .DLL /LDd 創建 .DLL 調試庫
/LN 創建 .netmole /F<num> 設置堆棧大小
/link [鏈接器選項和庫] /MD 與 MSVCRT.LIB 鏈接
/MT 與 LIBCMT.LIB 鏈接 /MDd 與 MSVCRTD.LIB 調試庫鏈接
/MTd 與 LIBCMTD.LIB 調試庫鏈接

-代碼分析-

/analyze[:WX-] 啟用代碼分析
WX- - 即使調用了 /WX,也不應將代碼分析警告視為錯誤

熱點內容
外網訪問黑群暉 發布:2025-02-08 05:45:59 瀏覽:559
中央存儲伺服器公司地址 發布:2025-02-08 05:38:48 瀏覽:821
伺服器如何查詢表空間的文件路徑 發布:2025-02-08 05:38:00 瀏覽:162
宏基4741g哪個配置好 發布:2025-02-08 05:37:56 瀏覽:810
混合料運輸車的配置是如何計算的 發布:2025-02-08 05:31:35 瀏覽:293
android紅包插件 發布:2025-02-08 05:31:34 瀏覽:365
ea伺服器怎麼連接 發布:2025-02-08 05:16:45 瀏覽:463
更加密更改 發布:2025-02-08 05:15:20 瀏覽:786
倉儲資源配置都需要開展哪些任務 發布:2025-02-08 05:13:51 瀏覽:676
探針資料庫 發布:2025-02-08 05:13:35 瀏覽:80