-2

我有两个字符串:

string word;
string alphabet;

word有一些我传递给的输入;比方说"XYZ"

alphabet有字母 except XYZ,所以它是"ABCDEFGHIJKLMNOPQRSTUVW"

当我试图用“+=”连接它们时,我得到的只是word(即"XYZ")。我想让它看起来像这样:

XYZABCDEFGHIJKLMNOPQRSTUVW 

我究竟做错了什么?代码基本就是这个vvvv

============================encryption.cpp==================== ==============================

#include <cstdlib>
#include <iostream>
#include "encryption.h"
#include <string>

encryption::encryption()
{
alphabet = "ABCDEFGHIJKLMNOPQRSTUVWXYZ";

}


string encryption::removeletter(string word,char letter)
{
    //remove letter
    string temp;
    int indis=0;
    for(int i = 0; word[i] != '\0'; i++)
    {
        if(word[i] != letter)
            {
                temp[indis]=word[i] ;
                indis++;
            }

    }

    word=temp;

    return word;
}

This is the function i have proplems with :

    void encryption::encrypt(string word)//main.cpp is just calling this atm
    {
        string temp;
        int pos;
         //getting rid of the repeating letters
        for(int i = 0; i < word.length(); i++)
        {
            if( (pos = temp.find(kelime[i])) < 0)
                temp += word[i];
        }
        word=temp;//that ends here
        //taking words letters out of the alphabet
        for(int i = 0; i < word.length(); i++)
        {
            alfabet=removeletter(alfabet,word[i]);

        }
        for(int i = 0; i < word.length()-1; i++)
        {
            for(int j=0;alfabet[j] !='\0'; j++)
                if(alfabet[j+1] =='\0') alfabet[j]='\0';
        }

        /* I tried += here */
    }

============================加密.h==================== =================================

#ifndef encryption_h
#define encryption_h
#include<string>

    class encryption

    {
    public:
        encryption();

        void encrypt(string word);
        string removeletter(string word,char letter);
        //some other functions,i deleted them atm

    public:
            string alphabet;
            string encryptedalphabet;
            //staff that not in use atm(deleted)

    };

#endif

============================main.cpp==================== ====================================

#include <cstdlib>
#include <iostream>
#include "encryption.h"
#include <string>

string word;
encryption encrypted;
cin>>word;
encrypted.encrypt( word);





 /****************************************************************************
orginalcode

**cikarmasifresi.cpp
****************************************************************************/

#include <cstdlib>
#include <iostream>
#include "cikarmasifresi.h"
#include <string>


using namespace std;

cikarmasifresi::cikarmasifresi()
{
alfabe = "ABCDEFGHIJKLMNOPQRSTUVWXYZ";

}



string cikarmasifresi::yazisifrele(string yazi)
{
    for(int j=0;yazi[j] != '\0'; j++)
    {
            for(int i = 0; alfabe[i] != '\0'; i++)
            {
                if(yazi[j]==alfabe[i])
                {
                    yazi[j]=sifrealfabesi[i];
                }

            }
    }

    return yazi;
}
string cikarmasifresi::sifrecoz(string sifreliyazi)
{
    for(int j=0;sifreliyazi[j] != '\0'; j++)
    {
            for(int i = 0; sifrealfabesi[i] != '\0'; i++)
            {
                if(sifreliyazi[j]==sifrealfabesi[i])
                {
                    sifreliyazi[j]=alfabe[i];
                }

            }
    }

    return sifreliyazi;
}
string cikarmasifresi::harfcikar(string kelime,char harf)
{
    //istenmiyen harfi cikar
    string temp;
    int indis=0;
    for(int i = 0; kelime[i] != '\0'; i++)
    {
        if(kelime[i] != harf)
            {
                temp[indis]=kelime[i] ;
                indis++;
            }

    }

    kelime=temp;

    return kelime;
}


void cikarmasifresi::sifrelialfabeolustur(string kelime)
{
    string temp,temp2;
    sifrekelimesi=kelime;

    int pos;
    for(int i = 0; i < kelime.length(); i++)
    {
        if( (pos = temp.find(kelime[i])) < 0)
            temp += kelime[i];
    }
    kelime=temp;

    for(int i = 0; i < kelime.length(); i++)
    {
        alfabe=harfcikar(alfabe,kelime[i]);

    }
    for(int i = 0; i < kelime.length()-1; i++)
    {
        for(int j=0;alfabe[j] !='\0'; j++)
            if(alfabe[j+1] =='\0')  alfabe[j]='\0';

    }


     sifrealfabesi=kelime+alfabe;




        cout<<sifrealfabesi;

    cout<<endl;
    cout<<endl;
        for(int i = 0; kelime[i] != '\0'; i++)
    {
        cout<<kelime[i];
    }






}





/****************************************************************************
** orginalcode

**cikarmasifresi.h
****************************************************************************/


#ifndef cikarmasifresi_h
#define cikarmasifresi_h
#include<string>

using namespace std;

class cikarmasifresi
{
public:
    cikarmasifresi();

    void sifrelialfabeolustur(string kelime);
    string yazisifrele(string yazi);
    string sifrecoz(string sifreliyazi);
    string harfcikar(string kelime,char harf);
public:
        string alfabe;
        string sifrealfabesi;
        string sifrekelimesi;

};

#endif





/****************************************************************************
 orginal code

**main.cpp
****************************************************************************/

#include <cstdlib>
#include <iostream>
#include "cikarmasifresi.h"
#include <string>

using namespace std;

int main()
{

        system("cls");
        string kelime,yazi,sifreli;
        cikarmasifresi sifrele;
        cout<<"********************************************************************************"<<endl;
        cout<<"sifre kelimesi gir :"<<endl;
        cin>>kelime;
        sifrele.sifrelialfabeolustur(kelime);/*
        cout<<"********************************************************************************"<<endl;
        cout<<"Alfabe  :"<<sifrele.alfabe<<endl;
        cout<<"********************************************************************************"<<endl;
        cout<<"Sifreli Alfabe :"<<sifrele.sifrealfabesi<<endl;
        cout<<"********************************************************************************"<<endl;
        cout<<"Cumle gir :"<<endl;
        cin>>yazi;

        cout<<"********************************************************************************"<<endl;
        cout<<"Sifre :"<< sifrele.yazisifrele(yazi)<<endl;
        cout<<"********************************************************************************"<<endl;
        cout<<"sifreli yazi gir :"<<endl;
        cin>>sifreli;
        cout<<"********************************************************************************"<<endl;
        cout<<"Sonuc :"<<sifrele.sifrecoz(sifreli)<<endl;

        */




}
4

1 回答 1

1

这是错误的

string cikarmasifresi::harfcikar(string kelime,char harf)
{
    //istenmiyen harfi cikar
    string temp;
    int indis=0;
    for(int i = 0; kelime[i] != '\0'; i++)
    {
        if(kelime[i] != harf)
            {
                temp[indis]=kelime[i] ;
                indis++;
            }

    }

    kelime=temp;

    return kelime;
}

应该

string cikarmasifresi::harfcikar(string kelime,char harf)
{
    //istenmiyen harfi cikar
    string temp;
    int indis=0;
    for(int i = 0; i < kelime.length(); i++)
    {
        if(kelime[i] != harf)
            {
                temp[indis]=kelime[i] ;
                indis++;
            }

    }

    kelime=temp;

    return kelime;
}

kelime 是一个 std::string。它没有像 C 字符串那样的空终止符。

这些功能以完全相同的方式错误

string cikarmasifresi::sifrecoz(string sifreliyazi)

string cikarmasifresi::yazisifrele(string yazi)

这是一个不同的错误

string cikarmasifresi::harfcikar(string kelime,char harf)
{
    //istenmiyen harfi cikar
    string temp;
    int indis=0;
    for(int i = 0; i < kelime.length(); i++)
    {
        if(kelime[i] != harf)
            {
                temp[indis]=kelime[i] ;
                indis++;
            }

    }
    kelime=temp;
    return kelime;
}

temp是一个零长度的字符串,所以temp[indis]是一个错误。你可能是temp += kelime[i]这个意思

string cikarmasifresi::harfcikar(string kelime,char harf)
{
    //istenmiyen harfi cikar
    string temp;
    for(int i = 0; i < kelime.length(); i++)
    {
        if(kelime[i] != harf)
            {
                temp += kelime[i];
                indis++;
            }

    }
    kelime=temp;
    return kelime;
}

这是第三个错误

for(int i = 0; i < kelime.length()-1; i++)
{
    for(int j=0;alfabe[j] !='\0'; j++)
        if(alfabe[j+1] =='\0')  alfabe[j]='\0';

}

我真的不知道你想在这里做什么,所以我不能建议修复。也许您应该删除此代码。

于 2013-04-11T13:37:53.427 回答