Advertisement
Guest User

Untitled

a guest
Jun 6th, 2012
35
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 3.94 KB | None | 0 0
  1. /*   (c) VlaoMao
  2.  *   RU, 2012
  3.  *
  4.  *
  5.  *
  6.  */
  7. #include <QApplication>
  8. #include <QFile>
  9. #include <QTextCodec>
  10. #include <QTextStream>
  11. #include <iostream>
  12.  
  13. void printToStd(QMap<QString,int> map);
  14. void printToFile(QMap<QString,int> map,QString fileName);
  15.  
  16. QString fileName;
  17. double all = 0;
  18.  
  19. int main(int argc,char* argv[])
  20. {
  21.     QTextCodec::setCodecForCStrings(QTextCodec::codecForName("UTF-8"));
  22.  
  23.     if(argc == 1)
  24.     {
  25.         printf("Корявенькая реализация подсчёта частоты появления символов в файле");
  26.         printf("\nИспользование: ./symbols filename\n");
  27.         printf("Из подсчёта исключаются символы \\r, \\n, \\t\n");
  28.         exit(EXIT_FAILURE);
  29.     }
  30.     fileName = QString::fromLocal8Bit(argv[1]);
  31.     bool toFile = false;
  32.     QString saveFileName;
  33.     if((argc > 2) && (QString(argv[2]) == QString("-f")))
  34.     {
  35.         toFile = true;
  36.         saveFileName = argv[3];
  37.     }
  38.     QFile file;
  39.     file.setFileName(fileName);
  40.     if(!file.open(QIODevice::ReadOnly))
  41.     {
  42.         printf("Ошибка открытия файла: %s\n",fileName.toStdString().c_str());
  43.         exit(EXIT_FAILURE);
  44.     }
  45.     QTextStream stream(&file);
  46.     QMap<QString,int> syms;
  47.     while(!stream.atEnd())
  48.     {
  49.         QString readedSymbol = stream.read(1);
  50.         QString::iterator it = readedSymbol.begin();
  51.         for(; it != readedSymbol.end(); ++it)
  52.         {
  53.             if((*it != '\n') && (*it != '\r') && (*it != '\t'))
  54.             {
  55.                 ++all;
  56.                 QString sym = (*it).toUpper();
  57.                 QMap<QString,int>::iterator it = syms.find(sym);
  58.                 if(it == syms.end())
  59.                 {
  60.                     syms.insert(sym,1);
  61.                 }else{
  62.                     syms[sym]++;
  63.                 }
  64.             }
  65.         }
  66.     }
  67.     if(!toFile)
  68.         printToStd(syms);
  69.     else
  70.         printToFile(syms,saveFileName);
  71.     exit(EXIT_SUCCESS);
  72. }
  73.  
  74. void printToStd(QMap<QString, int> map)
  75. {
  76.     printf("Всего символов в файле %s : \033[05m%d\033[00m\n",fileName.toStdString().c_str(),(int)all);
  77.     if(all == 0)
  78.         return ;
  79.     QMap<QString,int>::iterator strIt = map.begin();
  80.     printf("-------------------------------------------\n");
  81.     for(; strIt != map.end(); ++ strIt)
  82.     {
  83.         const char* s = strIt.key().toStdString().c_str();
  84.         if(*s == ' ')
  85.             printf("| Символ: |\033[32m' '\033[00m");
  86.         else
  87.             printf("| Символ: | \033[32m%s\033[00m ",s);
  88.         printf("| 0x%04x ",*((QString)strIt.key()).toStdWString().c_str());
  89.         printf("| \033[93m%5d\033[00m | ",strIt.value());
  90.         printf("\033[33m%6f\033[00m | \n",strIt.value()/all);
  91.     }
  92.     printf("-------------------------------------------\n");
  93. }
  94.  
  95. void printToFile(QMap<QString, int> map, QString fileName)
  96. {
  97.     if(all == 0)
  98.         return ;
  99.     QFile file;
  100.     file.setFileName(fileName);
  101.     if(!file.open(QIODevice::WriteOnly))
  102.         exit(EXIT_FAILURE);
  103.     QTextStream stream(&file);
  104.     stream << QString().sprintf("Всего символов в файле %s : %d\n",fileName.toStdString().c_str(),(int)all);
  105.     QMap<QString,int>::iterator strIt = map.begin();
  106.     stream << QString("-------------------------------------------\n");
  107.     for(; strIt != map.end(); ++ strIt)
  108.     {
  109.         QString s = strIt.key();
  110.         if(s.indexOf(" ") != -1)
  111.             stream << QString("| Символ: |' '");
  112.         else
  113.             stream << QString("| Символ: | %1 ").arg(s);
  114.         stream << QString().sprintf("| 0x%04x ",*((QString)strIt.key()).toStdWString().c_str());
  115.         stream << QString().sprintf("| %5d | ",strIt.value());
  116.         stream << QString().sprintf("%6f | \n",strIt.value()/all);
  117.     }
  118.     stream << QString("-------------------------------------------\n");
  119.     stream.flush();
  120.     file.close();
  121. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement