c++ primer(第五版)學習筆記及習題答案程式碼版(第一章)
阿新 • • 發佈:2019-01-09
筆記較為零散,都是自己不熟悉的知識點。
習題答案至於一個.cc中,需要執行某一題直接修改#define NUM**, 如執行第一題為#define NUM11,題1.24定義為NUM124
chapter 1
1、std::cout << "Entertwo numbers:" << std::endl;等價於
std::cout<< "Enter two numbers:";
std::cout << std::endl;
2、名稱空間的作用是防止與庫中定義的名字發生衝突,其副作用是,當使用標準庫中的名字時必須顯示地表達出使用的是名稱空間std下的名字。
3、for (int val = 1; val <= 10; ++val)
對於for迴圈內的val變數,當結束迴圈後不再可訪問
4、檔案結束符
ctrl+d for unix/linux/ etc
ctrl+z for windows/dos
一旦測試失敗,while 終止並退出迴圈體,執行 while 之後的語句。該語
句在輸出 sum 後輸出 endl,endl 輸出換行並重新整理與 cout 相關聯的緩衝區。
最後,執行 return,通常返回零表示程式成功執行完畢。
http://www.cnblogs.com/tangdoudou/p/3385149.html
exit或return,只能使用0~255之間的值,-1的unsigned值就是255.
#include<iostream> #include "Sales_item.h" #define NUM120 // using namespace std; int answerEample(int value1,int value2); int main(){ /*1.1*/ #ifdef NUM11 cout<<"C++程式一般涉及兩類檔案:標頭檔案和原始檔。檔案字尾通常表明檔案的型別," "如標頭檔案字尾可以是.h或.hpp;原始檔的字尾可以是.cc或.cpp,具體的字尾與使用的編譯器有關"<<endl; #endif /*1.2*/ #ifdef NUM12 cout<<"window和linux系統下不會報告main函式的執行失敗,所以程式返回-1或返回1在執行效果上並沒有什麼不同," "但是,DOS命令下執行程式,然後鍵入echo %ERRORLEVEL%,則會顯示返回值-1,linux系統下,鍵入echo $? 則會返回255"<<endl; #endif /*1.3*/ #ifdef NUM13 cout<<"Hello world"<<endl; #endif /*1.4*/ #ifdef NUM14 cout<<"Enter two number: "<<endl; int v1, v2; cin >> v1 >> v2; cout<< "The product of " <<v1<<" and "<<v2<<" is "<<v1*v2<<endl; #endif /*1.5*/ #ifdef NUM15 cout<<"Enter two number: "<<endl; int v1, v2; cin >> v1 >> v2; cout<< "The product of "; cout<<v1; cout<<" and "; cout<<v2; cout<<" is "; cout<<v1*v2; cout<<endl; #endif /*1.6*/ #ifdef NUM16 cout<<"程式碼不合法" "第1,2,3行的末尾有分號,表示這段程式碼分別構成三條語句." "'<<'是二元操作符,在第2,3兩行中,第一個'<<'缺少左運算元,因此不合法。應該在2,3行開頭加上cout. "<<endl; #endif /*1.7*/ #ifdef NUM17 /* * commet pairs /(兩個**) /cannot nest * "cannot nest" is considered source code. * as is the rest of the problem */ cout<<"錯誤資訊:註釋不可巢狀。"<<endl; #endif /*1.8*/ #ifdef NUM18 cout<< "第1,2,4行合法。" "第3行<<操作符之後到第二個雙引號之前的部分被註釋掉了,導致<<操作符的右運算元不是一個完整的字串."<<endl; #endif /*1.9*/ #ifdef NUM19 int sum(0), val(50); while(val <= 100){ sum += val; ++val; } cout<<" Sum of 50 to 100 is: "<<sum<<endl; #endif /*1.10*/ #ifdef NUM110 int i(10); while(i >= 0){ cout<< i << " "; --i; } cout<<endl; #endif /*1.11*/ #ifdef NUM111 int v1,v2,low,high; cout<< "Enter two numbers: "<<endl; cin >> v1 >> v2; while(v1 <= v2){ cout<< v1 <<" "; ++v1; } #endif /*1.12*/ #ifdef NUM112 int sum = 0; for(int i = -100; i<=100; ++i) sum += i; cout<<"-100 到 100 整數相加: "<<sum<<endl; #endif /*1.13*/ #ifdef NUM113 int sum(0), val(50); for(val=50; val <= 100; ++val) sum += val; cout<< "The sum of numbers from 50 to 100: "<<sum <<endl; int i(10); for(i=10; i>=0; --i) cout<< i <<" "; cout<<endl; int v1,v2,low,high; cout<< "Enter two numbers: "<<endl; cin >> v1 >> v2; if(v1 <= v2){ low = v1; high = v2; }else{ low = v2; high = v1; } for(;v1 <= v2; ++v1) cout<<v1 <<" "; cout<<endl; #endif /*1.14*/ #ifdef NUM114 cout<<" for迴圈,迴圈控制變數的初始化和修改都放在語句頭部分,形式簡潔,特別適用於迴圈次數已知的情況," " while迴圈,迴圈控制變數的初始化一般放在while迴圈的之前,迴圈控制變數的修改一般放在迴圈體中," "形式不如for簡潔,但是比較適用於迴圈次數不易預知的情況下。"<<endl; #endif /*1.16*/ #ifdef NUM116 int sum(0), val; while(cin >> val) sum += val; cout<< "The sum is: "<<sum <<endl; #endif /*1.17*/ #ifdef NUM117 cout<<"下載我上傳資源中的第五版原始碼測試。"<<endl; #endif /*1.19*/ #ifdef NUM119 int v1,v2,low,high; cout<< "Enter two numbers: "<<endl; cin >> v1 >> v2; if(v1 <= v2){ low = v1; high = v2; }else{ low = v2; high = v1; } while(v1 <= v2){ cout<< v1 <<" "; ++v1; } #endif /*1.20*/ #ifdef NUM120 Sales_item book; cout<<"Enter transactions: "<<endl; while(cin>> book){ cout << "ISBN, number of copies sold, " " total revenue, and average price are: "<<endl; cout<< book <<endl; } #endif /*1.21*/ #ifdef NUM121 Sales_item book1, book2; cin >> book1 >> book2; if(book1.same_isbn(book2)) cout<<book1 + book2 <<endl; else cout<<"The two books have different ISBN "<<endl; #endif /*1.22*/ #ifdef NUM122 Sales_item total, book; cout<<" Enter transactions: "<<endl; if(cin>>total){ while(cin >> book){ if(total.same_isbn(book)) total = total + book; else cout<<"differ ISNB "<<endl; return -1; } cout << "ISBN, number of copies sold, " " total revenue, and average price are: "<<endl; cout<<total<<endl; }else cout<<"Input error " <<endl; return -255; #endif /*1.23 */ #ifdef NUM123 Sales_item book1,book2; int count(0); cout<<"Enter transctions: "<<endl; cin >> book1; count = 1; while(cin >> book2) if(book1.same_isbn(book2)) ++count; else { cout<<"Transaction amount of previous ISBN: "<<endl; book1 = book2; count = 1; } cout << "Transction amount of the last ISBN "<< count <<endl; #endif
參考資料:
c++ primer中文版第五版,電子工業出版社。
c++ primer第四版習題解答,人民郵電出版社。