简体   繁体   English

如何比较结构中不同索引的元素

[英]How can I compare elements from different indexes in struct

#include<iostream>

using namespace std;

struct workspace {
    int ID;
    int price;
    int incoming_amount;
    int outgoing_amount;
    int date;
};

int main ()
{
    workspace works[5];
    string type_amount;
    int incoming_IDs[5];
    int incoming_IDs_counter = 0;
    for(int i = 0; i < 5; i++){
        cin >> works[i].ID;
        cin >> works[i].price;
        cout << "What type of amount (inc/out)?" << endl;
        cin >> type_amount;
        if(type_amount == "inc"){
            incoming_IDs[incoming_IDs_counter] = works[i].ID;
            incoming_IDs_counter++;
            works[i].outgoing_amount = 0;
            cin >> works[i].incoming_amount;
        }
        else if(type_amount == "out"){
            works[i].incoming_amount = 0;
            cin >> works[i].outgoing_amount;
        }
        cin >> works[i].date;
    }
    return 0;
}

This is my code so far, now I have to check the following: 到目前为止,这是我的代码,现在我必须检查以下内容:

  • Make sure the ID has an incoming amount added to it, before it can be an outgoing amount. 确保ID已添加入账金额,然后才可以成为出账金额。

  • Make sure the price for incoming amount if smaller than the outgoing amount. 如果收款金额小于收款金额,请确保价格。

So, I worked on the first one and I got this function: 因此,我从事第一个工作,并获得了以下功能:

bool INC_Exists(workspace &works, int &incoming_IDs_counter, int incoming_IDs[]){
    for(int i = 0; i < incoming_IDs_counter; i++){
        if(incoming_IDs[i] == works.ID){
            return true;
            break;
        }
    }
    return false;
}

It seems to work and does the job, but for the second one, I don't know how to work it out. 它似乎有效并且可以完成工作,但是对于第二个,我不知道如何解决。 How can I efficiently check if the price of the outgoing amount if smaller than the incoming amount for the same ID? 如果相同ID的收款额小于收款额,如何有效检查收款额的价格?

bool Valid_Price(workspace *works, int& incoming_IDs_counter, int incoming_IDs[], workspace &workz){
    bool valid_price;
    for(int j = 0; j < incoming_IDs_counter; j++){
        if(incoming_IDs[j] == workz.ID){
            for(int k = 0; k < 5; k++){
                if(works[k].ID == workz.ID){
                    if(works[k].price < workz.price){
                        valid_price = true;
                    }
                    else if(works[k].price > workz.price){
                        valid_price = false;
                    }
                }
            }
        }
    }
    return valid_price;
}

Tested and it seems to work.. 经过测试,它似乎可以工作。

This function 该功能

bool INC_Exists(workspace &works, int &incoming_IDs_counter, int incoming_IDs[]){
    for(int i = 0; i < incoming_IDs_counter; i++){
        if(incoming_IDs[i] == works.sifra){
            return true;
            break;
        }
    }
    return false;
}

is invalid because structure workspace has no data member sifra, 无效,因为结构工作区没有数据成员sifra,

If I have understood correctly the requirement 如果我正确理解要求

•Make sure the ID has an incoming amount added to it, before it can be an outgoing amount •确保ID已添加入金,然后才可以成为出金

you need to check whether object works is among elements of int incoming_IDs[]. 您需要检查对象是否工作在int incoming_IDs []的元素之间。 The function could look the following way 该函数可能如下所示

bool INC_Exists( const int incoming_IDs[], int incoming_IDs_counter,  const workspace &works )
{
    int i = 0;

    while ( i < incoming_IDs_counter && works.ID != incoming_IDs[ i ] ) i++; 

    return ( i != incoming_IDs_counter );
}

As for this condition 至于这种情况

•Make sure the price for incoming amount if smaller than the outgoing amount •如果小于收款金额,请确保收款金额

then it can be represented by expression 然后可以用表达式表示

works.price < works. outgoing_amount

But I don't know where this condition has to be used. 但我不知道必须在哪里使用这种条件。 From your description it is totally unclear. 根据您的描述,这还不清楚。

bool Valid_Price(workspace *works, int& incoming_IDs_counter, int incoming_IDs[], workspace &workz){
    for(int i = 0; i < 5; i++){
        for(int j = 0; j < incoming_IDs_counter; j++){
            if(incoming_IDs[j] == workz.ID){
                for(int k = 0; k < 5; k++){
                    if(works[k].price < workz.price){
                        return true;
                    }
                    else{
                        return false;
                    }
                }
            }
        }
    }
}

Try this code for the second part. 在第二部分中尝试使用此代码。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 如何比较两个结构实例? - How can I compare two struct instances? 如何在单个SET中比较struct的两个属性? - How can i compare two properties of struct in a single SET? 如何为不同的模板参数定义不同的结构成员? - How can I define different struct members for different template parameters? 如何将整个结构传递给包含所有元素的函数? - How can I pass the whole struct to a function including all elements? 如何比较std :: set的前N个元素? - How can I compare the first N elements of a std::set? c++:如何在每次打印矢量时打印矢量元素的索引? - c++: How can I print vector elements' indexes every time I print the vector? 如何使用一个 function 按结构中的不同变量排序 - How can I use one function to sort by different variables in a struct 如何在模板 function 中使用不同的结构作为模板参数? - How can I use different struct as template argument in a template function? 我如何定义一个运算符重载函数来比较来自两个不同类的两个对象? - how can i define an operator overloading function to compare two objects from two different classes? 我可以使用结构的元素指定 function 签名吗? - Can I specify a function signature with the elements of a struct?
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM