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

C++(1)入门

2015-08-07 18:23 330 查看
一、编写简单的C++程序

[cpp] view
plaincopy

//main函数是唯一一个被操作系统显式调用的函数。

int main()

{

/*

*该返回值作为状态指示器,需要返回给操作系统!

*以echo $?命令可以查看该返回值

*通常非零返回值表示有错误出现,由操作系统定义

*/

return 0;

}

二、初窥输入/输出

1、标准输入:cin

2、标准输出:cout

3、cerr、clog

【当写到cout、cerr与clog时,输出写至同一窗口!但是利用重定向可以将这些流与所选择的文件联系起来】

[cpp] view
plaincopy





#include <iostream>

//iostream库定义了接受各种内置类型的输入/输出操作符版本



int main()

{

/*

*当操作符是输入(>>)、输出(<<)操作符时,

*其结果是左操作数,

*也就是说:输入/输出操作返回的值是输入/输出流本身

*/



//将"..."的内容写入到cout对象中

std::cout << "Enter two numbers:" << std::endl;

int v1,v2;

//从istream操作数读入数据并保存在右操作数中

std::cin >> v1 >> v2;

//endl具有:1、输出换行 2、刷新与设备相关联的缓冲区 的作用

std::cout << "The sum of " << v1 << " and " << v2

<< " is " << v1 + v2 << std::endl;

return 0;

}

三、关于注释

1、注释用于:

概括算法、确认变量的用途、阐明难以理解的代码段

2、规则

1)多行注释:使用注释对/**/,同时,在注释的每一行以星号开始,指明整个范围是多行注释的一部分。

2)单行注释:使用//

3)通常最好是将一个注释块放在所解释代码的上方!

4)注释对不可嵌套

是否正确:

[cpp] view
plaincopy





#include <iostream>

/*

*comment pairs/* */ cannot nes.

*"cannot nest" is considered source code.

*as is the rest of the program

*/



int main()

{

return 0;

}



1、std::cout << "/*"; //输出”/*”

2、std::cout << "*/"; //输出”*/”

3、std::cout << /* "*/" */; //编译出错

四、控制结构

1、关于C++程序的缩排和格式:

一旦你选定了某种风格,就要始终如一地使用他!

2、再谈编译

编译器可以检查出的错误:

1)语法错误

2)类型错误

3)声明错误

编译器无法检测出的错误:

逻辑错误,该类错误一般需要单步跟踪等进行调试,工具如GDB等。

3、读入未知数目的输入

如果定义了变量intval;并执行语句while(std::cin >> val){...},会为判断条件,先执行输入操作,如果遇到文件结束符(end-of-file)或遇到无效的输入时,如读取了一个不是整数的值,则istream对象是无效的。处于无效状态的istream对象将导致条件失败。

4、从键盘输入文件结束符

Windows:Ctrl+z UNIX/Linux/MacOS:Ctrl+D【在Ubuntu下有时候需要连摁两下】

P14.习题

[cpp] view
plaincopy





//题1.10

#include <iostream>



int main()

{

int sum = 0;

int val = 50;

while (val <= 100)

{

sum += val;

++ val;

}

std::cout << sum << std::endl;

return 0;

}

[cpp] view
plaincopy





//题1.11

#include <iostream>



int main()

{

for (int i = 10;i >= 0; --i)

{

std::cout << i << std::endl;

}

return 0;

}

[cpp] view
plaincopy





//题1.12编译并认识下列错误

#include <iostream>



int main(

{

std::cout << "read" << std::endl:

std::cout << update << std::endl;

std::cout << "write" << std::endl;

return 0

}

[cpp] view
plaincopy





//题1.14: 试分析如果v1 == v2的情况下,该程序的输出结果

#include <iostream>



int main()

{

std::cout << "Enter two numbers:" << std::endl;

int v1,v2;

std::cin >> v1 >> v2;



int lower,upper;

if (v1 <= v2)

{

lower = v1;

upper = v2;

}

else

{

lower = v2;

upper = v1;

}



int sum = 0;

for (int i = lower; i <= upper; ++i)

{

sum += i;

}



std::cout << "Sum of " << lower

<< " to " << upper

<< " inclusive is "

<< sum << std::endl;



return 0;

}

[cpp] view
plaincopy





//1.16

#include <iostream>



int main()

{

std::cout << "Please input a sequence of numbers:" << std::endl;

int val;

int count = 0;

//为判断条件,先执行输入操作

while (std::cin >> val)

{

if (val < 0)

{

++ count;

}

}



std::cout << "There is " << count << " negatives" << std::endl;

return 0;

}

[cpp] view
plaincopy





//题1.19

#include <iostream>



int main()

{

std::cout << "Please input two numbers:" << std::endl;

int v1,v2;

int count = 1;

std::cin >> v1 >> v2;

for (int i = v1 ; i <= v2; ++i)

{

std::cout << i << ' ';

if (count % 10 == 0)

{

std::cout << std::endl;

}

++ count;

}



return 0;

}

五、类的简介

1、C++中,我们通过定义类来定义自己的数据结构

实际上,C++设计的主要焦点就是使所定义的类类型的行为可以像内置类型一样自然!!!

2、使用类的时候,我们不需要指定哦啊这个类是怎样实现的,相反,我们需要知道的是:这个类能够提供什么样的操作!

3、对于自定义的类,必须使得编译器可以访问和类相关的定义。

4、通常文件名和定义在头文件中的类名是一样的。通常后缀名是.h,但是有些IDE会挑剔头文件的后缀名!

5、当使用自定义头文件时,我们采用双引号(””)把头文件包含进来。

6、当调用成员函数的时候,(通常)指定函数要操作的对象,语法是使用点操作符(”.”),左操作符必须是类类型,右操作符必须指定该类型的成员。

P19

[cpp] view
plaincopy





//习题1.21

#include <iostream>

#include <fstream>

#include "Sales_item.h"



int main()

{

freopen("book_sales","r",stdin);

Sales_item item;

while (std::cin >> item)

{

std::cout << item << std::endl;

}



return 0;

}

[cpp] view
plaincopy





//习题1.22

#include <iostream>

#include <fstream>

#include "Sales_item.h"



int main()

{

freopen("book_sales","r",stdin);

Sales_item item1;

Sales_item item2;

while (std::cin >> item1 >> item2)

{

if (item1.same_isbn(item2))

{

std::cout << item1 + item2 << std::endl;

}

}



return 0;

}

六、C++程序

[cpp] view
plaincopy





#include <iostream>

#include <fstream>

#include "Sales_item.h"



int main()

{

//freopen("book_sales.","r",stdin);



Sales_item total,trans;



if (std::cin >> total)

{

while (std::cin >> trans)

{

if (total.same_isbn(trans))

{

total += trans;

}

else

{

std::cout << total << std::endl;

total = trans;

}

}

std::cout << total << std::endl;

}

else

{

std::cout << "No Data?!" << std::endl;

return -1;

}

return 0;

}

【说明:Sales_item类需要到图灵网站上下载并引用:http://www.ituring.com.cn/book/656

本文借鉴:http://blog.csdn.net/column/details/zjf666.html?&page=6
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: