Advertisement
Guest User

Untitled

a guest
Jan 18th, 2018
81
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 2.43 KB | None | 0 0
  1. #include <iostream>
  2. #include <cstdlib>
  3. #include <random>
  4. #include <fstream>
  5. #include "Complex.h"
  6.  
  7. using namespace std;
  8.  
  9. float random(){
  10.     static default_random_engine e{};
  11.     uniform_real_distribution<float> d;
  12.     return d(e);
  13. }
  14.  
  15.  
  16. int main()
  17. {
  18.  
  19. //zad.1.
  20.     int n;
  21.     cin >> n;
  22.  
  23.     vector<float> v;
  24.  
  25.     for(int i=0;i<n;i++){
  26.         float a = random();
  27.         v.push_back(a);
  28.     }
  29.  
  30.  
  31.  
  32.      cout << "ODCZYT z wektora: " <<endl;
  33.     for(int i=0;i<n;i++){
  34.         cout << v[i] << " ";
  35.     }
  36.  
  37.     cout << "ODCZYT z wektora ( nowy for): " <<endl;
  38.     for(auto x : v){
  39.         cout << x << " ";
  40.     }
  41.  
  42.  
  43.     string filename = "plik_bin.txt";
  44.     ofstream output(filename, ios::binary);
  45.  
  46.     string filename2 = "plik_txt.txt";
  47.     ofstream output2(filename2, ios::out);
  48.  
  49. //zapis w trybie bin
  50.     if(output != 0){
  51.          for(int i=0;i<n;i++){
  52.             output.write(reinterpret_cast<char*> (&v[i]), sizeof(float));
  53.          }
  54.     }
  55.     output.close();
  56.  
  57.     if(output2 != 0){
  58.          for(int i=0;i<n;i++){
  59.             output2 << v[i] << " ";
  60.          }
  61.     }
  62.     output2.close();
  63. //odczyt binarny
  64.      cout << "ODCZYT tryb binarny:" <<endl;
  65.  
  66.     ifstream input(filename, ios::binary);
  67.  
  68.     cout << "---------------------------------------------" <<endl;
  69.     //wczytywanie:
  70.     if(input != 0){
  71.          float f;
  72.          //int n = output.read(reinterpret_cast<char*> (&f), sizeof(float));
  73.          while(input.read(reinterpret_cast<char*> (&f), sizeof(float))){
  74.             cout << f << " ";
  75.             //n = output.read(reinterpret_cast<char*> (&f), sizeof(float))
  76.          }
  77.     }
  78.  
  79.     input.close();
  80.  
  81.     cout << "ODCZYT tryb tekstowy: " <<endl;
  82.  
  83. //odczyt w trybie tekstowym
  84.     ifstream input2(filename2, ios::in);
  85.     cout << "---------------------------------------------" <<endl;
  86.     if(input2 != 0){
  87.          float f;
  88.          //int n = output.read(reinterpret_cast<char*> (&f), sizeof(float));
  89.          while(input2 >> f){
  90.             cout << f << " ";
  91.             //n = output.read(reinterpret_cast<char*> (&f), sizeof(float))
  92.          }
  93.     }
  94.     input2.close();
  95.  
  96. //zad.2.
  97.     complex z1;
  98.     complex z2;
  99.  
  100.     z1.re = 21;
  101.     z1.im = 41;
  102.     z2.re = 21;
  103.     z2.im = 41;
  104.  
  105.  
  106. //    if( equals(z1, z2) ){
  107. //        cout << "z1=z2";
  108. //    }
  109. //    else{
  110. //        cout << "z1!=z2";
  111. //    }
  112.     equals(z1, z2) ? cout << "z1=z2" : cout << "z1!=z2";
  113.  
  114.  
  115.  
  116.  
  117.     return 0;
  118. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement