0

文字列をループして、各文字の使用数をカウントするプログラムを作成しようとしています。問題は、配列を適切に保存できないことです。どんな助けでも大歓迎です。

int main()
{
    string textRad = "";
    int histogram[ANTAL_BOKSTAVER];

    getline(cin, textRad);

    berakna_histogram_abs(histogram, textRad);

    cout << histogram[0] << endl;
    cout << histogram[2];

    return 0;
}

void berakna_histogram_abs(int histogram[], string textRad) 
{
    for(int i = 0; i < ANTAL_BOKSTAVER; i++) 
    {
        histogram[i] = 0;
    }

    for(int i = 0; i < textRad.length(); i++)
    {
        for(int j = 0; j < ANTAL_BOKSTAVER; j++)
        {
            int antal = 0;
            string alfabet = "ABCDEFGHIJKLMNOPQRSTUVWXYZ";

            if(char(toupper(textRad.at(i))) == alfabet.at(j))
            {
                antal++;
            }
            histogram[j] = antal;
        }
    }
}
4

3 に答える 3

1

ここで Javid と Tjofras の回答を考慮に入れると、完全で、より単純で、より安全な例になります。

#include <iostream>
#include <string>
#include <algorithm>
#include <vector>

void berakna_histogram_abs(std::vector<int>& histogram, const std::string& textRad);

int main() {

    const int ANTAL_BOKSTAVER = 26; //Assumed value.

    std::string textRad;
    std::vector<int> histogram(ANTAL_BOKSTAVER, 0);

    std::getline(std::cin, textRad);

    std::transform(textRad.begin(), textRad.end(), textRad.begin(), toupper);
    berakna_histogram_abs(histogram, textRad);

    std::cout << histogram[0] << std::endl;
    std::cout << histogram[2];

    return 0;
}

void berakna_histogram_abs(std::vector<int>& histogram, const std::string& textRad) {

    static std::string alfabet = "ABCDEFGHIJKLMNOPQRSTUVWXYZ";

    std::size_t s = alfabet.length();
    for(std::size_t i = 0; i < s; ++i) {
        histogram[i] = std::count(test_string.begin(), test_string.end(), alfabet[i]);
    }
}
于 2013-08-24T20:30:33.727 に答える
0

antal 変数の使い方が間違っています。各文字に対して 1 を超えることはありません。このようなものがうまくいくはずです:

void berakna_histogram_abs(int histogram[], string textRad) 
{
    for(int i = 0; i < ANTAL_BOKSTAVER; i++) 
    {
        histogram[i] = 0;
    }

    for(int i = 0; i < textRad.length(); i++)
    {
        for(int j = 0; j < ANTAL_BOKSTAVER; j++)
        {
            string alfabet = "ABCDEFGHIJKLMNOPQRSTUVWXYZ";

            if(char(toupper(textRad.at(i))) == alfabet.at(j))
            {
                histogram[j]++;
            }
        }
    }
}

また、変数/メソッド名には英語を使用することもお勧めします。

于 2013-08-24T19:46:01.847 に答える
0

これを試して:

int main()
{

    string textRad = "";
    int histogram[ANTAL_BOKSTAVER];

    getline(cin, textRad);

    berakna_histogram_abs(histogram, textRad);


    cout << histogram[0] << endl;
    cout << histogram[2];

    return 0;
}

void berakna_histogram_abs(int histogram[], string textRad) {

    for(int i = 0; i < ANTAL_BOKSTAVER; i++)
        histogram[i] = 0;

    string alfabet = "ABCDEFGHIJKLMNOPQRSTUVWXYZ";
    for(int i = 0; i < textRad.length(); i++) {
        for(int j = 0; j < alfabet.length(); j++)
            if(char(toupper(textRad.at(i))) == alfabet.at(j))
                histogram[j]++;
    }

}
于 2013-08-24T19:45:42.100 に答える