标题:串的操作程序出现了错误?
只看楼主
henji
Rank: 1
等 级:新手上路
帖 子:227
专家分:0
注 册:2009-4-19
结帖率:38.67%
 问题点数:0 回复次数:0 
串的操作程序出现了错误?

#include "stdafx.h"
#include "stdio.h"
#include "string.h"
#include "stdlib.h"
#define OK 1
#define ERROR 0
#define TRUE 1
#define FALSE 0
#define MAXSTRLEN 255
typedef int Status;
typedef unsigned char SString[MAXSTRLEN+1];

Status StrAssigned(SString &T,char *chars)
{
    int i=0;
    int len;
    len=strlen(chars);
    if(strlen(chars)>MAXSTRLEN)
    {
        return ERROR;
    }
    else
    {
        T[0]=strlen(chars);
        for(i=1;i<=T[0];i++)
        {
            T[i]=chars[i-1];
        }
    }
    return OK;
}

Status StrConcat(SString &T,SString S1,SString S2)
{
    int i=0;
    if(S1[0]+S2[0]<=MAXSTRLEN)
    {
        T[0]=S1[0]+S2[0];
        for(i=1;i<=S1[0];i++)
        {
            T[i]=S1[i];
        }
        for(i=1;i<=S2[0];i++)
        {
            T[S1[0]+i]=S2[i];
        }
        return OK;
    }else if(S1[0]>MAXSTRLEN&&S1[0]+S2[0]>MAXSTRLEN)
    {
        T[0]=MAXSTRLEN;
        for(i=1;i<=S1[0];i++)
        {
            T[i]=S1[i];
        }
        for(i=1;i<=MAXSTRLEN-S1[0];i++)
        {
            T[S1[0]+i]=S2[0];
        }
        return OK;
    }
    else
    {
        T[0]=MAXSTRLEN;
        for(i=0;i<MAXSTRLEN;i++)
        {
            T[i]=S1[0];
        }
    }
    return OK;
}

Status SubString(SString &Sub,SString S,int pos,int len)//这个函数当len大于pos的时候,运行的结果出现了乱码字符,程序改如何修改?
{
    int i,j=1;
    if(pos<1||pos>S[0])
    {
        return ERROR;
    }
    if(len>S[0]-pos+1)
    {
        return ERROR;
    }
    for(i=pos;i<=len+len-pos;i++)
    {
        Sub[j]=S[j];
        j++;
    }
    Sub[0]=len;
    return OK;
}

Status StrPrint(SString T)
{
    int i=0;
    for(i=1;i<=T[0];i++)
    {
        printf("%c",T[i]);
    }
    printf("\n");
    return OK;
}

int main(int argc, char* argv[])
{
    int pos,len,k;
    SString T,Sub,S1,S2;
    SString S;
    char chars[20];   
    printf ("input S1:\n");
    scanf ("%s",chars);
    StrAssigned(S1,chars);
    fflush(stdin);
    printf("input S2:\n");
    scanf("%s",chars);
    StrAssigned(S2,chars);
    fflush(stdin);
    printf("after lianjie:\n");
    StrConcat(T,S1,S2);
    StrPrint(T);
    printf("\n");
    printf ("input a string S:\n");
    scanf ("%s",chars);
    StrAssigned(S,chars);
    printf("input pos:\n");
    scanf("%d",&pos);
    printf("input len:\n");
    scanf("%d",&len);
    SubString(Sub,S,pos,len);
    StrPrint(Sub);
    return 0;
}
搜索更多相关主题的帖子: include return 
2010-03-27 23:23



参与讨论请移步原网站贴子:https://bbs.bccn.net/thread-300751-1-1.html




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

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