C Primer 第5版 1 5 1節練習

2021-08-15 19:09:48 字數 995 閱讀 3593

練習 1.20: 編寫乙個程式,讀取一組書籍銷售記錄,將每條記錄列印到標準輸出上

#include #include "sales_item.h"

int main() {

sales_item book;

std::cin>>book;

std::cout《輸入:

0-201-70636-x 5 99.0

輸出:0-201-70636-x 5 495 99

練習1.21: 編寫程式,讀取兩個isbn相同的sales_item物件,輸出它們的和。

#include #include "sales_item.h"

int main() {

sales_item item1,item2;

std::cin>>item1>>item2;

std::cout《輸入:

0-201-70636-x 5 99.0

0-201-70636-x 8 90.0

輸出:0-201-70636-x 13 1215 93.4615

練習1.22: 編寫程式,讀取多個具有相同isbn的銷售記錄,輸出所有記錄的和。

#include #include "sales_item.h"

int main() {

sales_item item1,item2,item3,item4,item5;

std::cin>>item1>>item2>>item3>>item3>>item4>>item5;

std::cout《輸入:

0-201-70636-x 5 99.0

0-201-70636-x 2 99.0

0-201-70636-x 5 90.0

0-201-70636-x 2 98.0

0-201-70636-x 8 92.0

輸出:0-201-70636-x 17 1625 95.5882

C Primer 第5版 1 4 4節練習

練習1.17 如果輸入的所有值都是相等的,本節的程式會輸出什麼?如果沒有重複值,輸出又會是怎麼樣的?include int main int currval 0,val 0 if std cin currval int cnt 1 while std cin val if val currval c...

《C Primer第5版》 課後練習答案

第一章 開始 getting started 第二章 變數和基本型別 variables and basic types 第三章 字串 向量和陣列 strings,vectors,and arrays 第四章 表示式 expressions 第五章 語句 statements 第六章 函式 func...

C Primer第5版讀書筆記(第7章 類)

第7章 類 const成員函式 7.1.4 建構函式 預設建構函式,可以使用 default進行定義。和其他函式一樣,如果 default在類的內部,則預設建構函式是內聯的 如果它在類的外部,則該成員預設情況下不是內聯的 7.2 訪問控制與封裝 class與struct的唯一區別為class的預設訪...