olexander_dan

Untitled

May 6th, 2019
311
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 3.47 KB | None | 0 0
  1. // LABKA 2.5 v.2 Matrix
  2. #include <iostream>
  3. #include <fstream>
  4. #include <string>
  5. #include <ctime>
  6. #define NOMINMAX
  7. #include <Windows.h>
  8.  
  9. #include "vList.h"
  10. #include "grRepres.h"
  11.  
  12. void studentInfo();
  13. void labInfo();
  14. bool readFromFile(const std::string&, GrA&);
  15. bool writeToFile(const std::string&, GrA&);
  16. std::string getFilePath(char);
  17. std::string formatTime(std::clock_t);
  18.  
  19. int main() {
  20.     //system("mode con COLS=700");
  21.     //ShowWindow(GetConsoleWindow(), SW_MAXIMIZE);
  22.     //SendMessage(GetConsoleWindow(), WM_SYSKEYDOWN, VK_RETURN, 0x20000000);
  23.  
  24.     studentInfo();
  25.     labInfo();
  26.  
  27.     ////MATRIX////
  28.     std::cout << "\nMatrix representation";
  29.     GrMA grMat;
  30.     std::clock_t generalTimeMatrix = 0;
  31.     while (true) {
  32.         std::string inputFilePathM = getFilePath('i');
  33.         std::clock_t timeStart = std::clock();
  34.         if (readFromFile(inputFilePathM, grMat)) {
  35.             std::clock_t timeEnd = std::clock();
  36.             std::cout << "Input in matrix, CPU time used: " << formatTime(timeEnd - timeStart) << std::endl;
  37.             generalTimeMatrix += (timeEnd - timeStart);
  38.             break;
  39.         } else
  40.             continue;
  41.     }
  42.     while (true) {
  43.         std::string outputFilePathM = getFilePath('o');
  44.         std::clock_t timeStart = std::clock();
  45.         if (writeToFile(outputFilePathM, grMat)) {
  46.             std::clock_t timeEnd = std::clock();
  47.             std::cout << "Output in matrix, CPU time used: " << formatTime(timeEnd - timeStart) << std::endl;
  48.             generalTimeMatrix += (timeEnd - timeStart);
  49.             break;
  50.         } else
  51.             continue;
  52.     }
  53.     std::cout << "General matrix processing time: " << formatTime(generalTimeMatrix) << std::endl << std::endl;
  54.     system("pause");
  55.     return 0;
  56. }
  57.  
  58. void studentInfo() {
  59.     std::cout << "Laboratory work 2 - 5 Graphs Processing\n" <<
  60.                  "Group: K-14 Danilchenko Alexander" << std::endl;
  61. }
  62. void labInfo() {
  63.     std::cout << "\nThis programm reads information about graph from the input file and converts it to an adjacency matrix and adjacency list\n" <<
  64.                  "After that it outputs representation of graph with another format" << std::endl;
  65. }
  66.  
  67. std::string getFilePath(char ioVar) {
  68.     std::string filePath = "";
  69.     if (std::cin.bad()) {
  70.         std::cin.clear();
  71.         std::cin.ignore(std::numeric_limits<std::streamsize>::max(), '\n');
  72.     }
  73.     if (ioVar == 'i')
  74.         std::cout << "\nEnter the path to the input file: ";
  75.     else // ioVar == 'o'
  76.         std::cout << "\nEnter the path to the output file: ";
  77.     getline(std::cin, filePath);
  78.     if (filePath.find('\\') == std::string::npos and filePath.find('/') == std::string::npos) {
  79.         std::string rootFolder = "D:/Studying/Programming/LABS/Labka 2.5/Labka 2.5 text files/";
  80.         filePath.insert(0, rootFolder); // prepend address of the root folder
  81.         filePath.append(".txt"); // append extension of a text file
  82.     }
  83.     return filePath;
  84. }
  85.  
  86. bool readFromFile(const std::string& inputFilePath, GrA& g) {
  87.     std::ifstream inputS;
  88.     inputS.open(inputFilePath);
  89.     if (inputS.fail())
  90.         return false;
  91.     bool isDir; size_t n;
  92.     inputS >> isDir >> n;
  93.     g.setisDir(isDir);
  94.     if (!(g.create(n)))
  95.         return false;
  96.     size_t i, j;
  97.     while (inputS >> i >> j)
  98.         g.addArc(i, j);
  99.     inputS.close();
  100.     return true;
  101. }
  102.  
  103. bool writeToFile(const std::string& outputFilePath, GrA& g) {
  104.     std::ofstream outputS;
  105.     outputS.open(outputFilePath);
  106.     if (outputS.fail())
  107.         return false;
  108.  
  109.  
  110.     (g.dfs()).output(outputS);
  111. }
  112.  
  113. std::string formatTime(std::clock_t time) {
  114.     std::string resultStr = "";
  115.     size_t sec = time / 1000;
  116.     resultStr.append(std::to_string(sec) + "s. ");
  117.     size_t ms = time % 1000;
  118.     resultStr.append(std::to_string(ms) + "ms.");
  119.     return resultStr;
  120. }
Advertisement
Add Comment
Please, Sign In to add comment