C++初学者声明涉及结构数组的函数

标签 c++ arrays function struct

我有几行代码想定义为一个函数,因为我打算多次使用它。部分代码如下:

// loop within loop used to reorder with highest price at the top
for(i=0;i<x;i++){
    for(t=i;t<x;t++){
        if(f[i].price < f[t].price) {
            temp = f[i].price;
            f[i].price = f[t].price;
            f[t].price = temp;
        }

    }
}

我希望每次调用该函数时都能为 x 和 f 输入新值。我在下面包含了我的所有代码。如果我不清楚我的目标,请随时询问。对于我不熟悉的不正确的术语,我提前道歉。谢谢

#include <iostream>
using namespace std;

struct List
    {
        char name[10];
        int price;
        };

int main()
{
    //x represents number of structures within array!
     int x; 

    cout << "How many items would you like to list under the fruit menu?\n";
    cin >> x;


    //array of structures fruit
    struct List f[x];

    int i;

    //input values into structure
    for (i = 0; i < x; i++) {
      cout << "\nEnter fruit name, and price.\n";
      cin >> f[i].name;
      cin >> f[i].price;
    };

    //variables for reordering
     int temp;
     int t;


    // loop within loop used to reorder with highest price at the top
    for(i=0;i<x;i++){
        for(t=i;t<x;t++){
            if(f[i].price < f[t].price) {
                temp = f[i].price;
                f[i].price = f[t].price;
                f[t].price = temp;
            }

        }
    }


    //Output of menus
    //fruit Menu
    cout << "\n\nFruit Menu";
     for (i = 0; i < x; i++) {
        cout << "\n" << f[i].name << " $" << f[i]. price;
        };


    return 0;
}

最佳答案

我想这是一个作业,上面写着“实现你自己的排序函数来对水果数组进行排序”,所以我采用给定的数据结构“水果数组”。当然,您可以将其更改为 vector<struct Fruit>也一样,但那是另一个话题。

也许以下片段可以帮助您完成代码。它包含用于输入、排序和打印数组的函数,以及一些如何处理参数和调用的示例。您必须完成代码。

玩得开心!

#include <iostream>
using namespace std;

struct Fruit
{
    char name[10];
    int price;
};

// enter up to nrOfFruis; return number of fruits actually entered
int enterFruits(struct Fruit *fruits, int maxNrOfFruits) {
    int entered = 0;
    while (entered < maxNrOfFruits) {

        cin >> fruits[entered].name;

        entered++;
    }
    return entered;
}

void sortFruits(struct Fruit* fruits, int nrOfFruits) {

    // your sort code goes here

    // example for swaping two elements:
    Fruit temp = fruits[0];
    fruits[0] = fruits[1];
    fruits[1] = temp;
}

void printFruits(struct Fruit *fruits, int nrOfFruits) {

    cout << "\n\nFruit Menu";
    for (int i = 0; i < nrOfFruits; i++) {
        cout << "\n" << fruits[i].name << " $" << fruits[i]. price;
    };
}



int main()
{
    // Your task: put a proper loop and exit condition arround the following lines...

    int x;
    cout << "How many items would you like to list under the fruit menu?\n";
    cin >> x;

    struct Fruit fruits[x];
    int entered = enterFruits(fruits, x);
    sortFruits(fruits, entered);
    printFruits(fruits, entered);

    return 0;
}

关于C++初学者声明涉及结构数组的函数,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/42198502/

相关文章:

c++ - 为什么构造 std::string(0) 不会发出编译器警告?

c++ - 构造函数采用引用参数的显式修饰符

c++ - 寻找一种更好的方法来初始化 Eigen3 矩阵

c++ - 在访问 Eigen::VectorXd 时使用零作为第二个索引是否安全?

java - 编写一个使用 float 组和 boolean 值作为参数的 boolean 方法

javascript - Js函数返回undefined

javascript - 无法在 node.js 中打印数组

c++ - Arduino/C++ 中的函数/方法定义错误。还是语法错误?

javascript - Controller 中功能的 Angular 更多功能

php - 如果...否则发出 : removing the matched variable