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

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

2007-08-15 14:13 423 查看
有"/n",这段用的是全局变量

//Ex2_09.cpp
//Demonstration namespace names
#include <iostream>
int value=0;
int main()
{
std::cout<<"enter an integer:";
std::cin>>value;
std::cout<<"/nYou entered "<<value<<std::endl;
return 0;
}

有"/n",这段是关于命名空间的声明的

//Ex2_10.cpp
//Declaring a namespace 声明命名空间
#include <iostream>
namespace myStuff
{
int value=0;
}
int main()
{
std::cout<<"enter an integer:";
std::cin>>myStuff::value;
std::cout<<"/nYou entered "<<myStuff::value <<std::endl;
return 0;
}

//Ex2_11.cpp
//using a using directive
#include <iostream>
namespace myStuff
{
int value=0;
}
using namespace myStuff;
int main()
{
std::cout<<"enter an integer:";
std::cin>>value;
std::cout<<"/nYou entered "<<value;
std::cout<<std::endl;
return 0;
}

// Ex2_12_CLR.cpp: 主项目文件。

#include "stdafx.h"

using namespace System;

int main(array<System::String ^> ^args)
{
int apples,oranges;
int fruit;
apples=5;oranges=6;
fruit=apples+oranges;
Console::WriteLine(L"/nOranges are not the only fruit...");//“L”的含义指后面的字符是宽字符(每个字符,包括英文占2字节)
Console::Write(L" And we have ");//Write 写在同一行,WriteLine换行。
Console::Write(fruit);
Console::Write(L" fruits in all./n");
return 0;
}


//Ex2_13.cp:main project file.


//Calculating the price of a carpet


#include "stdafx.h"


using namespace System;


int main(array<System::String ^> ^args)




...{


double carpetPriceSqYd=27.95;


double roomWidth=13.5;


double roomLength=24.75;


const int feetPerYard=3;


double roomWidthYds=roomWidth/feetPerYard;


double roomLengthYds=roomLength/feetPerYard;


double carpetPrice=roomWidthYds*roomLengthYds*carpetPriceSqYd;


Console::WriteLine(L"Room size is {0:F2} yards by {1:F2} yards",roomLengthYds,roomWidthYds);


//{0:F2}中的0表示WriteLine的第一个参数的该位置由WriteLine的第二个参数来填补。F2表示输出形式(格式)是小数,且保留2位小数


Console::WriteLine(L"Room area is {0:F2} square yards",roomLengthYds*roomWidthYds);


Console::WriteLine(L"Carpet price is ${0:F2}",carpetPrice);


return 0;


}


//Ex2_14.cpp:main project file


//Defining and using a C++/CLI enumeration.枚举


#include "stdafx.h"


using namespace System;




//Define the enumeration at global scope


enum class Suit




...{


Clubs,Diamonds,Hearts,Spades


};


int main(array<System::String ^> ^args)




...{


Suit suit =Suit::Clubs;


int value=safe_cast<int>(suit);


Console::WriteLine(L"Suit is {0} and the values is {1}",suit,value);


suit=Suit::Diamonds;


value=safe_cast<int>(suit);


Console::WriteLine(L"Suit is {0} and the values is {1}",suit,value);


suit=Suit::Hearts;


value=safe_cast<int>(suit);


Console::WriteLine(L"Suit is {0} and the values is {1}",suit,value);


suit=Suit::Spades;


value=safe_cast<int>(suit);


Console::WriteLine(L"Suit is {0} and the values is {1}",suit,value);


return 0;


}
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: 
相关文章推荐