私はプログラミングが初めてで、これは難しいです。ユーザーの最初の入力が-999であればプログラムを終了し、account_num、last_name、およびbalanceを入力する必要はありません。しかし、ユーザーが最初の入力後に-999を入力すると、入力を終了して結果を表示します。クライアント全体の999の部分をどのように動作させて、クライアント[x] .account_numを昇順でソートするかを理解できません。私のコードは以下の通りです。データをどのように並べ替えるのですか?
#include <stdio.h>
void bubble_sort(int[], int);
struct information
{
int account_num;
char last_name[30];
float balance;
};
int main()
{
/* declare variables */
struct information client[5];
int i, x, temp;
char c;
/* Prompt user */
printf ("Enter account number, last name, and balance.\n");
printf ("Enter -999 to end input:\n\n");
/* prompt user to enter number of people */
x = 0;
while (client[x].account_num != -999)
{
int tmp;
scanf ("%i", &tmp);
/* if types -999 as their first input program ends */
if (tmp == -999)
{
break;
}
else if (tmp < 1 || tmp > 1000)
{
printf ("*** Invalid account number. Please enter 1 - 1000 or -999 to exit ***\n");
}
else
{
client[x].account_num = tmp;
x ++;
}
bubble_sort(client[x].account_num, i);
scanf("%s", client[x].last_name);
while ((c = getchar() != '\n') && c != EOF); /* clear input buffer. */
scanf("%.2f", &client[x].balance);
}
for (x = 0; x < 5; x++)
printf("%i\n", &client[x].account_num);
return 0;
}
void bubble_sort(int list[], int i)
{
int e, d, t;
for (e = 0; e < (i - 1); e++)
{
for (d = 0; d < i - e - 1; d++)
{
if (list[d] > list[d + 1])
{
/* swapping */
t = list[d];
list[d] = list[d + 1];
list[d + 1] = t;
}/* end for*/
}/*end for*/
}/* end for */
}/* end function */
ありがとう、私はこれをどうやってやっていくのかを考えてみました。私が試したたびに、 "無効な"行が表示されます。 – user5932832
しかし、どうすればループが5行後に終了するのではないでしょうか? -999が入力された場合にのみ終了するにはどうすればいいですか? – user5932832
@ user5932832 5クライアントを保持する 'client [5]'を定義したので、ループは5エントリ後に終了する必要があります。それ以外の場合は、クライアント配列の範囲外が実行されます。私はコードをもう少しパラメータ化し、MAX_CLIENTS定義を先頭に追加したので、最大値を変更するだけです。 1か所にあるクライアントの数結局、それはあなたが達成したいものに依存します - ユーザーがクライアントの数を指定したいかどうか、同じクライアントの詳細の複数の再編集を許可したいかどうかなどあなたの例は現在それを少し残しています機能性がどの方向に向かうべきか不明である。 – Guido