紧急!!在线求解! C语言输入两个字符串,并且进行合并,输出合并后的字符串,然后将该合并字符串逆序输出

如题所述

利用栈和队列就可以了,
# include "stdio.h"
# include "stdlib.h"
# include "stdio.h"
# include "stdlib.h"
# define True 1
# define FALSE 0
# define OK 1
# define ERROR 0
# define INFEASIBLE -1
# define OVERFLOW -2
# define STACK_INIT_SIZE 100
# define STACK_INCREMENT 10
# define QUEUE_INIT_SIZE 100
typedef char SElemType;
typedef char QElemType;
typedef int Status;
typedef struct SqStack
{
SElemType *base;
SElemType *top;
int stacksize;
}SqStack;
typedef struct
{
QElemType *base;
int front;
int rear;
}SqQueue;
Status InitStack (SqStack &S);
Status GetTop(SqStack S,SElemType &e);
Status Push (SqStack &S,SElemType e);
Status Pop(SqStack &S,SElemType &e);
Status DestroyStack(SqStack &S);
Status ClearStack(SqStack &S);
int StackLength(SqStack S);
Status StackEmpty(SqStack &S);
Status StackTraverse(SqStack S,Status (* Func)(SElemType e));
Status InitSqQueue(SqQueue & Q);
Status EnQueue (SqQueue &Q,QElemType e);
Status CreateSqQueue(SqQueue &Q);
int QueueLength(SqQueue Q);
Status DeQueue(SqQueue & Q,QElemType &e);
Status DestroySqQueue(SqQueue &Q);
Status QueueTraverse(SqQueue Q,Status (* Func)(QElemType e));
Status Print(char e);
Status Verse(SqQueue Q);
Status hebing(SqQueue &Q1,SqQueue Q2);
int main()
{
SqQueue Q,Q1;
InitSqQueue(Q);
InitSqQueue(Q1);
CreateSqQueue(Q);
CreateSqQueue(Q1);
hebing(Q,Q1);
Verse(Q);
return 0;
}
Status InitStack (SqStack &S)
{
S.base=(SElemType *)malloc(STACK_INIT_SIZE * sizeof(SElemType));
if(!S.base) exit(OVERFLOW);
S.top=S.base;
S.stacksize=STACK_INIT_SIZE;
return OK;
}
Status GetTop(SqStack S,SElemType &e)
{
if(S.base==S.top) return ERROR;
e=*(S.top-1);
return OK;
}
Status Push (SqStack &S,SElemType e)
{
if(S.top-S.base==S.stacksize)
{
S.base=( SElemType*)realloc(S.base,STACK_INCREMENT*sizeof(SElemType));
if(!S.base)exit(OVERFLOW);
S.top=S.base+S.stacksize;
S.stacksize+=STACK_INCREMENT;
}
*S.top++=e;
return OK;
}
Status Pop(SqStack &S,SElemType &e)
{
if(S.top==S.base) return ERROR;
e=*(--S.top);
return OK;
}
Status DestroyStack(SqStack &S)
{
if(!S.base) return ERROR;
free(S.base);
return OK;
}
Status ClearStack(SqStack &S)
{
if(!S.base) return ERROR;
S.top=S.base;
return OK;
}
int StackLength(SqStack S)
{
if(!S.base) return ERROR;
return S.top-S.base;
}
Status StackEmpty(SqStack &S)
{
if(!S.base) return ERROR;
if(StackLength(S)==0) return 1;
return 0;
}
Status StackTraverse(SqStack S,Status (* Func)(SElemType e))
{
if(S.top==S.base) return ERROR;
for(SElemType *p=S.top-1;p>=S.base;p--)
{
Func(*p);
}
return OK;
}
Status InitSqQueue(SqQueue & Q)
{
Q.base=(QElemType *)malloc(QUEUE_INIT_SIZE*sizeof(QElemType));
if(!Q.base) exit (OVERFLOW);
Q.front=Q.rear=0;
return OK;
}
Status EnQueue (SqQueue &Q,QElemType e)
{
if((Q.rear+1)%QUEUE_INIT_SIZE==Q.front) return ERROR;
Q.base[Q.rear]=e;
Q.rear=(Q.rear+1)%QUEUE_INIT_SIZE;
return OK;
}
Status CreateSqQueue(SqQueue &Q)
{
QElemType a;
printf("请输入字符串:(以字符'@'结束)\n");
while ((a=getchar())!='@')
{
if(a!='\n')
EnQueue(Q,a);
}
return OK;
}
int QueueLength(SqQueue Q)
{
return (Q.rear-Q.front+1)%QUEUE_INIT_SIZE;
}
Status DeQueue(SqQueue & Q,QElemType &e)
{
if(Q.front==Q.rear) return ERROR;
e=Q.base[Q.front];
Q.front=(Q.front+1)%QUEUE_INIT_SIZE;
return OK;
}
Status DestroySqQueue(SqQueue &Q)
{
if(!Q.base) return ERROR;
free(Q.base);
return OK;
}
Status QueueTraverse(SqQueue Q,Status (* Func)(QElemType e))
{
if(Q.rear==Q.front) return ERROR;
for(int i=Q.front;i%QUEUE_INIT_SIZE!=Q.rear;i++)
{
Func(Q.base[i]);
}
return OK;
}
Status Print(char e)
{
printf("%c",e);
return OK;
}
Status Verse(SqQueue Q)
{
QElemType e;
SqStack S;
SqQueue Q2;
InitStack (S);
InitSqQueue (Q2);
while (Q.rear!=Q.front)
{
DeQueue(Q,e);
EnQueue(Q2,e);
Push(S,e);
}
printf("源数据为:\n");
QueueTraverse(Q2,Print);
printf("\n");
printf("逆置的数据为:\n");
StackTraverse(S,Print);
printf("\n");
return OK;
}
Status hebing(SqQueue &Q,SqQueue Q1)
{
char e;
while(Q1.front!=Q1.rear)
{
DeQueue(Q1,e);
EnQueue (Q, e);
}
return 0;
}
温馨提示:答案为网友推荐,仅供参考
第1个回答  2012-06-07
#include<stdio.h>
int main()
{ char a[100],b[50];
int i,j;
gets(a);
gets(b);
for(i=0;a[i]!='\0';i++) ;//找a的有效字符末尾
for(j=0;b[j]!='\0';j++)
a[i++]=b[j];//把b中的字符顺序存入a中,从刚才找到的a的有效字符末尾开始存
a[i]='\0';//在连接后的字符串最后加字符串结束标志
puts(a);//输出连接后的字符串
i=strlen(a)-1;//计算数组a的有效字符个数,例如a有5个字符,那它的第5个字符的下标应该是4
for(;i>=0;i--)
putchar(a[i]);//逆序输出数组a中的字符串
return 0;
}本回答被提问者和网友采纳
相似回答