Visual C++ 2008入门经典 第三章判断和循环
// 第三章判断和循环 /* 3.1 比较数据值 3.1.1 if语句 //A-Z 是65-90 //a-z 97-122 //有一个将小写字母转换为大写的ISO/ANSI C++库函数 toupper() 位于标准文件<ctype>中 3.1.3 扩展的if语句 3.1.4 嵌套的if-else语句 3.1.5 逻辑运算符和表达式 && 逻辑与 || 逻辑或 ! 逻辑非 3.1.6 条件运算符 3.1.7 switch语句 3.1.8 无条件转移 3.2 重复执行语句块 3.2.1 循环的概念 3.2.2 for循环的变体 1. 使用continue 2. 浮点循环计数器 3.2.3 while循环 3.2.4 do - while循环 3.2.5 嵌套的循环 3.3 C++/CLI编程 */ //练习题一 //#include <iostream> //using namespace std; //int main() //{ //while循环 /*int value=0, sum=0, i=0; bool tmp=true; while(tmp==true) { cout<<"请输入你要计算的数值(值为0时结束):"; cin>>value; if(value == 0){ tmp=false; } sum +=value; i++; } cout<<"一共输入了"<<i<<"次,总数为:"<<sum<<endl;*/ /*int value=0, sum=0, i=0; bool tmp=true; for(;;){ cout<<"请输入你要计算的数值(值为0时结束):"; cin>>value; if(value == 0){ break; } sum +=value; i++; } cout<<"一共输入了"<<i<<"次,总数为:"<<sum<<endl;*/ /*int value=0, sum=0, i=0; bool tmp=true; do{ cout<<"请输入你要计算的数值(值为0时结束):"; cin>>value; if(value == 0){ tmp=false; }else{ sum +=value; i++; } }while(tmp==true); cout<<"一共输入了"<<i<<"次,总数为:"<<sum<<endl; return 0;*/ //} //练习题二 /*#include <iostream> using namespace std; int main() { char value=0; int number=0; for(;;) { cout<<"请输入字符进行计算(输入q退出):"; cin>>value; if(value=='q' || value == 'Q'){ break; } switch(value){ case 'a': case 'e': case 'i': case 'o': case 'u': number += 1; break; } } cout<<"一共输入了"<<number<<"次元音字母"<<endl; return 0; }*/ //练习题三 /*#include <iostream> #include <iomanip> using namespace std; int main() { //cout<<setw(17); for(int i=1; i<=12; i++){ if(i==1) cout<<setw(8)<<" "; cout<<setw(5)<<i; } cout<<endl; for(int i=2; i<=12; i++) { cout<<setw(5)<<i<<" | "<<setw(5); for(int j=1; j<=i; j++) { cout<<setw(5)<<i*j; } cout<<endl; } return 0; }*/ //练习题四 #include <iostream> using namespace std; //十六进制数 const int text = 0x01; //0000 0000 0000 0001 ===1 const int binary = 0x02; //0000 0000 0000 0002 ===2 const int read = 0x10; //0000 0000 0000 0010 ===16 const int write = 0x20; //0000 0000 0000 0020 ===32 const int append = 0x40; //0000 0000 0000 0040 ===64 int main() { int mode=0; for(int modeSelect =0; modeSelect <6; modeSelect++) { cout<<"modeSelect:"<<modeSelect<<endl; switch(modeSelect) { case 0: mode = text | read; //0000 0000 0000 0001 //0000 0000 0000 0010 //0000 0000 0000 0011 ==17 //cout<<"mode:"<<mode<<endl; break; case 1: mode = text | write; //0000 0000 0000 0001 //0000 0000 0000 0020 //0000 0000 0000 0021 == 33 //cout<<"mode:"<<mode<<endl; break; case 2: mode = text | append; //0000 0000 0000 0001 //0000 0000 0000 0040 //0000 0000 0000 0041 ==65 //cout<<"mode:"<<mode<<endl; break; case 3: mode = binary | read; //0000 0000 0000 0002 //0000 0000 0000 0010 //0000 0000 0000 0012 ==18 //cout<<"mode:"<<mode<<endl; break; case 4: mode = binary | write; //0000 0000 0000 0002 //0000 0000 0000 0020 //0000 0000 0000 0022 ==34 //cout<<"mode:"<<mode<<endl; break; case 5: mode = binary | append; //0000 0000 0000 0002 //0000 0000 0000 0040 //0000 0000 0000 0042 ==66 //cout<<"mode:"<<mode<<endl; break; } //确定被设定的模式,并输出它 //0000 0000 0000 0011 //0000 0000 0000 0001 //0000 0000 0000 0001 ok text //0000 0000 0000 0011 //0000 0000 0000 0012 //0000 0000 0000 0010 ok binary //=====这里又弄不错了样,不知道&咱算出来的,是这样吗? //弄不懂哦 //如果将16进制转换为32位的二进制然后进行 与操作,好像出现的是正确的 if(mode & text){ cout<<"mode is (text,"; }else if(mode & binary){ cout<<"mode is (binary,"; } cout<<endl; if(mode & read){ cout<<"read)"; }else if(mode & write){ cout<<"write)"; }else if(mode & append){ cout<<"append)"; } cout<<endl; } return 0; } //练习题五 -> //第五题 /*#include "stdafx.h" #include <iostream> using namespace System; int main(array<System::String ^> ^args) { ConsoleKeyInfo keypress; do{ keypress = Console::ReadKey(true); Console::Write(L"You Pressed "); if(safe_cast<int>(keypress.Modifiers) > 0) { Console::WriteLine(L" {0}, ", keypress.Modifiers); } Console::WriteLine(L"{0} which is the {0} character",keypress.Key, keypress.KeyChar); }while(keypress.Key != ConsoleKey::Escape); system("pause"); return 0; }*/ //第六题 #include "stdafx.h" #include <iostream> using namespace System; int main(array<System::String ^> ^args) { String^ str="Hello world Xlc Aeiou @@@"; int count=0, noLetter=0, Bletter=0, Sletter=0; for each(wchar_t ch in str) { //Console::Write(L"{0}",ch); //Char::IsLetter(ch) if(Char::IsLetter(ch)){ if(ch>='A' && ch<='Z'){ Bletter++; }else if(ch>='a' && ch<='z'){ Sletter++; } }else{ noLetter++; } count++; } Console::WriteLine(L"字符串长度为:{0} 大写字母为:{1} 小写字母为:{2} 其它字符为:{3}",count,Bletter,Sletter,noLetter); //cout<<"字符串长度为:"<<count<<" "<<Bletter<<" "<<Sletter<<" "<<noLetter<<endl; system("pause"); return 0; }