String find_for läuft übers ende hinaus// Anregung für Aufagenlösung



  • Hallo,
    ich hab mal wieder ne Frage zu einer Aufgabe aus den C++ Primer 5th.
    Aufgabe:

    Exercise 9.51: Write a class that has three unsigned members
    representing year, month, and day. Write a constructor that takes a string
    representing a date. Your constructor should handle a variety of date
    formats, such as January 1, 1900, 1/1/1900, Jan 1, 1900, and so on.

    Und hier meine Code:

    //timeStamp.cpp
    #include "timeStamp.h"
    
    timeStamp::timeStamp(std::string str): 
    	day(0), month(0), year(0)
    {
    	std::cout << "String in: " << str << std::endl;
    	const std::string numbers = "0123456789";
    	std::string::size_type pos(0);
    	std::array<int, 3> temp;
    	unsigned int count = 0;
    
    	while((pos = str.find_first_of(numbers, pos)) != std::string::npos && count <= 2){
    		std::string::size_type posEnd = str.find_first_not_of(numbers,pos);
    		std::cout << posEnd << std::endl;
    		temp.at(count) = atoi(str.substr(pos, posEnd-pos).c_str());
    		pos = posEnd+1;
    		++count;
    	}
    	if(temp.at(1) > 12){
    		day = temp.at(1);
    		month = temp.at(0);
    	}else{
    		day = temp.at(0);
    		month = temp.at(1);
    	}
    	year = temp.at(2);
    
    }
    
    std::ostream &timeStamp::print(std::ostream &os){
    	os << day << " / " << month << " / " << year;
    	return os;
    }
    
    //timeStamp.h
    
    #ifndef TIMESTAMP_H_
    #define TIMESTAMP_H_
    
    #include <iostream>
    #include <string>
    #include <array>
    #include <cstdlib>
    
    class timeStamp{
    	unsigned short day, month, year;
    
    public:
    	timeStamp(std::string str);
    	std::ostream &print(std::ostream &os);
    };
    
    #endif
    
    //main.cpp - timeStamp
    #include <iostream>
    #include <string>
    #include "timeStamp.h"
    
    int main(){
    	std::string in;
    
    	while(std::getline(std::cin,in)){
    		timeStamp time(in);
    		time.print(std::cout);
    	}
    
    	return 0;
    }
    

    Irgendwie steh ich grade auf den Schlauch und weiß nicht wie ich jetzt verschiedene Formate verarbeiten. Ich hab ja das man Monat und Tag vertauschen kann aber wie ist das mit Jahr und wenn der Monat aus keiner Zahl besteht?
    Meine einzige Idee das zu lösen besteht aus sehr vielen If abfragen.
    Habt ihr vielleicht ein paar Tipps wie ich das lösen könnte?
    Ich hab mir die Variable "posEnd" ausgeben lassen und diese hat einen sehr hohen Wert beim letzt lauf...ist das normal? Hab ich was falsch gemacht oder kann ich angeben bis wohin der suchen soll. Also das ich als Parameter str.cend() übergebe?

    mit freundlichen Grüßen
    Jonas


  • Mod

    Habt ihr vielleicht ein paar Tipps wie ich das lösen könnte?

    Lege dich auf N verschiedene (eindeutige!) Formate fest. Am Anfang prüfst du, welches der N Formate zutrifft, dann parst du das jeweilige (letzteres sollte ohne viele if-Abfragen möglich sein).
    Falls zwei Formate gleich anfangen, dann parst du den Anfang, und machst noch eine Fallunterscheidung.

    Das fällt mir auf Anhieb ein. Geht bestimmt cleverer.


Anmelden zum Antworten