| 网站首页 | 业界新闻 | 小组 | 威客 | 人才 | 下载频道 | 博客 | 代码贴 | 在线编程 | 编程论坛
欢迎加入我们,一同切磋技术
用户名:   
 
密 码:  
共有 356 人关注过本帖
标题:只在乎字符串!!!谁能帮我改啊、、、
取消只看楼主 加入收藏
概率
Rank: 1
来 自:江西省奉新县
等 级:新手上路
帖 子:11
专家分:0
注 册:2011-6-18
结帖率:100%
收藏
 问题点数:0 回复次数:2 
只在乎字符串!!!谁能帮我改啊、、、
#include<iostream.h>
#include<string>
class wang
{
public:
    wang(string&s1){};
    wang(wang*s1);
    wang operator=();
    wang operator==(wang);
    wang operator<(wang);
    wang operator>(wang);
    wang operator+(wang);
    chuanchang(wang*s1);
    char *charu(char *s1,char *s2,int a);
    shanchu(wang*s1,wang*s2);
    zichuan(wang*s1);
    void GetNext(char *t,int *next);
}
wang operator==(wang)
{

    cout<<"请输入要比较的字符串s2:"<<endl;
    cin>>s2;
    if((s2)==0)
        return ture;
    else
        return false;
}
wang operator<(wang)
{

    cout<<"请输入要比较的字符串s2:"<<endl;
    cin>>s2;
    if((s2)<0)
        return ture;
    else
        return false;
}
wang operator>(wang)
{
    cout<<"请输入要比较的字符串s2:"<<endl;
    cin>>s2;
    if((s2)>0)
        return ture;
    else
        return false;
}
wang operator+(wang)
{
    cout<<"请输入想要连接的字符串:"<<endl;
    cin>>s2;
    char*temp;
    temp=new char[strlen(s1)+strlen(s2)+1];
    strcpy(temp,s1);
    strcat(temp,s2);
    wang result(temp);
    return result;
}
wang::chuanchang(wang*s1)
{
    cout<<"该字符串的长度是:"<<strlen(s1)<<endl;
}
wang::char *charu(char *s1,char *s2,int a)
{
    int a;
    cout<<"请输入要插入的字符串:"<<endl;
    cin>>s2;
    cout<<"输入插入处的下标:"<<endl;
    cin>>a;
    for(int m = 0;s1[m] != '\0';m++);
    for(int n = 0;s2[n] != '\0';n++);
    char *ch = new char [m + n];
    int i,j;
    for(i = 0;i <= a;i++)
    ch[i] = s1[i];
    for(j = 0;s2[j] != '\0';j++,i++)
    ch[i] = s2[j];
    for(j = a + 1;s1[j] != '\0';i++,j++)
    ch[i] = s1[j];
    ch[i] = '\0';
    char *p = charu(s1,s2,a);
    cout<<"插入后的字符串为:"<<p<<endl;
}
wang::shanchu(wang*s1)
{
    int i,j;
    for(i=0,j=0;s[i]!=0;i++,j++)
        if(s[j]>='0'&&s[j]<'10')
            i--;
        else
            s[i]=s[j];cout<<"字符串删除后输出是:"<<s<<endl;
}
wang::zichuan(wang&s1)
{
    int start,n,s3;
    cout<<"请输入字串的起始位置:"<<endl;
    cin>>start;
    cout<<"请输入子串的长度:"<<endl;
    cin>>n;
    s3=s1.assign(s1,start-1,n);
    cout<<s3.data()<<endl;
}
wang::void GetNext(char *s2,int *next)
{
    cout<<"请输入s2串:"<<endl;
    cin>>s2;
    int i = 1,j = 0;//设置初始下标i和j
    int n = strlen(s2);//s2串的长度为n
    next[0] = -1;
    next[1] = 0;
    while(i < n)
    {
        if((j == -1) || (s2[j] == s2[i]))
        {next[++i] = ++j;}
        else
        {j = next[j];}
    int KMP(char *s1,char *s2)
    {
    int n = strlen(s1);  //输入s1串的长度n
    int m = strlen(s2);  //输入s2串的长度m
    int *next = new int[m];
    GetNext(s2,next);
    for(int i=0,j=0;i<n&&j<m;)//循环直到串s1中所剩字符长度小于s2的长度或s2中所有字符均比较完;
    {
        if((j == -1)||(s1[i] == s2[j]))
        {
            i++;
            j++;
        }
        else
        {j = next[j];}
    }
    return (j==m?i-j:-1);
    }

    int pos;//中间量,判断是否在s1串中找到s2串
    pos = KMP(s1,s2); //调用kmp算法
    if(pos != -1)//pos等于-1,说明没有在s1串中找到s2串
    {
    int i = 0;
    cout<<"匹配成功!"<<endl;
    cout<<"找到s2串:";
    while(i < strlen(s2))
    {
    cout<<s1[pos+i];
    i++;
    }
    cout<<endl;
    }
        else  
        {
            cout<<"在串s1中没有找到s2串!"<<endl;
        }
    return 0;
    }
}
void main(void)
{
    int num=0,s1;
    cout<<"请输入一个字符串s1:"<<endl;
    cin>>s1;
    while(num!=10)
    {
        cout<<"请选择功能序号:"<<endl;
        cout<<"1>比较是否相等       2>比较是否小于       3>比较是否大于       4>连接       5>求串长       6>串插入       7>串删除       8>求子串       9>串匹配       10>退出"<<endl;
        cin>>num;
        if(num==1)bool operator==(wang*s1,wang*s2);
        if(num==2)bool operator<(wang*s1,wang*s2);
        if(num==3)bool operator>(wang*s1,wang*s2);
        if(num==4)bool operator+(wang*s1,wang*s2);
        if(num==5)chuanchang(wang*s);
        if(num==6)char *charu(char *s1,char *s2,int a);
        if(num==7)shanchu(wang*s1);
        if(num==8)zichuan(wang*s1);
        if(num==9)void GetNext(char *t,int *next);
    }
}
我必须得承认,这个程序写的一无是处,前两天我把它像伤口一样在这里晒过,没有得到我想要的结果,今天我依然坚持。谁能帮我改好啊!!!
搜索更多相关主题的帖子: include public 字符串 
2011-06-22 16:46
概率
Rank: 1
来 自:江西省奉新县
等 级:新手上路
帖 子:11
专家分:0
注 册:2011-6-18
收藏
得分:0 
真的无人可解么?
2011-06-22 20:54
概率
Rank: 1
来 自:江西省奉新县
等 级:新手上路
帖 子:11
专家分:0
注 册:2011-6-18
收藏
得分:0 
现在我改成了这样的:
#include<iostream.h>
#include<string>
class wang
{
public:
    wamg();
    wang(char s1);
    wang(char s1,char s2);
    friend bool wang operator==(wang& s1,wang& s2);
    friend bool wang operator<(wang& s1,wang& s2);
    friend bool wang operator>(wang& s1,wang& s2);
    friend wang operator+(wang& s1,wang& s2);
    chuanchang(wang*s1);
    char *charu(char *s1,char *s2,int a);
    shanchu(wang*s1,wang*s2);
    zichuan(wang*s1);
    void GetNext(char *t,int *next);
}
friend bool wang operator==(wang& s1,wang&s2)
{
    if((s2)==0)
        return true;
    else
        return false;
}
friend bool wang operator<(wang& s1,wang& s2)
{
    if((s2)<0)
        return true;
    else
        return false;
}
friend bool wang operator>(wang& s1,wang& s2)
{
    if((s2)>0)
        return true;
    else
        return false;
}
friend wang operator+(wang& s1,wang& s2)
{
    char*temp;
    temp=new char[strlen(s1)+strlen(s2)+1];
    strcpy(temp,s1);
    strcat(temp,s2);
    wang result(temp);
    return result;
}
wang::chuanchang(wang*s1)
{
    cout<<"该字符串的长度是:"<<strlen(s1)<<endl;
}
wang::char *charu(char *s1,char *s2,int a)
{
    int a;
    cout<<"输入插入处的下标:"<<endl;
    cin>>a;
    for(int m = 0;s1[m] != '\0';m++);
    for(int n = 0;s2[n] != '\0';n++);
    char *ch = new char [m + n];
    int i,j;
    for(i = 0;i <= a;i++)
    ch[i] = s1[i];
    for(j = 0;s2[j] != '\0';j++,i++)
    ch[i] = s2[j];
    for(j = a + 1;s1[j] != '\0';i++,j++)
    ch[i] = s1[j];
    ch[i] = '\0';
    char *p = charu(s1,s2,a);
    cout<<"插入后的字符串为:"<<p<<endl;
}
wang::shanchu(wang*s1)
{
    int i,j;
    for(i=0,j=0;s[i]!=0;i++,j++)
        if(s[j]>='0'&&s[j]<'10')
            i--;
        else
            s[i]=s[j];cout<<"字符串删除后输出是:"<<s<<endl;
}
wang::zichuan(wang*s1)
{
    int start,n,s3;
    cout<<"请输入字串的起始位置:"<<endl;
    cin>>start;
    cout<<"请输入子串的长度:"<<endl;
    cin>>n;
    s3=s1.assign(s1,start-1,n);
    cout<<s3.data()<<endl;
}
wang::void GetNext(char *s2,int *next)
{
    int i = 1,j = 0;//设置初始下标i和j
    int n = strlen(s2);//s2串的长度为n
    next[0] = -1;
    next[1] = 0;
    while(i < n)
    {
        if((j == -1) || (s2[j] == s2[i]))
        {next[++i] = ++j;}
        else
        {j = next[j];}
    int KMP(char *s1,char *s2)
    {
    int n = strlen(s1);  //输入s1串的长度n
    int m = strlen(s2);  //输入s2串的长度m
    int *next = new int[m];
    GetNext(s2,next);
    for(int i=0,j=0;i<n&&j<m;)//循环直到串s1中所剩字符长度小于s2的长度或s2中所有字符均比较完;
    {
        if((j == -1)||(s1[i] == s2[j]))
        {
            i++;
            j++;
        }
        else
        {j = next[j];}
    }
    return (j==m?i-j:-1);
    }

    int pos;//中间量,判断是否在s1串中找到s2串
    pos = KMP(s1,s2); //调用kmp算法
    if(pos != -1)//pos等于-1,说明没有在s1串中找到s2串
    {
    int i = 0;
    cout<<"匹配成功!"<<endl;
    cout<<"找到s2串:";
    while(i < strlen(s2))
    {
    cout<<s1[pos+i];
    i++;
    }
    cout<<endl;
    }
        else  
        {
            cout<<"在串s1中没有找到s3串!"<<endl;
        }
    return 0;
    }
}
void main(void)
{
    int num=0,s1,s2;
    cout<<"请输入一个字符串s1:"<<"请输入一个字符串s2"<<endl;
    cin>>s1>>s2;
    while(num!=10)
    {
        cout<<"请选择功能序号:"<<endl;
        cout<<"1>比较是否相等       2>比较是否小于       3>比较是否大于       4>连接       5>求串长       6>串插入       7>串删除       8>求子串       9>串匹配       10>退出"<<endl;
        cin>>num;
        if(num==1)bool operator==(wang*s1,wang*s2);
        if(num==2)bool operator<(wang*s1,wang*s2);
        if(num==3)bool operator>(wang*s1,wang*s2);
        if(num==4)bool operator+(wang*s1,wang*s2);
        if(num==5)chuanchang(wang*s);
        if(num==6)char *charu(char *s1,char *s2,int a);
        if(num==7)shanchu(wang*s1);
        if(num==8)zichuan(wang*s1);
        if(num==9)void GetNext(char *t,int *next);
    }
}
可是还有问题
忘指正
2011-06-22 22:47
快速回复:只在乎字符串!!!谁能帮我改啊、、、
数据加载中...
 
   



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

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