一、统计元音字母、空格、制表符、换行符个数
#include "stdafx.h"
#include <iostream>
using namespace std;
int main()
{
char cval;
int aCnt = 0, eCnt = 0, iCnt = 0, oCnt = 0, uCnt = 0, otherCnt = 0, spaceCnt = 0, tableCnt = 0, newlineCnt = 0;
while (cin >> std::noskipws >> cval) // 此处的std::noskipws表示的是不忽略任何地方的空白
{
switch (cval)
{
case 'a':
case 'A':
++aCnt;
break;
case 'e':
case 'E':
++eCnt;
break;
case 'i':
case 'I':
++iCnt;
break;
case 'o':
case 'O':
++oCnt;
break;
case 'u':
case 'U':
++uCnt;
break;
case ' ':
++spaceCnt;
break;
case '\t':
++tableCnt;
break;
case '\n':
++newlineCnt;
break;
}
}
cout << "元音字母a的个数为:" << aCnt << endl;
cout << "元音字母e的个数为:" << eCnt << endl;
cout << "元音字母i的个数为:" << iCnt << endl;
cout << "元音字母o的个数为:" << oCnt << endl;
cout << "元音字母u的个数为:" << uCnt << endl;
cout << "空格的个数为:" << spaceCnt << endl;
cout << "制表符的个数为:" << tableCnt << endl;
cout << "换行符的个数为:" << newlineCnt << endl;
}
二、C5–P166 5.14:记录连续重复出现次数最多的单词及出现次数
#include "stdafx.h"
#include <iostream>
#include<string>
#include<vector>
using namespace std;
int main()
{
string Nowstring, Beforestring;
vector<string>vecstring;
vector<int>vectime;
int repeatnum = 1, temp = 0, i = 0;
while (cin >> Nowstring)
{
if (Nowstring == Beforestring)
{
++repeatnum;
}
else
{
vectime.push_back(repeatnum);
repeatnum = 1;
Beforestring = Nowstring;
vecstring.push_back(Beforestring);
}
}
vectime.push_back(repeatnum);
int a = 0;
vector<int>::iterator it1 = vectime.begin();
for (it1; it1 != vectime.end(); ++it1)
{
if (*it1 > a)
a = *it1;
}
for (int i = 0; i <vectime.size(); i++)
{
if (vectime[i] == a)
{
cout << "单词" << vecstring[i - 1] << "出现的次数为: " << vectime[i] << endl;
}
}
}
三、C5 P168 5.17:检查其中一个对象是否为另一个对象的前缀,输出真或假;
#include "stdafx.h"
#include <iostream>
#include<string>
#include<vector>
using namespace std;
int main()
{
vector<int> vec1 = { 1,1,2,4 };
vector<int> vec2 = { 1,1,2,4,3,5,8 };
int size = vec1.size() < vec2.size() ? vec1.size() : vec2.size();
for (int i = 0; i < size; i++)
{
if (vec1[i] != vec2[i])
{
cout << "False!" << endl;
return 0;
}
}
cout << "True!" << endl;
return 0;
}
四、C5 P170 5.19:do while循环比较两个string的大小并输出较小的string
#include "stdafx.h"
#include <iostream>
#include<string>
#include<vector>
using namespace std;
int main()
{
string str1, str2,str3;
do {
cout << "Please enter two words to compare: ";
cin >> str1 >> str2;
str3 = str1.size() < str2.size() ? str1 : str2;
cout << "The short word is: " << str3 << endl;
} while (cin);
}
五、C5 P171 5.20
#include "stdafx.h"
#include <iostream>
#include<string>
#include<vector>
using namespace std;
int main()
{
string str1, str2;
do
{
cout << "Please enter the string: ";
cin >> str1;
if (str1 == str2)
{
cout << "重复出现的单词为:"<<str1 << endl;
break;
}
else
str2 = str1;
} while (cin);
}
六、C5 P196 6.22交换两个int指针
#include "stdafx.h"
#include <iostream>
#include <string>
using namespace std;
int swap(int* &a, int* &p)
{
int* temp=NULL;
temp = a;
a = p;
p = temp;
return 0;
}
int main()
{
int c = 3, d = 5;
int *g = &d;
int *j = &c;
cout << *g << *j << endl;
swap(j, g);
cout << *g << *j << endl;
}