C++ (Qt)void MainWindow::bufferButtonSave(){ QClipboard *clipboard = QApplication::clipboard(); QString originalText = clipboard->text(); QString countItemFullString; QString countItemString; QStringList itemList; int countItem(0); int countItemFull(0); originalText = originalText.toLower(); originalText.replace(QRegExp("\\W"), " "); originalText.replace(QRegExp("\\d"), " "); // ЗАДАЧА1: ЭТО НЕ РАБОТАЕТ originalText.replace(QRegExp("[а-я]"), ""); textory.clear(); itemList = originalText.split(QRegExp("\\s+"),QString::SkipEmptyParts); QMap <QString, int> frequencyMap; foreach (QString Item, itemList) frequencyMap[Item] = (frequencyMap.contains(Item)) ? frequencyMap[Item]+1 : 1; textory.enableAdd( true ); QMapIterator<QString,int> Iter(frequencyMap); while(Iter.hasNext()) { Iter.next(); countItem = Iter.value(); countItemString.setNum(countItem); countItemFull = countItemFull + countItem; // ЗАДАЧА2: ОРГАНИЗОВАТЬ ВЫВОД В ПОРЯДКЕ ЧАСТОТНОСТИ textory.addItem( Textory::Item( 1, Iter.key() + "\t" + countItemString) ); } countItemFullString.setNum(countItemFull); ui.textoryWidget->getTextoryLabel().setText( "Frequency list (" + countItemFullString + ")" );}
C++ (Qt) foreach (QString Item, itemList) frequencyMap[Item] = (frequencyMap.contains(Item)) ? frequencyMap[Item]+1 : 1;
C++ (Qt) foreach (QString Item, itemList) ++frequencyMap[Item];
C++ (Qt)struct CDataBlock { QString mSource; // строка - первоисточник QStringList mWords; // список слов QMap <QString, int> mFreqMap; // частотная карта};
C++ (Qt)QStringList CDataBlock::GetByOrder( void ) const; // выдаем строки по порядку с приклееной частотойQStringList CDataBlock::GetByFrequency( bool ascending ) const; // выдаем строки по частотt
C++ (Qt) QMapIterator<QString,int> Iter(frequencyMap); Iter.toBack(); while(Iter.hasPrevious()) { Iter.previous(); countItem = Iter.value(); countItemString.setNum(countItem); countItemFull = countItemFull + countItem; // ЗАДАЧА2: ОРГАНИЗОВАТЬ ВЫВОД В ПОРЯДКЕ ЧАСТОТНОСТИ textory.addItem( Textory::Item( 1, Iter.key() + "\t" + countItemString) ); }
C++ (Qt)// заполняем векторQVector <CFreq> theFreq;for (TMapStr::const_iterator it = theMap.begin(); it != theMp.end(); ++it) theFreq.push_back(CFreq(it->value(), &it->key()));
C++ (Qt)// заполняем векторQVector <CFreq> theFreq;theFreq.reserve( theMap.size() );
C++ (Qt)#include <QRegExp>#include <QStringList>#include <QFile>#include <QTime>#include <QDebug> int main( int argc, char *argv[] ){ QFile in( "test.txt" ); if( !in.open( QIODevice::ReadOnly ) ) { qWarning( "Test file not found." ); return 1; } const int num_iter = 100; QString src = QString::fromUtf8( in.readAll() ); QHash<QString, int> hash; QMap <QString, int> MyMap; QTime tm; tm.start(); for( int i = 0; i < num_iter; ++i ) { int start = 0; int cur = 0; QRegExp rx( "[\\s\\.,:;-()]+" ); for(;;) { cur = rx.indexIn( src, cur ); QString word = src.mid( start, (cur != -1)? cur - start : -1 ); if( !word.isEmpty() ) hash[ word ]++; if( cur == -1 ) break; start = ++cur; } } qDebug() << tm.elapsed() << hash.size(); tm.start(); for( int i = 0; i < num_iter; ++i ) { QStringList List = src.split(QRegExp("[\\s\\.,:;-()]+"),QString::SkipEmptyParts); foreach (QString Item, List) MyMap[Item] = (MyMap.contains(Item)) ? MyMap[Item]+1 : 1; } qDebug() << tm.elapsed() << MyMap.size(); return 0;}