c语言gf
⑴ 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,也不应将代码分析警告视为错误