levimoosky

prog222

Mar 20th, 2015
248
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 3.01 KB | None | 0 0
  1. #include<iostream>
  2. #include<vector>
  3. #include<fstream>
  4.  
  5. using namespace std;
  6.  
  7. void beolvasas_g(vector<vector<int> > &mx);
  8. bool beolvasas_f(vector<vector<int> > &mx);
  9. bool mindenSorTsz(const vector<vector<int> > &mx);
  10. bool vanTSz(const vector<vector<int> > &mx,size_t i);
  11. bool TSz(int n);
  12. void kiiras(const vector<vector<int> > &mx);
  13.  
  14. //1. feladat - irjuk meg a teteleket
  15. //2. feladat - irjuk meg a fajlbol bekerest!
  16. //(elore ismert meret, hiba eseten kilepes)
  17.  
  18. int main()
  19. {
  20.     vector<vector<int> > mx;
  21.     //beolvasas_g(mx);
  22.     if(beolvasas_f(mx))
  23.     {
  24.         kiiras(mx);
  25.         if(mindenSorTsz(mx))
  26.             cout << "Van." << endl;
  27.         else
  28.             cout << "Nincs." << endl;
  29.     //  ...tetel hivasa...
  30.     }
  31.     else
  32.         cout<<"Hibas file"<<endl;
  33.  
  34.  
  35.  
  36.     return 0;
  37. }
  38.  
  39. void kiiras(const vector<vector<int> > &mx)
  40. {
  41.     for(size_t i=0;i<mx.size();++i)
  42.     {
  43.         for(size_t j=0;j<mx[i].size();++j)
  44.         {
  45.             cout<<mx[i][j]<<" ";
  46.         }
  47.         cout<<endl;
  48.     }
  49. }
  50.  
  51. void beolvasas_g(vector<vector<int> > &mx)
  52. {
  53.     for(size_t i=0;i<mx.size();++i)
  54.         mx[i].resize(0);
  55.     mx.resize(0);
  56.  
  57.     int n;
  58.     bool hiba;
  59.     cout<<"Hanyszor hanyas?"<<endl;
  60.     do
  61.     {
  62.         cin>>n;
  63.         hiba = cin.fail() || n<0;
  64.         if(hiba)
  65.         {
  66.             cin.clear();
  67.             cin.ignore(1000,'\n');
  68.             cout<<"Nem jo. Ujra!"<<endl;
  69.         }
  70.     }
  71.     while(hiba);
  72.  
  73.     mx.resize(n);
  74.  
  75.     for(int i=0;i<n;++i)
  76.     {
  77.         mx[i].resize(n);
  78.         for(int j=0;j<n;++j)
  79.         {
  80.             do
  81.             {
  82.                 cin>>mx[i][j];
  83.                 hiba = cin.fail() || mx[i][j]<0;
  84.                 if(hiba)
  85.                 {
  86.                     cin.clear();
  87.                     cin.ignore(1000,'\n');
  88.                     cout<<"Nem jo. Ujra!"<<endl;
  89.                 }
  90.             }
  91.             while(hiba);
  92.         }
  93.     }
  94. }
  95.  
  96. bool beolvasas_f(vector<vector<int> > &mx)
  97. {
  98.     for(size_t i=0;i<mx.size();++i)
  99.         mx[i].resize(0);
  100.     mx.resize(0);
  101.  
  102.     cout<<"Irj be egy fajl nevet"<<endl;
  103.     string s;
  104.     cin >> s;
  105.     ifstream ifs(s.c_str());
  106.     if(ifs.fail())
  107.         return false;
  108.  
  109.     int n;
  110.     ifs>>n;
  111.     if(ifs.fail() || n < 0)
  112.     {
  113.         ifs.clear();
  114.         ifs.ignore(1000,'\n');
  115.         ifs.close();
  116.         return false;
  117.     }
  118.  
  119.     mx.resize(n);
  120.  
  121.     for(int i=0;i<n;++i)
  122.     {
  123.         mx[i].resize(n);
  124.         for(int j=0;j<n;++j)
  125.         {
  126.             ifs>>mx[i][j];
  127.             if(ifs.fail() || mx[i][j] < 0)
  128.             {
  129.                 ifs.clear();
  130.                 ifs.ignore(1000,'\n');
  131.                 ifs.close();
  132.                 return false;
  133.             }
  134.         }
  135.     }
  136.  
  137.     string temp;
  138.     ifs >> temp;
  139.     if(!ifs.fail())
  140.     {
  141.         ifs.close();
  142.         return false;
  143.     }
  144.     ifs.close();
  145.     return true;
  146. }
  147.  
  148. bool mindenSorTsz(const vector<vector<int> > &mx)
  149. {
  150.     bool l = true;
  151.     size_t i = 0;
  152.  
  153.     while(l && i < mx.size())
  154.     {
  155.         l = vanTSz(mx,i);
  156.         ++i;
  157.     }
  158.  
  159.     return l;
  160. }
  161.  
  162. bool vanTSz(const vector<vector<int> > &mx,size_t i)
  163. {
  164.     bool l = false;
  165.     for (size_t j = 0;!l && j < mx[i].size();++j)
  166.         l = TSz(mx[i][j]);
  167.     return l;
  168. }
  169.  
  170. bool TSz(int n)
  171. {
  172.     if (n == 0)
  173.         return false;
  174.  
  175.     int s = 0;
  176.     for(int i=1; i < n; ++i)
  177.         if(n % i == 0)
  178.             s += i;
  179.     return n == s;
  180. }
Advertisement
Add Comment
Please, Sign In to add comment