olexander_dan

Untitled

Apr 9th, 2019
420
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 2.18 KB | None | 0 0
  1. //LABKA 2-4 v.1
  2. #include <iostream>
  3. #include <fstream>
  4. #include <string>
  5. #include "classes.h"
  6.  
  7. void putAboutStudent();
  8.  
  9. std::istream& setIStream(std::ifstream& f);
  10. std::ostream& setOStream(std::ofstream& f);
  11.  
  12. int main() {
  13.     putAboutStudent();
  14.     Polynom operandA, operandB;
  15.    
  16.     // INPUT
  17.     try {
  18.         std::ifstream f;
  19.         std::istream& s_inA = setIStream(f);
  20.         s_inA >> operandA;
  21.         std::istream& s_inB = setIStream(f);
  22.         s_inB >> operandA;
  23.     } catch (std::exception& ex) {
  24.         std::cerr << "\n\t" << ex.what() << std::endl;
  25.         system("pause");
  26.         return -1;
  27.     }
  28.    
  29.     Polynom resultPlus = operandA + operandB;
  30.     Polynom resultMinus = operandA - operandB;
  31.     operandA += operandB;
  32.    
  33.     // OUTPUT
  34.     std::ofstream g;
  35.     std::ostream& s_outP = setOStream(g);
  36.     s_outP << resultPlus;
  37.     std::ostream& s_outM = setOStream(g);
  38.     s_outM << resultMinus;
  39.     std::ostream& s_outPE = setOStream(g);
  40.     s_outPE << operandA;
  41.    
  42.     // CLEANING UP MEMORY
  43.     operandA.~Polynom();
  44.     operandB.~Polynom();
  45.     resultPlus.~Polynom();
  46.     resultMinus.~Polynom();
  47.     system("pause");
  48.     return 0;
  49. }
  50.  
  51. void putAboutStudent() {
  52.     std::cout << "Laboratory work 2 - 4 Linked Lists" << std::endl;
  53.     std::cout << "Group: K-14 Danilchenko Alexander" << std::endl;
  54.     std::cout << "VAR: 70" << std::endl;
  55. }
  56.  
  57. std::istream& setIStream(std::ifstream& f) {
  58.     //int i;
  59.     std::cout << "... Enter 1(kbd) or 2(file): ";
  60.     //std::cin >> i;
  61.  
  62.     int z = 0;
  63.     std::cin >> z;
  64.     if (z == 1)
  65.         return std::cin;
  66.     else
  67.         while (true) {
  68.             std::cout << "... Enter filepath: ";
  69.             std::string fN;
  70.             std::cin.get(); // pass Enter from previous input
  71.             getline(std::cin, fN);
  72.             if (fN.length() == 0)
  73.                 fN = "in.txt";
  74.             f.open(fN);
  75.             if (f.fail())
  76.                 continue;
  77.             return (std::istream&)f;
  78.         }
  79. }
  80.  
  81. std::ostream& setOStream(std::ofstream& f) {
  82.     int i;
  83.     std::cout << "... Enter for out: 1(screen) or 2(file): ";
  84.     std::cin >> i;
  85.     if (i == 1)
  86.         return std::cout;
  87.     else
  88.         while (true) {
  89.             std::cout << "... Enter filepath: ";
  90.             std::string fN;
  91.             std::cin.get(); // pass Enter from previous input
  92.             getline(std::cin, fN);
  93.             if (fN.length() == 0)
  94.                 fN = "out.txt";
  95.             f.open(fN);
  96.             if (f.fail())
  97.                 continue;
  98.             return (std::ostream&)f;
  99.         }
  100. }
Advertisement
Add Comment
Please, Sign In to add comment