[VC++2005入门经典]第二章源代码分析_上部Ex2_01至Ex2_08

 [美]Ivor Horton著作《Visual C++ 2005入门经典》

Ex2_01:

 

// Ex2_01_main.cpp
// A Simple Example of a Program
#include  < iostream >   // 标准输入输出流库文件 与C的“stdio”有区别,在C++中输入源和输出目的称为流

using  std::cout;  // ANSI C++所有标准库工具都被定义在名为“std”的命名空间,cout为标准输出流
using  std::endl;  // endl表示换行字符

int  main()
{
    
int apples,oranges;
    
int fruit;
    apples
=5;oranges=6;
    fruit
=apples+oranges;
    cout
<<endl;//“<<”这个符号表示将换行符号输出到输出流
    cout<<"橘子不是唯一的水果---"<<endl<<"我们一共有"<<fruit<<"个水果!";//<<这样的写法是可以的。<<右边的字符都会输出到cout(输出流)
    return 0;
}

 未完待续,马上陪老婆逛易初莲花...

继续:

Ex2_03.cpp

// Ex2_03.cppp
// Exercising output
#include  < iostream >
#include 
< iomanip >

using  std::cout;
using  std::endl;
using  std::setw; // 操作符号在头文件<iomanip>中定义,setw(n)的作用是输出的值在n个空格(字符)宽的字段中遵循右对齐。

int  main()
{
    
int num1=1234,num2=5678;
    cout
<<endl;
    cout
<<setw(6)<<num1<<setw(6)<<num2;//setw操作符:只对紧跟在它的插入运算符后的单个输入值起作用。
    cout<<endl;
    
return 0;
}

 Ex2_04

 (CSDN好象对/n等转义字符在代码方式下有错误,所以这段贴原码,不用代码方式)

//Ex2_04.cpp
//Using escape sequences|转义序列的使用
#include <iostream>
#include <iomanip>

using std::cout;

int main()
{
 char newline='/n';///n的作用与endl一致
 cout<<newline;
 cout<<"/"We/'ll make our escapes in sequence/",he said.";//  “/"”代表字符“"”
 cout<<"/n/tThe program/'s over,it/'s time take make abeep beep./a/a/a/a/a";// “/n”代表换行“/t”代表跳到下一个制表符
 // “/a”代表PC扬声器发“嘟”一声,牛!
 cout<<newline;
 return 0;
}

 

// Ex2_05.cpp
// Calculating how many rolls of wall paper are required for a room
// 计算你家墙用多少米的墙纸帖才够?
// 哈哈,老外的书很有意思。
#include  < iostream >
using  std::cout;
using  std::cin; // cin是输入源,与cout相对
using  std::endl;

int  main()
{
    
double height=0.0,width=0.0,length=0.0;//Room dimensions 房间长宽高
    double perimeter=0.0;//Room perimeter
    const double rollwidth=21.0;
    
const double rolllength=12.0*33.0;
    
int strips_per_roll=0;
    
int strips_reqd=0;
    
int nrolls=0;
    cout
<<endl<<"Enter the height of the rom in inches:";
    cin
>>height;
    cout
<<endl<<"Now enter the length and width in inches:";
    cin
>>length>>width;

    strips_per_roll
=rolllength/height;
    perimeter
=2.0*(length+width);
    strips_reqd
=perimeter/rollwidth;
    nrolls
=strips_reqd/strips_per_roll;
    cout
<<endl<<"For your rom you need "<<nrolls<<" rolls of wallpaper.";
}

 Ex2_06 

// Ex2_06.cpp
// Exercising the comma operator逗号运算符号
#include  < iostream >
using  std::cout;
using  std::endl;

int  main()
{
    
long num1=0,num2=0,num3=0,num4=0;
    num4
=(num1=10,num2=20,num3=30);//想想,如果写成:num4=num1=10,num2=20,num3=30 呢?num4的值为几?
    cout<<endl<<"The values of a series of expreesions is the value of the rightmost:"<<num4;
    cout
<<endl;
    
return 0;
}

 变量的作用域:

// Ex2_07.cpp
// Demonstrating variable scope 作用域对自动变量的影响
#include  < iostream >
using  std::cout;
using  std::endl;
int  main()
{
    
int count1=10;
    
int count3=50;
    cout
<<endl<<"Value of outer count1="<<count1<<endl;
    
{
        
int count1=20;
        
int count2=30;
        cout
<<"value of inner count1="<<count1<<endl;
        count1
+=3;
        count3
+=count2;
    }

    cout
<<"Value of outer count1="<<count1<<endl;
    cout
<<"Value of outer count3="<<count3<<endl;
    
//cout<<count2<<endl;
}

// 最后的结果是,外层括号打出的count1不变,count3发生变化,注释部分代码如果有则报错,因为外层括号并不知道有count2

 全局变量代码 

 
// Ex2_08.cpp
// Demonstrating variable scope
#include  < iostream >
using  std::cout;
using  std::endl;
int  count1 = 100 ; // 此count1是全局变量
int  main()
{
    
int count1=10;//此count1是局部变量
    int count3=50;
    cout
<<endl
        
<<"Value of outer count1="<<count1
        
<<endl;
    cout
<<"Value of global count1="<<::count1
        
<<endl;
    
{
        
int count1=20;
        
int count2=30;
        cout
<<"Value of inner count1="<<count1
            
<<endl;
        cout
<<"Value of global count1="<<::count1
            
<<endl;
        count1
+=3;
        count3
+=count2;
    }

    cout
<<"Value of outer count1="<<count1
        
<<endl
        
<<"Value of outer count3="<<count3
        
<<endl;
    
return 0;
}

// 本例子需要注意::count1这样的全局变量
  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值