gpt4 book ai didi

c++ - 我正在尝试使用类 vector 编写代码

转载 作者:行者123 更新时间:2023-11-28 06:14:07 24 4
gpt4 key购买 nike

我要做的是使用类 vector 而不是并行 vector ,将所有数据存储在代码的“收据”部分。

我首先做的是使用并行 vector 来执行此操作(这就是代码中所写的内容),但现在我需要使用类 vector ,但我不确定该怎么做。执行此操作的正确方法是什么?

#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 :

vector<FrozenYogurt> listOfItems;

你可以制作 FrozenYogurt 的对象并像这样插入到这个 vector 列表中

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();

关于c++ - 我正在尝试使用类 vector 编写代码,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/30657171/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com