Hilfe bei Programm



  • Die Ausgabe ist ganz einfach:

    std::copy( vec.begin(), vec.end(), std::ostream_iterator< std::string >( bar, "\n" ) );
    

    Wenn du einen std::vector< std::string > hast der die Daten hält und bar vom Typ std:ostream ist. 😉

    Fehler erkennst du indem du einfach prüfst ob "Nachname: ", "Vorname: " und die korrekten Namensangaben vorhanden sind.



  • Leider ist das eine Methode, die wir noch nicht im geringsten besprochen haben. Es würde auffallen, wenn ich es auf die Weise abgeben würde. Wie gesagt, so viel haben wir wirklich noch nicht gemacht. Trotzdem danke!



  • Jenni_243 schrieb:

    Leider ist das eine Methode, die wir noch nicht im geringsten besprochen haben.

    Genau! Du hast nämlich (zumindest) das falsche Subforum ausgewählt. Der Grund dafür steht in Deiner Aufgabenstellung; hat was mit strcmp(...), <string.h> und "++" zu tun.

    Zeig doch ruhig mal Deinen Code mit dem Du ja schon so weit vorangekommen bist!

    Grüsse

    *this



  • Naja ... Gast++ ... nicht ganz richtig. Dein Professor oder wer auch immer dir diese Aufgabe gestellt hat @Jenni, hat sich die vollkommene Blöße gegeben. Seine Aufgabe ist, zumindest was 3 betrifft, mit dem Kursthema wiedersprüchlich ... es sei denn ihr habt von ihm einen Header string.h bekommen ^^

    => #include <cstring> => std::strcmp ... kannst ihn ja mal fragen, was er dazu meint 😃



  • (D)Evil schrieb:

    Naja ... Gast++ ... nicht ganz richtig.

    Bestenfalls unentscheidbar. Bei dem etwas naiv anmutenden Vorgehen der OP halte ich die Hypothese "Er macht erstmal C-Grundlagen und sie hat es nicht mitgekriegt" für plausibler.
    Die frühe Behandlung vopn Bubblesort würde zu solch einem Vorgehen passen.

    Grüsse

    *this



  • Hier, mit strcmp: 😃 Alle Punkte erfüllt, oder? 😉

    [ironie]

    #include <iostream>
    #include <vector>
    #include <algorithm>
    #include <string>
    #include <fstream>
    #include <stdexcept>
    #include <boost/format.hpp>
    
    namespace str_utils
    {
    	inline std::string& trim( std::string& value, char delim = ' ' )
    	{
    		std::string::size_type pos;
    
    		pos = value.find_last_not_of( delim );
    
    		if ( pos != std::string::npos )
    		{
    			value.erase( pos+1 );
    			pos = value.find_first_not_of( delim );
    
    			if ( pos != std::string::npos )
    			{
    				value.erase( 0, pos );
    			}
    		}
    		else
    		{
    			value.clear();
    		}
    
    		return value;
    	}
    }
    
    class simple_parser
    {
    public:
    	typedef std::string::size_type size_type;
    
    	struct parse_error 
    		: public std::exception
    	{
    		parse_error( int line ) throw()
    		{
    			message = boost::str( boost::format( "Fehler in Zeile %1%" ) % line );
    		}
    
    		~parse_error() throw()
    		{}
    
    		const char* what() const throw()
    		{
    			return message.c_str();
    		}
    
    	private:
    		std::string message;
    	};
    
    	class entry
    	{
    	public:
    		entry( const std::string& _name, const std::string& _nachname )
    			: name( _name )
    			, nachname( _nachname )
    		{}
    
    		bool operator!() const
    		{
    			return ( name == "" || nachname == "" );
    		}
    
    		bool operator<( const entry& rhs ) const
    		{
    			int i = strcmp( nachname.c_str(), rhs.nachname.c_str() );
    			return ( i == 0 ? ( strcmp( name.c_str(), rhs.name.c_str() ) < 0 ) : ( i < 0 ) );
    		}
    
    		friend std::ostream& operator<<( std::ostream& out, const entry& e )
    		{
    			return ( out << e.name << ' ' << e.nachname );
    		}
    
    	private:
    		std::string name;
    		std::string nachname;
    	};
    
    	simple_parser( std::ifstream& _stream, bool auto_close = true )
    		: stream( _stream )
    		, line( 0 )
    		, close( auto_close )
    	{}
    
    	~simple_parser()
    	{
    		if ( close && stream.is_open() )
    			stream.close();
    	}
    
    	void parse( std::vector< entry >& container )
    	{
    		if ( !stream )
    			throw simple_parser::parse_error( -1 );
    
    		std::string tmp;
    		line = 0;
    
    		while ( stream.good() )
    		{
    			std::getline( stream, tmp );
    			++line;
    
    			size_type p1 = tmp.find( "Vorname: " );
    			size_type p2 = tmp.find( "Nachname: " );
    
    			if ( p1 == std::string::npos ||
    				p2 == std::string::npos )
    			{
    				throw simple_parser::parse_error( line );
    			}
    
    			p1 += 9;
    			entry new_entry( str_utils::trim( tmp.substr( p1, p2-p1 ) ), str_utils::trim( tmp.substr( p2+10 ) ) );
    
    			if ( !new_entry )
    			{
    				throw simple_parser::parse_error( line );
    			}
    
    			container.push_back( new_entry );
    		}
    	}
    
    private:
    	std::ifstream& stream;
    	int line;
    	bool close;
    };
    
    struct sorter
    	: public std::binary_function< simple_parser::entry, simple_parser::entry, bool >
    {
    	bool operator()( const simple_parser::entry& e1, const simple_parser::entry& e2 ) const
    	{
    		return ( e1 < e2 );
    	}
    };
    
    int main()
    {
    	std::ifstream foo( "ss07.in" );
    	std::ofstream bar( "ss07.out" );
    	std::vector< simple_parser::entry > vec;
    
    	if ( !foo || !bar )
    	{
    		std::cout << "Fehler beim oeffnen der Datei" << std::endl;
    		return 0;
    	}
    
    	simple_parser parser( foo );
    
    	try
    	{
    		parser.parse( vec );
    	}
    	catch ( const simple_parser::parse_error& e )
    	{
    		bar << e.what() << std::endl;
    	}
    
    	std::sort( vec.begin(), vec.end(), sorter() );
    	std::copy( vec.begin(), vec.end(), std::ostream_iterator< simple_parser::entry >( bar, "\n" ) );
    	bar.close();
    
    	std::cin.get();
    }
    

    [/ironie]



  • @David_pb: Hast du dich neuerdings mit Swordfish befreundet 😃



  • KasF schrieb:

    @David_pb: Hast du dich neuerdings mit Swordfish befreundet 😃

    lol 😉



  • KasF schrieb:

    ...
    🙂 Entsprang das gerade deiner Kreativität ?

    Jupp. Ist mir gerade (bzw. inzwischen gestern) aus der Tastatur gesprungen. 😃

    Gruß,

    Simon2.



  • David_pb schrieb:

    KasF schrieb:

    @David_pb: Hast du dich neuerdings mit Swordfish befreundet 😃

    lol 😉

    😃 😃 😃

    *duck 'n run*

    greetz, Swordfish


Anmelden zum Antworten