2

これはCクラス用です。私は次の方法を持っています。元の単語が子音で始まる場合は、最初の文字を末尾 (句読点を終了する前) に移動して newWord を作成し、文字列に「ay」を追加します。これに対する私のすべてのロジックとケースはうまくいきます。私が抱えている問題は、最後に char* newWord を通過して印刷すると、値が元の単語のままになることです。また、isConsonant や isCapital などのメソッドを自分で定義しました。EndPunc は、最初の終了句読点のインデックスを取得します。

例: word = "猫?!!" newWord = "Atc?!!"

word = "りんご" newWord = "appleay"

しかし、後で newWord をトラバースすると、まだ「Cat?!!」です。または「リンゴ」。私は何を間違っていますか?

#include <stdio.h>
//#include <string.h>

#define MAXLENGTH 31

char word[31] = "Aat!??!";
char newWord[31] = "";
char* w = word; char* n = newWord;

int isConsonant(char c) // return 1 if consonant, 0 if vowel, -1 if neither
{
    int i = 0;
    while(i < 33)
    {
        if(c == 'a'-i || c == 'e'-i || c == 'i'-i || c == 'o'-i || c == 'u'-i)
            return 0;
        i+=32;
    }
    if(c >= 65 && c <= 122) // its a letter
        return 1;
    else return -1;
}

int isCapital(char c)
{
    if(c >= 97 && c <= 122) // lowerCase
        return 0;
    else return 1; // capital   

}

int isPunctuation(char c)
{
    if(c == '!' || c == ',' || c == '.' || c == '?' || c == ';' || c == ':')
        return 1;

    return 0;   
}

int endPuncIndex(int wordLength, char* word)
{  
    int index = wordLength;

    word += wordLength-1;
    while(isPunctuation(*word--))
        index--;

    return index;
}

int pigLatin(char* word, char* newWord)
{
    int length = 0;
    char* tempWord = word;

    while(*tempWord++ != '\0')
        if(++length > MAXLENGTH)
            return -1;

    tempWord = tempWord-length-1;

    int puncIndex = endPuncIndex(length, word); // get index of last punctuation, if none, index will be length of string

    if(isConsonant(*word) == 1) // first letter is consonant
    {
        char firstLetter = *tempWord;
        char secondLetter = *(++tempWord);
        tempWord++;
        if(isCapital(firstLetter))
        {
            firstLetter += 32; // makes it lowercase, will need to move this to the end
            if(isCapital(secondLetter) == 0) // if second letter isn't capital, make it capital
                secondLetter -= 32;
        }

        int start = 0;
        newWord = &secondLetter;
        newWord++;
        while(start++ < puncIndex-2) // go up to punct index (or end of String if no ending punct)
        {
            newWord = tempWord++;
            newWord++;
        }
        newWord = &firstLetter;
        newWord++;
    }
    else // vowel, just copies the word letter for letter, no insert or shifting
    {
        int start = 0;
        while(start++ < puncIndex) // go up to punct index (or end of String if no ending punct)
        {
            newWord = tempWord++;
            newWord++;
        }
    }

    // add "ay"
    newWord = "a";
    newWord++; 
    newWord = "y";
    newWord++;
    //then add remaining punctuation
    while(puncIndex++ < length)
    {
        newWord = tempWord++;
        newWord++;
    }
    newWord = newWord-(length);
    while(*newWord != '\0')
        printf("%c",*(newWord++));

    return length+3;
}

int main()
{
    pigLatin(w,n);
    printf("\n");
    system("PAUSE");
    return 0;   
}
4

3 に答える 3

1

純粋なローカル変数に変更を加えています:

    char firstLetter = *tempWord;      /* these are both values not pointers */
    char secondLetter = *(++tempWord); 

さらに悪いことに、これらのローカル変数へのポインターをキャプチャしてから、変更しようとしていたメモリではなく、それらを変更しています。

    newWord = &firstLetter;
    newWord++;

    /* ... */

    newWord = &secondLetter;
    newWord++;

おそらくあなたは次のことを意味しました:

    *newWord = firstLetter; /* store the letter in the position */
于 2012-10-11T16:05:39.227 に答える
0

提案することは他にもたくさんあります...ただし、コードのいくつかの部分を変更しました。ファイルコンパレータを使用して変更を確認してください...

#include <stdio.h>
//#include <string.h>

#define MAXLENGTH 31

char word[31] = "Aat!\?\?!";
char newWord[31] = {0,};
char* gword = word;
char* gnewWord = newWord;



int isConsonant(char c) // return 1 if consonant, 0 if vowel, -1 if neither
{
    int i = 0;
    while (i < 33)
    {
        if (c == 'a' - i || c == 'e' - i || c == 'i' - i || c == 'o' - i || c
                == 'u' - i)
            return 0;
        i += 32;
    }
    if (c >= 65 && c <= 122) // its a letter
        return 1;
    else
        return -1;
}

int isCapital(char c)
{
    if (c >= 97 && c <= 122) // lowerCase
        return 0;
    else
        return 1; // capital

}

int isPunctuation(char c)
{
    if (c == '!' || c == ',' || c == '.' || c == '?' || c == ';' || c == ':')
        return 1;

    return 0;
}

int endPuncIndex(int wordLength, char* word)
{
    int index = wordLength;

    word += wordLength - 1;
    //while(isPunctuation(*word--))
    while (isPunctuation(*word))
    {
        word--;
        index--;
    }

    return index;
}

int pigLatin(char* word, char* newWord)
{
    int length = 0;
    char* tempWord = word;

    while (*tempWord++ != '\0')
        if (++length > MAXLENGTH)
            return -1;

    tempWord = tempWord - length - 1;

    // get index of last punctuation, if none, index will be length of string
    int puncIndex = endPuncIndex(length, word);

    if (isConsonant(*word) == 1) // first letter is consonant
    {
        char firstLetter = *tempWord;
        char secondLetter = *(++tempWord);
        tempWord++;
        if (isCapital(firstLetter))
        {
            firstLetter += 32; // makes it lowercase, will need to move this to the end
            if (isCapital(secondLetter) == 0) // if second letter isn't capital, make it capital
                secondLetter -= 32;
        }

        int start = 0;
        *newWord = secondLetter;
        newWord++;
        while (start++ < puncIndex - 2) // go up to punct index (or end of String if no ending punct)
        {
            *newWord = *tempWord;
            tempWord++;
            newWord++;
        }
        *newWord = firstLetter;
        newWord++;
    }
    else // vowel, just copies the word letter for letter, no insert or shifting
    {
        int start = 0;
        while (start++ < puncIndex) // go up to punct index (or end of String if no ending punct)
        {
            *newWord = *tempWord;
            tempWord++;
            newWord++;
        }

        // add "ay"
        *newWord = 'a';
        newWord++;
        *newWord = 'y';
        newWord++;

    }

    //then add remaining punctuation
    while (puncIndex++ < length)
    {
        *newWord = *tempWord;
        newWord++;
        tempWord++;
    }
    fprintf(stderr, "Modified word: %s", gnewWord);

    return length + 3;
}


int main(void)
{
    pigLatin(gword, gnewWord);
    printf("\n");
    // system("PAUSE");
    return 0;
}
于 2012-10-11T16:16:00.050 に答える
0

使用する場合:

firstLetter += 32;

ローカル変数に値を割り当てるだけです。その後、あなたは次のようなことをします

newWord = &secondLetter;

これは、ローカル変数のアドレスを受け取り、それを newWord に割り当てます。newWord は文字列のアドレス、つまり文字列の最初の文字のアドレスであるため、あまり意味がないようです。たぶん、単語のこの場所で文字の値にアクセスしたかったのでしょう

newWord[0] = 秒文字;

firstLetter と secondLetter の実際の値をどこにも割り当てていないため、単語が変更されませんでした。幸いなことに、最後に newWord を tempWord に置き換えることができたので、最後に元の単語が再び表示されます。間違っているのはこの場所だけではありません。実際に間違っているように見える newWord への割り当てがたくさんあります。ほとんどの場合、文字列の基になるデータではなくポインターを変更しています。

ポインターの使い方を正しく理解しているとは思えません。http://pw1.netcom.com/~tjensen/ptr/pointers.htmなどの優れたチュートリアルをご覧ください。

ポインターと文字列の使用方法に慣れたら、コードを見直して、newWord の実際の値ではなくポインターのみを変更していることを簡単に確認してください。

于 2012-10-11T16:02:11.383 に答える