本文主要是介绍C语言入门级教程九,希望对大家解决编程问题提供一定的参考价值,需要的开发者们随着小编来一起学习吧!
C语言入门级教程 icesongqiang
2016.12.12
总结
scanf("%s,%s",a,b)
中间只有‘,’是达不到效果的,scanf
是连同‘,’一起读入a的;- 结构体变量要注意到底需要的是哪一个元素;
简单的图书结构体
/*
* 1.为图书建立相应的结构体
* 2.利用结构体数组实现多本图书的存取
*/#define _CRT_SECURE_NO_WARNINGS
#include <stdio.h>
#include <string.h>
#include <stdlib.h>
#include <malloc.h>typedef char B_NAME[20];
typedef char B_ISBN[20];
typedef char B_AUTHOR[20];
typedef char B_PUBLISH[20];
typedef double B_PRICE;typedef struct BOOK{B_ISBN bisbn;B_NAME bname; B_AUTHOR bauthor;B_PUBLISH bpublish;B_PRICE bprice;
}BOOK;BOOK * setBooks(int n)
{printf("Input %d books (ISBN NAME AUTHOR PUBLISH)\n",n);// 978-7-208-07774-4BOOK* books = (BOOK *)malloc(n*sizeof(BOOK));for (int i = 0; i < n; ++i){scanf("%s%s%s%s", (books + i)->bisbn, (books + i)->bname, (books + i)->bauthor, (books + i)->bpublish); do{printf("PRICE, and make sure that the price is positive.\n");scanf_s("%lf", &((books + i)->bprice));} while ((books + i)->bprice<0);if (i<n-1) printf("next..\n");}return books;
}double averagePrice(BOOK* books, int n)
{if (n < 1) return 0; // 书的数目至少大于1本double total = books->bprice;for (int i = 1; i < n; ++i){total += (books + i)->bprice;}return total / n;
}void freeBooks(BOOK* books)
{free(books);
}int main()
{int n = 2;BOOK * books = setBooks(n);printf("the average price of the %d books is %lf.\n", n, averagePrice(books, n));free(books); system("pause");return 0;
}
添加日期和read(),print()函数
/*
* 1.为图书建立相应的结构体,添加日期和read(),print()函数
* 2.利用结构体数组实现多本图书的存取
* @author: icesongqiang
*/#define _CRT_SECURE_NO_WARNINGS
#include <stdio.h>
#include <string.h>
#include <stdlib.h>
#include <malloc.h>typedef char B_NAME[20];
typedef char B_ISBN[20];
typedef char B_AUTHOR[20];
typedef char B_PUBLISH[20];
typedef double B_PRICE;typedef struct{int year;int month;int day;
}B_DATE;typedef struct BOOK{B_ISBN bisbn;B_NAME bname;B_AUTHOR bauthor;B_PUBLISH bpublish;B_PRICE bprice;B_DATE bdate;void read(){printf("\nISBN NAME AUTHOR PUBLISH\n");scanf("%s%s%s%s", bisbn, bname, bauthor, bpublish);do{printf("PRICE, and make sure that the price is positive.\n");scanf_s("%lf", &bprice);} while (bprice<0);printf("input Date.(year month day)\n");scanf("%d %d %d",&bdate.year,&bdate.month,&bdate.day); // 此处还应注意日期的正确性检查,如Febr-29-year.}void print(){printf("\nISBN:%s\tNAME:%s\tAUTHOR: %s\tPUBLISH:%s\n", bisbn, bname, bauthor, bpublish);printf("PRICE:%lf\n", bprice);printf("Date: %d-%d-%d\n", bdate.year, bdate.month, bdate.day); // 此处还应注意日期的正确性检查,如Febr-29-year.}
}BOOK;BOOK * setBooks(int n)
{// 978-7-208-07774-4BOOK* books = (BOOK *)malloc(n*sizeof(BOOK));printf("Input %d books",n);for (int i = 0; i < n; ++i){(books + i)->read();if (i<n - 1) printf("________________________________");}return books;
}void printBooks(BOOK *books, int n)
{for (int i = 0; i < n; ++i){(books + i)->print();if (i<n - 1) printf("________________________________");}
}double averagePrice(BOOK* books, int n)
{if (n < 1) return 0; // 书的数目至少大于1本double total = books->bprice;for (int i = 1; i < n; ++i){total += (books + i)->bprice;}return total / n;
}void freeBooks(BOOK* books)
{free(books);
}int main()
{int n = 2;BOOK * books = setBooks(n);printBooks(books,n);printf("the average price of the %d books is %lf.\n", n, averagePrice(books, n));free(books);system("pause");return 0;
}
这篇关于C语言入门级教程九的文章就介绍到这儿,希望我们推荐的文章对编程师们有所帮助!