追忆~似水年华 2019-08-18 12:17 采纳率: 0%
浏览 267
已采纳

为什么执行修改功能后会出错(乱码或者多4个空格),是哪个地方出现了问题,具体应该怎么改?

//功能:顺序串的基本功能
#include <iostream.h>
#include <conio.h>
#include <windows.h>
#include <iomanip.h>
#include <string>

using namespace std;
#define maxsize 50//顺序串的总空间大小
enum returninfo{success,fail,overflow,underflow,range__error,empty};//定义返回信息清单

class String
{
public:
    String();//构造函数
    ~String();//析构函数
    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();//求串长
private:
    char *str;//串
    int length;//长度
};

String::String()//构造函数
{
    str=new char[maxsize];//申请数组空间
}

String::~String()//析构函数
{}

returninfo String::strcreate()//创建串
{
    int i=-1,ch;
    cout<<"请输入要创建的字符串(Ctrl+z结束输入):"<<endl;
    while((ch=getch())!=26)
    {
        cout<<char(ch);
        i++;
        if(ch!=13)
            str[i]=char(ch);
        else
            i=i-1;
        cout.flush();//为了每次输入后可以立即显示所输入的字符,先要清除缓冲区
    }
    length=i+1;
    cout<<endl;
    return success;
}

returninfo String::strinsert(int position,char newstr[],int str_length)//插入,当插入的字符串在原串末尾时,就相当于合并字符串
{
    int j;
    if(position>length+1||position<=0)//如果位置错误,返回位置错误
        return range__error;
    if(str_length+length>maxsize)
        return overflow;
    for(j=length-1;j>=position-1;j--)//数据移动
        str[j+str_length]=str[j];
    position=position-1;
    for(j=0;j<str_length;position++,j++)//插入
        str[position]=newstr[j];
    length=str_length+length;
    return success;
}

returninfo String::strdelete(int beginposition,int endposition)//删除
{
    int i,j;
    if(length==0)
        return empty;
    if(beginposition>length||endposition>length||beginposition<=0||endposition<=0||beginposition>endposition)
        return range__error;//如果位置错误则返回错误标志
    for(i=beginposition,j=endposition;j<length;j++,i++)
        str[i-1]=str[j];
    length=length-(endposition-beginposition+1);
    return success;
}

returninfo String::strmodify(int beginposition,int endposition,char newstr[])//修改
{
    int i,j,k,str_length,count,newlength,returnvalue;
    char *newdata;
    count=endposition-beginposition+1;
    str_length=strlen(newstr);
    if(length==0)
        return empty;
    if(beginposition>length||endposition>length||beginposition<=0||endposition<=0||beginposition>endposition)
        return range__error;//如果位置错误则返回错误标志
    for(i=0,j=beginposition-1;i<str_length&&i<count;j++,i++)//处理相同长度的一部分
            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;
    }
    if(str_length<count)//当输入串的长度小于需要修改串的长度时,直接删除一部分
        strdelete(beginposition+str_length,endposition);
    return success;
}

int String::strsearch(char newstr[])//查找
{
    int i=0,str_length,position=0,count=0;//是否相等标志,count用来确定比较时原串的移动
    if(length==0)
        return -1;
    str_length=strlen(newstr);
    for(;i<length&&count<str_length;i++)
    {
        if(str[i]==newstr[count])
        {
            position=i-str_length+2;
            count++;
            continue;
        }
        else
        {
            if(position==1)
                i=i-count;
            count=0;
            position=0;
        }
    }
    return position;
}

void String::strtraverse()//遍历
{
    int i,j;
    if(length>0)
    {
        cout<<endl<<"实际数据从1开始"<<endl;
        cout<<"位置: ";
        for(i=0;i<=length/10;i++)
            cout<<"|---"<<i<<"----|";
        cout<<endl;
        cout<<"位置: ";
        for(i=0;i<=length/10;i++)
        {
            for(j=0;j<=9;j++)
                cout<<j;
        }
        cout<<endl;
        cout<<"当前串:";
        for(i=0;i<length;i++)
            cout<<str[i];
        cout<<endl;
    }
    else
        cout<<"字符串为空!"<<endl;
}

int String::strlength()//求串长
{
    return length;
}

/*
定义一个实现顺序表功能的菜单处理类interfacebase
*/
class interfacebase
{
private:
    String Stringonface;
public:
    void clearscreen(void);//清屏
    void showmenu(void);//显示菜单函数
    int userchoice(void);//用户的选项
    returninfo processmenu(int menuchoice);//菜单函数
};

void interfacebase::clearscreen(void)
{
    system("cls");
}

void interfacebase::showmenu(void)
{
    cout<<"顺序串基本功能菜单"<<endl;
    cout<<"=================="<<endl;
    cout<<"1.创建一个串"<<endl;
    cout<<"2.插入"<<endl;
    cout<<"3.删除"<<endl;
    cout<<"4.修改"<<endl;
    cout<<"5.查找"<<endl;
    cout<<"6.遍历"<<endl;
    cout<<"7.求串长"<<endl;
    cout<<"0.退出"<<endl;
    cout<<"=================="<<endl;
}

int interfacebase::userchoice(void)
{
    int menuchoice;
    cout<<"请输入您的选择:";
    cin>>menuchoice;
    return menuchoice;
}

returninfo interfacebase::processmenu(int menuchoice)
{
    int position,str_length,beginposition,endposition,returnvalue;

    switch(menuchoice)//根据用户的选择进行相应的操作
    {
    case 1:
        returnvalue=Stringonface.strcreate();
        if(returnvalue==success)
        {
            cout<<"建立顺序串操作成功!"<<endl;
        }
        break;
    case 2:
        {
            cout<<"请输入需要插入的位置:";
            cin>>position;

            string str;
            char temp;
            cout<<"输入需要插入的字符串,按回车键结束输入:"<<endl;
            while((temp=getchar())!='\n')
            {
                str +=temp;
            }
            const int LEN =str.length();
            char* newstr = new char[LEN];//根据输入字符串的长度,创建字符数组
            for(int i=0;i<LEN;i++) //将字符串保存到字符数组中
            {
                newstr[i]=str[i];
            }

            cout<<"请输入串长:";
            cin>>str_length;

            returnvalue=Stringonface.strinsert(position,newstr,str_length);
            if(returnvalue==range__error)
                cout<<"对不起,插入的位置超出了范围!"<<endl;
            else if(returnvalue==overflow)
                cout<<"对不起,插入后的串长超出了范围!"<<endl;
            else
                cout<<"顺序串插入操作成功!"<<endl;
            delete newstr;
        }
        break;
    case 3:
        {
            cout<<"请输入串需要删除的开始位置:";
            cin>>beginposition;
            cout<<"请输入串需要删除的结束位置:";
            cin>>endposition;
            returnvalue=Stringonface.strdelete(beginposition,endposition);
            if(returnvalue==empty)
                cout<<"对不起,顺序串为空!"<<endl;
            else if(returnvalue==range__error)
                cout<<"对不起,删除的位置超出了范围!"<<endl;
            else
                cout<<"删除操作成功!"<<endl;
        }
        break;
    case 4:
        {
            cout<<"请输入串需要修改的开始位置:";
            cin>>beginposition;
            cout<<"请输入串需要修改的结束位置:";
            cin>>endposition;
            string str;
            char temp;
            cout<<"请输入修改的新字符串,按回车键结束输入:"<<endl;
            while((temp=getchar())!='\n')
            {
                str +=temp;
            }
            const int LEN =str.length();
            char* newstr = new char[LEN];//根据输入字符串的长度,创建字符数组
            for(int i=0;i<LEN;i++) //将字符串保存到字符数组中
            {
                newstr[i]=str[i];
            }

            returnvalue=Stringonface.strmodify(beginposition,endposition,newstr);
            if(returnvalue==empty)
                cout<<"对不起,顺序串为空!"<<endl;
            else if(returnvalue==range__error)
                cout<<"对不起,修改的位置超出了范围!"<<endl;
            else if(returnvalue==overflow)
                cout<<"对不起,修改后的串长超出了范围!"<<endl;
            else
                cout<<"修改操作成功!"<<endl;
            delete newstr;
        }
        break;
    case 5:
        {
            string str;
            char temp;
            cout<<"请输入需要查找的字符串,按回车键结束输入:"<<endl;
            while((temp=getchar())!='\n')
            {
                str +=temp;
            }
            const int LEN =str.length();
            char* newstr = new char[LEN];//根据输入字符串的长度,创建字符数组
            for(int i=0;i<LEN;i++) //将字符串保存到字符数组中
            {
                newstr[i]=str[i];
            }
            position=Stringonface.strsearch(newstr);
            cout<<"需要查找的字符串出现的位置:"<<position<<endl;
            delete newstr;
        }
        break;
    case 6:
        Stringonface.strtraverse();
        cout<<"遍历操作成功!"<<endl;
        break;
    case 7:
        cout<<"顺序串目前的长度为:"<<Stringonface.strlength()<<endl;
        cout<<"求顺序串长度操作成功!"<<endl;
        break;
    case 0:
        exit(0);
    default:
        cout<<"对不起,您输入的功能编号有错!请重新输入!!!"<<endl;
        break;
    }
    return success;
}

/*
程序主入口
*/
void main(void)
{
    int menuchoice;//定义变量,菜单选单项的选择
    interfacebase interfacenow;
    system("color f0");//修改屏幕的背景色和字的颜色
    interfacenow.clearscreen();//清屏
    while(1)//永真循环
    {
        interfacenow.showmenu();//显示菜单
        menuchoice=interfacenow.userchoice();//获取用户的选择
        interfacenow.processmenu(menuchoice);//处理用户的选择
        system("pause");//暂停
        interfacenow.clearscreen();//清屏
    }
}//主函数结束

  • 写回答

1条回答 默认 最新

  • JonathanYan 2019-08-18 12:35
    关注

    能不能把你的运行结果发上来,我这里修改字符串都没法输入,应该是你读入字符有问题。
    为什么你这里面既有自定义string,也有c++的string还有char[],不能全用string吗,很乱。

    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论

报告相同问题?

悬赏问题

  • ¥15 树莓派与pix飞控通信
  • ¥15 自动转发微信群信息到另外一个微信群
  • ¥15 outlook无法配置成功
  • ¥30 这是哪个作者做的宝宝起名网站
  • ¥60 版本过低apk如何修改可以兼容新的安卓系统
  • ¥25 由IPR导致的DRIVER_POWER_STATE_FAILURE蓝屏
  • ¥50 有数据,怎么建立模型求影响全要素生产率的因素
  • ¥50 有数据,怎么用matlab求全要素生产率
  • ¥15 TI的insta-spin例程
  • ¥15 完成下列问题完成下列问题