Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #ifndef __PROGTEST__
- #include <cstdio>
- #include <cstdlib>
- #include <cstring>
- #include <iostream>
- #include <iomanip>
- #include <string>
- #include <fstream>
- #include <sstream>
- #include <vector>
- #include <algorithm>
- using namespace std;
- #endif /* __PROGTEST__ */
- class CStudent
- {
- public:
- CStudent ( void );
- CStudent ( string name, int x, int y );
- bool EqualName ( CStudent x );
- bool EqualName ( string x );
- bool LessThan ( CStudent x );
- string m_name;
- int m_mark1;
- int m_mark2;
- protected:
- };
- CStudent::CStudent ( string name, int mark1 = -1, int mark2 = -1 )
- {
- m_name = name;
- m_mark1 = mark1;
- m_mark2 = mark2;
- }
- CStudent::CStudent ( void )
- {
- m_name = "unnamed";
- m_mark1 = -1;
- m_mark2 = -1;
- }
- bool CStudent::EqualName ( string x )
- {
- return ( m_name == x );
- }
- bool CStudent::EqualName ( CStudent x )
- {
- return ( m_name == x.m_name );
- }
- bool CStudent::LessThan ( CStudent x )
- {
- if ( strcmp( m_name.c_str(), x.m_name.c_str() ) > 0 )
- return false;
- return true;
- }
- class CSchool
- {
- public:
- CSchool ();
- bool AddStudent ( CStudent x );
- bool AddMark ( CStudent x );
- void PrintAll ();
- void SortByName ();
- bool PrintAll ( const char * outFile );
- int m_count;
- CStudent m_Students [1000];
- protected:
- };
- void CSchool::SortByName ()
- {
- int i, j, minIdx;
- CStudent tmp;
- for ( i = 0; i < m_count - 1; i ++ )
- {
- minIdx = i;
- for ( j = i + 1; j < m_count; j ++ )
- if ( m_Students[j].LessThan ( m_Students[minIdx] ) )
- minIdx = j;
- tmp = m_Students[ minIdx ];
- m_Students[minIdx] = m_Students[i];
- m_Students[i] = tmp;
- }
- }
- void CSchool::PrintAll ()
- {
- for ( int i = 0; i < m_count; i++ )
- cout << m_Students[i].m_name << "\t" << m_Students[i].m_mark1 << "\t" << m_Students[i].m_mark2 << endl;
- }
- bool CSchool::PrintAll ( const char * outFile )
- {
- ofstream f;
- f.open ( outFile );
- if ( ! f.good() )
- return false;
- for ( int i = 0; i < m_count; i++ )
- {
- if ( ! ( f << m_Students[i].m_name << " " << m_Students[i].m_mark1 << " " << m_Students[i].m_mark2 << endl ) )
- {
- remove( outFile );
- break;
- }
- }
- f.close();
- return true;
- }
- CSchool::CSchool ( void )
- {
- m_count = 0;
- }
- bool CSchool::AddStudent ( CStudent x )
- {
- if ( m_count >= 1000 ) return ( false );
- for ( int i = 0; i < m_count; i ++ )
- if ( m_Students[i].EqualName ( x ) )
- return ( false );
- m_Students [m_count] = x;
- m_count++;
- return ( true );
- }
- bool CSchool::AddMark ( CStudent x )
- {
- if ( m_count >= 1000 ) return ( false );
- for ( int i = 0; i < m_count; i ++ )
- if ( m_Students[i].EqualName ( x ) )
- {
- m_Students[i].m_mark2 = x.m_mark1;
- return true;
- }
- m_Students [m_count] = CStudent ( x.m_name, 0, x.m_mark1 );
- m_count++;
- return ( true );
- }
- bool readStudents ( const char * file, CSchool & school )
- {
- fstream f;
- f.open ( file, fstream::in );
- if ( ! f.good() )
- return false;
- string l;
- while ( f.good() )
- {
- getline ( f, l );
- string name;
- stringstream line(l);
- if ( ! ( line >> name ) )
- break;
- if ( name[0] > '0' && name[0] < '9' )
- return false;
- int markA = 0;
- line.ignore(1000, ' ');
- if ( line.peek() == '+' )
- return false;
- if ( ! ( line >> markA ) )
- return false;
- int markB = 0;
- line >> markB;
- if ( ! school.AddStudent ( CStudent ( name, markA, markB ) ) || ! line.eof() || markA < 0 || markB < 0)
- {
- f.close();
- return false;
- }
- }
- f.close();
- return true;
- }
- bool joinFiles ( const char * inFile1, const char * inFile2, const char * outFile )
- {
- CSchool school;
- if ( ! readStudents ( inFile1, school ) )
- return false;
- CSchool part2;
- if ( ! readStudents ( inFile2, part2 ) )
- return false;
- for ( int i = 0; i < part2.m_count; i ++ )
- school.AddMark ( part2.m_Students[i] );
- school.SortByName();
- if ( ! school.PrintAll( outFile ) )
- return false;
- return true;
- }
- #ifndef __PROGTEST__
- int main ( int argc, char * argv [] )
- {
- joinFiles ( argv[1], argv[2], argv[3] );
- return 0;
- }
- #endif /* __PROGTEST__ */
Advertisement
Add Comment
Please, Sign In to add comment