简体   繁体   中英

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.

  • 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?

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,

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

you need to check whether object works is among elements of 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.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM