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
|
#include <iostream>
#include <iomanip>
#include <string>
#include <vector>
using namespace std;
struct FMT
{
int w, p;
FMT( int w, int p = 6 ) : w(w), p(p) {}
friend ostream & operator << ( ostream &out, const FMT &fmt ){ return out << setw( fmt.w ) << setprecision( fmt.p ) << fixed; }
};
struct Fruit
{
string name;
int number;
double unitPrice;
};
int main()
{
vector<Fruit> fruits{ { "Apple", 5, 0.35 }, { "Coconut", 7, 0.6 }, { "Watermelon", 10, 0.8 }, { "Banana", 15, 0.12 } };
int W = 0;
for ( Fruit &f: fruits ) if ( f.name.size() > W ) W = f.name.size();
for ( Fruit &f: fruits ) cout << left << FMT( W + 1 ) << f.name << right << FMT( 3 ) << f.number << FMT( 6, 2 ) << f.number * f.unitPrice << '\n';
}
|