一、基本控制結(jié)構(gòu)
ISO/ANSI C++中的控制與循環(huán)全部適用于C++/CLI。下例展示了C++/CLI控制臺(tái)程序中的控制循環(huán):
例子:基本循環(huán)控制
- - - - - - - - - - - - - - - - <<== 華麗的分割線 ::開(kāi)始==>> [Ex3_15.cpp] - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
// Ex3_15.cpp : main project file.
#include "stdafx.h"
using namespace System;
int main(array<System::String ^> ^args)
{
wchar_t letter;
Console::Write(L"Enter a letter:");
letter = Console::Read();
if(letter>='A')
if(letter<='Z')
{
Console::WriteLine(L"You entered a captial letter.");
return 0;
}
if(letter>='a')
if(letter<='z')
{
Console::WriteLine(L"You entered a small letter.");
return 0;
}
Console::WriteLine(L"You did not enter a letter.");
return 0;
}
- - - - - - - - - - - - - - - - <<== 華麗的分割線 ::結(jié)束==>> [Ex3_15.cpp] - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
letter被聲明為wchar_t類型,映射為C++/CLI中的System::Char類型,它具有一些特殊的功能,其中包括將字符代碼轉(zhuǎn)換為大寫和小寫的函數(shù):Char::ToUpper()和Char::ToLower(),被轉(zhuǎn)換的函數(shù)作為參數(shù)被傳遞給它:
wchar_t uppercaseLetter = Char::ToUpper(letter);
此外還包括檢測(cè)字母是否大寫或小寫的函數(shù):IsUpper()和IsLower(),因此上例可改為
wchar_t letter;
wchar_t upper;
Console::Write(L"Enter a letter:");
letter = Console::Read();
upper = Char::ToUpper(letter);
if(upper>='A' && upper<='Z')
Console::WriteLine(L"You entered a {0} letter.", Char::IsUpper(letter) ? "Capital":"Small");
else
Console::WriteLine(L"You entered a small letter.");
Console::ReadKey()函數(shù)用于測(cè)試按鍵,并將結(jié)果存儲(chǔ)在ConsoleKeyInfo類對(duì)象里。該類有3個(gè)可訪問(wèn)屬性用于幫助確定被按下的鍵是哪個(gè)或哪些鍵。屬性Key識(shí)別被按下的鍵是哪個(gè),屬性KeyChar是被按鍵的Unicode字符碼,屬性Modifiers表示Shift,Alt,Ctrl鍵的按位組合,它是定義在System命名空間中的枚舉類型ConsoleModifiers的常量,包括Shift\Alt\Control。
應(yīng)該注意的是,在C++/CLI中枚舉常量在用作數(shù)值之前必須被顯示的強(qiáng)制轉(zhuǎn)換為值類型(整數(shù)類型)
例子:使用Console::ReadKey()函數(shù)
- - - - - - - - - - - - - - - - <<== 華麗的分割線 ::開(kāi)始==>> [Ex3_16.cpp] - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
// Ex3_16.cpp : main project file.
#include "stdafx.h"
using namespace System;
int main(array<System::String ^> ^args)
{
ConsoleKeyInfo keyPress;
Console::WriteLine(L"Press a key combination - press Escape to quit.");
do{
keyPress = Console::ReadKey(true);
Console::Write(L"You pressed");
if(safe_cast<int>(keyPress.Modifiers)>0)
Console::Write(L" {0}", keyPress.Modifiers);
Console::WriteLine(L" {0} which is the {1} character", keyPress.Key, keyPress.KeyChar);
}while(keyPress.Key != ConsoleKey::Escape);
return 0;
}
- - - - - - - - - - - - - - - - <<== 華麗的分割線 ::結(jié)束==>> [Ex3_16.cpp] - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
該程序的輸入示例如下:
Press a key combination - press Escape to quit.
You pressed Enter which is the character
You pressed Spacebar which is the character
You pressed Spacebar which is the character
從輸出中可以看出,當(dāng)不只一個(gè)鍵被按下時(shí),用一條語(yǔ)句就可以得到所有的鍵。這是因?yàn)镸odifiers枚舉類型是用FlagsAttribute屬性定義的,該屬性表明這種枚舉類型是一組唯一的位標(biāo)志。這使得該枚舉類型的變量可以由若干與在一起的標(biāo)志位組成,而Write()或WriteLine()函數(shù)可以識(shí)別并輸出各標(biāo)志位。
二、for each循環(huán)
以例子開(kāi)始
- - - - - - - - - - - - - - - - <<== 華麗的分割線 ::開(kāi)始==>> [Ex3_17.cpp] - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
// Ex3_17.cpp : main project file.
#include "stdafx.h"
using namespace System;
int main(array<System::String ^> ^args)
{
int volwels = 0;
int consonants = 0;
String^ proverb = L"A nod is as good as a wink to a blind horse.";
for each(wchar_t ch in proverb)
{
if(Char::IsLetter(ch))
{
ch = Char::ToLower(ch);
switch(ch)
{
case 'a': case 'e': case 'i': case 'o': case 'u':
++volwels;
break;
default:
++consonants;
break;
}
}
}
Console::WriteLine(proverb);
Console::WriteLine(L"The proverb contains {0} volwels and {1} consonants.",
volwels, consonants);
return 0;
}
- - - - - - - - - - - - - - - - <<== 華麗的分割線 ::結(jié)束==>> [Ex3_17.cpp] - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
輸出為:
A nod is as good as a wink to a blind horse.
The proverb contains 14 volwels and 18 consanants.
注意:由于proverb字符串中的字符都是Unicode字符,因此用wchar_t(映射為Char類型)類型的變量來(lái)存儲(chǔ)這些字符。變量ch為循環(huán)內(nèi)的局部變量。