Visual C++ 2008入门经典 第四章数组 字符串(练习题)
//练习题一: /*int arraySize = 5; //数组的长度 double* values = new double[arraySize]; //初始化一个values的指针,成员为五个 double* temp = 0; //定义一个零时指针 double inputValue = 0.0; int index = 0; for(;;) { cout<<"请输入数值(按0退出):"; cin>>inputValue; if(inputValue == 0.0){ break; } values[index] = inputValue; index++; if(index == arraySize) { arraySize += 5; temp = new double[arraySize]; for(int i=0; i<index; i++){ temp[i] = values[i]; } delete[] values; values = temp; temp = 0; } } cout<<"index:"<<index<<endl; for(int i=0; i < index; i++) { cout<<i<<":"<<values[i]<<" "; inputValue += values[i]; if(i % 5 == 0){ cout<<endl; } } cout<<"输入的总数为:"<<inputValue<<endl;*/ //第二题 /*int arraySize = 5; //数组的长度 double* values = new double[arraySize]; //初始化一个values的指针,成员为五个 double* temp = 0; //定义一个零时指针 double inputValue = 0.0; int index = 0; for(;;) { cout<<"请输入数值(按0退出):"; cin>>inputValue; if(inputValue == 0.0){ break; } *(values+index) = inputValue; //values[index] = inputValue; index++; if(index == arraySize) { arraySize += 5; temp = new double[arraySize]; for(int i=0; i<index; i++){ //temp[i] = values[i]; *(temp+i) = *(values+i); } delete[] values; values = temp; temp = 0; } } cout<<"index:"<<index<<endl; for(int i=0; i < index; i++) { cout<<i<<":"<<*(values+i)<<" "; inputValue += *(values+i); if((i+1) % 5 == 0){ cout<<endl; } } cout<<"输入的总数为:"<<inputValue<<endl;*/ //第三题 /*char ch[] = "Doctor Livingstone, I presume?"; cout<<"ch:"<<ch<<endl; for(int i=0; i<strlen(ch); i++) { if(ch[i] >='a' && ch[i] <='z'){ ch[i] -= 32; } } cout<<"ch:"<<ch<<endl;*/ //第四题 /*Random^ gandomAr = gcnew Random; array<int>^ values = gcnew array<int>(gandomAr->Next(10,20)); for(int i=0; i<values.Length; i++) { values[i] = gandomAr->Next(100,1000); } for(int j=0; j<values.Length; j++) { Console::WriteLine("{0}",values[j]); } //声明一下bool型的数组 array<bool>^ used = gcnew array<bool>(values->Length); Array::Clear(used, 0, values->Length); //将所有数组进行赋值操作,为false int minimum = Int32::MaxValue; //设置为最大可能值电流最小, 这句真还不错,Int32::MaxValue值吗? Console::WriteLine("Int32::MaxValue:{0}",Int32::MaxValue); //2147483647 int minIndex = 0; for(int count = 0; count<values->Length; count++) { for(int i=0; i<values->Length; i++) { //首先条件是值并没有输出过的情况下 //这是一个整的循环,是为了找出最小的值来 if(!used[i] && minimum > values[i]) //看看这个mininum值到底是多少 { minimum = values[i]; minIndex = i; //break; //刚才这里写了break所以不行的 } } used[minIndex] = true; Console::Write("{0,10}", minimum); if((count+1) % 5 == 0){ Console::WriteLine(); } minimum = Int32::MaxValue; }*/ //第五题 /*Random^ randomObj = gcnew Random; int value = randomObj->Next(100000,Int32::MaxValue-10000); Console::WriteLine("This is Values {0}",value); array<String^>^ digitWord ={L" Zero", L" one", L" two", L" three", L" four", L" five", L" six", L" seven", L" eight", L" nine"}; //Console::WriteLine(); String^ inWords = L""; while(value > 0) { inWords = digitWord[value % 10] + inWords; value = value / 10; } Console::WriteLine("this is value:{0}", inWords);*/ //第六题 array<String^>^ values = { L"\"Madam I'm Adam.\"", L"\"Don't cry for me, Marge and Tina.\"", L"\"Lid off a daffodil.\"", L"\"Red lost soldier.\"", L"\"Cigar? Toss it in a can. It is so tragic.\""}; /*for(int i=0; i<values->Length; i++) { Console::WriteLine(values[i]); }*/ String^ letters; bool isPalindrome; for each(String^ phrase in values) { letters = L""; //提取字符的小字 for each(wchar_t ch in phrase) { if(Char::IsLetter(ch)){ //判断是否为什么东东 letters += Char::ToLower(ch); //然后变成小写,保存到letters } } isPalindrome = true; //分两半来循环 for(int i=0; i<letters->Length/2; i++) { //取得当前i, 在取得与i对应的后半段值 if(letters[i] != letters[letters->Length - i - 1]) { isPalindrome = false; break; //为什么要停止,这个得把一半对比完才行呢 //还是说只对比一个值吗? //这里只对比一次两个值就成了 } } Console::WriteLine(L"\"" + phrase + L"\"" + (isPalindrome ? " is " : " is not")+L" a palindrome."); }