标签:
1 ********************************************************************** 2 C++ Primer第一章 笔记 3 ********************************************************************** 4 序言(书籍推荐): 5 1.《C++标准程序库》 6 2.Scott Meryers 《Effective C++》 7 3.Anothony Williams 《C++ Concurrency in Action》 开发并发程序 8 4.《Linux多线程服务端编程》 服务器开发(实践) 9 10 1.1编写一个简单的C++程序 11 int main(){ 12 return 0; 13 } 14 15 IDE(Integrated Developed Enviroments)集成开发环境 16 17 1.2初识输入输出 18 cerr 输出警告和错误信息 19 clog 输出程序运行时的一般信息 20 21 #include<iostream> 22 int main(){ 23 std::cout<<"Enter two numbers:"<<std::endl; 24 int v1=0,v2=0; 25 std::cin>>v1>>v2; 26 std::cout<<"The sum of "<<v1<<" and "<<v2<<" is "<<v1+v2<<std::endl; 27 return 0; 28 } 29 30 写入endl的效果是结束当前行,并将与设备相关联的缓冲区中的内容刷到设备中。缓冲刷新操作可以保证到目前为止程序所产生的所有输出都真正写入输出流中,而不是仅停留在内存中等待写入流 31 32 作用域运算符(::) 33 34 1.3注释简介 35 #include<iostream> 36 /* 37 读两个数求他们的和 38 */ 39 int main(){ 40 std::cout<<"Enter two numbers:"<<std::endl;//提示用户输入两个数 41 int v1=0,v2=0;//保存我们读入的输入数据的变量 42 std::cin>>v1>>v2;//读取输入数据 43 std::cout<<"The sum of "<<v1<<" and "<<v2<<" is "<<v1+v2<<std::endl; 44 return 0; 45 } 46 47 注释界定符不能嵌套 48 49 1.4控制流 50 /* 51 while语句求1到10的和 52 */ 53 #include<iostream> 54 int main(){ 55 int i=1,sum=0; 56 while(i<=10){ 57 sum+=i; 58 i++; 59 } 60 std::cout<<"the sum of 1 to 10 is "<<sum<<std::endl; 61 return 0; 62 } 63 64 /* 65 for语句求1到10的和 66 */ 67 #include<iostream> 68 int main(){ 69 int sum=0; 70 for(int i=1;i<=10;i++){ 71 sum+=i; 72 } 73 std::cout<<"the sum of 1 to 10 is "<<sum<<std::endl; 74 return 0; 75 } 76 77 /* 78 读取数量不定的输入数据 79 */ 80 #include<iostream> 81 int main(){ 82 int sum=0,value=0; 83 while(std::cin>>value) //windows系统以Crtl+Z结束输入 84 sum+=value; 85 std::cout<<"sum is "<<sum<<std::endl; 86 return 0; 87 } 88 89 windows系统输入文件结束符的方法是Crtl+Z 90 Unix和Mac OS X系统输入文件结束符的方法是Crtl+D 91 92 编译器可以检查出的错误: 93 语法错误 94 类型错误 95 声明错误 96 97 /* 98 统计每个数出现的次数 99 */ 100 #include<iostream> 101 int main(){ 102 int currVal=0,val=0; 103 if(std::cin>>currVal){ 104 int count=1; 105 while(std::cin>>val){ 106 if(currVal==val) 107 count++; 108 else{ 109 std::cout<<currVal<<" occurs "<<count<<" times"<<std::endl; 110 count=1; 111 } 112 currVal=val; 113 } 114 std::cout<<currVal<<" occurs "<<count<<" times"<<std::endl; 115 } 116 return 0; 117 } 118 119 1.5类简介 120 对象和成员函数 121 122 123 124 125 ********************************************************************** 126 练习 127 ********************************************************************** 128 练习1.1 129 int main(){ 130 return 0; 131 } 132 133 练习1.2 134 int main(){ 135 return -1; 136 } 137 138 练习1.3 139 /* 140 输出Hello,World! 141 */ 142 #include<iostream> 143 int main(){ 144 std::cout<<"Hello,World!"<<std::endl; 145 return 0; 146 } 147 148 练习1.4 149 /* 150 the multiply of a and b 151 */ 152 #include<iostream> 153 int main(){ 154 int a,b; 155 std::cout<<"Enter two numbers:"<<std::endl; 156 std::cin>>a>>b; 157 std::cout<<a<<"*"<<b<<"="<<a*b<<std::endl; 158 return 0; 159 } 160 161 练习1.5 162 /* 163 the multiply of a and b(seperate statement) 164 */ 165 #include<iostream> 166 int main(){ 167 int a,b; 168 std::cout<<"Enter two numbers:"; 169 std::cout<<std::endl; 170 std::cin>>a>>b; 171 std::cout<<a; 172 std::cout<<"*"; 173 std::cout<<b; 174 std::cout<<"="; 175 std::cout<<a*b; 176 std::cout<<std::endl; 177 return 0; 178 } 179 180 练习1.6 181 /* 182 判断程序是否合法 183 */ 184 std::cout<<"The sum of "<<v1; 185 <<" and "<<v2; 186 <<" is "<<v1+v2<<std::endl; 187 不合法 188 189 练习1.7 190 /* 191 编译一个包含不正确嵌套注释的程序 192 */ 193 #include<iostream> 194 int main(){ 195 std::cout<<"Hello,World!"<<std::endl;///* 196 std::cout<<"Hello,World!"<<std::endl;*/ 197 return 0; 198 } 199 200 练习1.8 201 /* 202 判断下列注释语句哪些是合法 203 */ 204 #include<iostream> 205 int main(){ 206 std::cout<<"/*"; //合法 输出/* 207 std::cout<<"*/"; //合法 输出*/ 208 std::cout<</* "*/" */; //不合法 209 std::cout<</* "*/" /* "/*" */; //合法 输出 /* 210 } 211 212 练习1.9 213 /* 214 while语句求50到100的和 215 */ 216 #include<iostream> 217 int main(){ 218 int i=50,sum=0; 219 while(i<=100){ 220 sum+=i; 221 i++; 222 } 223 std::cout<<"the sum of 50 to 100 is "<<sum<<std::endl; 224 return 0; 225 } 226 227 练习1.10 228 /* 229 while语句输出10到1之间的整数 230 */ 231 #include<iostream> 232 int main(){ 233 int i=9; 234 while(i>0){ 235 std::cout<<i<<std::endl; 236 --i; 237 } 238 return 0; 239 } 240 241 练习1.11 242 /* 243 输出给定两个数之间的所有整数(后者比前者大) 244 */ 245 #include<iostream> 246 int main(){ 247 int a,b; 248 std::cin>>a>>b; 249 while(a<b-1){ 250 std::cout<<a+1<<" "; 251 a++; 252 } 253 std::cout<<std::endl; 254 return 0; 255 } 256 257 练习1.12 258 /* 259 for循环完成了什么功能,sum的终值是多少 260 */ 261 int sum=0; 262 for(int i=-100;i<=100;i++) 263 sum+=i; 264 //for循环完成了-100到100的整数求和,sum终值为0; 265 266 练习1.13 267 /* 268 for循环重做练习1.9 269 求50到100的和 270 */ 271 #include<iostream> 272 int main(){ 273 int sum=0; 274 for(int i=50;i<=100;i++){ 275 sum+=i; 276 } 277 std::cout<<"the sum of 50 to 100 is "<<sum<<std::endl; 278 return 0; 279 } 280 /* 281 for循环重做练习1.10 282 输出10到1之间的整数 283 */ 284 #include<iostream> 285 int main(){ 286 for(int i=9;i>0;i--){ 287 std::cout<<i<<std::endl; 288 } 289 return 0; 290 } 291 /* 292 for循环重做练习1.11 293 输出给定两个数之间的所有整数(后者比前者大) 294 */ 295 #include<iostream> 296 int main(){ 297 int a,b; 298 std::cin>>a>>b; 299 for(int i=a;i<b-1;i++){ 300 std::cout<<i+1<<" "; 301 } 302 std::cout<<std::endl; 303 return 0; 304 } 305 306 练习1.14 307 for循环和while循环优缺点: 308 for循环多用于指定初始值和已知终止条件的循环 309 while循环 是在循环开始前判断是否满足条件进行循环 310 311 练习1.15 312 语法错误 313 类型错误 314 声明错误 315 316 练习1.16 317 /* 318 从cin读取一组数,输出其和 319 */ 320 #include<iostream> 321 int main(){ 322 int sum=0,value=0; 323 while(std::cin>>value) //windows系统以Crtl+Z结束输入 324 sum+=value; 325 std::cout<<"sum is "<<sum<<std::endl; 326 return 0; 327 } 328 329 练习1.17 330 输入的值都相等,输出结果是:这个值出现了输入的次数 331 输入的值没重复的,输出结果是:每个值出现了一次 332 333 练习1.18 334 如1.17 335 336 练习1.19 337 /* 338 输出给定两个数之间的所有整数(if控制语句的使用) 339 */ 340 #include<iostream> 341 int main(){ 342 int a,b; 343 std::cin>>a>>b; 344 if(a<b){ 345 for(int i=a;i<b-1;i++) 346 std::cout<<i+1<<" "; 347 } 348 else{ 349 for(int i=b;i<a-1;i++) 350 std::cout<<i+1<<" "; 351 } 352 std::cout<<std::endl; 353 return 0; 354 } 355 356 习题1.20 357 /* 358 读入一组书籍销售记录并打印 359 */ 360 #include<iostream> 361 #include"Sales_item.h" 362 int main(){ 363 Sales_item book; 364 std::cin>>book; 365 std::cout<<book<<std::endl; 366 return 0; 367 } 368 369 习题1.21 370 /* 371 读入两个相同ISBN号的书籍销售记录并求和 372 */ 373 #include<iostream> 374 #include"Sales_item.h" 375 int main(){ 376 Sales_item book1,book2; 377 std::cin>>book1>>book2; 378 std::cout<<book1+book2<<std::endl; 379 return 0; 380 } 381 382 习题1.22 383 /* 384 读入多个相同ISBN号的书籍销售记录并求和 385 */ 386 #include<iostream> 387 #include"Sales_item.h" 388 int main(){ 389 Sales_item bookAll,bookCurr; 390 std::cin>>bookAll; 391 while(std::cin>>bookCurr) 392 bookAll+=bookCurr; 393 std::cout<<bookAll<<std::endl; 394 return 0; 395 } 396 397 习题1.23 398 /* 399 统计每个ISBN有几条销售记录 400 */ 401 #include<iostream> 402 #include"Sales_item.h" 403 int main(){ 404 Sales_item book,currbook; 405 if(std::cin>>currbook){ 406 int count=1; 407 while(std::cin>>book){ 408 if(currbook.isbn()==book.isbn()) 409 count++; 410 else{ 411 std::cout<<currbook.isbn()<<" occurs "<<count<<" times"<<std::endl; 412 count=1; 413 } 414 currbook=book; 415 } 416 std::cout<<currbook.isbn()<<" occurs "<<count<<" times"<<std::endl; 417 } 418 return 0; 419 } 420 421 习题1.24 422 如1.23 423 424 习题1.25 425 /* 426 统计每个ISBN的销售之和 427 */ 428 #include<iostream> 429 #include"Sales_item.h" 430 int main(){ 431 Sales_item book,currbook; 432 if(std::cin>>currbook){ 433 while(std::cin>>book){ 434 if(currbook.isbn()==book.isbn()) 435 currbook+=book; 436 else{ 437 std::cout<<currbook<<std::endl; 438 currbook=book; 439 } 440 } 441 std::cout<<currbook<<std::endl; 442 } 443 return 0; 444 }
**********************************************************************
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;
}
标签:
原文地址:http://www.cnblogs.com/ftdwlei/p/4584352.html