用C/C++实现录入字符串,将字符串以:为分隔符分组,重复出现的组只输出前面一组,最后将处理后的字符串输

例如:

输入ab13:cb23:adf13:ab13::

输出ab13:cb23:adf13::

其中双:为结束符号

用二叉树实现

#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#define N 50//定义字符串长度

//定义二叉树链式结构
typedef struct BitSortNode
{
char *data; //数据域
struct BitSortNode *lchild;//左子树
struct BitSortNode *rchild;//右子树
}BitSortNode,*BiSortTree;

//递归实现二叉排序树插入
int InsertBiSortTree(BiSortTree *bst,char* item)
{
if(*bst==NULL)
{
//把按照item元素生成的新结点链接到已找到的插入位置
BiSortTree p;
p=(BiSortTree)malloc(sizeof(BitSortNode));
if(p==NULL)
{
printf("申请二叉排序树内存出错\n");
return 0;
}

p->data=(char*)malloc(sizeof(char)*(strlen(item)+1));
if(p->data==NULL)
{
printf("申请内存出错\n");
return 0;
}
strcpy(p->data,item);//拷贝数据
p->data[strlen(item)]='\0';//末尾置结束符号
p->lchild=p->rchild=NULL;
(*bst)=p;
}
else if(strcmp(item,(*bst)->data)==0) return 1;//相等直接返回,不插入
//else if(strcmp(item,(*bst)->data)< 0) //向左子树中插入元素
//InsertBiSortTree((*bst)->lchild,item);
else //向右子树中插入元素
InsertBiSortTree(&(*bst)->rchild,item);

return 1;
}

//创建二叉排序树
int CreateBiSortTree(BiSortTree *bst)
{

char *s,*p;
int i=0,flag=0;
int max=N;
s=(char*)malloc(sizeof(char)*N);
if(s==NULL)
return 0;

printf("录入数据,双冒号结束\n");
while(1)
{
while(i<max)//输入个数不超过定义长度
{
s[i]=getchar();

if(s[i]==':')//如果是:
{
if(flag==1)//如果是连续:,终止输入
{
s[i+1]='\0';//置结束符号
break;
}
else
flag=1;//否则置标志
}
else
flag=0;//其余字符不置标志

i++;
}

if(i >=max)//如果长度超过定义,扩充
{
s=(char*)realloc(s,sizeof(char)*(max/N+1)*N);//变成多个N的长度
if(s==NULL)
return 0;
max+=N;//累加限定长度
}
else
break;
}

//printf("%s\n",s);
p=strtok(s,":");//分解冒号
while(p!=NULL)
{
if(InsertBiSortTree(bst,p)==0)
return 0;
//printf("%s\n",p);
p=strtok(NULL,":");
}

free(s);
return 1;
}

//递归实现中序遍历
void InorderBST(BiSortTree bst)
{
if(bst!=NULL)
{
//如果不为空
InorderBST(bst->lchild); //递归遍历左子树
printf("%s:",bst->data); //输出根节点值
InorderBST(bst->rchild); //递归遍历右子树
}
}

//二叉树释放内存
void FreeBiSortTree(BiSortTree bst)
{
if(bst!=NULL)
{
if(bst->lchild!=NULL)
FreeBiSortTree(bst->lchild);
if(bst->rchild!=NULL)
FreeBiSortTree(bst->rchild);
free(bst);
}
}

int main(void)
{
BiSortTree bst=NULL;

//建立二叉排序树
if(CreateBiSortTree(&bst)==0)
return 1;

//输出
InorderBST(bst);
printf(":\n");
//释放空间
FreeBiSortTree(bst);

return 0;
}
温馨提示:答案为网友推荐,仅供参考
第1个回答  2011-07-08
结束时自然回车就行 不要输入双:
当然 你如果有特殊要求 这个也可以考虑

//////////////////////////////以下是运行效果和代码/////////////////////////////
234234:sdfsd:23csdc:23dcc:
234234:
sdfsd:
23csdc:
23dcc:
Press any key to continue

#include "stdio.h"
#include "string.h"

main()
{
int i,j,k,nLen,cnt;
char a[100],b[10][20];
gets(a);
nLen = strlen(a);
j=k=cnt=0;
for (i=0;i<nLen;i++)
{
b[j][k] = a[i];
k++;
if (a[i]==':')
{
b[j][k]='\0';
k=0;j++;cnt++;
}
}
for (k=0;k<cnt;k++)
{
puts(b[k]);
}
}追问

::的意思是输入为::时表示字符串结束,同时输出也应该以::结束,以及是WIN7的问题吗,我这调试为什么报错?

追答

我用的是vc6 你用的什么 报什么错 你的编译器 其他程序能编译通过吗

改好了

1231:sdfdf:df34fd::
1231:
sdfdf:
df34fd::
Press any key to continue

#include "stdio.h"
#include "string.h"

main()
{

int i,j,k,nLen,cnt;
char a[100],b[10][20];
gets(a);
nLen = strlen(a);
j=k=cnt=0;
for (i=0;i<nLen;i++)
{
b[j][k] = a[i];
k++;
if (a[i]==':' && a[i+1]!=':')
{
b[j][k]='\0';
k=0;j++;cnt++;
}
}
for (k=0;k<cnt;k++)
{
puts(b[k]);
}
}

本回答被网友采纳
第2个回答  2011-07-08
很好!
相似回答