lundi 21 janvier 2019

How should I approach programming problems and solve them in the most efficient way?

I've been learning C++ for a bit for time now and I am more or less familiar with the basics. However the problem I am facing is coding efficiency and speed. I know they can only be trained by doing problems, but whenever I do something I end up rewriting code, changing little things here and there, sometimes I even get lost in my own code.

Recently I faced a programming problem that involved iostream facility. I have an input file that I have to read the info from and an output file example. Input file has a train travel destination followed by a number of trains going to that destination. The trains have their departure times (I still have to learn how to work with time more efficiently). Here's an example of the input file:

3 (thats the amount of travel destinations in the file)
Bangor - Bath 
Train786 8(hours) 20(minutes)
Train546 7 30
...
Belfast - Birmingham
Train146 12 30
Train986 13 50
Train234 10 20
...
Bradford - Bristol
Train222 10 15
...

The output file has to have all of the train destinations followed by the earliest train ID and its time of departure, so I assume I would do some sort of sorting.

Could you please give me some tips on how to solve this problem by writing clean code? Should I handle the input file reading and output file creation in seperate functions? Should those functions only read or could they have some additional functionality? What would be the best coding practice? Would I create 3 objects(train destinations) that would hold a vector(maybe other type of a data container) of train objects and then do the sorting for each vector? Also, how would I determine which time is the earliest without too much code and what would be the best way to do it? If you could recommend C++ or just any programming books in general to strengthen this part of programming I would also really appreciate it.

I'm sorry that the question had to be so long, but I'm in a need of a pointer to the right direction to improve both my coding efficiency, code planning and cleanliness. Hopefully I wrote my question in a comprehendible way.

Aucun commentaire:

Enregistrer un commentaire