當前位置:首頁 » 編程語言 » 簡單行編輯程序c語言
擴展閱讀
webinf下怎麼引入js 2023-08-31 21:54:13
堡壘機怎麼打開web 2023-08-31 21:54:11

簡單行編輯程序c語言

發布時間: 2022-07-06 19:38:25

『壹』 怎樣用c語言編出一個簡單的程序

C語言寫的程序 要想運行必須有一定的集成開發環境支持(建議VC6.0),然後在裡面創建工程,再在工程 中創建一個C的源文件(如example.c),在裡面添加你要寫的代碼,編譯運行就OK了
你添加的代碼,如簡單的程序:
#include"stdio.h"

void main(){
printf("This is my first program\n");
}

『貳』 c語言程序設計 簡單的行編輯器

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

【備注】完全原創,編寫時間:2010-7-13。請把所有的注釋信息提取出來就可以寫程序設計報告。
*/
#include <stdio.h> /*標准文件流操作,這里使用了fopen/fclose/fprintf/printf/scanf/gets函數*/
#include <stdlib.h> /*標准系統庫,這里使用了malloc/free/exit*/
#include <string.h> /*標准字元串庫,這里使用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 <= to) fprintf(f, "%d: %s", index, t->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<%d>", view(lines, 0, 0, 0));
/*從鍵盤輸入一個命令行*/
memset(line, 0, szLINE);
gets(line);
/*根據命令行掃描關鍵詞代碼表,根據代碼表取得執行函數的地址,再根據執行函數地址調用行編輯命令*/
for(i = 0; i < CMDS; i++) {
if(identified(line, keywords[i])) {
fun = functions[i];
(*fun)(&lines, line);
break;
}/*end if*/
}/*next*/
}/*next*/
return 0;
}/*end main*/

『叄』 c語言編寫一個程序,非常簡單

(1)

#include"stdio.h"
#include"math.h"
intmain(intargc,char*argv[]){
floatm,r;
intn;
printf("Pleaseinput,. ");
if(scanf("%f,%d,%f",&m,&n,&r)!=3||m<0||n<0||r<0){
printf("Inputerror,exit... ");
return0;
}
printf("Interest:%.0f ",m*(pow(1+r,n)-1)*.8);
return0;
}

運行樣例:

『肆』 如何編程如何用C語言編輯一些簡單的程序

首先打開vs2013,發現有新建項目 和 打開項目 的選項,選擇新建項目。
之後會有一個新建項目的界面,左側有各種語言的編寫環境,比如VB,VC#,VC++,如果編寫C語言就選擇VC++,因為c++基本兼容C語言。這時的項目不用添加後綴,這相當於一個大的工程。
選擇好類型,寫好項目名稱之後點擊確定。
點擊下一步
這里比較關鍵。勾上空項目,不選的話因為程序會默認為你添加一些代碼,用不上有時會編譯出錯。比較適合初學者。
這時已經新建好一個項目了,右鍵源文件添加新建項。
添加新建項之後命名,C語言的代碼一定要修改後綴,將.cpp改為.c,向程序說明這時一個C語言代碼。
寫好代碼後調試運行,如果沒有寫讓程序停留的代碼就不要直接按F5,你可以按ctrl+F5調試。
平時默認為Debug,如果開發完成一個程序後給別人使用就要選擇Release版的調試,這樣別人在沒有vs的環境下也可以使用。

分享

『伍』 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語言 簡單行編輯程序

你的這個要求是絕對不可能有人滿足你的。因為你的這個要求已經遠遠不只是一個最簡單的 C 語言源程序代碼了,它至少可以看成是一個 C 語言大作業(是一個行編輯程序,類似 UNIX/Linux 系統的 vi 程序了)。
而且了,任何一個編程人員,即使他們的編程水平再高,但是他們也畢竟不是神,看到你的需求之後馬上就能夠直接編寫出全部、且正確的程序源代碼,還必須要保證能夠馬上運行出正確的結果。
因為在整個編寫程序的過程中,在程序代碼調試上所花費的時間通常要比編寫程序代碼所花費的時間多得多。
而且再說了,編寫任何程序都必須要在自己的電腦上安裝一個程序開發、調試環境,通過自己親自上機編寫程序、且通過自己的艱苦調試程序,最終調試出程序的正確運行結果。
以上就是我個人多年編程的親身體會。

『柒』 C語言 行編輯程序

/*TC2編譯通過*/
/*以回車符為結束符輸入一行*/
#include <stdio.h>
#define MAXSIZE 128

int main()
{
char base[MAXSIZE];
char *stack,*p;
char c;
stack=base;
while((c=getchar())!='\n')
{
if(c=='@') stack=base;
else if(c=='#'){if(stack!=base) stack--;}
else *(++stack)=c;
}
if(stack==base) printf("√");/*沒有可輸出的字元*/
p=base;
while(p!=stack) printf("%c",*(++p));
getch();
}

『捌』 求簡單C語言程序代碼!

輸入2個正整數m和n,求其最大公約數和最小公倍數

#include

#include

int main()

int m,n,p,q,s,r;

printf("請輸入兩個正整數;m,n ");

scanf("%d,%d",&m,&n);

#include<stdio.h>

main()

int a,b,t=0;

scanf("%d %d",&a,&b);

if (a<b)

printf("%d %d %d %d %d",(a+b),(a-b),(a/b),(a*b),(a%b));

}

主要特點

C語言是一種結構化語言,它有著清晰的層次,可按照模塊的方式對程序進行編寫,十分有利於程序的調試,且c語言的處理和表現能力都非常的強大,依靠非常全面的運算符和多樣的數據類型,可以輕易完成各種數據結構的構建,通過指針類型更可對內存直接定址以及對硬體進行直接操作,因此既能夠用於開發系統程序,也可用於開發應用軟體。

以上內容參考:網路-c語言

『玖』 高分懸賞一C語言程序設計題(簡單行編輯)

我有一個電話本程序似乎差不多喔,就差統計了。發給你看下,參考下吧。

#include <stdio.h>
#include <conio.h>
#define MAX 300
FILE *book;
char number[20],name[10];
main()
{
if((book=fopen("cell phone book.txt","a+"))==NULL)
{
printf("此為第一次運行。\n");
}
else
printf("電話本己打開。\n");
start();
fclose(book);
}
start()
{
int c;
int n=1;
do
{
printf("1·查找電話。\n");
printf("2·加入新電話。\n");
printf("3·顯示整個電話本。\n");
printf("4·刪除電話。\n");
printf("5·退出。\n");
c=getch();
printf("您選擇了%c\n",c);

switch (c)
{
case '1':search();
break;
case '2':adser();
break;
case '3':print();
break;
case '4':del();
break;
case '5':n=0;
break;
default:
break;
}
}while(n!=0);
}
del()
{
char temp[30],name[10];
FILE *tempbook;
int n,p=0;
tempbook=fopen("temp.txt","w");
fclose(tempbook);
tempbook=fopen("temp.txt","a");
book=fopen("cell phone book.txt","r");
printf("輸入要刪除的人名。\n");
scanf("%s",name);
while(fgets(temp,MAX,book)!=NULL)
{
for(n=0;n<10;n++)
{
if(temp[n]!=name[n])
{
p=1;
break;
}
if(temp[n]==NULL)
break;
}
if(p==1)
{
fwrite(temp,2,5,tempbook);
fwrite(&temp[10],1,12,tempbook);
fwrite("\n",1,1,tempbook);
}
p=0;
}
fclose(tempbook);
fclose(book);
book=fopen("cell phone book.txt","w");
fclose(book);
book=fopen("cell phone book.txt","a");
tempbook=fopen("temp.txt","r");
while(fgets(temp,MAX,tempbook)!=NULL)
{
fwrite(temp,2,5,book);
fwrite(&temp[10],1,12,book);
fwrite("\n",1,1,book);
}
fclose(tempbook);
fclose(book);
}
search()
{
char tempbook[30],name[10],p=0;
int n=0;
book=fopen("cell phone book.txt","rt");
printf("輸入要查找的姓名。\n");
scanf("%s",name);
while(fgets(tempbook,MAX,book)!=NULL)
{
if(tempbook[0]==name[0]&&tempbook[1]==name[1]&&tempbook[2]==name[2]&&tempbook[3]==name[3]&&tempbook[4]==name[4])
printf("%s%12s\n",tempbook,&tempbook[10]);
}
fclose(book);
}
adser()
{
book=fopen("cell phone book.txt","a+");
printf("請輸入姓名:\n");
scanf("%s",name);
printf("請輸入電話號碼:\n");
scanf("%s",number);
fwrite(name,2,5,book);
fwrite(number,1,12,book);
fwrite("\n",1,1,book);
fclose(book);
}
print()
{
char c;
book=fopen("cell phone book.txt","rt");
do
{
c=fgetc(book);
putchar(c);
}while(c!=EOF);
fclose(book);
}

『拾』 急求數據結構題目:簡單行編輯程序C語言

可以減少表的空餘欄位,減少拆表的必要,例如用戶集合可以一條記錄帶有 admin: true 屬性,其他不帶有這個屬性,而在關系資料庫中這類帶來大量空餘欄位的屬性最好拆表。
PostgreSQL 打開 HStore 擴展後也可以實現這樣的結構。
如果覺得 admin: true 的例子太簡單,可以考慮下怎麼儲存 gemspec 的內容並讓它可索引。