1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76
|
#include <iostream>
#include <string>
#include <iomanip>
#include <limits>
const unsigned TOTAL_WEEKS = 4;
int main()
{
std::string weekday_names[7] = {"Monday", "Tuesday", "Wednesday", "Thursday",
"Friday", "Saturday", "Sunday"};
std::string weekday_shortnames[7] = {"M", "T", "W", "Th", "F", "S", "Su"};
int allowance[TOTAL_WEEKS][7];
//input
for(unsigned week = 0;week < TOTAL_WEEKS; ++week) {
std::cout << "Week " << (week + 1) << std::endl;
for(unsigned day_of_week = 0; day_of_week < 7; ++day_of_week) {
std::cout << weekday_names[day_of_week] << ": ";
std::cin >> allowance[week][day_of_week];
}
}
//calculate max allowance
int weektotal[TOTAL_WEEKS] = {0};
int weekdaytotal[7] = {0};
for(unsigned i = 0; i < TOTAL_WEEKS; ++i)
for(unsigned j = 0; j < 7; ++j) {
weektotal[i] += allowance[i][j];
weekdaytotal[j] += allowance[i][j];
}
//Find optimal table columns width
int size[9];
size[0] = std::to_string(TOTAL_WEEKS).length();
for(unsigned j = 0; j < 7; ++j) {
int max = std::numeric_limits<int>::min();
for(unsigned i = 0; i < TOTAL_WEEKS; ++i)
if (max < allowance[i][j])
max = allowance[i][j];
if (max < weekdaytotal[j])
max = weekdaytotal[j];
size[j + 1] = std::to_string(max).length() + 1;
size[j + 1] = std::max(size[j + 1], 3);
}
int max = std::numeric_limits<int>::min();
for(unsigned i = 0; i < TOTAL_WEEKS; ++i)
if (max < weektotal[i])
max = weektotal[i];
size[8] = std::to_string(max).length() + 1;
size[8] = std::max(size[8], 4);
//Outputting table header:
std::cout << std::setw(size[0]) << " ";
for(unsigned i = 0; i < 7; ++i)
std::cout << std::setw(size[i + 1]) << weekday_shortnames[i];
std::cout << std::setw(size[8]) << "Ttl";
std::cout << std::endl;
//Outputting table:
for(unsigned i = 0; i < TOTAL_WEEKS; ++i){
std::cout << std::setw(size[0]) << (i + 1); //Week number
for(unsigned j = 0; j < 7; ++j) //Outputting allowance value for a week
std::cout << std::setw(size[j + 1]) << allowance[i][j];
std::cout << std::setw(size[8]) << weektotal[i]; //Outputting total
std::cout << std::endl;
}
//Footer (Weekday totals)
std::cout << std::setw(size[0]) << " ";
for(int i = 0; i < 7; ++i)
std::cout << std::setw(size[i + 1]) << weekdaytotal[i];
std::cout << std::endl;
return 0;
}
|