Nirvana 15:38:49 #include <stdio.h> #include <conio.h> #include <stdbool.h> #define MAX_QUEUE_SIZE 1000 void main() { char queue[MAX_QUEUE_SIZE]; int front=0,rear=0; char ch; ch=getchar(); while(ch!='#') { queue[rear]=ch; rear++; ch=getchar(); } while(front<rear) { printf("%c",queue[front]); front++; } printf("\n"); getch(); } Nirvana 15:39:07 #include <stdio.h>#include <conio.h>#define MAX_STACK_MAX 1000void main(){ char stack[MAX_STACK_MAX]; int top=0,i; char ch;ch=getchar(); while (ch!='!'&&top<MAX_STACK_MAX) { switch(ch) { case'#': if(top>0) top--; break; case'@': top=0; break; default: stack[top]=ch; top++; break; } ch=getchar(); } for(i=0;i<top;i++) printf("%c",stack[i]); printf("\n"); getch();}将这两段代码合为一段代码
时间: 2024-03-13 07:42:41 浏览: 129
niRvana 4.5.3主题模板.zip
5星 · 资源好评率100%
以下是将两段代码合并后的代码:
```c
#include <stdio.h>
#include <conio.h>
#include <stdbool.h>
#define MAX_QUEUE_SIZE 1000
#define MAX_STACK_MAX 1000
void main() {
char queue[MAX_QUEUE_SIZE];
int front = 0, rear = 0;
char stack[MAX_STACK_MAX];
int top = 0, i;
char ch;
ch = getchar();
while (ch != '#') {
queue[rear] = ch;
rear++;
ch = getchar();
}
ch = getchar();
while (ch != '!' && top < MAX_STACK_MAX) {
switch(ch) {
case '#':
if (top > 0) {
top--;
}
break;
case '@':
top = 0;
break;
default:
stack[top] = ch;
top++;
break;
}
ch = getchar();
}
printf("Queue output: ");
while (front < rear) {
printf("%c", queue[front]);
front++;
}
printf("\n");
printf("Stack output: ");
for (i = 0; i < top; i++) {
printf("%c", stack[i]);
}
printf("\n");
getch();
}
```
这个合并后的代码可以分别读入字符到队列和栈中,并输出它们的内容。其中,遇到 `#` 时队列弹出元素,遇到 `@` 时栈清空。
阅读全文