| 网站首页 | 业界新闻 | 小组 | 威客 | 人才 | 下载频道 | 博客 | 代码贴 | 在线编程 | 编程论坛
欢迎加入我们,一同切磋技术
用户名:   
 
密 码:  
共有 1087 人关注过本帖
标题:我用C语言写了一个顺序串基本功能代码,为啥有几个函数显示不合法?懂得指点 ...
只看楼主 加入收藏
中午的星星
Rank: 1
等 级:新手上路
帖 子:9
专家分:0
注 册:2016-6-29
结帖率:33.33%
收藏
已结贴  问题点数:10 回复次数:3 
我用C语言写了一个顺序串基本功能代码,为啥有几个函数显示不合法?懂得指点下啊。
/*
本次实验为C版,用一维数组实现字符串的基本功能。
主要的功能的函数全部删除完毕,
希望学生自己能自己画出示意图,充分的理解工作原理,
注意这次串并不是索引结构,而是一般的一维数组,
所以数据移动是一个问题,
从这个角度,再次复习了线性表的基本操作的具体实现。
*/

//功能:顺序串的基本基本功能
#include <stdio.h>  
#include <conio.h>     
#include <windows.h>
#include <iomanip>
using namespace std;
#define maxsize 30//顺序串的总空间大小
enum returnInfo{success,fail,overflow,underflow,rangeError,empty};//定义返回信息清单
void initString();//初始化函数
returnInfo strCreate(); //创建串
returnInfo strInsert(int position,char newStr[],int str_Length); //插入
returnInfo strDelete(int beginPosition,int endPosition); //删除
returnInfo strModify(int beginPosition,int endPosition,char newStr[]); //修改
int strSearch(char newStr[]);  //查找
void strTraverse(); //遍历
int strLength();//求串长
typedef struct
{
    char *str;  //串
    int length; //长度
}String;         

void initString(String *string)
{
    string->str=(char *)malloc(maxsize);//申请数组空间
}

returnInfo strCreate(String *string)
{   
    int i=-1,ch;
    printf("请输入要创建的字符串(ctrl+z结束输入):\n");
    fflush(stdin);
    while((ch=getch())!=26)
    {
        putch(ch);
        i++;
        if(ch!=13)
            string->str[i]=char(ch);
        else i=i-1;
        fflush(stdin);//为了每次输入后可以立即显示所输入的字符,则先清除缓冲区
    }
    string->length=i+1;
    printf("\n");
    return success;
}
returnInfo strInsert(String *string,int position,char newStr[],int str_Length)  //当插入的字符串在原串末尾时,就相当于合并
{  
        int j;
    if(position>string->length+1||position<=0)
        return rangeError;
    if(str_Length+string->length>maxsize)
        return underflow;
    for(j=string->length-1;j>=position-1;j--)
        string->str[j+str_Length]=string->str[j];
    position=position-1;
    for(j=0;j<str_Length;position++,j++)
        string->str[position]=newStr[j];
    string->length=str_Length+string->length;
    return success;


}
returnInfo strDelete(String *string,int beginPosition,int endPosition)
{
    int i,j;
    if(string->length==0)
  return empty;
    if(beginPosition>string->length||endPosition>string->length||beginPosition<=0||endPosition<=0||beginPosition>endPosition)
      return rangeError;
 for(i=beginPosition,j=endPosition;j<string->length;j++,i++)
  string->str[i-1]=string->str[j];
 string->length=string->length-(endPosition-beginPosition+1);
 return success;



}
returnInfo strModify(String *string,int beginPosition,int endPosition,char newStr[])
{
    int i,j,k,str_Length,count,newLength,returnvalue,beginposition,endposition;
    char *newdata;
 count=endPosition-beginPosition+1;
 str_Length=strlen(newStr);
 if(string->length=0)
  return empty;
    if(beginPosition>string->length||endPosition>string->length||beginPosition<=0||endPosition<=0||beginPosition>endPosition)
      return rangeError;
 for(i=0,j=beginPosition-1;i<str_Length&&i<count;j++,i++)
  string->str[j]=newStr[i];
    if(str_Length>count)
 {
  newLength=str_Length-count;
  newdata=new char[newLength];
  for(i=count,k=0;i<str_Length;i++,k++)
   newdata[k]=newStr[i];
  returnvalue=strInsert(endPosition+1,newdata,newLength);
  if(returnvalue==overflow)
   return overflow;



}
int strSearch(String *string,char newStr[])
{
        int i=0,str_Length,position=0,count=0, Str[100];
    if(length=0)
        return -1;
    str_Length=strlen(newStr);
    for(;i<length&&count<StrLength;i++)
    {
        if(Str[i]==newStr[count])
        {
            position=1-str_Length+2;count++;continue;
        }
        else
        {
            if(position=1)
                i=i-count;
            count=0;
            position=0;
        }
   
    }



}
void strTraverse(String *string)
{
    int i,j;
    if(string->length>0)
    {        
        printf("位置:   ");
        for(i=0;i<=(string->length)/10;i++)
            printf("|---%d----|",i);           
        printf("\n");
        printf("位置:   ");
        for(i=0;i<=(string->length)/10;i++)
        {
            for(j=0;j<=9;j++)
            printf("%d",j);
        }
        printf("\n");
        printf("当前串:  ");
        for(i=0;i<string->length;i++)
            printf("%c",string->str[i]);        
        printf("\n");
    }
    else
        printf("字符串为空!\n");   
}
int strLength(String *string)
{
    return string->length;
}
void menu(void)
{
    printf("顺序串基本功能菜单\n");
    printf("==========\n");
    printf("1.建立顺序串(仅限单行的字符串,建议不用汉字。)\n");
    printf("2.显示串\n");
    printf("3.修改串\n");
    printf("4.插入串\n");
    printf("5.删除串\n");
    printf("6.查找串\n");
    printf("7.求串长度\n");
    printf("8.结束程序\n");
    printf("==========\n");
}
int main()   
{
    system("color f0");
    int flag=0,returnValue,position,beginPosition,endPosition,length;
    char choice,choose,newStr[maxsize];
    String newString;
    initString(&newString);
    menu();
    while(1)
    {
        printf("请输入选择:");
        fflush(stdin);
        scanf("%c",&choice);
        switch(choice)
        {
            case '1':
                if(flag)
                 {
                    printf("数据已存在,是否覆盖?(y/n)");
                    fflush(stdin);
                    scanf("%c",&choose);
                    if(choose=='N'||choose=='n')
                    {
                        printf("数据没有发生更改!\n");
                        break;
                    }
                    if(choose=='Y'||choose=='y')
                    {
                        returnValue=strCreate(&newString);
                        if(returnValue==success)
                        printf("创建成功!\n");
                        flag=1;
                    }
                    else
                        printf("输入错误!\n");
                 }
                 else
                 {
                     returnValue=strCreate(&newString);
                     if(returnValue==success)
                         printf("创建成功!\n");
                     flag=1;
                 }
                 break;

            case '2':

                if(flag==0)
                    printf("请先建立串!\n");
                else
                    strTraverse(&newString);                        
                break;
               
            case '3':

                if(flag==0)
                    printf("请先建立串!\n");
                else
                {
                    strTraverse(&newString);
                    printf("请输入要修改的初始位置:");
                    fflush(stdin);
                    scanf("%d",&beginPosition);
                    printf("请输入要修改的终止位置:");
                    fflush(stdin);
                    scanf("%d",&endPosition);
                    printf("请输入修改后的字符串:");
                    fflush(stdin);
                    scanf("%s",newStr);
                    returnValue=strModify(&newString,beginPosition,endPosition,newStr);
                    if(returnValue==success)
                    {
                        printf("修改成功!\n");
                        strTraverse(&newString);
                    }
                    if(returnValue==overflow)
                    {   
                        printf("字符串的最大长度为%d\n",maxsize);
                        printf("发生溢出,操作失败!\n");
                    }
                    if(returnValue==rangeError)
                        printf("位置错误,操作失败!\n");
                    if(returnValue==empty)
                        printf("字符串为空,操作失败!\n");
                }
                break;
                     
            case '4':

                if(flag==0)
                    printf("请先建立串!\n");
                else
                {
                    strTraverse(&newString);
                    printf("请输入要插入的位置:");
                    fflush(stdin);
                    scanf("%d",&position);
                    printf("请输入要插入的字符串:");
                    fflush(stdin);
                    scanf("%s",newStr);
                    length=strlen(newStr);
                    returnValue=strInsert(&newString,position,newStr,length);
                    if(returnValue==success)
                    {
                        printf("插入成功!\n");
                        strTraverse(&newString);
                    }
                    if(returnValue==overflow)
                    {   
                        printf("字符串的最大长度为%d\n",maxsize);
                        printf("发生溢出,操作失败!\n");
                    }
                    if(returnValue==rangeError)
                        printf("位置错误,操作失败!\n");
                    }
                    break;
                     
            case '5':

                if(flag==0)
                    printf("请先建立串!\n");
                else
                {
                    strTraverse(&newString);
                    printf("请输入要删除的初始位置:");
                    fflush(stdin);
                    scanf("%d",&beginPosition);
                    printf("请输入要删除的终止位置:");
                    fflush(stdin);
                    scanf("%d",&endPosition);
                    returnValue=strDelete(&newString,beginPosition,endPosition);
                    if(returnValue==success)
                    {
                        printf("删除成功!\n");
                        strTraverse(&newString);
                    }
                    if(returnValue==rangeError)
                        printf("位置错误,操作失败!\n");
                    if(returnValue==empty)
                        printf("字符串为空,操作失败!\n");
                }                        
                break;
                        
            case '6':

                if(flag==0)
                    printf("请先建立串!\n");
                else
                {
                    strTraverse(&newString);
                    printf("请输入要查找的字符串:");
                     fflush(stdin);
                    scanf("%s",&newStr);
                    position=strSearch(&newString,newStr);
                    if(position>0)
                    {
                        printf("已找到!\n");
                        printf("字符串出现的初始位置为:%d\n",position);
                    }
                    if(position==0)
                        printf("您要查找的字符串不存在!\n");
                    if(position==-1)
                        printf("字符串为空,操作失败!\n");
                }                        
                break;
                     
            case '7':

                if(flag==0)
                    printf("请先建立串!\n");
                else
                {
                    strTraverse(&newString);
                    length=strLength(&newString);
                    printf("当前串的长度为:%d\n",length);
                }
                break;
                     
            case '8':

                exit(0);
                break;

            default:
                printf("输入错误!\n");break;
        }
        printf("按任意键继续......\n");
        getch();
        system("cls");
        menu();
    }
    getch();
    return 0;
}
搜索更多相关主题的帖子: include success 工作原理 C语言 示意图 
2016-10-27 22:09
rjsp
Rank: 20Rank: 20Rank: 20Rank: 20Rank: 20
等 级:版主
威 望:528
帖 子:9025
专家分:54030
注 册:2011-1-18
收藏
得分:4 
为啥有几个函数显示不合法?
难道让别人用肉眼编译?你到是说出哪个不合法呀!
比如 某某行内容,编译器报错信息为什么什么

2016-10-28 09:39
书生牛犊
Rank: 14Rank: 14Rank: 14Rank: 14
来 自:星夜征程
等 级:贵宾
威 望:10
帖 子:1101
专家分:5265
注 册:2015-10-27
收藏
得分:4 
我复制了一下代码,进行“自动格式化”整理了一下代码格式后发现其中某个函数的花括号不匹配。
图片附件: 游客没有浏览图片的权限,请 登录注册

但是我直接在那里补上花括号以后又报错有几个变量使用前未定义。。这代码这么长,从代码前的注释又可以看出那是别人写的代码剪切掉一部分后让你们补充的。所以怎么修补完整还是题主自己琢磨吧。


φ(゜▽゜*)♪
2016-10-28 11:17
炎天
Rank: 13Rank: 13Rank: 13Rank: 13
来 自:桃花岛
等 级:贵宾
威 望:29
帖 子:1218
专家分:4986
注 册:2016-9-15
收藏
得分:4 

int strSearch(char newStr[]);  
int strSearch(String *string,char newStr[])

早知做人那么辛苦!  当初不应该下凡
2016-10-28 11:21
快速回复:我用C语言写了一个顺序串基本功能代码,为啥有几个函数显示不合法?懂 ...
数据加载中...
 
   



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

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