第一章 快速入门
一个使用IO库的程序
- #include <iostream>
- int main()
- {
- std::cout<<"Enter two numbers:"<<std::endl;
- int v1,v2;
- std::cin>>v1>>v2;
- std::cout<<"The sum of "<<v1<<" and "<<v2<<" is "<<v1+v2<<std::endl;
- return 0;
- }
Std::cout等等的应用是一个亮点,不用在预处理之后加上using namespace std;d的说明,程序简单的话很好用。
除了cout,还有ceer,clog等输出用法;
另外putchar也可以哦,例如“putchar('a\n')”头文件依旧是#include<iostream>;
Endl可以刷新与设备关联的缓冲区buffer。经常刷新buffer是个好习惯!
习题1.3
打印Hello world!
- #include <iostream>
- int main()
- {
- std::cout<<"Hello world !"<<std::endl;
- return 0;
- }
习题1.4:
求两数之积
- #include <iostream>
- int main()
- {
- std::cout<<"Enter two numbers:"<<std::endl;
- int v1,v2;
- std::cin>>v1>>v2;
- std::cout<<"The product of "<<v1<<" and "<<v2<<" is "<<v1*v2<<std::endl;
- return 0;
- }
习题1.5
使用单独语句打印
- #include <iostream>
- int main()
- {
- std::cout<<"Enter two numbers:"<<std::endl;
- int v1,v2;
- std::cin>>v1>>v2;
- std::cout<<"The product of ";
- std::cout<<v1;
- std::cout<<" and ";
- std::cout<<v2;
- std::cout<<" is ";
- std::cout<<v1*v2;
- std::cout<<std::endl;
- return 0;
- }
1.3关于注释
有两种 单行注释 // 和多行注释/**/
注释不可嵌套!!
注释是忽略一大段代码的好方法,在每一行上加一个//而不是在首尾处加上多行注释(可能有注释嵌套,造成erro)
- #include <iostream>
- int main()
- {
- std::cout<<"/*";
- std::cout<<"*/";
- std::cout<</*"*/"*/"; //小心注释
- return 0;
- }
While语句运用,从1加到n;
- #include <iostream>
- int main()
- {
- int sum=0,i,;
- std::cout<<"Enter a postive number:"<<std::endl;
- std::cin>>i;
- int j=i;
- while(i) {
- sum+=i;
- i--;
- }
- std::cout<<"sum of "<<1<<" to "<<j<<" is "<<sum<<std::endl ;
- return 0;
- }
For语句的运用,将刚才的程序改变一下:
- #include <iostream>
- int main()
- {
- int sum=0,i,;
- std::cout<<"Enter a postive number:"<<std::endl;
- std::cin>>i;
- int j=i;
- for(;i!=0;i--) {
- sum+=i;
- }
- std::cout<<"sum of "<<1<<" to "<<j<<" is "<<sum<<std::endl ;
- return 0;
- }
习题1.10
- #include <iostream>
- int main()
- {
- int sum=0;
- for(int val=50;val!=101;val++) {
- sum+=val;
- }
- std::cout<<"sum of "<<50<<" to "<<500<<" is "<<sum<<std::endl ;
- return 0;
- }
用while改变
- #include <iostream>
- int main()
- {
- int sum=0,val=50;
- while(val<=100) {
- sum+=val;
- val++;
- }
- std::cout<<"sum of "<<50<<" to "<<500<<" is "<<sum<<std::endl ;
- return 0;
- }
习题1.11
- #include <iostream>
- int main()
- {
- int val=10;
- while(val>=0) {
- std::cout<<val<<std::endl;
- val--;
- }
- return 0;
- }
用for循环改编
- #include <iostream>
- int main()
- {
- int val=10;
- for(;val>=0;val--) {
- std::cout<<val<<std::endl;
- }
- return 0;
- }
求某两个数的和
If语句
- #include <iostream>
- using namespace std;
- int main()
- {
- int v1,v2,lower,upper,sum=0;
- cout<<"Enter two numbers"<<endl;
- cin>>v1>>v2;
- if(v1>v2) {
- upper=v1;
- lower=v2;
- }
- else {
- upper=v2;
- lower=v1;
- }
- for(int val=lower;val<=upper;val++) {
- sum+=val;
- }
- cout<<"The sum between "<<lower<<" and "<<upper<<" is "<<sum<<endl;
- return 0;
- }
习题1.16
- #include <iostream>
- using namespace std;
- int main()
- {
- cout<<"enter two numbers to find the bigger one:";
- int v1,max;
- cin>>v1>>max;
- if(v1>max) {
- max=v1;
- }
- cout<<"the bigger one is "<<max<<endl;
- return 0;
- }
习题1.17
- #include <iostream>
- using namespace std;
- int main()
- {
- cout<<"enter lots of numbers to find how many positive numbers in them:"<<endl<<"press ctrl+z to stop entering"<<endl;
- int val,count=0;
- while(cin>>val)
- if(val>0) count++;
- cout<<"There are "<<count<<" positive numbers in them."<<endl;
- return 0;
- }
读入未知数目数字求和
- #include <iostream>
- using namespace std;
- int main()
- {
- cout<<"enter lots of numbers to calculate the sum of them:"<<endl<<"press ctrl+z to stop entering"<<endl;
- int val,sum=0;
- while(cin>>val)
- if(val>0){
- sum+=val;
- }
- cout<<"The sum of them is"<<sum<<endl;
- return 0;
- }
习题1.18
- #include <iostream>
- using namespace std;
- int main()
- {
- cout<<"enter two numbers :"<<endl;
- int v1,v2,upper,lower;
- cin>>v1>>v2;
- if(v1>v2) {
- upper=v1;
- lower=v2;
- }
- else {
- upper=v2;
- lower=v1;
- }
- int val;
- for(val=lower;val<=upper;val++)
- cout<<val<<endl;
- return 0;
- }
习题1.19
- #include <iostream>
- #include <iomanip>
- using namespace std;
- int main()
- {
- cout<<"enter two numbers :"<<endl;
- int v1,v2,upper,lower;
- cin>>v1>>v2;
- if(v1>v2) {
- upper=v1;
- lower=v2;
- }
- else {
- upper=v2;
- lower=v1;
- }
- int val,amount=0;
- for(val=lower;val<=upper;val++) {
- cout<<setw(2)<<val<<" ";
- amount++;
- if(amount==10) {
- cout<<endl;
- amount=0;
- }
- }
- return 0;
- }
习题1.20
- #include <iostream>
- using namespace std;
- int main()
- {
- int v1,v2,amount,sum=0;
- cout<<"Enter two numbers"<<endl;
- cin>>v1>>v2;
- amount=v1-v2;
- if(amount<0) amount=-amount+1;
- else amount+=1;
- sum=(v1+v2)*(amount)/2;
- cout<<"The sum between "<<v1<<" and "<<v2<<" is "<<sum<<endl;
- return 0;
- }
类
标准库的头文件用<>;非标准库的头文件用""
如
- #include <iostream>
- #include"Sales_item.h"
- using namespace std;
- int main()
- {
- Sales_item item1,item2;
- cin>>item1>>item2;
- cout<<item1+item2<<endl;
- return 0;
- }
习题1.21
- #include <iostream>
- #include"Sales_item.h"
- using namespace std;
- int main()
- {
- Sales_item book;
- while(cin>>book) {
- cout<<"ISBN,numbers of copies sold,total revenur,and average price are:"
- <<endl<<book<<endl;
- }
- return 0;
- }
习题1.22
- #include <iostream>
- #include"Sales_item.h"
- using namespace std;
- int main()
- {
- Sales_item item1,item2,item;
- cout<<"Enter two objects:\n";
- cin>>item1>>item2;
- if(item1.same_isbn(item2))
- cout<<"ISBN,numbers of copies sold,total revenur,and average price are:"
- <<endl<<item1+item2<<endl;
- else
- cout<<"The two objects have different ISBN"<<endl;
- return 0;
- }
习题1.23
- #include <iostream>
- #include"Sales_item.h"
- using namespace std;
- int main()
- {
- Sales_item item,book;
- cout<<"Enter many objects:\n";
- cin>>book;
- while(cin>>item)
- if(book.same_isbn(item)) {
- book+=item;
- }
- else {
- cout<<"The objects have different ISBN"<<endl;
- return -1;
- }
- cout<<"ISBN,numbers of copies sold,total revenur,and average price are:"
- <<endl<<book<<endl;
- return 0;
- }
习题1.24
- #include <iostream>
- #include"Sales_item.h"
- using namespace std;
- int main()
- {
- Sales_item item1,item2;
- int amount=1;
- cout<<"Enter many objects:\n";
- cin>>item1;//读入第一笔交易,即初始交易。
- while(cin>>item2)
- if(item1.same_isbn(item2)) {
- ++amount;
- }
- //ISBN不同时首先计算之前ISBN的总和,然后再将amount赋值为1
- else {
- cout<<"transaction amount of previous ISBN:"
- <<endl<<amount<<endl;
- amount=1;
- item1=item2;
- }
- cout<<"transaction amount of the last ISBN :"<<amount<<endl;
- return 0;
- }
习题1.25
- #include <iostream>
- #include"Sales_item.h"
- using namespace std;
- int main()
- {
- Sales_item total,trans;
- cout<<"Enter some objects:\n(press ctrl+z to stop entering)\n";
- if(cin>>total) {
- while(cin>>trans)
- if(total.same_isbn(trans))
- total+=trans;
- else {
- cout<<"transaction of previous :"<<total<<endl;
- total=trans;
- }
- }
- else {
- cout<<"no input! no data!"<<endl;
- return -1;
- }
- return 0;
- }