/*
 * uppgift2.cc    TDIU06 Programmering, g.k. 2008-03-27
 */
#include <iostream>
#include <fstream>
#include <iomanip>
#include <string>
using namespace std;

struct Country
{
   Country(const string& code, const int rank, Country* next = 0)
      : code_(code), rank_(rank), next_(next) {}

   string   code_;
   int      rank_;
   Country* next_;
};

int main(int argc, char* argc[])
{
   if (argc != 2)
   {
      cout << "Använd: " << argv[0] << " fil\n";
      return 1;
   }

   ifstream in(argv[1]);

   if (! in)
   {
      cout << "Kunde inte öppna filen " << argv[1] << '\n';
      return 2;
   }
   
   Country* ranking_list = 0;
   string   code;
   int      gold;
   int      silver;
   int      bronze;
   int      rank;

   while (in >> code)
   {
      in >> gold >> silver >> bronze;
      rank = 3 * gold + 2 * silver + bronze;

      if (ranking_list == 0 || rank > ranking_list->rank_)
      {
	 ranking_list = new Country(code, rank, ranking_list);
      }
      else
      {
	 Country* previous = ranking_list;
	 Country* current  = ranking_list->next_;

	 while (current != 0 && rank <= current->rank_)
	 {
	    previous = current;
	    current  = current->next_;
	 }

	 previous->next_ = new Country(code, rank, current);
      }
   }

   cout << "Rang  Land  Poäng\n";
   int rank = 0;

   for (Country* p = ranking_list; p != 0; p = p->next_)
   {
      cout << setw(3) << ++rank 
	   << setw(7) << right << p->code_
	   << setw(6) << right << p->rank_  
	   << '\n';
   }

   return 0;
}
