C语言栈的行编辑程序讲解

简介: C语言栈的行编辑程序讲解

C语言栈的行编辑程序是一个模拟文本编辑器行编辑功能的程序,它使用栈数据结构来保存用户的编辑历史,从而支持撤销(undo)和重做(redo)操作。下面是一个简单的C语言栈行编辑程序的讲解和示例代码。

栈的定义

首先,我们需要定义一个栈的数据结构。栈是一种后进先出(LIFO)的数据结构,我们可以使用数组和指针来实现它。

 

#include <stdio.h> 

 

#include <stdlib.h> 

 

#include <string.h> 

 

#include <stdbool.h> 

 

 

 

#define MAX_STACK_SIZE 100

 

#define MAX_LINE_LENGTH 100

 

 

 

typedef struct {

 

char data[MAX_STACK_SIZE][MAX_LINE_LENGTH];

 

int top;

 

} Stack;

 

 

 

void initStack(Stack *s) {

 

s->top = -1;

 

}

 

 

 

bool isEmpty(Stack *s) {

 

return s->top == -1;

 

}

 

 

 

bool push(Stack *s, const char *str) {

 

if (s->top >= MAX_STACK_SIZE - 1) {

 

return false;

 

}

 

strcpy(s->data[++s->top], str);

 

return true;

 

}

 

 

 

bool pop(Stack *s, char *str) {

 

if (isEmpty(s)) {

 

return false;

 

}

 

strcpy(str, s->data[s->top--]);

 

return true;

 

}

 

 

 

void printStack(Stack *s) {

 

for (int i = s->top; i >= 0; i--) {

 

printf("%s\n", s->data[i]);

 

}

 

}

行编辑功能

接下来,我们实现行编辑功能,包括输入字符、撤销和重做。

 

void appendChar(Stack *undoStack, char *currentLine) {

 

char ch;

 

printf("Enter a character to append: ");

 

scanf(" %c", &ch); // 注意空格以跳过任何剩余的换行符

 

currentLine[strlen(currentLine)] = ch;

 

push(undoStack, currentLine);

 

// 清空 redoStack,因为当前操作会覆盖任何可能的重做操作

 

while (!isEmpty(redoStack)) {

 

char dummy[MAX_LINE_LENGTH];

 

pop(&redoStack, dummy);

 

}

 

}

 

 

 

void undoOperation(Stack *undoStack, Stack *redoStack, char *currentLine) {

 

if (isEmpty(undoStack)) {

 

printf("No more undo operations.\n");

 

return;

 

}

 

char prevLine[MAX_LINE_LENGTH];

 

pop(undoStack, prevLine);

 

strcpy(currentLine, prevLine);

 

push(redoStack, prevLine);

 

}

 

 

 

void redoOperation(Stack *undoStack, Stack *redoStack, char *currentLine) {

 

if (isEmpty(redoStack)) {

 

printf("No more redo operations.\n");

 

return;

 

}

 

char nextLine[MAX_LINE_LENGTH];

 

pop(redoStack, nextLine);

 

strcpy(currentLine, nextLine);

 

push(undoStack, nextLine);

 

}

主程序

最后,我们编写主程序来整合这些功能。

 

int main() {

 

Stack undoStack, redoStack;

 

initStack(&undoStack);

 

initStack(&redoStack);

 

char currentLine[MAX_LINE_LENGTH] = "";

 

char input;

 

 

 

while (true) {

 

printf("Current line: %s\n", currentLine);

 

printf("Enter 'a' to append a character, 'u' to undo, 'r' to redo, or 'q' to quit: ");

 

scanf(" %c", &input); // 注意空格以跳过任何剩余的换行符

 

 

 

switch (input) {

 

case 'a':

 

appendChar(&undoStack, currentLine);

 

break;

 

case 'u':

 

undoOperation(&undoStack, &redoStack, currentLine);

 

break;

 

case 'r':

 

redoOperation(&undoStack, &redoStack, currentLine);

 

break;

 

case 'q':

 

printf("Exiting the line editor.\n");

 

return 0;

 

default:

 

printf("Invalid input. Please try again.\n");

 

}

 

}

 

 

 

return 0;

 

}

 

2.

C语言栈的行编辑程序通常用于模拟一个基本的文本编辑器,允许用户输入字符、撤销和重做操作。下面是一个简单的C语言栈行编辑程序的讲解和示例代码。

栈的定义与操作

首先,我们需要定义栈的数据结构以及相关的操作函数,如初始化栈、判断栈是否为空、入栈和出栈。

 

#include <stdio.h> 

 

#include <stdlib.h> 

 

#include <string.h> 

 

#include <stdbool.h> 

 

 

 

#define MAX_STACK_SIZE 100

 

#define MAX_LINE_LENGTH 100

 

 

 

typedef struct {

 

char data[MAX_STACK_SIZE][MAX_LINE_LENGTH];

 

int top;

 

} Stack;

 

 

 

void initStack(Stack *s) {

 

s->top = -1;

 

}

 

 

 

bool isEmpty(Stack *s) {

 

return s->top == -1;

 

}

 

 

 

bool push(Stack *s, const char *str) {

 

if (s->top >= MAX_STACK_SIZE - 1) {

 

return false;

 

}

 

strcpy(s->data[++s->top], str);

 

return true;

 

}

 

 

 

bool pop(Stack *s, char *str) {

 

if (isEmpty(s)) {

 

return false;

 

}

 

strcpy(str, s->data[s->top--]);

 

return true;

 

}

行编辑功能

然后,我们实现行编辑功能,包括输入字符、撤销和重做。这里我们使用两个栈:一个用于撤销(undoStack),另一个用于重做(redoStack)。

 

void appendChar(Stack *undoStack, char *currentLine) {

 

char ch;

 

printf("Enter a character to append: ");

 

scanf(" %c", &ch); // 注意空格以跳过任何剩余的换行符

 

currentLine[strlen(currentLine)] = ch;

 

push(undoStack, currentLine);

 

// 清空 redoStack,因为当前操作会覆盖任何可能的重做操作

 

while (!isEmpty(redoStack)) {

 

char dummy[MAX_LINE_LENGTH];

 

pop(&redoStack, dummy);

 

}

 

}

 

 

 

void undoOperation(Stack *undoStack, Stack *redoStack, char *currentLine) {

 

if (isEmpty(undoStack)) {

 

printf("No more undo operations.\n");

 

return;

 

}

 

char prevLine[MAX_LINE_LENGTH];

 

pop(undoStack, prevLine);

 

strcpy(currentLine, prevLine);

 

push(redoStack, currentLine);

 

}

 

 

 

void redoOperation(Stack *undoStack, Stack *redoStack, char *currentLine) {

 

if (isEmpty(redoStack)) {

 

printf("No more redo operations.\n");

 

return;

 

}

 

char nextLine[MAX_LINE_LENGTH];

 

pop(redoStack, nextLine);

 

strcpy(currentLine, nextLine);

 

push(undoStack, nextLine);

 

}

主程序

最后,我们编写主程序来整合这些功能。

 

int main() {

 

Stack undoStack, redoStack;

 

initStack(&undoStack);

 

initStack(&redoStack);

 

char currentLine[MAX_LINE_LENGTH] = "";

 

char input;

 

 

 

while (true) {

 

printf("Current line: %s\n", currentLine);

 

printf("Enter 'a' to append a character, 'u' to undo, 'r' to redo, or 'q' to quit: ");

 

scanf(" %c", &input); // 注意空格以跳过任何剩余的换行符

 

 

 

switch (input) {

 

case 'a':

 

appendChar(&undoStack, currentLine);

 

break;

 

case 'u':

 

undoOperation(&undoStack, &redoStack, currentLine);

 

break;

 

case 'r':

 

redoOperation(&undoStack, &redoStack, currentLine);

 

break;

 

case 'q':

 

printf("Exiting the line editor.\n");

 

return 0;

 

default:

 

printf("Invalid input. Please try again.\n");

 

}

 

}

 

 

 

return 0;

 

}

运行示例

当运行这个程序时,用户可以通过输入字符来编辑当前行,通过输入 'u' 来撤销上一步操作,通过输入 'r' 来重做上一步撤销的操作,或者通过输入 'q'

 

目录
相关文章
|
11天前
|
前端开发 C语言 C++
C语言03----第一个程序HelloWorld(vs版)
C语言03----第一个程序HelloWorld(vs版)
|
21天前
|
C语言 图形学 C++
|
22天前
|
C语言
|
24天前
|
C语言
C语言---函数---请输入乘法口诀表的行,来打印几几乘法表
C语言---函数---请输入乘法口诀表的行,来打印几几乘法表
12 1
|
18天前
|
自然语言处理 C语言 C++
程序与技术分享:C++写一个简单的解析器(分析C语言)
程序与技术分享:C++写一个简单的解析器(分析C语言)
|
18天前
|
程序员 编译器 C语言
详解C语言入门程序:HelloWorld.c
详解C语言入门程序:HelloWorld.c
12 0
|
18天前
|
机器学习/深度学习 C语言 Windows
程序与技术分享:C语言学生宿舍管理系统代码(可运行)
程序与技术分享:C语言学生宿舍管理系统代码(可运行)
13 0
|
21天前
|
程序员 C语言 C++
【C语言】:柔性数组和C/C++中程序内存区域划分
【C语言】:柔性数组和C/C++中程序内存区域划分
14 0
|
24天前
|
C语言
C语言-----打印用“*”组成的X形图案,输出一个数表示行数
C语言-----打印用“*”组成的X形图案,输出一个数表示行数
|
1月前
|
C语言 C++
【数据结构】C语言实现:栈(Stack)与队列(Queue)
【数据结构】C语言实现:栈(Stack)与队列(Queue)