# 请问大家：急！！！！！！！！真的很急，谢谢了

ambition2005 2003-01-10 01:50:10

//还没有弄清楚，具体的在做标记处

#include"stdio.h"
#include"malloc.h"
#include"stdlib.h"
#define NULL 0

typedef struct node {
int num;
}NODE;

NODE *create(NODE *h){
//创建一个字符串，返回头结点
char c;
NODE *p,*q;
h=(NODE *)malloc(sizeof(NODE));
h->num=0;
q=h;
printf("input character\n");
while((c=getchar())!='\n'){
++h->num;
p=(NODE *)malloc(sizeof(NODE));
p->num=c;
q=p;
}
return h;
}

NODE *subs(NODE *h,int pos,int n){
//取出串中指定位置开始的若干字符
NODE *p,*q,*h1,*m;
int i=0;
if((pos+n-1)>h->num){
printf("error\n");//至此，程序正确
return NULL;//返回是否错误？？？？？？？？？
}//取出的字符数多与字符串中的字符数
else{
while(i<pos){
q=p;
++i;
}
m=p;
i=0;
h1=(NODE *)malloc(sizeof(NODE));
h1->num=n;
q=h1;//取出字符串的头结点
while(i<n){
++i;
p=(NODE *)malloc(sizeof(NODE));
p->num=m->num;
q=p;
}
return h1;
}

}

NODE *insert(NODE *h,NODE *h1,int pos){
int i=0;
NODE *p,*q;
p=h;
while(i<(pos-1)){
++i;
}
h->num=h1->num+h->num;
free(h1);
return h;
}

int ddelete(NODE *h,int pos,int n){
//成功删除，返回0；否则返回-1
int i=0;
NODE *p,*q,*m;
if((pos+n-1)>h->num)
return -1;
if(n==0)
return 0;
while(i<pos-1){
++i;
}
q=p;
i=1;
while(i<n){
++i;
m=p;
free(m);
}
free(p);
h->num=h->num-n;
return 0;
}

int position(NODE *h,NODE *h1){//该函数在找不到指定串时正常运行，如果串
//能找到，运行时会发生非法操作
//串匹配，如不存在，返回-1；存在，返回所在位置POS
NODE *p,*q,*m;
int pos=1;
while(p->num!=q->num){
++pos;
}
m=p;
while(p!=NULL&&((pos+h1->num-1)<h->num)){
while(p->num==q->num&&p!=NULL&&q!=NULL){//该出语句出错
}
if(p==NULL&&q!=NULL)
return -1;
else if(q==NULL)
return pos;
++pos;
}
return -1;
}

int main(){
long int w=1,pos,n;
char a,c;
NODE *h,*h1,*p,*q;
while(w){
a=getchar();
if(a=='y'||a=='Y'){
getchar();
h=create(h);
w=0;
}
if(a=='n'||a=='N')
exit(1);
}
while(a!='5'){
printf("Get part of the string? If you want to do so,please input 1;\nFind part of the string? If you want to do so,please input 2;\nInsert another string? If you want to do so,please input 3;\nDelete part of the string? If you want to do so,please input 4;\nExit? If you want to do so,please input 5.\n");
a=getchar();
getchar();//用来消除ENTER键
switch(a){
case '1':{
printf("input the position of the string you want to get\n");
scanf("%d",&pos);
printf("input the number of the string you want to get\n");
scanf("%d",&n);
p=subs(h,pos,n);
if(p=NULL){
printf("heliangliang\n");// 当返回 NULL时，该语句没有被执行,该语句没作用，仅来检测是否被执行
break;
}
else{
printf("%d",p->num);
printf("the following is the string you want to get:\n");
while(p!=NULL){
printf("%c",p->num);
}
printf("\n");
break;
}
}
case '2':{
printf("Please input the string you want to find\n");
h1=(NODE *)malloc(sizeof(NODE));
h1->num=0;
q=h1;
while((c=getchar())!='\n'){
++h1->num;
p=(NODE *)malloc(sizeof(NODE));
p->num=c;
q=p;
}
pos=position(h,h1);
if(pos==-1)
printf("The string cannot be found !\n");
else
printf("The string's position is %d.\n",pos);
break;
}
case '3':{
printf("Please input the string you want to insert\n");
h1=(NODE *)malloc(sizeof(NODE));
h1->num=0;
q=h1;
while((c=getchar())!='\n'){
++h1->num;
p=(NODE *)malloc(sizeof(NODE));
p->num=c;
q=p;
}//至此，程序正确
printf("input the position of the string where you want to insert\n");
scanf("%d",&pos);
h=insert(h,h1,pos);
printf("The new string which you have inserted another string is as follows:\n");
while(p!=NULL){
printf("%c",p->num);
}
printf ("\n");
break;
}
case '4':{
printf("Please input the position where you want yo delete\n");
scanf("%d",&pos);
printf("Please input the number of chars of the string you want to delete\n");
scanf("%d",&n);
if(ddelete(h,pos,n))
printf("There is something error that cannot delete the string you offer successfully\n");
else{
printf("The new string is as follows: \n");
while(p!=NULL){
printf("%c",p->num);
}
printf("\n");
}
break;
}
case '5': exit(1);
}
}
return 0;
}

...全文
8 点赞 收藏 7

7 条回复

liao2001 2003-03-06

andyfr1210 2003-03-06

riverboat 2003-03-06

programmer200x 2003-03-06

ambition2005 2003-01-14

ambition2005 2003-01-10

ambition2005 2003-01-10

1.5w+

C/C++ 非技术区

2003-01-10 01:50