现代程序设计 homework-09
本次作业要求:
1. 了解Lambda的用法
计算“Hello World!”中
a.字母‘e’的个数
b. 字母‘l’的个数
2. 练习使用智能指针
打印“Hello World!”循环右移n位的结果
Example:
n = 1, output = “!Hello World”
n = 3, output = “ld!Hello Wor”
这次作业比较简单,也没有什么好说的,
第一个采用Lambda表达式确实比较方便,
只要这样一行代码就可以搞定
1 cnt=count_if(str.cbegin(),str.cend(),[=](char tempCh){return tempCh==ch;});
第二个关于数组的循环移位问题,<编程之美>中已经有不错的介绍,这里要求用智能指针实现,反而觉得有些画蛇添足的感觉
1 unique_ptr<string>p(new string(str)); 2 for(int i=0;i<k;i++) 3 { 4 char ch=(*p)[n-1]; 5 for(int j=n-1;j>0;j--) 6 { 7 (*p)[j]=(*p)[j-1]; 8 } 9 (*p)[0]=ch; 10 } 11 cout<<*p<<endl;
当然可以用O(n)的算法去实现循环移位
1 void MyRightShiftStr::RightShift() 2 { 3 k%=n; 4 ReverseStr(0,n-k-1); 5 ReverseStr(n-k,n-1); 6 ReverseStr(0,n-1); 7 } 8 void MyRightShiftStr::ReverseStr(int left,int right) 9 { 10 for( ;left<right;left++,right--) 11 { 12 char temp=str[right]; 13 str[right]=str[left]; 14 str[left]=temp; 15 } 16 }
封装好的完整代码如下:
1 // homework-09.cpp : 定义控制台应用程序的入口点。 2 // 3 4 #include "stdafx.h" 5 #include<iostream> 6 #include<cstdio> 7 #include<cstring> 8 #include<cstdlib> 9 #include<string> 10 #include<vector> 11 #include<memory> 12 #include<algorithm> 13 using namespace std; 14 class MyRightShiftStr 15 { 16 public: 17 MyRightShiftStr(); 18 ~MyRightShiftStr(); 19 bool GetString(); 20 bool GetK(); 21 void PrintResult(); 22 void ReverseStr(int left,int right); 23 void RightShift(); 24 void UptrShift(); 25 private: 26 string str; 27 int k; 28 int n; 29 }; 30 31 32 MyRightShiftStr::MyRightShiftStr() 33 { 34 } 35 36 MyRightShiftStr::~MyRightShiftStr() 37 { 38 } 39 bool MyRightShiftStr::GetString() 40 { 41 cout<<"Please input the str you want to RightShift: "<<endl; 42 try 43 { 44 if(getline(cin,str)==nullptr) 45 { 46 throw("Input Error"); 47 } 48 } 49 catch(char *ExceptionStr) 50 { 51 cout<<"Exception: "<<ExceptionStr<<endl; 52 return false; 53 } 54 n=str.length(); 55 } 56 bool MyRightShiftStr::GetK() 57 { 58 cout<<"Please input the K: "<<endl; 59 try 60 { 61 if(scanf_s("%d",&k)!=1) 62 { 63 throw("Input Error"); 64 } 65 } 66 catch(char *ExceptionStr) 67 { 68 cout<<"Exception: "<<ExceptionStr<<endl; 69 return false; 70 } 71 if(k<0) 72 { 73 k=n+k%n; 74 } 75 } 76 void MyRightShiftStr::UptrShift() 77 { 78 unique_ptr<string>p(new string(str)); 79 for(int i=0;i<k;i++) 80 { 81 char ch=(*p)[n-1]; 82 for(int j=n-1;j>0;j--) 83 { 84 (*p)[j]=(*p)[j-1]; 85 } 86 (*p)[0]=ch; 87 } 88 cout<<*p<<endl; 89 } 90 void MyRightShiftStr::RightShift() 91 { 92 k%=n; 93 ReverseStr(0,n-k-1); 94 ReverseStr(n-k,n-1); 95 ReverseStr(0,n-1); 96 } 97 void MyRightShiftStr::ReverseStr(int left,int right) 98 { 99 for( ;left<right;left++,right--) 100 { 101 char temp=str[right]; 102 str[right]=str[left]; 103 str[left]=temp; 104 } 105 } 106 void MyRightShiftStr::PrintResult() 107 { 108 cout<<"The str after RightShift: "<<endl; 109 cout<<str<<endl; 110 } 111 112 class MyCountStr 113 { 114 public: 115 MyCountStr(); 116 ~MyCountStr(); 117 bool GetString(); 118 bool GetChar(); 119 void CountLambda(); 120 121 private: 122 string str; 123 char ch; 124 int cnt; 125 }; 126 127 MyCountStr::MyCountStr() 128 { 129 } 130 131 MyCountStr::~MyCountStr() 132 { 133 } 134 135 bool MyCountStr::GetString() 136 { 137 cout<<"Please input the str you want to Count: "<<endl; 138 try 139 { 140 if(getline(cin,str)==nullptr) 141 { 142 throw("Input Error"); 143 } 144 } 145 catch(char *ExceptionStr) 146 { 147 cout<<"Exception: "<<ExceptionStr<<endl; 148 return false; 149 } 150 } 151 152 bool MyCountStr::GetChar() 153 { 154 cout<<"Please input the char you want to Count: "<<endl; 155 try 156 { 157 if((ch=getc(stdin))==NULL) 158 { 159 throw("Input Error"); 160 } 161 } 162 catch(char *ExceptionStr) 163 { 164 cout<<"Exception: "<<ExceptionStr<<endl; 165 return false; 166 } 167 } 168 169 void MyCountStr::CountLambda() 170 { 171 cnt=count_if(str.cbegin(),str.cend(),[=](char tempCh){return tempCh==ch;}); 172 cout<<"The num of "<<ch<<" is "<<cnt<<endl; 173 } 174 175 176 int _tmain(int argc, _TCHAR* argv[]) 177 { 178 while(true) 179 { 180 MyCountStr myCS=MyCountStr::MyCountStr(); 181 if(!myCS.GetString()) 182 { 183 continue; 184 } 185 if(!myCS.GetChar()) 186 { 187 continue; 188 } 189 myCS.CountLambda(); 190 myCS.~MyCountStr(); 191 break; 192 } 193 while(true) 194 { 195 MyRightShiftStr myRSS=MyRightShiftStr::MyRightShiftStr(); 196 if(!myRSS.GetString()) 197 { 198 continue; 199 } 200 if(!myRSS.GetK()) 201 { 202 continue; 203 } 204 myRSS.RightShift(); 205 myRSS.PrintResult(); 206 // myRSS.UptrShift(); 207 myRSS.~MyRightShiftStr(); 208 break; 209 } 210 return 0; 211 }
运行截图如下: