您的位置:首页 > 编程语言 > C语言/C++

C++ Primer第一章

2015-06-17 20:53 393 查看
**********************************************************************
C++ Primer第一章 笔记
**********************************************************************
序言(书籍推荐):
1.《C++标准程序库》
2.Scott Meryers 《Effective C++》
3.Anothony Williams 《C++ Concurrency in Action》 开发并发程序
4.《Linux多线程服务端编程》 服务器开发(实践)

1.1编写一个简单的C++程序
int main(){
return 0;
}

IDE(Integrated Developed Enviroments)集成开发环境

1.2初识输入输出
cerr 输出警告和错误信息
clog 输出程序运行时的一般信息

#include<iostream>
int main(){
std::cout<<"Enter two numbers:"<<std::endl;
int v1=0,v2=0;
std::cin>>v1>>v2;
std::cout<<"The sum of "<<v1<<" and "<<v2<<" is "<<v1+v2<<std::endl;
return 0;
}

写入endl的效果是结束当前行,并将与设备相关联的缓冲区中的内容刷到设备中。缓冲刷新操作可以保证到目前为止程序所产生的所有输出都真正写入输出流中,而不是仅停留在内存中等待写入流

作用域运算符(::)

1.3注释简介
#include<iostream>
/*
读两个数求他们的和
*/
int main(){
std::cout<<"Enter two numbers:"<<std::endl;//提示用户输入两个数
int v1=0,v2=0;//保存我们读入的输入数据的变量
std::cin>>v1>>v2;//读取输入数据
std::cout<<"The sum of "<<v1<<" and "<<v2<<" is "<<v1+v2<<std::endl;
return 0;
}

注释界定符不能嵌套

1.4控制流
/*
while语句求1到10的和
*/
#include<iostream>
int main(){
int i=1,sum=0;
while(i<=10){
sum+=i;
i++;
}
std::cout<<"the sum of 1 to 10 is "<<sum<<std::endl;
return 0;
}

/*
for语句求1到10的和
*/
#include<iostream>
int main(){
int sum=0;
for(int i=1;i<=10;i++){
sum+=i;
}
std::cout<<"the sum of 1 to 10 is "<<sum<<std::endl;
return 0;
}

/*
读取数量不定的输入数据
*/
#include<iostream>
int main(){
int sum=0,value=0;
while(std::cin>>value)      //windows系统以Crtl+Z结束输入
sum+=value;
std::cout<<"sum is "<<sum<<std::endl;
return 0;
}

windows系统输入文件结束符的方法是Crtl+Z
Unix和Mac OS X系统输入文件结束符的方法是Crtl+D

编译器可以检查出的错误:
语法错误
类型错误
声明错误

/*
统计每个数出现的次数
*/
#include<iostream>
int main(){
int currVal=0,val=0;
if(std::cin>>currVal){
int count=1;
while(std::cin>>val){
if(currVal==val)
count++;
else{
std::cout<<currVal<<" occurs "<<count<<" times"<<std::endl;
count=1;
}
currVal=val;
}
std::cout<<currVal<<" occurs "<<count<<" times"<<std::endl;
}
return 0;
}

1.5类简介
对象和成员函数

**********************************************************************
练习
**********************************************************************
练习1.1
int main(){
return 0;
}

练习1.2
int main(){
return -1;
}

练习1.3
/*
输出Hello,World!
*/
#include<iostream>
int main(){
std::cout<<"Hello,World!"<<std::endl;
return 0;
}

练习1.4
/*
the multiply of a and b
*/
#include<iostream>
int main(){
int a,b;
std::cout<<"Enter two numbers:"<<std::endl;
std::cin>>a>>b;
std::cout<<a<<"*"<<b<<"="<<a*b<<std::endl;
return 0;
}

练习1.5
/*
the multiply of a and b(seperate statement)
*/
#include<iostream>
int main(){
int a,b;
std::cout<<"Enter two numbers:";
std::cout<<std::endl;
std::cin>>a>>b;
std::cout<<a;
std::cout<<"*";
std::cout<<b;
std::cout<<"=";
std::cout<<a*b;
std::cout<<std::endl;
return 0;
}

练习1.6
/*
判断程序是否合法
*/
std::cout<<"The sum of "<<v1;
<<" and "<<v2;
<<" is "<<v1+v2<<std::endl;
不合法

练习1.7
/*
编译一个包含不正确嵌套注释的程序
*/
#include<iostream>
int main(){
std::cout<<"Hello,World!"<<std::endl;///*
std::cout<<"Hello,World!"<<std::endl;*/
return 0;
}

练习1.8
/*
判断下列注释语句哪些是合法
*/
#include<iostream>
int main(){
std::cout<<"/*";                //合法 输出/*
std::cout<<"*/";                //合法 输出*/
std::cout<</* "*/" */;          //不合法
std::cout<</* "*/" /* "/*" */;  //合法 输出 /*
}

练习1.9
/*
while语句求50到100的和
*/
#include<iostream>
int main(){
int i=50,sum=0;
while(i<=100){
sum+=i;
i++;
}
std::cout<<"the sum of 50 to 100 is "<<sum<<std::endl;
return 0;
}

练习1.10
/*
while语句输出10到1之间的整数
*/
#include<iostream>
int main(){
int i=9;
while(i>0){
std::cout<<i<<std::endl;
--i;
}
return 0;
}

练习1.11
/*
输出给定两个数之间的所有整数(后者比前者大)
*/
#include<iostream>
int main(){
int a,b;
std::cin>>a>>b;
while(a<b-1){
std::cout<<a+1<<" ";
a++;
}
std::cout<<std::endl;
return 0;
}

练习1.12
/*
for循环完成了什么功能,sum的终值是多少
*/
int sum=0;
for(int i=-100;i<=100;i++)
sum+=i;
//for循环完成了-100到100的整数求和,sum终值为0;

练习1.13
/*
for循环重做练习1.9
求50到100的和
*/
#include<iostream>
int main(){
int sum=0;
for(int i=50;i<=100;i++){
sum+=i;
}
std::cout<<"the sum of 50 to 100 is "<<sum<<std::endl;
return 0;
}
/*
for循环重做练习1.10
输出10到1之间的整数
*/
#include<iostream>
int main(){
for(int i=9;i>0;i--){
std::cout<<i<<std::endl;
}
return 0;
}
/*
for循环重做练习1.11
输出给定两个数之间的所有整数(后者比前者大)
*/
#include<iostream>
int main(){
int a,b;
std::cin>>a>>b;
for(int i=a;i<b-1;i++){
std::cout<<i+1<<" ";
}
std::cout<<std::endl;
return 0;
}

练习1.14
for循环和while循环优缺点:
for循环多用于指定初始值和已知终止条件的循环
while循环 是在循环开始前判断是否满足条件进行循环

练习1.15
语法错误
类型错误
声明错误

练习1.16
/*
从cin读取一组数,输出其和
*/
#include<iostream>
int main(){
int sum=0,value=0;
while(std::cin>>value)      //windows系统以Crtl+Z结束输入
sum+=value;
std::cout<<"sum is "<<sum<<std::endl;
return 0;
}

练习1.17
输入的值都相等,输出结果是:这个值出现了输入的次数
输入的值没重复的,输出结果是:每个值出现了一次

练习1.18
如1.17

练习1.19
/*
输出给定两个数之间的所有整数(if控制语句的使用)
*/
#include<iostream>
int main(){
int a,b;
std::cin>>a>>b;
if(a<b){
for(int i=a;i<b-1;i++)
std::cout<<i+1<<" ";
}
else{
for(int i=b;i<a-1;i++)
std::cout<<i+1<<" ";
}
std::cout<<std::endl;
return 0;
}

习题1.20
/*
读入一组书籍销售记录并打印
*/
#include<iostream>
#include"Sales_item.h"
int main(){
Sales_item book;
std::cin>>book;
std::cout<<book<<std::endl;
return 0;
}

习题1.21
/*
读入两个相同ISBN号的书籍销售记录并求和
*/
#include<iostream>
#include"Sales_item.h"
int main(){
Sales_item book1,book2;
std::cin>>book1>>book2;
std::cout<<book1+book2<<std::endl;
return 0;
}

习题1.22
/*
读入多个相同ISBN号的书籍销售记录并求和
*/
#include<iostream>
#include"Sales_item.h"
int main(){
Sales_item bookAll,bookCurr;
std::cin>>bookAll;
while(std::cin>>bookCurr)
bookAll+=bookCurr;
std::cout<<bookAll<<std::endl;
return 0;
}

习题1.23
/*
统计每个ISBN有几条销售记录
*/
#include<iostream>
#include"Sales_item.h"
int main(){
Sales_item book,currbook;
if(std::cin>>currbook){
int count=1;
while(std::cin>>book){
if(currbook.isbn()==book.isbn())
count++;
else{
std::cout<<currbook.isbn()<<" occurs "<<count<<" times"<<std::endl;
count=1;
}
currbook=book;
}
std::cout<<currbook.isbn()<<" occurs "<<count<<" times"<<std::endl;
}
return 0;
}

习题1.24
如1.23

习题1.25
/*
统计每个ISBN的销售之和
*/
#include<iostream>
#include"Sales_item.h"
int main(){
Sales_item book,currbook;
if(std::cin>>currbook){
while(std::cin>>book){
if(currbook.isbn()==book.isbn())
currbook+=book;
else{
std::cout<<currbook<<std::endl;
currbook=book;
}
}
std::cout<<currbook<<std::endl;
}
return 0;
}
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: