Версия для печати темы
Нажмите сюда для просмотра этой темы в оригинальном формате
Форум программистов > Центр помощи > [C++]Подсчет в строке количества букв


Автор: Russs 17.5.2007, 16:21
Нужна помощь!  smile  
Программа считывает из первого файла алфавит, из второго произвольный текст. Нужно сравнить алфавит с текстом и вывести на экран солько раз встречается каждая буква.
 smile 

Автор: _hunter 17.5.2007, 16:56
ты разделом ошибся: такое в "центр помощи"/"работа" постить нужно...

Автор: MAKCim 17.5.2007, 18:14
Для домашних заданий, курсовых, существует "Центр Помощи".

Тема перенесена! 

Автор: Xenon 17.5.2007, 19:08
Код

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

struct pair
{
    char m_symbol;
    int m_count;
    pair (char symbol, int count = 0 ):m_symbol(symbol), m_count(count){}
};

void display (pair* obj)
{
    std::cout << obj->m_symbol << ' ' << obj->m_count << std::endl;
}


int main(int argc, char* argv[])
{
    std::string path_alpha;
    std::string path_text;
    std::cout << "Enter path to the file with alphabet: ";
    std::getline(std::cin, path_alpha);
    std::ifstream in_file( path_alpha.c_str() );
    if ( !in_file.is_open() ) 
    {
        std::cout << "File does not exist";
        std::cin.get();
        return -1;
    }
    std::vector<pair*> sym_stats;
    while (true)
    {
        char buf = '\0';
        in_file.get(buf);
        if ( in_file.eof() ) break;
        if (buf != ' ' && buf != '\n') sym_stats.push_back( new pair(buf) );
    }
    in_file.close();
    in_file.clear();
    std::cout << "Enter path to the file with text: ";
    std::getline(std::cin, path_text);
    in_file.open( path_text.c_str() );
    if ( !in_file.is_open() ) 
    {
        std::cout << "File does not exist";
        std::cin.get();
        return -1;
    }
    if ( !in_file.is_open() ) return -1;
    while (true)
    {
        char buf = '\0';
        in_file.get(buf);
        if ( in_file.eof() ) break;
        if (buf != ' ' && buf != '\n') 
        {
            for (std::vector<pair*>::const_iterator iter = sym_stats.begin(); iter != sym_stats.end(); ++iter)
                if ( (*iter)->m_symbol == buf ) ++(*iter)->m_count;
        }
    }
    std::cout << "Results:\n"
    std::for_each(sym_stats.begin(), sym_stats.end(), display);
    std::cin.sync();
    std::cin.get();
    return 0;
}


Powered by Invision Power Board (http://www.invisionboard.com)
© Invision Power Services (http://www.invisionpower.com)