我试图写一个代码使用类的向量
I am trying to write a code using vector of classes
我要做的是使用类的向量,而不是并行向量,将所有数据存储在代码的"收据"部分。
我首先做的是使用并行向量来做到这一点(这就是在代码中编写的内容),但现在我需要使用类的向量,我不确定如何做到这一点。正确的做法是什么?
#include <iostream>
#include <string>
#include <cmath>
#include <cctype>
#include <iomanip>
#include <vector>
#include <fstream>
#include <cstdlib>
using namespace std;
vector <string> flavor;
vector <string> sizes;
vector <double> price;
void printWelcomeMessage() {
cout << "Welcome to My Frozen Yogurt!" << endl;
cout << endl;
cout<< endl;
}
char toLowerCase (char c) {
char c_lower = c;
if ( (c >= 'A') && (c <= 'Z')) c_lower = c + 32;
return c_lower;
}
void toLowerCase (string& str) {
for (int k=0; k< str.size(); k++) {
str[k] = toLowerCase (str[k]);
}
return;
}
double getYogurtSize(string& yogurtSize) {
double subtotal=0.;
const double TAX= 0.0875, SMALLSIZE= 2.19, MEDIUMSIZE = 3.49, LARGESIZE = 4.49;
cout << "What size would you like? Please enter small, medium, or large: ";
getline(cin,yogurtSize);
toLowerCase(yogurtSize);
//push_back to enter content into vector
sizes.push_back(yogurtSize);
//pop_back to delete any times the user mispells the size
if (yogurtSize != "small" && yogurtSize != "medium" && yogurtSize != "large")
sizes.pop_back();
while (yogurtSize != "small" && yogurtSize != "medium" && yogurtSize != "large") {
cout << "What size would you like? Please enter small, medium, or large: ";
getline (cin, yogurtSize);
sizes.push_back(yogurtSize);
}
if (yogurtSize== "small") {
subtotal += SMALLSIZE;
price.push_back(SMALLSIZE); }
else if (yogurtSize == "medium") {
subtotal += MEDIUMSIZE;
price.push_back(MEDIUMSIZE); }
else if (yogurtSize=="large") {
subtotal+= LARGESIZE;
price.push_back(LARGESIZE); }
return subtotal;
}
void getYogurtFlavors(string& flavor1, string& flavor2, string& flavor3) {
cout << "Enter flavor 1: ";
getline (cin, flavor1);
toLowerCase (flavor1);
//use push_back to into content into vector
flavor.push_back(flavor1);
flavor.push_back("-");
cout << "Enter flavor 2: ";
getline (cin, flavor2);
toLowerCase(flavor2);
flavor.push_back(flavor2);
flavor.push_back("-");
cout << "Enter flavor 3: ";
getline (cin, flavor3);
toLowerCase(flavor3);
flavor.push_back(flavor3);
flavor.push_back("-");
}
// function printOrder prints out the flavors of each input
void printOrder (string yogurtSize, string flavor1, string flavor2, string flavor3, int){
int orderNumber= 0;
orderNumber++;
cout<< endl;
cout << "************************" << endl;
cout << "Order: " << orderNumber << flavor1.substr(0,4) << "-" << flavor2.substr(0,4) << "-"<< flavor3.substr(0,4) << " " << yogurtSize << endl;
cout << "************************" << endl;
cout << endl;
return;
}
bool addAnotherOrderQ(){
string order;
string yes = "yes";
string no = "no";
bool moreorders= true;
cout << "Would you like another order? ";
getline (cin, order );
while (order != yes && order != no) {
cout << "Would you like another order? Please enter yes or no: ";
getline (cin, order);
cout << endl;
}
if (order == no){
moreorders= false;
cout << endl;
}
return moreorders;
}
void printTotalCosts (double& subtotal, int& orderNumber) {
const double TAX= 0.0875;
int setpre= 2;
int totalwidth=15;
int taxwidth= 9;
int subwidth =12;
int itemwidth= 6;
cout << "===================== Receipt ===========================" << endl;
cout << endl;
// for for loops to print the size, flavors, and price in the receipt screen, and in the file
for (int i=0; i< flavor.size(); i++) {
cout << flavor[i].substr(0,4)<< " ";
}
for (int i=0; i< sizes.size(); i++) {
cout << sizes[i]<< " " ;
}
for (int i=0; i < price.size(); i++) {
cout << price[i] << endl;
}
cout << "Number of items: " << setw(itemwidth) << orderNumber << endl;
cout << "Subtotal: " << "$" << fixed << setprecision (setpre) << setw (subwidth) << subtotal << endl;
cout << "Tax (8.75%): " << "$"<< setw(taxwidth) << TAX * subtotal << endl;
cout << "Total: " << "$" << setw(totalwidth) << (TAX *subtotal) + subtotal << endl;
cout << endl;
cout << "=========================================================" <<endl;
cout << endl;
}
class YogurtOrder {
public:
YogurtOrder (); //default constructor
YogurtOrder (string, string, string, string, double); //constructor with parameters: size, flavor1, flavor2, flavor3, and price
string getSize ();
string getMixedFlavor ();
double getPrice ();
private:
string size;
string flavor1, flavor2, flavor3;
double price;
};
YogurtOrder:: YogurtOrder() {}
YogurtOrder:: YogurtOrder (string s, string first, string second, string third, double p){
s= size;
first= flavor1;
second= flavor2;
third= flavor3;
p= price;
}
string YogurtOrder:: getSize () {
return size;
}
string YogurtOrder:: getMixedFlavor() {
return flavor1, flavor2, flavor3;
}
double YogurtOrder:: getPrice() {
return price;
}
int main () {
const double TAX= 0.0875;
int setpre= 2;
int totalwidth=15;
int taxwidth= 9;
int subwidth =12;
int itemwidth= 6;
//Print the welcome message
printWelcomeMessage();
// initialize the loop variables
bool more_order = true;
int orderNumber= 0;
// varible for cost
double subtotal= 0.;
//variable for suze and flavors default intitialized to ""
string yogurtSize, flavor1, flavor2, flavor3, order;
//continye to get order until the user is done
while (more_order) {
//increment order number
orderNumber++;
//update the size and subtotal
subtotal= subtotal +getYogurtSize(yogurtSize);
//update the flavors
getYogurtFlavors(flavor1, flavor2, flavor3);
// print the current order
printOrder (yogurtSize, flavor1, flavor2, flavor3, orderNumber);
//determine whther or not to order more
more_order= addAnotherOrderQ();
}
// Print out the subtotal, tax, and total
vector <YogurtOrder> flavors;
printTotalCosts(subtotal, orderNumber);
return 0;
}
您需要做的是创建一个包含必要数据成员的类,如下所示:
class FrozenYogurt
{
private:
string flavor;
string size;
double price;
public:
//constructor
public FrozenYogurt(string f,string s,double p)
{
this.flavor=f;
this.size=s;
this.price=p;
}
//Getters
public string getFlavor(){return this.flavor;}
public string getSize(){return this.size;}
public double getPrice{return this.price;}
....//All other necessary functions
}
然后创建这个类的向量:
vector<FrozenYogurt> listOfItems;
你可以制作FrozenYogurt的对象,并像这样插入到向量列表中
string flavor="somthing";
string size="somthing";
double price=123;
FrozenYogurt item(flavor,size,price);
listOfItems.push_back(item);
现在你可以这样访问条目:
listOfItems[index].getSize();
listOfItems[index].getFlavor();
listOfItems[index].getPrice();
相关文章:
- 我不明白这段代码是如何对这个pythonlist()进行排序的,也不明白如何用C++中的向量来重现它
- 代码使用向量成功运行,但使用数组显示错误
- 向量加法代码 OpenCL 返回 clCreateKernel 错误 -46
- 为什么向量不在代码块中运行?
- 使用指针向量(带代码)C++(以及对其使用的便利性的怀疑)时出现问题
- 代码超出范围.如何动态分配此向量?
- 为什么此代码无法将字符push_back到向量中<char>?
- 我可以在一行代码中将向量中与条件匹配的所有元素插入到集合中吗?
- 代码将向量存储为向量<vector>,为什么没有错误消息?
- 如何在<typename> <long> 不更改给定代码的情况下将自定义向量与 STL 向量相互转换?
- 在将其尺寸较大的向量移动到容量较小的向量之前,是否可以通过使用Reserve()来提高代码性能
- 为什么下面的代码返回向量大小为 10
- 为什么我的代码没有将每个对象添加到向量中
- 无法将代码整数值硬代码为向量
- 使用向量(STL)时,未定义的行为,请说明理性背后的以下代码输出
- 返回向量<对<字符串,字符串>>会导致 gcc 编译代码 (curlcpp) 崩溃
- 元素向量乘法 C++(代码不起作用)
- 如何编写自己的代码来替换`sTD :: min_element`以在向量中找到最小的值
- 使用 Agner 的向量类库编译多架构代码
- OpenCV查找轮廓的示例代码:向量释放问题