0

所以我有一个数组:

accounts[MAX]

它是一个具有 amount[MAX] 和 debitorcredit[MAX] 数组的并行数组,如果 debitorcredit 数组是 'c' 或 'd' 值并且 amount 持有 $$ 金额,则 debitorcredit 数组成立。如何搜索accounts[3] 是否与accounts[5] 具有相同的帐号(或任何数字组合),如果它们相同,则添加金额值并组合数组?因此,如果

accounts[3] = 1500 and accounts[5] = 1500

amount[3] = 100, amount[5] = 130

debitorcredit[3] = 'c'   , debitorcredit[5] = 'd'

它会将 1500 的帐户 # 合并到 1 个数组中,金额值为 30 (130 - 100)?

4

4 回答 4

2

要测试所有帐号对以查看哪些是相等的,请使用

for (i = 0; i < MAX; i++) {
    for (j = i; j < MAX; j++) { // note: starts from i, not 0
        if(accounts[i] == accounts[j]) {
            ...
        }
    }
}

但是,您不能将两个帐号合并到一个数组元素中而只删除另一个数组元素,因为您已经定义accounts了固定大小MAX并且以这种方式分配的数组不能动态重新分配。在您的示例中,您可能希望将所有数组的第 5 个索引设置为某个虚拟值,例如 -1。然后,当您从数组中读取数据时,您可以使用此虚拟值传递所有元素。

于 2012-11-19T00:44:49.080 回答
1

我认为最简单的事情是按帐号对数组进行排序。这最好通过将数据重新排列成结构然后使用qsort.

struct account_entry {
    int account_num;
    int amount;        // assuming int
    char debit_credit;
};

然后你会有一个数组:

struct account_entry acc[MAX];
for( i = 0; i < MAX; i++ ) {
    acc[i].account_num = accounts[i];
    acc[i].amount = amount[i];
    acc[i].debit_credit = debit_or_credit[i];
}

并对其进行排序:

int compare_entry( const void* a, const void* b )
{
    const struct account_entry *aa = (const account_entry*)a;
    const struct account_entry *bb = (const account_entry*)b;
    if( aa->account_num < bb->account_num ) return -1;
    if( aa->account_num > bb->account_num ) return 1;
    return 0;  // No need to do second-tier sorting if accounts are equal.
}

qsort( acc, MAX, sizeof(struct account_entry), compare_entry );

现在您只需遍历阵列并进行整合。最容易整合到新阵列中。

struct account_entry consolidated[MAX];
int c = 0, i;
consolidated[0] = acc[0];

for( i = 1; i < MAX; i++ ) {
    if( acc[i].account_num != consolidated[c].account_num ) {
        c++;
        consolidated[c] = acc[i];
    } else {
        // Add the amounts and work out whether it's debit or credit...  Do
        // not increment c.  Dunno why you don't drop this field altogether
        // and allow negative values, but nevermind.  As such, I won't put
        // code for it in here.
    }
}
于 2012-11-19T01:01:30.847 回答
0

这是两个数组中的简单迭代,比较每个值:

int i, j;

for (i = 0; i < MAX; ++i)
{
    for (j = 0; j < MAX; ++j)
    {
         if (array_one[i] == array_two[j])
         {
              // do stuff ...
         }
     }
 }
于 2012-11-19T00:52:51.550 回答
0

您可能应该阅读结构:

typedef struct record
{
    const char* name;
    double amount;    /* fixed point, 3 decimals? */
    char deb_cred;    /* 'd' or 'c' (prefer to use `signed char` for a direct sign? */
} record_t;

所以而不是

#define MAX 15

const char* accounts [MAX] = { "John Mayor", "Paula Bean", "Gary S.", "John Mayor" }; 
double amount        [MAX] = { 100.00      , 200.00      , 300.00   , 400.00       }; 
char debitorcredit   [MAX] = { 'd'         , 'd'         , 'c'      , 'c'          }; 

简单的功能会变得相当复杂:

double get_balance_arrays(const char* name)
{
    double total = 0;
    int i;
    for(i=0; i<MAX && accounts[i]; ++i)
    {
        if(0 == strcmp(accounts[i], name))
            switch(debitorcredit[i])
            {
                case 'c': total += amount[i]; break;
                case 'd': total -= amount[i]; break;
                default:
                    puts("Invalid transaction");
                    exit(255);
            }
    }
    return total;
}

你会写

record_t transactions[MAX] =
{
    { "John Mayor", 100.00, 'd' },
    { "Paula Bean", 200.00, 'd' },
    { "Gary S."   , 300.00, 'c' },
    { "John Mayor", 400.00, 'c' },
    { 0 } /* sentinel */
};

现在简单的函数可以更方便地实现:

double get_balance_struct(const char* name)
{
    double total = 0;
    record_t* tx;
    for (tx = transactions; tx->name; ++tx)
    {
        if (0 == strcmp(tx->name, name)) /* NOTE string comparison! */
        switch(tx->deb_cred)
        {
            case 'c': total += tx->amount; break;
            case 'd': total -= tx->amount; break;
                default:
                    puts("Invalid transaction");
                    exit(255);
        }
    }
    return total;
}

测试:

int main(int argc, const char *argv[])
{
    printf("Balance: %0.02f\n", get_balance_struct("John Mayor"));
    printf("Balance: %0.02f\n", get_balance_arrays("John Mayor"));
    return 0;
}

印刷:

Balance: 300.00
Balance: 300.00
于 2012-11-19T00:54:38.200 回答