第2章 与c++第一次亲密接触

第2章 与c++第一次亲密接触

2.1 一个c++程序的自白
2.1.1 用visual studio创建c++程序

书中居然用了visual studio 2010 作为IDE。这太高端了,完全没有必要。

我们可以选择guide,dev,或者visual c++ 6 来作为IDE就可以了。

在VC上运行的Cpp程序,有其一定的特殊性,我们要掌握的,是一般性的、通用的Cpp程序。

2.1.2 以手工方式创建c++程序

#include <iostream> //包含头文件,输入输出流头文件
using namespace std; //使用std命名空间
int main()
{
  cout << "Hello World!" << endl; //输出。。并换行
  return 0; //返回0,意味着程序正常结束。
}

2.1.3 c++程序=预编译指令+程序代码+注释
2.1.4 编译器和链接器
2.1.5 c++程序的执行过程
2.1.6 程序的两大任务:描述数据与处理数据
2.2 基本输入/输出流
2.2.1 标准的输入和输出对象

#include <iostream>
using namespace std;
int main()
{
  cout << 1 << endl;
  cout << "Hello World!" << endl;
  cout << "1 + 2 = " << 1+2 << endl;
  return 0;
}
#include <iostream>
#include <string>
using namespace std;
int main()
{
  int nAge;
  string strName;
  cin >> nAge >> strName; //观察输入可以知道,以空格或回车作为间隔,都可以
  cout << nAge << endl;
  cout << strName << endl;
  return 0;
}
#include <iostream>
using namespace std;
int main()
{
  int v1,v2;
  cout<<"请输入两个整数:"<<endl;
  cin>>v1>>v2;
  cout<<"两个整数"<<v1<<""<<v2
    <<"的和是:"<<v1+v2<<endl;
  return 0;
}

2.2.2 输出格式控制

#include <iostream>
#include <bitset> //为了显示2进制
using namespace std;
int main()
{
  int i;
  i=255;
  cout<<dec<<i<<endl; //10进制
  cout<<hex<<i<<endl; //16进制
  cout<<oct<<i<<endl; //8
  //cout<<bin<<i<<endl; //没有提供这个功能
  cout<<bitset<32>(i)<<endl; //这样才能显示32位的2进制
  return 0;
}
#include <iostream>
#include <iomanip> //为了设置场宽、小数
using namespace std;
int main()
{
  int i;
  double d;
  i=255;
  d=123.123456789;
  cout<<setw(10)<<i<<endl;
  cout<<setw(4)<<i<<endl;
  cout<<setprecision(2)<<d<<endl; //以科学记数法。。
  cout<<fixed<<setprecision(2)<<d<<endl;
  cout<<fixed<<setprecision(5)<<d<<endl; //有四舍五入
  return 0;
}

setiosflags()就不试了。

Cpp的细节非常多,一辈子也学不完。

我们现在的目的,是掌握常用的,为了OJ。

以后碰到问题,再查资料,或者查百度。网上也有专业网站可以查询。


2.2.3 读/写文件

//读写文件,进行了改写,改为求两数之和
//从有路径文件,读取并输出。即输入文件没有放在程序文件的同一目录下。
#include <iostream>
#include <fstream> //为了读写文件
using namespace std;
int main()
{
  int i,j;
  //尝试打开testin.txt文件,并将其作为输入流
  /*因为字符串中,可以有转义字符,所以\表示的是 */
  ifstream fin("x:\testin.txt");   
  ofstream fout("x:\testout.txt"); //如输出文件不存在,会自动创建
  if( fin.good() ) //如果指定文件存在,并可以正常打开
  {                //用bad检测不出文件不存在
    fin>>i>>j;
    fout<<"The sum is: "<<i+j<<endl;
    fin.close();
    fout.close();
  }
  else
  {
    cout<<"can't open the input file!"<<endl;
  }
  return 0;
}
/*
这篇文章,对bad、good等,有较详细讲解
http://blog.csdn.net/kingstar158/article/details/6859379
*/
//读写文件,进行了改写,改为求两数之和
//从有路径文件,读取并输出。即输入文件没有放在程序文件的同一目录下。
//如不检测输入文件。。
#include <iostream>
#include <fstream> //为了读写文件
using namespace std;
int main()
{
  int i,j;
  //尝试打开testin.txt文件,并将其作为输入流
  /*因为字符串中,可以有转义字符,所以\表示的是 */
  ifstream fin("x:\ttestin.txt");   
  ofstream fout("x:\testout.txt"); //如输出文件不存在,会自动创建
  //如不检测,当输入文件不存在时,还会自动创建一个输出文件,并得到错误结果
    fin>>i>>j;
    fout<<"The sum is: "<<i+j<<endl;
    fin.close();
    fout.close();
  //
  return 0;
}
/*
如niop复赛,以这种形式提交,问题也不是很大。但不建议。。
因为:
一、中间的输入输出,用fin,fout,不利于调试。
二、当数据量大时,fin的速度比scanf要慢很多。
*/
#include <iostream>
#include <fstream>
using namespace std;
int main()
{
  int i,j;
  ifstream fin("testin.txt");   
  ofstream fout("testout.txt");
  //
  fin>>i>>j;
  fout<<i+j<<endl;
  //
  fin.close(); fout.close();
  return 0;
}
/*
当调试OJ程序时,当输入数据量大时,可以考虑用这种方式,
进行文件输入,屏幕输出。
当提交程序时,把ifstream那一行,用//注释掉就可以了。
——
一般情况下,也不鼓励这么做。
只要把输入文件中的内容,复制一下。
然后粘贴到DOS窗口,作为输入,就可以测试了。
——
但这也要看IDE,在VC++6中不可以粘贴进去,在Guide中就可以
*/
#include <iostream>
#include <fstream>
using namespace std;
int main()
{
  int i,j;
  ifstream cin("x:\testin.txt");   
  //
  cin>>i>>j;
  cout<<i+j<<endl;
  //
  return 0;
}
/*
整了半天,还是C语言中的好使。。
无论是noip复赛提交,还是平时读取数据OJ。
反正,C是Cpp的子集么,大胆用。。
*/
#include <iostream>
#include <stdio.h>
using namespace std;
int main()
{
  int i,j;
  freopen("x:\testin.txt","r",stdin);
  //freopen("x:\testout.txt","w",stdout);
  //
  cin>>i>>j;
  cout<<i+j<<endl;
  //
  //fclose(stdin); fclose(stdout);  
  return 0;
}


2.3 最常用的开发环境visual studio

对于noier来说,高大上的visual studio绝对不是学习Cpp的最常用的IDE。最多玩下VC++6,简化版。

所以这节以下内容,可以忽略。

不,是这章以下内容,可以忽略。。

2.3.1 visual c++的常用菜单
2.3.2 visual c++的常用视图
2.4 c++世界旅行必备的物品
2.4.1 编程助手visual assist
2.4.2 代码配置管理工具visual source safe
2.4.3 codeproject和codeguru
2.4.4 c++百科全书msdn

TOP

原文地址:https://www.cnblogs.com/xin-le/p/4077673.html