首页 > 解决方案 > 我想从主函数中取出一些代码

问题描述

我想将以下部分放在主要功能之外..我已经为我的一项大学评估编写了一个咖啡机代码..我已经在主要功能中编写了整个代码。但是现在我想把那个代码部分放在外面主要功能..我的意思是不同功能中的那些部分,并在主要功能中调用该功能。请帮助我。

enter code 


 int main() {
    softdrink drink[7];


strcpy(drink[0].name,"Espresso"); drink[0].itemprice=120;drink[0].country="Italy"; drink[0].quantity=20;
strcpy(drink[1].name,"Iced coffee"); drink[1].itemprice=150;drink[1].country="France"; drink[1].quantity=20;
strcpy(drink[2].name,"Long black"); drink[2].itemprice=80;drink[2].country="Australia"; drink[2].quantity=20;
strcpy(drink[3].name,"Americano"); drink[3].itemprice=100;drink[3].country="America"; drink[3].quantity=20;
strcpy(drink[4].name,"Latte"); drink[4].itemprice=200;drink[4].country="Italy"; drink[4].quantity=20;
strcpy(drink[5].name,"Irish coffee"); drink[5].itemprice=130;drink[5].country="Ireland"; drink[5].quantity=20;
strcpy(drink[6].name,"Cappuccino"); drink[6].itemprice=180;drink[6].country="Italy"; drink[6].quantity=20;

    cout << fixed;
    cout << setprecision(2);


    int coffeetype = 1;

while(coffeetype != 8){
cout<<"\n 1) "<<drink[0].name<<"\t\t"<<drink[0].itemprice<<"\t\t"<<drink[0].country<<"\t\t("<<drink[0].quantity<<") remaining";
cout<<"\n 2) "<<drink[1].name<<"\t\t"<<drink[1].itemprice<<"\t\t"<<drink[1].country<<"\t\t("<<drink[1].quantity<<") remaining";
cout<<"\n 3) "<<drink[2].name<<"\t\t"<<drink[2].itemprice<<"\t\t"<<drink[2].country<<"\t("<<drink[2].quantity<<") remaining";
cout<<"\n 4) "<<drink[3].name<<"\t\t"<<drink[3].itemprice<<"\t\t"<<drink[3].country<<"\t\t("<<drink[3].quantity<<") remaining";
cout<<"\n 5) "<<drink[4].name<<"\t\t"<<drink[4].itemprice<<"\t\t"<<drink[4].country<<"\t\t("<<drink[4].quantity<<") remaining";
cout<<"\n 6) "<<drink[5].name<<"\t"<<drink[5].itemprice<<"\t\t"<<drink[5].country<<"\t\t("<<drink[5].quantity<<") remaining";
cout<<"\n 7) "<<drink[6].name<<"\t\t"<<drink[6].itemprice<<"\t\t"<<drink[6].country<<"\t\t("<<drink[6].quantity<<") remaining";

cout<<"\n 8) Leave the drink machine \n\n";
cout<<"\n Choose one:";
cin >> coffeetype;

标签: c++

解决方案


您的代码任意复杂且不可读,您可以在声明它时初始化drink,并且您可以使用for循环打印每个条目,而不是一直编写相同的代码,例如:

#include <iostream>
#include <string>
#include <iomanip>

using namespace std;

struct softdrink {
  string name;
  int itemprice;
  string country;
  int quantity;
};

int main() {
    softdrink drink[] = {
      { "Espresso", 120, "Italy", 20 },
      { "Iced coffee", 150, "France", 20 },
      { "Long black", 80, "Australia", 20 },
      { "Americano", 100, "America", 20 },
      { "Latte", 200, "Italy", 20 },
      { "Irish coffee", 130, "Ireland", 20 },
      { "Cappuccino", 180, "Italy", 20 }
    };

    cout << fixed;
    cout << setprecision(2);

    int coffeetype = 1;

    while (coffeetype != 8) {
      int i;

      for (i = 0; i != sizeof(drink)/sizeof(drink[0]); ++i)
        cout<< "\n " << i+1 << ") "<<drink[i].name<<"\t\t"<<drink[i].itemprice<<"\t\t"<<drink[i].country<<"\t\t("<<drink[i].quantity<<") remaining";

      cout<<"\n " << i+1 << ") Leave the drink machine \n\n";
      cout<<"\n Choose one:";
      cin >> coffeetype;
    }
}

当然你也可以定义operator<<on softdrink并在main中使用它。如果您在数组中添加新元素,则8将不是正确的值,使用文字值是危险的,因为您可能会错过更新它

#include <iostream>
#include <string>
#include <iomanip>

using namespace std;

struct softdrink {
  string name;
  int itemprice;
  string country;
  int quantity;
};

ostream & operator<<( ostream & os, const softdrink & sd)
{
  os << sd.name << "\t\t" << sd.itemprice << "\t\t" << sd.country << "\t\t(" << sd.quantity << ") remaining";
  return os;
}

int main() {
    softdrink drink[] = {
      { "Espresso", 120, "Italy", 20 },
      { "Iced coffee", 150, "France", 20 },
      { "Long black", 80, "Australia", 20 },
      { "Americano", 100, "America", 20 },
      { "Latte", 200, "Italy", 20 },
      { "Irish coffee", 130, "Ireland", 20 },
      { "Cappuccino", 180, "Italy", 20 }
    };

    cout << fixed;
    cout << setprecision(2);

    int coffeetype = 1;

    while (coffeetype != sizeof(drink)/sizeof(drink[0])) {
      int i;

      for (i = 0; i != sizeof(drink)/sizeof(drink[0]); ++i)
        cout << "\n " << i+1 << ") " <<  drink[i];

      cout<<"\n " << i+1 << ") Leave the drink machine \n\n";
      cout<<"\n Choose one:";
      cin >> coffeetype;
    }
}

编译和执行:

pi@raspberrypi:/tmp $ g++ -Wall d.cc
pi@raspberrypi:/tmp $ ./a.out

 1) Espresso        120     Italy       (20) remaining
 2) Iced coffee     150     France      (20) remaining
 3) Long black      80      Australia       (20) remaining
 4) Americano       100     America     (20) remaining
 5) Latte       200     Italy       (20) remaining
 6) Irish coffee        130     Ireland     (20) remaining
 7) Cappuccino      180     Italy       (20) remaining
 8) Leave the drink machine 


 Choose one:8
pi@raspberrypi:/tmp $ 

如您所见,使用tab不是拥有常规列的正确方法,因此例如修改operator<<为 have :

ostream & operator<<( ostream & os, const softdrink & sd)
{
  os << left << setw(5) << sd.name 
     << left << setw(10) << sd.itemprice 
     << left << setw(15) << sd.country 
     << '(' << sd.quantity << ") remaining";
  return os;
}

现在结果是:

pi@raspberrypi:/tmp $ ./a.out

 1) Espresso       120       Italy          (20) remaining
 2) Iced coffee    150       France         (20) remaining
 3) Long black     80        Australia      (20) remaining
 4) Americano      100       America        (20) remaining
 5) Latte          200       Italy          (20) remaining
 6) Irish coffee   130       Ireland        (20) remaining
 7) Cappuccino     180       Italy          (20) remaining
 8) Leave the drink machine 


 Choose one:8
pi@raspberrypi:/tmp $ 

如果要operator<<为您的级别定义 is to early,您可以使用标准函数:

void output(int i, const softdrink sd[])
{
  cout << "\n " << i+1 <<  ") "
     << left << setw(15) << sd[i].name 
     << left << setw(10) << sd[i].itemprice 
     << left << setw(15) << sd[i].country 
     << '(' << sd[i].quantity << ") remaining";
}

主要是:

for (i = 0; i != sizeof(drink)/sizeof(drink[0]); ++i)
  output(i, drink);

请注意,虽然饮料永远不会被修改,但您也可以拥有它const

const softdrink drink[] = {
...

我使用int了索引i但正确的类型是size_t索引,在这种情况下还要修改输出

另外你在 C++ 中,你有一些有趣的类来管理数组,例如std::vector,它们有几个优点,你可以获得元素的数量,它们的大小可以改变(甚至这里不需要),它们有迭代器等,例如输出签名变成

void output(size_t i, const vector<softdrink> & sd)

主要

int main() {
    const vector<softdrink> drink = {
      { "Espresso", 120, "Italy", 20 },
      { "Iced coffee", 150, "France", 20 },
      { "Long black", 80, "Australia", 20 },
      { "Americano", 100, "America", 20 },
      { "Latte", 200, "Italy", 20 },
      { "Irish coffee", 130, "Ireland", 20 },
      { "Cappuccino", 180, "Italy", 20 }
    };
    ...
    size_t coffeetype = 1;

    while (coffeetype != drink.size()) {
      size_t i;

      for (i = 0; i != drink.size(); ++i)

还要注意你的whileis ado while因为你初始化了 coffeetype来测试whilefalse :

size_t coffeetype;

do {
  ...
} while (coffeetype != drink.size());

此外,如果在您阅读coffeetype时给出了一个非整数,您将永远不会再阅读并循环而不结束,当您输入时始终检查它是否成功,例如,如果您决定退出以防输入无效:

do {
  ...
  cout<<"\n Choose one:";
} while ((cin >> coffeetype) && (coffeetype != drink.size()));

ETC

如您所见,可以做很多事情来改进代码


推荐阅读