| 网站首页 | 业界新闻 | 小组 | 威客 | 人才 | 下载频道 | 博客 | 代码贴 | 在线编程 | 编程论坛
欢迎加入我们,一同切磋技术
用户名:   
 
密 码:  
共有 495 人关注过本帖
标题:求子串问题啊
只看楼主 加入收藏
小小小丹丹
Rank: 2
等 级:论坛游民
帖 子:26
专家分:13
注 册:2010-4-6
结帖率:62.5%
收藏
已结贴  问题点数:18 回复次数:6 
求子串问题啊
status SubString(HString pS1, int pos,int len){   //用sub返回串s的第pos个字符起长度为len的子串
   int i;
   printf("Please input the pos and length!\n ");
   scanf("%d%d",&pos,&len);                                                 //循环控制变量定义
   if(pos<1||pos>pS1.length||len<0||len>pS1.length-pos+1)     //pos位置不合法
     return(ERROR);
   if(Sub.ch)  free(Sub.ch);                              //释放串sub空间原有的数据                              
   if(!len){Sub.ch=NULL;Sub.length=0;}                    //返回长度为0,sub为空串
   else{
     Sub.ch=(char *)malloc(len * sizeof(char));           //为sub分配长度为len的空间
     for(i=0;i<pos+len-2;i++)                             //将pos后长度为len的子串赋给sub串
     Sub.ch=pS1.ch[pos-1+i];                              
     Sub.length=len;                                      //修改sub的串长
    };
return OK;
}求高手改下啊
搜索更多相关主题的帖子: return status 空间 
2010-05-25 16:13
韩明海
Rank: 8Rank: 8
等 级:蝙蝠侠
帖 子:253
专家分:749
注 册:2010-4-3
收藏
得分:3 
你的sub在哪定义的?
2010-05-25 16:23
韩明海
Rank: 8Rank: 8
等 级:蝙蝠侠
帖 子:253
专家分:749
注 册:2010-4-3
收藏
得分:15 
status SubString(HString pS1, int pos,int len){   //用sub返回串s的第pos个字符起长度为len的子串
    int i;
    HString Sub;   //改过
   printf("Please input the pos and length!\n ");
   scanf("%d%d",&pos,&len);                                                 //循环控制变量定义
   if(pos<1||pos>pS1.length||len<0||len>pS1.length-pos+1)     //pos位置不合法
     return(ERROR);
   if(Sub.ch)  free(Sub.ch);                                   
   if(!len)
  {
    Sub.ch=NULL;
    Sub.length=0;
         
   else
    {
         Sub.ch=(char *)malloc(len * sizeof(char));           
         for(i=0;i<pos+len-1;i++)        //改过                  
             Sub.ch=pS1.ch[pos-1+i];                              
         Sub.length=len;                        
    };
return OK;
}
2010-05-25 16:31
小小小丹丹
Rank: 2
等 级:论坛游民
帖 子:26
专家分:13
注 册:2010-4-6
收藏
得分:0 
有道理再帮我看看这个啊
栈里函数的怎么调用
#include"stdio.h"
#include"stdlib.h"
#include"malloc.h"

#define STACK_INIT_SIZE 100;
#define STACKINCREMENT 10;

typedef struct
{
    int *base;
    int *top;
    int stacksize;
}SqStack;

typedef  int ElemType;

int InitStack(SqStack **S) //为栈S分配存储空间,并置S为空栈
{
    int size = STACK_INIT_SIZE;
    (*S) = (SqStack *)malloc(sizeof(SqStack));
    (*S)->base=(int *)malloc(size*sizeof(ElemType));
    if(!(*S)->base) return 0;
    (*S)->top=(*S)->base; //置栈S为空栈
    (*S)->stacksize=STACK_INIT_SIZE;
    return 1;
}
nt  GetTop(SqStack *S,int *e)  //若栈不空,则用e返回S的栈顶元素
{
    if(S->top==S->base) return 0;
    *e=*(S->top-1);
    return 1;
}
                                                                                                                  
int  Push(SqStack *S, int e) /*进栈函数,将e插入栈S中,并使之成为栈顶元素*/                                         
{                                                                                                                 
    if(S->top-S->base>=S->stacksize) /*栈满,追加存储空间*/                                                      
    {                                                                                                            
        int stackinvrement = STACKINCREMENT;                                                                     
        S->base=(ElemType *) realloc(S->base,                                                                     
            (S->stacksize+stackinvrement)*sizeof(ElemType));                                                      
        if(!S->base) return 0; /*存储分配失败*/                                                                  
        S->stacksize+=STACKINCREMENT;                                                                             
    }                                                                                                            
    *S->top++=e;                                                                                                  
    return 1;                                                                                                     
}                                                                                                                 
                                                                                                                  
int  Pop(SqStack *S,int *e)/*出栈函数,若栈S不空,则删除S的栈顶元素,用e返回其值*/                                   
{                                                                                                                 
    if(S->top==S->base) return 0;                                                                                 
    *e=*--S->top;                                                                                                
    return 1;                                                                                                     
}  
void OutputStack(SqStack *S)                                                                                      
{                                                                                                                 
    int *q,i=0;                                                                                                   
    q=S->top-1;                                                                                                   
    for(i=0;i<S->top-S->base;i++)                                                                                 
    {                                                                                                            
        printf("%d ",*q);q--;                                                                                    
    }                                                                                                            
}                                                                                                                 
                                                                                                                  
int main()                                                                                                        
{                                                                                                                 
    int a,b,c,d,e;                                                                                                
    SqStack *h;                                                                                                   
    InitStack(&h);                                                                                                
    printf("你要输入几个元素:\n");                                                                                
    scanf("%d",&a);                                                                                               
    printf("请输入你要的元素:\n");                                                                                
    for(b=0;b<a;b++)                                                                                             
    {                                                                                                            
        scanf("%d",&c);                                                                                          
        Push(h,c);                                                                                                
    }                                                                                                            
    OutputStack(h);                                                                                               
    GetTop(h,&e);                                                                                                  
    Pop(h,&e);                                                                                                   
    printf("返回的栈顶元素:%d\n",e);                                                                              
}
还有push,pop,Output,GetTop等函数怎么调用啊帮帮忙啊我调用函数好多弄不明白呢
2010-05-25 18:34
韩明海
Rank: 8Rank: 8
等 级:蝙蝠侠
帖 子:253
专家分:749
注 册:2010-4-3
收藏
得分:0 
int  Push(SqStack *S, int e) /*进栈函数,将e插入栈S中,并使之成为栈顶元素*/                                         
{                                                                                                                 
    if(S->top-S->base>==S->stacksize) /*栈满,追加存储空间*/          改过                                             
    {                                                                                                            
        int stackinvrement = STACKINCREMENT;                                                                     
        S->base=(ElemType *) realloc(S->base,                                                                     
            (S->stacksize+stackinvrement)*sizeof(ElemType));                                                      
        if(!S->base) return 0; /*存储分配失败*/                                                                  
        S->stacksize+=STACKINCREMENT;                                                                             
    }                                                                                                            
    *S->base=e;       //改过,第一次你只申请了一个结构体,你把指针加加,就出界了。每次只申请一个位置,那么base肯定是指向当前可以操作的地方
                                                                  
    return 1;                                                                                                     
}
void OutputStack(SqStack *S)                                                                                      
{                                                                                                                 
    int *q,i=0;                                                                                                   
    q=S->top;    //改过                                                                                               
    for(i=0;i<S->top-S->base;i++)   你这么减也不行啊,malloc申请的空间不是连续的,相见的结果不一定是中间的结构数
                                                                        
    {                                                                                                            
        printf("%d ",*q);q--;                                                                                    
    }                                                                                                            
}   
nt  GetTop(SqStack *S,int *e)  //若栈不空,则用e返回S的栈顶元素
{
    if(S->top==S->base) return 0;如果按你入站方式,对top++,那么top就应该总是等于base的
    *e=*(S->top-1);
    return 1;
}
int  Pop(SqStack *S,int *e)/*出栈函数,若栈S不空,则删除S的栈顶元素,用e返回其值*/                                   
{                                                                                                                 
    if(S->top==S->base) return 0;                                                                                 
    *e=*--S->top;            系统的堆栈是连续的,可以--,但是这个堆栈是你用malloc做的,不是连续的啊
                                                                                    
    return 1;                                                                                                     
}
其实调用函数很简单,只要形参和实参类型一直就可以了,你的初始化函数,我觉得用一维指针就可以了,因为top是在结构体里,是一维的一个元素,
虽然是指针里的指针,但和二维不一样
2010-05-25 20:07
小小小丹丹
Rank: 2
等 级:论坛游民
帖 子:26
专家分:13
注 册:2010-4-6
收藏
得分:0 
我还是不会啊你能帮我调用一下么写给我看看啊
2010-05-25 20:18
韩明海
Rank: 8Rank: 8
等 级:蝙蝠侠
帖 子:253
专家分:749
注 册:2010-4-3
收藏
得分:0 
OutputStack(h);                                                                                               
    GetTop(h,&e);                                                                                                  
    Pop(h,&e);
你写的这些就是调用函数啊,这没什么好解释的,需要改的的主要是你的top,base的使用
2010-05-28 12:35
快速回复:求子串问题啊
数据加载中...
 
   



关于我们 | 广告合作 | 编程中国 | 清除Cookies | TOP | 手机版

编程中国 版权所有,并保留所有权利。
Powered by Discuz, Processed in 0.027550 second(s), 7 queries.
Copyright©2004-2024, BCCN.NET, All Rights Reserved