c++ primer读书笔记(第一章)

第一章 快速入门

一个使用IO库的程序

[cpp]  view plain copy
  1. #include <iostream>  
  2. int main()  
  3. {  
  4. std::cout<<"Enter two numbers:"<<std::endl;  
  5. int v1,v2;  
  6. std::cin>>v1>>v2;  
  7. std::cout<<"The sum of "<<v1<<" and "<<v2<<" is "<<v1+v2<<std::endl;  
  8. return 0;  
  9. }  


Std::cout等等的应用是一个亮点,不用在预处理之后加上using namespace std;d的说明,程序简单的话很好用。

除了cout,还有ceer,clog等输出用法;

另外putchar也可以哦,例如“putchar('a\n')”头文件依旧是#include<iostream>;

Endl可以刷新与设备关联的缓冲区buffer。经常刷新buffer是个好习惯!

习题1.3

打印Hello world!

[cpp]  view plain copy
  1. #include <iostream>  
  2. int main()  
  3. {  
  4. std::cout<<"Hello world !"<<std::endl;  
  5. return 0;  
  6. }  


习题1.4

求两数之积

[cpp]  view plain copy
  1. #include <iostream>  
  2. int main()  
  3. {  
  4. std::cout<<"Enter two numbers:"<<std::endl;  
  5. int v1,v2;  
  6. std::cin>>v1>>v2;  
  7. std::cout<<"The product of "<<v1<<" and "<<v2<<" is "<<v1*v2<<std::endl;  
  8. return 0;  
  9. }  


习题1.5

使用单独语句打印

[cpp]  view plain copy
  1. #include <iostream>  
  2. int main()  
  3. {  
  4. std::cout<<"Enter two numbers:"<<std::endl;  
  5. int v1,v2;  
  6. std::cin>>v1>>v2;  
  7. std::cout<<"The product of ";  
  8. std::cout<<v1;  
  9. std::cout<<" and ";  
  10. std::cout<<v2;  
  11. std::cout<<" is ";  
  12. std::cout<<v1*v2;  
  13. std::cout<<std::endl;  
  14. return 0;  
  15. }  


1.3关于注释

有两种 单行注释 // 和多行注释/**/

注释不可嵌套!!

注释是忽略一大段代码的好方法,在每一行上加一个//而不是在首尾处加上多行注释(可能有注释嵌套,造成erro

[cpp]  view plain copy
  1. #include <iostream>  
  2. int main()  
  3. {  
  4. std::cout<<"/*";  
  5. std::cout<<"*/";  
  6. std::cout<</*"*/"*/";   //小心注释  
  7.    
  8. return 0;  
  9. }  


While语句运用,从1加到n

[cpp]  view plain copy
  1. #include <iostream>  
  2. int main()  
  3. {  
  4. int sum=0,i,;  
  5. std::cout<<"Enter a postive number:"<<std::endl;  
  6. std::cin>>i;  
  7. int j=i;       
  8. while(i) {  
  9. sum+=i;  
  10. i--;  
  11. }   
  12. std::cout<<"sum of "<<1<<" to "<<j<<" is "<<sum<<std::endl ;  
  13. return 0;  
  14. }  


For语句的运用,将刚才的程序改变一下:

[cpp]  view plain copy
  1. #include <iostream>  
  2. int main()  
  3. {  
  4. int sum=0,i,;  
  5. std::cout<<"Enter a postive number:"<<std::endl;  
  6. std::cin>>i;  
  7. int j=i;       
  8. for(;i!=0;i--) {  
  9. sum+=i;  
  10. }   
  11. std::cout<<"sum of "<<1<<" to "<<j<<" is "<<sum<<std::endl ;  
  12. return 0;  
  13. }  


习题1.10

[cpp]  view plain copy
  1. #include <iostream>  
  2. int main()  
  3. {  
  4. int sum=0;     
  5. for(int val=50;val!=101;val++) {  
  6. sum+=val;  
  7. }   
  8. std::cout<<"sum of "<<50<<" to "<<500<<" is "<<sum<<std::endl ;  
  9. return 0;  
  10. }  


while改变

[cpp]  view plain copy
  1. #include <iostream>  
  2. int main()  
  3. {  
  4. int sum=0,val=50;      
  5. while(val<=100) {  
  6. sum+=val;  
  7. val++;  
  8. }  
  9. std::cout<<"sum of "<<50<<" to "<<500<<" is "<<sum<<std::endl ;  
  10. return 0;  
  11. }  


习题1.11

[cpp]  view plain copy
  1. #include <iostream>  
  2. int main()  
  3. {  
  4. int val=10;    
  5. while(val>=0) {  
  6. std::cout<<val<<std::endl;  
  7. val--;  
  8. }  
  9. return 0;  
  10. }  


for循环改编

[cpp]  view plain copy
  1. #include <iostream>  
  2. int main()  
  3. {  
  4. int val=10;    
  5. for(;val>=0;val--) {  
  6. std::cout<<val<<std::endl;  
  7. }  
  8. return 0;  
  9. }  

求某两个数的和

If语句 

[cpp]  view plain copy
  1. #include <iostream>  
  2. using namespace std;  
  3. int main()  
  4. {  
  5. int v1,v2,lower,upper,sum=0;  
  6. cout<<"Enter two numbers"<<endl;  
  7. cin>>v1>>v2;  
  8. if(v1>v2) {  
  9. upper=v1;  
  10. lower=v2;  
  11. }  
  12. else {  
  13. upper=v2;  
  14. lower=v1;  
  15. }  
  16. for(int val=lower;val<=upper;val++) {  
  17. sum+=val;  
  18. }  
  19. cout<<"The sum between "<<lower<<" and "<<upper<<" is "<<sum<<endl;  
  20. return 0;  
  21. }  


习题1.16

[cpp]  view plain copy
  1. #include <iostream>  
  2. using namespace std;  
  3. int main()  
  4. {  
  5. cout<<"enter two numbers to find the bigger one:";  
  6. int v1,max;  
  7. cin>>v1>>max;  
  8. if(v1>max) {  
  9. max=v1;  
  10. }  
  11. cout<<"the bigger one is "<<max<<endl;  
  12. return 0;  
  13. }  


习题1.17

[cpp]  view plain copy
  1. #include <iostream>  
  2. using namespace std;  
  3. int main()  
  4. {  
  5. cout<<"enter lots of numbers to find how many positive numbers in them:"<<endl<<"press ctrl+z to stop entering"<<endl;  
  6. int val,count=0;  
  7. while(cin>>val)  
  8. if(val>0) count++;  
  9. cout<<"There are "<<count<<" positive numbers in them."<<endl;  
  10. return 0;  
  11. }  


读入未知数目数字求和

[cpp]  view plain copy
  1. #include <iostream>  
  2. using namespace std;  
  3. int main()  
  4. {  
  5. cout<<"enter lots of numbers to calculate the sum of them:"<<endl<<"press ctrl+z to stop entering"<<endl;  
  6. int val,sum=0;  
  7. while(cin>>val)  
  8. if(val>0){  
  9. sum+=val;  
  10. }  
  11. cout<<"The sum of them is"<<sum<<endl;  
  12. return 0;  
  13. }  


习题1.18

[cpp]  view plain copy
  1. #include <iostream>  
  2. using namespace std;  
  3. int main()  
  4. {  
  5. cout<<"enter two numbers :"<<endl;  
  6. int v1,v2,upper,lower;  
  7. cin>>v1>>v2;  
  8. if(v1>v2) {  
  9. upper=v1;  
  10. lower=v2;  
  11. }  
  12. else {  
  13. upper=v2;  
  14. lower=v1;  
  15. }  
  16. int val;  
  17.     for(val=lower;val<=upper;val++)  
  18.     cout<<val<<endl;  
  19. return 0;  
  20. }  


习题1.19

[cpp]  view plain copy
  1. #include <iostream>  
  2. #include <iomanip>  
  3. using namespace std;  
  4. int main()  
  5. {  
  6. cout<<"enter two numbers :"<<endl;  
  7. int v1,v2,upper,lower;  
  8. cin>>v1>>v2;  
  9. if(v1>v2) {  
  10. upper=v1;  
  11. lower=v2;  
  12. }  
  13. else {  
  14. upper=v2;  
  15. lower=v1;  
  16. }  
  17. int val,amount=0;  
  18.     for(val=lower;val<=upper;val++) {  
  19.             cout<<setw(2)<<val<<" ";  
  20. amount++;  
  21. if(amount==10) {  
  22.         cout<<endl;  
  23.         amount=0;  
  24.     }  
  25.     }  
  26.       
  27. return 0;  
  28. }  


习题1.20

[cpp]  view plain copy
  1. #include <iostream>  
  2. using namespace std;  
  3. int main()  
  4. {  
  5. int v1,v2,amount,sum=0;  
  6. cout<<"Enter two numbers"<<endl;  
  7. cin>>v1>>v2;  
  8. amount=v1-v2;  
  9. if(amount<0) amount=-amount+1;  
  10. else amount+=1;   
  11. sum=(v1+v2)*(amount)/2;  
  12. cout<<"The sum between "<<v1<<" and "<<v2<<" is "<<sum<<endl;  
  13. return 0;  
  14. }  


标准库的头文件用<>;非标准库的头文件用""

[cpp]  view plain copy
  1. #include <iostream>  
  2. #include"Sales_item.h"   
  3. using namespace std;  
  4. int main()  
  5. {  
  6.    Sales_item item1,item2;  
  7.    cin>>item1>>item2;  
  8.    cout<<item1+item2<<endl;  
  9.    return 0;    
  10. }  


习题1.21

[cpp]  view plain copy
  1. #include <iostream>  
  2. #include"Sales_item.h"   
  3. using namespace std;  
  4. int main()  
  5. {  
  6.    Sales_item book;  
  7.    while(cin>>book) {  
  8.     cout<<"ISBN,numbers of copies sold,total revenur,and average price are:"  
  9.     <<endl<<book<<endl;  
  10.    }  
  11.     return 0;   
  12. }  


习题1.22

[cpp]  view plain copy
  1. #include <iostream>  
  2. #include"Sales_item.h"   
  3. using namespace std;  
  4. int main()  
  5. {  
  6.    Sales_item item1,item2,item;  
  7.    cout<<"Enter two objects:\n";  
  8.    cin>>item1>>item2;  
  9.    if(item1.same_isbn(item2))  
  10.     cout<<"ISBN,numbers of copies sold,total revenur,and average price are:"  
  11.     <<endl<<item1+item2<<endl;  
  12.     else   
  13.     cout<<"The two objects have different ISBN"<<endl;  
  14.     return 0;   
  15. }  


习题1.23

[cpp]  view plain copy
  1. #include <iostream>  
  2. #include"Sales_item.h"   
  3. using namespace std;  
  4. int main()  
  5. {  
  6.    Sales_item item,book;  
  7.    cout<<"Enter many objects:\n";  
  8.    cin>>book;  
  9.    while(cin>>item)  
  10.    if(book.same_isbn(item)) {  
  11.    book+=item;  
  12.    }     
  13.     else {  
  14.         cout<<"The objects have different ISBN"<<endl;  
  15.         return -1;  
  16.    }  
  17.     cout<<"ISBN,numbers of copies sold,total revenur,and average price are:"  
  18.     <<endl<<book<<endl;  
  19.     return 0;   
  20. }  


习题1.24

[cpp]  view plain copy
  1. #include <iostream>  
  2. #include"Sales_item.h"   
  3. using namespace std;  
  4. int main()  
  5. {  
  6.    Sales_item item1,item2;  
  7.    int amount=1;  
  8.    cout<<"Enter many objects:\n";  
  9.    cin>>item1;//读入第一笔交易,即初始交易。   
  10.    while(cin>>item2)  
  11.    if(item1.same_isbn(item2)) {  
  12.    ++amount;  
  13.    }   
  14.    //ISBN不同时首先计算之前ISBN的总和,然后再将amount赋值为1    
  15.     else {  
  16.         cout<<"transaction amount of previous ISBN:"  
  17.     <<endl<<amount<<endl;  
  18.        amount=1;  
  19.        item1=item2;          
  20.    }   
  21.     cout<<"transaction amount of the last ISBN :"<<amount<<endl;  
  22.     return 0;   
  23. }  


习题1.25

[cpp]  view plain copy
  1. #include <iostream>  
  2. #include"Sales_item.h"   
  3. using namespace std;  
  4. int main()  
  5. {  
  6.    Sales_item total,trans;  
  7.    cout<<"Enter some objects:\n(press ctrl+z to stop entering)\n";  
  8.    if(cin>>total) {  
  9.    while(cin>>trans)  
  10.    if(total.same_isbn(trans))  
  11.      total+=trans;  
  12.    else {  
  13.     cout<<"transaction of previous :"<<total<<endl;  
  14.     total=trans;  
  15.    }    
  16.    }  
  17.    else {  
  18.     cout<<"no input!  no data!"<<endl;   
  19.     return -1;  
  20.    }    
  21.     return 0;   
  22. }  
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值