C++ 1. 基础语法 输入输出、流程控制、初识类

来源:互联网 发布:网易新闻评论知乎 编辑:程序博客网 时间:2024/06/03 17:46

C++实现 源代码的扩展名

UNIX Ccccxxc                    

GNU C++Ccccxxcppc++        命令g++

Borland C++Cpp

Microsoft Visual C++ cppcxxcc

  如GNU编译命令: g++ -o me me.cc   生成可执行文件 me     win平台会生成.exe后缀的可执行文件


访问main()的返回值,依赖于系统,win:echo %ERRORLEVEL%   unix: echo $?


文件结束符   win:ctrl+z    uninx(含mac os x) ctrl+d


#include <iostream>  //引入 标准输入输出库 iostreamusing namespace std;int main(int argc, char *argv[]) {cout << "enter two numbers:" << endl; int c1 = 0, c2 = 0;std::cin >> c1 >> c2; //标准输入std::cout <<"the sum is " << c1+c2 << std::endl; //使用namespace后 可省略前缀cout << "查看换行"; //输出还有cerr 、clogcout << "没有" << endl; //endl 结束当前行,并刷新缓冲区(buffer),将目前为止产生的所有输出都写到输出流中/*<< 输出运算符   >>输入运算符   naespace命名空间  endl操作符  ::作用域运算符*/for (int i = 10; i >= 0; --i) cout << "item is " << i << endl;while (c1 > 0) {cout << c1-- <<endl;}int sum = 0, value = 0;while (cin >> value) {//标准输入遇文件结束符 或 错误输入类型时 退出sum += value;}cout << "sum is " << sum << endl;   bool a = false, b = true;if (a | b) {cout << "true" << endl;}return ++c1;}

#include <iostream>#include "Sales_item.h"using namespace std;int main(int argc, char *argv[]) {Sales_item total; if (cin >> total) {//输入: isbn 数量 单价Sales_item trans;while (cin >> trans) {if (total.isbn() == trans.isbn()) {total += trans;} else {cout << total << endl; //输出:isbn 总量 总价 平均价total = trans;}}cout << total << std::endl; // last book } else {cerr << "no data." << endl;return -1;}return 0;}


文件重定向

如 有一个功能为 循环输入book信息,并输出统计信息的 可执行文件  addItems

$ addItems <infile >outfile

<infile  从infile中读入book信息;  >outfile  输出统计信息到outfile           


注:

http://download.csdn.net/detail/jjwwmlp456/9122573   源码中第1章中有 Sales_item.h


0 0