我正尝试使用向量迭代器按列输出我的字符串,如下图所示。 现在,我仅显示一个使用名称和苹果酒顺序的测试用例,然后再将其复制给其他人。
下面是我当前的代码:
// values for controlling format
const int name_width = 15 ;
const int int_width = 7 ;
const int dbl_width = 12 ;
const int num_flds = 7 ;
const std::string sep = " |" ;
auto total_width = name_width*2 + int_width*2 + dbl_width*3 + sep.size() * num_flds ;
const std::string line = sep + std::string( total_width-1, '-' ) + '|' ;
cout<<"How many people ordered? ";
cin>>odrs; // Store number of orders
for(int i=1; i<=odrs; i++){
cout<<"Enter the name of person #"<<i<<":"<<endl;;
cin>>names; // Store names of users
odrNames.push_back(names); // Store names as entered as
cout<<"How many orders of cider did "<<names<<" have? ";
cout<<endl;
cin>>odrciderjuice; // Store Cider order item
sbCider = odrciderjuice * 5.5; // Calculate Cider order per price
odrCider.push_back(odrciderjuice); // Store Cider order item based on entry
SbCider.push_back(sbCider); // Store calculated Cider order per price
cout<<"How many orders of apple juice did "<<names<<" have? ";
cout<<endl;
cin>>odrapplejuice; // Store Juice order item
sbJuice = odrapplejuice * 4.5; // Calculate Juice order per price
odrApple.push_back(odrapplejuice); // Store Juice order item based on entry
SbJuice.push_back(sbJuice); // Store calculated Juice order per price
cout<<endl;
total = sbCider + sbJuice; // Calculate total between Cider and Juice
GTotal.push_back(total); // Store total values after calculation
cout<<endl;
}
for(vector<string>::iterator naming = odrNames.begin(); naming!= odrNames.end(); ++naming)
cout << sep << std::setw(name_width) << *naming<<"\v";
for(vector<int>::iterator ciderOdr = odrCider.begin(); ciderOdr!= odrCider.end(); ++ciderOdr)
cout <<*ciderOdr;
答案 0 :(得分:1)
使用迭代器确实是浏览集合的惯用方式。简单来说,您需要一个对象集合,而不是一堆无关的集合!
此外,您应该在名称上保持一致,并且仅在单个值及其集合之间使用大写是将来维护的噩梦...
C ++是一种OO语言,而OO编程是一种将复杂程序拆分为较小单元(类)的方法,这些较小单元仅对较大程序的一小部分负责。这是为了使代码更易于测试和维护。
Order
类可以包含:
由于您的格式设置并不简单,因此我将使用备用类来处理它。该类将包含:
vector<Order>
的引用ostream
的引用然后主体将仅包含用于加载订单的代码。
代码可能是:
#include <iostream>
#include <string>
#include <vector>
#include <iomanip>
using namespace std;
struct Order {
static constexpr double ciderPrice = 5.5;
static constexpr double juicePrice = 4.5;
std::string name;
int cider;
int juice;
double sbCider() const {
return cider * ciderPrice;
}
double sbJuice() const {
return juice * juicePrice;
}
double total() const {
return sbCider() + sbJuice();
}
};
class Bill {
// values for controlling format
static const int name_width = 10 ;
static const int int_width = 6 ;
static const int dbl_width = 17 ;
const std::vector<Order>& orders;
std::ostream& out;
...
public:
Bill(const std::vector<Order>& orders, std::ostream& out):orders(orders),out(out) { }
Bill& show_line() {
...
return *this;
}
Bill& show_header() {
...
return *this;
}
Bill& show_order(const Order &order) {
...
return *this
}
Bill& show_total() {
return *this
}
Bill &show_average() {
...
return *this;
}
Bill& show() {
...
show_header().show_line();
for (const Order& order: orders) {
show_order(order);
}
return show_line().show_total().show_average().show_line();
}
};
int main() {
Order order;
vector<Order> orders;
int odrs;
cout<<"How many people ordered? ";
cin>>odrs; // Store number of orders
for(int i=1; i<=odrs; i++){
cout<<"Enter the name of person #"<<i<<":"<<endl;;
cin>>order.name; // Store names of users
cout<<"How many orders of cider did "<<order.name<<" have? "<<endl;
cin>>order.cider; // Store Cider order item
cout<<"How many orders of apple juice did "<<order.name<<" have? "<<endl;
cin>>order.juice; // Store Juice order item
orders.push_back(order);
}
Bill bill(orders, std::cout);
bill.show();
return 0;
}