michael_hartman_cz

PA2 - úkol 1.2 (Spojování seznamů)

Mar 28th, 2013
346
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 4.21 KB | None | 0 0
  1. #ifndef __PROGTEST__
  2. #include <cstdio>
  3. #include <cstdlib>
  4. #include <cstring>
  5. #include <iostream>
  6. #include <iomanip>
  7. #include <string>
  8. #include <fstream>
  9. #include <sstream>
  10. #include <vector>
  11. #include <algorithm>
  12. using namespace std;
  13. #endif /* __PROGTEST__ */
  14.  
  15. class CStudent
  16.  {
  17.     public:
  18.         CStudent ( void );
  19.         CStudent ( string name, int x, int y );
  20.         bool EqualName ( CStudent x );
  21.         bool EqualName ( string x );
  22.         bool LessThan ( CStudent x );
  23.         string m_name;
  24.         int m_mark1;
  25.         int m_mark2;
  26.     protected:
  27.  
  28.  };
  29.        
  30. CStudent::CStudent ( string name, int mark1 = -1, int mark2 = -1 )
  31.  {
  32.    m_name = name;
  33.    m_mark1 = mark1;
  34.    m_mark2 = mark2;
  35.  }
  36.  
  37. CStudent::CStudent ( void )
  38.  {
  39.    m_name = "unnamed";
  40.    m_mark1 = -1;
  41.    m_mark2 = -1;
  42.  }
  43.  
  44. bool CStudent::EqualName ( string x )
  45.  {
  46.    return ( m_name == x );
  47.  }
  48.  
  49. bool CStudent::EqualName ( CStudent x )
  50.  {
  51.    return ( m_name == x.m_name );
  52.  }
  53.  
  54. bool CStudent::LessThan ( CStudent x )
  55.  {
  56.     if ( strcmp( m_name.c_str(), x.m_name.c_str() ) > 0 )
  57.         return false;
  58.     return true;
  59.  }
  60.  
  61. class CSchool
  62.  {
  63.     public:
  64.         CSchool ();
  65.         bool AddStudent ( CStudent  x );
  66.         bool AddMark ( CStudent x );
  67.         void PrintAll ();
  68.         void SortByName ();
  69.         bool PrintAll ( const char * outFile );
  70.         int m_count;
  71.         CStudent m_Students [1000];    
  72.     protected:
  73.  
  74.  };
  75.  
  76. void CSchool::SortByName ()
  77.  {
  78.    int i, j, minIdx;
  79.    CStudent tmp;
  80.    for ( i = 0; i < m_count - 1; i ++ )
  81.     {
  82.       minIdx = i;
  83.       for ( j = i + 1; j < m_count; j ++ )
  84.         if ( m_Students[j].LessThan ( m_Students[minIdx] ) )
  85.             minIdx = j;
  86.  
  87.       tmp = m_Students[ minIdx ];
  88.       m_Students[minIdx] = m_Students[i];
  89.       m_Students[i] = tmp;
  90.     }
  91.  }
  92.  
  93. void CSchool::PrintAll ()
  94.  {
  95.     for ( int i = 0; i < m_count; i++ )
  96.         cout << m_Students[i].m_name << "\t" << m_Students[i].m_mark1 << "\t" << m_Students[i].m_mark2 << endl;
  97.  }
  98.  
  99. bool CSchool::PrintAll ( const char * outFile )
  100.  {
  101.     ofstream f;
  102.     f.open ( outFile );
  103.     if ( ! f.good() )
  104.      return false;
  105.    
  106.     for ( int i = 0; i < m_count; i++ )
  107.      {
  108.         if ( ! ( f << m_Students[i].m_name << " " << m_Students[i].m_mark1 << " " << m_Students[i].m_mark2 << endl ) )
  109.          {
  110.             remove( outFile );
  111.             break;
  112.          }
  113.      }
  114.     f.close();
  115.     return true;
  116.  }
  117.  
  118. CSchool::CSchool ( void )
  119.  {
  120.     m_count = 0;
  121.  }
  122.  
  123. bool CSchool::AddStudent ( CStudent  x )
  124.  {  
  125.     if ( m_count >= 1000 ) return ( false );
  126.     for ( int i = 0; i < m_count; i ++ )
  127.         if ( m_Students[i].EqualName ( x ) )
  128.             return ( false );
  129.     m_Students [m_count] = x;
  130.     m_count++;
  131.     return ( true );
  132.  }
  133.  
  134. bool CSchool::AddMark ( CStudent x )
  135.  {  
  136.     if ( m_count >= 1000 ) return ( false );
  137.     for ( int i = 0; i < m_count; i ++ )
  138.         if ( m_Students[i].EqualName ( x ) )
  139.          {
  140.             m_Students[i].m_mark2 = x.m_mark1;
  141.             return true;
  142.          }
  143.            
  144.     m_Students [m_count] = CStudent ( x.m_name, 0, x.m_mark1 );
  145.     m_count++;
  146.     return ( true );
  147.  }
  148.  
  149. bool readStudents ( const char * file, CSchool & school )
  150.  {
  151.     fstream f;
  152.     f.open ( file, fstream::in );
  153.     if ( ! f.good() )
  154.         return false;
  155.     string l;
  156.     while ( f.good() )
  157.      {
  158.         getline ( f, l );
  159.         string name;
  160.         stringstream line(l);
  161.         if ( ! ( line >> name ) )
  162.             break;
  163.         if ( name[0] > '0' && name[0] < '9' )
  164.             return false;
  165.         int markA = 0;
  166.         line.ignore(1000, ' ');
  167.         if ( line.peek() == '+' )
  168.             return false;
  169.         if ( ! ( line >> markA ) )
  170.             return false;
  171.         int markB = 0;
  172.         line >> markB;
  173.         if ( ! school.AddStudent ( CStudent ( name, markA, markB ) ) || ! line.eof() || markA < 0 || markB < 0)
  174.          {
  175.             f.close();
  176.             return false;
  177.          }
  178.      }
  179.     f.close();
  180.     return true;
  181.  }
  182.  
  183. bool joinFiles  ( const char * inFile1, const char * inFile2, const char * outFile )
  184.  {
  185.     CSchool school;
  186.     if ( ! readStudents ( inFile1, school ) )
  187.         return false;  
  188.    
  189.     CSchool part2;
  190.     if ( ! readStudents ( inFile2, part2 ) )
  191.         return false;
  192.    
  193.     for ( int i = 0; i < part2.m_count; i ++ )
  194.         school.AddMark ( part2.m_Students[i] );
  195.     school.SortByName();
  196.     if ( ! school.PrintAll( outFile ) )
  197.         return false;
  198.        
  199.     return true;
  200.  }
  201.  
  202. #ifndef __PROGTEST__
  203. int main ( int argc, char * argv [] )
  204.  {
  205.    joinFiles ( argv[1], argv[2], argv[3] );
  206.    return 0;
  207.  }
  208. #endif /* __PROGTEST__ */
Advertisement
Add Comment
Please, Sign In to add comment