用C语言,写一个简单的行编辑程序代码,要求如下:输入一行字符串,用户在输入错误时可以用#和@进行修 正,其中#为退格符,@为退行符。要求输出修正后的字符串。要求使用到switch 要求用栈作为存储结构实现。编写一个读入一个字符串,把它顺序读入队列,并输出队列 的程序。
时间: 2024-02-01 14:13:46 浏览: 56
行编辑程序代码:
```c
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#define MAX_SIZE 100
typedef struct {
char data[MAX_SIZE];
int top;
} Stack;
void initStack(Stack *s) {
s->top = -1;
}
void push(Stack *s, char c) {
if (s->top == MAX_SIZE - 1) {
printf("Stack is full!\n");
return;
}
s->data[++s->top] = c;
}
char pop(Stack *s) {
if (s->top == -1) {
printf("Stack is empty!\n");
return '\0';
}
return s->data[s->top--];
}
int main() {
printf("Please input a string:\n");
char str[MAX_SIZE];
fgets(str, MAX_SIZE, stdin);
int len = strlen(str);
Stack s;
initStack(&s);
for (int i = 0; i < len; i++) {
switch (str[i]) {
case '#':
pop(&s);
break;
case '@':
s.top = -1;
break;
default:
push(&s, str[i]);
break;
}
}
printf("Corrected string: ");
while (s.top != -1) {
printf("%c", pop(&s));
}
printf("\n");
return 0;
}
```
顺序读入队列并输出的程序代码:
```c
#include <stdio.h>
#include <stdlib.h>
#define MAX_SIZE 100
typedef struct {
char data[MAX_SIZE];
int front, rear;
} Queue;
void initQueue(Queue *q) {
q->front = q->rear = 0;
}
int isFull(Queue *q) {
return (q->rear + 1) % MAX_SIZE == q->front;
}
int isEmpty(Queue *q) {
return q->front == q->rear;
}
void enqueue(Queue *q, char c) {
if (isFull(q)) {
printf("Queue is full!\n");
return;
}
q->data[q->rear] = c;
q->rear = (q->rear + 1) % MAX_SIZE;
}
char dequeue(Queue *q) {
if (isEmpty(q)) {
printf("Queue is empty!\n");
return '\0';
}
char c = q->data[q->front];
q->front = (q->front + 1) % MAX_SIZE;
return c;
}
int main() {
printf("Please input a string:\n");
char str[MAX_SIZE];
fgets(str, MAX_SIZE, stdin);
Queue q;
initQueue(&q);
int len = strlen(str);
for (int i = 0; i < len; i++) {
if (str[i] != '\n') {
enqueue(&q, str[i]);
}
}
printf("Queue: ");
while (!isEmpty(&q)) {
printf("%c ", dequeue(&q));
}
printf("\n");
return 0;
}
```
阅读全文