【问题标题】:Pancake glutton program. (C++)煎饼贪吃计划。 (C++)
【发布时间】:2011-08-09 07:13:57
【问题描述】:

要求: 变量、数据类型和数值运算符 基本输入/输出 逻辑(if 语句、switch 语句) 循环(for、while、do-while) 数组

编写一个程序,要求用户输入 10 个人(第 1 个人、第 2 个人、...、第 10 个人)早餐吃的煎饼数量 输入数据后,程序必须分析数据并输出哪个人早餐吃的煎饼最多。

★ 修改程序,让它也输出哪个人早餐吃的煎饼最少。

★★★★ 修改程序,使其按照10个人吃的煎饼数量的顺序输出一个列表。 IE。 第4人:吃了10个煎饼 第 3 个人:吃了 7 个煎饼 第 8 人:吃了 4 个煎饼 ... 第 5 个人:吃了 0 个煎饼

我写的当前版本:http://codepad.org/QHnt11CT

#include <iostream>
#include <string>


void bubbleSort(int arr[], int n) {
      bool swapped = true;
      int j = 0;
      int tmp;
      while (swapped) {
            swapped = false;
            j++;
            for (int i = 0; i < n - j; i++) {
                  if (arr[i] > arr[i + 1]) {
                        tmp = arr[i];
                        arr[i] = arr[i + 1];
                        arr[i + 1] = tmp;
                        swapped = true;
                  }
            }
      }
}

int main()
{
    int pancakeAmount[10];
    std::string consumers[10];

    for (int i = 0, j = 1; i < 10; i++, j++) {
        std::cout << "Please enter an amount of pancakes eaten by consumer"\
        " number " << j << "." << std::endl;
        std::cin >> pancakeAmount[i];
        std::cout << "Please enter the name of the person who ate that amount"\
        " of pancakes." << std::endl;
        getline(std::cin, consumers[i]);
    }

    std::cout << "The results from least amount eaten to the greatest amount"\
    " eaten are as follows:" << std::endl;

    bubbleSort(pancakeAmount, 10);

    for (int k = 0; k < 10; k++) {
        std::cout << pancakeAmount[k] << std::endl;
    }

    return 0;
}

这就是我目前正在解决的问题。截至目前,这个问题的前两个目标已经解决,没有任何值得注意的问题。

然而,第三个目标被证明有点困难。我很难设计/实现带有适当标签的排序列表。在第 47 行,我试图获取一个名称或标签以提供相应的金额。

我遇到了一个问题,控制台将接受我要分配的金额,但会完全忽略对 getline() 函数的调用并循环回请求另一个金额。在调用“std::cin >> pancakeAmount[i]”之前调用 getline() 函数时,我可以在第一个循环中提供输入,但后续循环会产生我在 getline() 函数运行时遇到的错误在代码中的原始位置。

我是否试图以不正确的方式使用字符串数组,或者 getline() 函数没有被正确使用?

【问题讨论】:

  • ★★★★★ 为你的“真实”问题添加新行,如果你期望答案,不要下命令,在你的问题上付出一些努力。
  • 为什么要分行输入?将“名称#”作为您的输入很容易。作为替代实现,您可以将每个“煎饼记录”存储为类或结构。然后创建这些类型的数组。当你遍历你的列表时,你可以在你的排序算法中移动整个记录(移动指针会更有效,但是对于这么小的集合大小,你可以使用完整的副本)。排序后,遍历完整实施的列表,并在最后记录最少煎饼数量。

标签: c++


【解决方案1】:

所以今天是你的幸运日,因为我通常不做作业。
您的问题是您需要忽略 cin 的返回。

所以这应该可以解决你的问题,但它没有经过测试:

struct PanCakeEater
{
    int pancakeEaten;
    std::string name;
};
bool PanCakeSort(PanCakeEater const& eater1, PanCakeEater const& eater2)
{
    return eater1.pancakeEaten < eater2.pancakeEaten;
}

int main()
{
    std::vector<PanCakeEater> eaters;
    const size_t numberOfEaters = 3;
    for(size_t i = 0; i < numberOfEaters; ++i)
    {
        PanCakeEater p;
        std::cout << "Please enter an amount of pancakes eaten by consumer number " << i << "." << std::endl;
        std::cin >> p.pancakeEaten;
        std::cin.ignore(); // ignore the \n
        std::cout << "Please enter the name of the person who ate that amount of pancakes." << std::endl;
        getline(std::cin, p.name);
        eaters.push_back(p);
    }
    std::sort(eaters.begin(), eaters.end(), PanCakeSort);
    for(auto iter = eaters.begin(); iter != eaters.end(); ++iter)
    {
        std::cout << iter->name << ": " << iter->pancakeEaten << std::endl;
    }
    return 0;
}

【讨论】:

  • -1 在知道这是作业的情况下给出解决方案
  • @Jaywalker:我看不出我在哪里给出了解决方案。我的更聪明,但基本问题由 OP 解决。 cin 上的缺失忽略是一个很常见的问题,我经常看到。
  • 我应该补充一点,这不是家庭作业。这是在 cplusplus.com 论坛上找到的练习。有一篇文章包含练习,旨在与该网站上的教程布局一起扩展。
  • @Mr.Zero:其实我不在乎是不是作业。错误是缺少std::cin.ignore(),所以我希望我的定位对你有所帮助。但无论如何,你应该仔细看看 STL,我的程序中还有一些可以改进的地方,但我会留给你。
  • 谢谢你。您确实回答了我的问题,尽管这是一个简单的更正(除了您在代码中所做的改进),但这是我不知道的,现在已经学会了。
【解决方案2】:
    #include<iostream>

    using namespace std;

    int main()

    {

     int ate[10],eater[10],hold,temp;

     cout<<"\t\tEnter How Many Pancakes Did Each Person Eat?\n\n";

     for(int h=0;h<10;h++)

     {

      eater[h]=h;

     }

     for(int q=0;q<10;q++)

     {

      cout<<"Person "<<eater[q]+1<<" Ate: ";

      cin>>ate[q];

     }

     cout<<"____________________________________________\n\n"<<"\t\tOrdered  List Of Above.\n\n";

     for(int a=0;a<10;a++)

 {

  for(int b=0;b<10;b++)

  {

   if(ate[b]<ate[b+1])

   {

    hold=ate[b];

    ate[b]=ate[b+1];

    ate[b+1]=hold;

    temp=eater[b];

    eater[b]=eater[b+1];

    eater[b+1]=temp;

   }

  }

 }

 for(int w=0;w<10;w++)

 {

  cout<<"Person "<<eater[w]+1<<" Ate "<<ate[w]<<" Pancakes\n";

 }

 return 0;

}

【讨论】:

    【解决方案3】:

    这是一个简单的版本。对于一些初学者来说一定更容易理解。

    #include <iostream>
      #include <string>
      using namespace std;
      int main() {
          int y;
          cout << "How many people do you want to enter=  \n";
          cin >> y;
          string names[10];
          int pancakes[10];
    
          for (int i = 0; i <= y-1 ; i++) {
              cout << "enter name= ";
              cin >> names[i];
              cout << "num of pancakes= ";
              cin >> pancakes[i];
          }
          int maxEaten = pancakes[0];
          int minEaten = pancakes[0];
          string maxPer = names[0];
          string minPer = names[0];
    
          for (int k = 1; k < y; k++) {
              if (pancakes[0] < pancakes[k]) {
                  maxEaten = pancakes[k];
                  maxPer = names[k];
              }
              if (pancakes[0] > pancakes[k]) {
                  minEaten = pancakes[k];
                  minPer = names[k];
              }
          }
    
    
          cout << maxPer << " ate " << maxEaten << " which is the maximum." << endl;
          cout << minPer << " ate " << minEaten << " which is the minimum." << endl;
    
          system("pause");
    }
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2018-12-18
      • 2021-09-26
      • 1970-01-01
      • 1970-01-01
      • 2021-03-24
      • 1970-01-01
      相关资源
      最近更新 更多