struct
を渡す方法についてのあなたの質問の一部は十分に答えられているようですが、聞かせて私は2番目の部分に着きます:どのように私はその質問に取り組むでしょう。構造体が十分に説明されるべきであるので、我々はあまり考えず書き込むことができますどのような
:
/*
Define a struct named: Book.
Each book should have a name with exactly 3 letters (abbreviation).
Each book should also have a page count (integer),
and a price (integer).
*/
typedef struct book {
// three characters plus '\0'
char name[4];
int pagec;
int price;
} book_t;
(typedef
の必要はありますが、その部分を残さないとstruct books
と直接構造体を使用することができます)
彼らは3つの機能を望んで、1つはmain()
の3つのうちの1つとして使用できます。私は情報を得ること、メモリを割り当てることなどはここではmain()
関数の良い使用だと思います。
/*
Write a program which
reads an integer n first,
then reads the names,
page counts
and prices of n books.
*/
int main()
{
int n, i;
book_t **books;
puts("How many books?");
scanf("%d", &n);
// we need enough memory to safe "n" structs
// at first allocate memory for n pointers
books = malloc(n * sizeof(book_t *));
// at each pointer allocate enough memory for one struct books
for (i = 0; i < n; i++) {
books[i] = malloc(sizeof(book_t));
}
// red the info from stdin
for (i = 0; i < n; i++) {
printf("Name of book (3 letter abbrev.):");
scanf("%3s", books[i]->name);
printf("Number of pages:");
scanf("%d", &books[i]->pagec);
printf("Price of book (incl. taxes):");
scanf("%d", &books[i]->price);
}
// call function to sort them
sort_books(books, n);
// call a function to print the sorted list
print_books(books, n);
// we don't need the memory anymore, so free it
// at first free the individual structs
for (i = 0; i < n; i++) {
free(books[i]);
}
// then free the memory holding all of the pointers
free(books);
exit(EXIT_SUCCESS);
}
印刷用の2つの機能とソートは、すべてが非常に簡単
/*
Using that function, your program should
print the names and page counts of each book with the order of their prices.
*/
void print_books(book_t ** books, int length)
{
int i;
for (i = 0; i < length; i++) {
printf("Name %s, pages %d, price %d\n",
books[i]->name, books[i]->pagec, books[i]->price);
}
}
であることが正確な順序については何も言いません、それらを印刷
/*
Write a function which takes an array of books,
and sorts them according to their prices.
Doesn't say something about output, so sort in situ
*/
void sort_books(book_t ** books, int length)
{
// OP had bubble sort, so lets do a bubble sort. Why not?
int i, j;
book_t *tmp;
for (i = 0; i < length - 1; i++) {
for (j = 0; j < length -i - 1; j++) {
if (books[j]->price < books[j + 1]->price) {
tmp = books[j];
books[j] = books[j + 1];
books[j + 1] = tmp;
}
}
}
}
を扱う引数に似ています私は降順(最高から最低)を使うために自由を取った。あなたは昇順をしたい場合(最低から最高へ)ソートアルゴリズムで比較を変更:
void sort_books(book_t ** books, int length)
{
// OP had bubble sort, so lets do a bubble sort
int i, j;
book_t *tmp;
for (i = 0; i < length - 1; i++) {
for (j = 0; j < length - i - 1; j++) {
// if (books[j]->price < books[j + 1]->price) {
if (books[j]->price > books[j + 1]->price) {
tmp = books[j];
books[j] = books[j + 1];
books[j + 1] = tmp;
}
}
}
}
私はすべてチェックを省略していることに注意してください! malloc()
とscanf()
の戻り値を確認する必要があります。n
が整数の場合、他のすべての数字が数字などである場合はチェックしてください。
まず、「厳密に3文字の名前」と「char name [10];」は一致しません。 – molbdnilo
(a) '%c'は1文字('%3c'は3を読みます)を読み込みます、(b) 'printf()'に '%c 'を持つ型不一致があり、' char'と ' books [i] .name'は 'char *'を渡します。注意深く印刷する必要があります。ヌル終了文字列はありません。私は '%3c'も出力に関して仕事をしなければならないと思いますが、マニュアル(例えば[printf()'](http://pubs.opengroup.org/onlinepubs/9699919799/functions/printf.html)を確認してください。注意深く私は二重チェックしなければならないだろう。私はこの千年紀記法を使っていません。スキャンするとき、 '%3c'はスキップしないので、おそらく'%3c 'の前にスペースが必要です。 –
あなたのスワップアルゴリズム 'books [j] .price = tmp; 本[j] .price = books.price [j + 1]; books.price [j + 1] = tmp; '最初に' tmp'に割り当てる必要があります: 'tmp = books [j] .price;' –