Advertisement
Guest User

Blackone

a guest
Nov 15th, 2009
274
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 1.83 KB | None | 0 0
  1. // Source: thetripproblem.cpp
  2. // Author: Jeffrey Cartagena
  3. // Date: May 2, 2009
  4.  
  5. #include <iostream>
  6. #include <fstream>
  7. #include <cmath>
  8. using namespace std;
  9.  
  10. // Global variables
  11. double *allMoney;
  12.  
  13. // Prototypes
  14. void readFromFile(ifstream &inFile, int & qty);
  15. void writeToFile(ofstream & outFile, double exchagedMoney);
  16. double calculateExchangedMoney(int qty);
  17.  
  18. int main()
  19. {
  20.     ifstream inFile;
  21.     ofstream outFile;
  22.     int qty;
  23.    
  24.    
  25.     try
  26.     {
  27.         inFile.open("input.in");
  28.         outFile.open("output.out");
  29.     }
  30.    
  31.     catch(...)
  32.     {
  33.         cout << "There are problems opening the files\n";
  34.         cin.ignore();
  35.         return 1;
  36.     }
  37.    
  38.     readFromFile(inFile, qty);
  39.     while(!inFile.eof())
  40.     {
  41.         double exchangedMoney = calculateExchangedMoney(qty);
  42.         delete[] allMoney;
  43.         writeToFile(outFile, exchangedMoney);
  44.         readFromFile(inFile, qty);
  45.     }
  46.     delete[] allMoney;
  47.     cout << "Finishing the problem\n";
  48.     cin.ignore();
  49.     inFile.close();
  50.     outFile.close();   
  51.     return 0;
  52. }
  53.  
  54. double calculateExchangedMoney(int qty)
  55. {
  56.     double total = 0.0;
  57.     double average = 0.0;
  58.     int count = 0;
  59.    
  60.     for(int i=0; i < qty; i++)
  61.         average += allMoney[i];
  62.    
  63.    
  64.     average = average / qty;
  65.    
  66.     for(int j=0; j<qty; j++)
  67.         if(allMoney[j] - average > 0)
  68.         {
  69.             total += allMoney[j] - average;
  70.             if(fmod(total, .01) <= 0.009)
  71.                 total = total - (fmod(total, .01));            
  72.         }  
  73.     return total;          
  74. }
  75.  
  76. void writeToFile(ofstream & outFile, double exchangedMoney)
  77. {
  78.     if(fmod(exchangedMoney, 01) >= 0.009)
  79.         outFile << '$' << exchangedMoney << endl;
  80.     else
  81.         outFile << '$' << exchangedMoney << ".00" << endl;
  82. }
  83.  
  84. void readFromFile(ifstream & inFile, int & qty)
  85. {
  86.     try
  87.     {
  88.         inFile >> qty;
  89.         allMoney = new double[qty];
  90.    
  91.         for(int i=0; i<qty; i++)
  92.             inFile >> allMoney[i];
  93.     }
  94.     catch(...)
  95.     {
  96.         cout << "There are problems in the format of the input File\n";
  97.         exit(1);
  98.     }
  99. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement