0

だから私は配列を持っています:

accounts[MAX]

そして、これは amount[MAX] および debitorcredit[MAX] 配列を持つ並列配列です。debitorcredit 配列は、その 'c' または 'd' 値であり、amount が $$ 金額を保持している場合に保持されます。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]) {
            ...
        }
    }
}

ただし、2つのアカウント番号を1つの配列要素にマージして、他の配列要素を削除することはできません。これ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

以下は、すべての値を比較する 2 つの配列での単純な反復です。

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 に答える