5.5
#include <iostream>
using namespace std;
int main()
{
double score;
string grade = "FEDCBA";
char letterGrade;
while (cin >> score)
{
if (score < 60)
letterGrade = grade[0];
else
{
letterGrade = grade[(score - 50) / 10];
}
cout << letterGrade << endl;
}
return 0;
}
5.6
#include <iostream>
using namespace std;
int main()
{
double score;
string grade = "FEDCBA";
char letterGrade;
while (cin >> score)
{
letterGrade = (score < 60) ? grade[0] : (grade[(score - 50) / 10]);
cout << letterGrade << endl;
}
return 0;
}
5.9
#include <iostream>
using namespace std;
int main()
{
char c;
int aCount = 0, eCount = 0, iCount = 0, oCount = 0, uCount = 0;
while (cin >> c)
{
if (c == 'a')
++aCount;
else if (c == 'e')
++eCount;
else if (c == 'i')
++iCount;
else if (c == 'o')
++oCount;
else if (c == 'u')
++uCount;
}
cout << "aCount = " << aCount << endl;
cout << "eCount = " << eCount << endl;
cout << "iCount = " << iCount << endl;
cout << "oCount = " << oCount << endl;
cout << "uCount = " << uCount << endl;
return 0;
}
5.10
#include <iostream>
using namespace std;
int main()
{
char c;
int aCount = 0, eCount = 0, iCount = 0, oCount = 0, uCount = 0;
while (cin >> c)
{
if (c == 'a' || c == 'A')
++aCount;
else if (c == 'e' || c == 'E')
++eCount;
else if (c == 'i' || c == 'I')
++iCount;
else if (c == 'o' || c == 'O')
++oCount;
else if (c == 'u' || c == 'U')
++uCount;
}
cout << "aCount = " << aCount << endl;
cout << "eCount = " << eCount << endl;
cout << "iCount = " << iCount << endl;
cout << "oCount = " << oCount << endl;
cout << "uCount = " << uCount << endl;
return 0;
}
5.11
#include <iostream>
using namespace std;
int main()
{
char c;
int aCount = 0, eCount = 0, iCount = 0, oCount = 0, uCount = 0;
int spaceCount = 0, tabCount = 0, newLineCount = 0;
while (cin >> noskipws >> c)
{
if (c == 'a' || c == 'A')
++aCount;
else if (c == 'e' || c == 'E')
++eCount;
else if (c == 'i' || c == 'I')
++iCount;
else if (c == 'o' || c == 'O')
++oCount;
else if (c == 'u' || c == 'U')
++uCount;
else if (c == ' ')
++spaceCount;
else if (c == '\t')
++tabCount;
else if (c == '\n')
++newLineCount;
}
cout << "aCount = " << aCount << endl;
cout << "eCount = " << eCount << endl;
cout << "iCount = " << iCount << endl;
cout << "oCount = " << oCount << endl;
cout << "uCount = " << uCount << endl;
cout << "spaceCount = " << spaceCount << endl;
cout << "tabCount = " << tabCount << endl;
cout << "newLineCount = " << newLineCount << endl;
return 0;
}
5.12
#include <iostream>
using namespace std;
int main()
{
char c;
int aCount = 0, eCount = 0, iCount = 0, oCount = 0, uCount = 0;
int spaceCount = 0, tabCount = 0, newLineCount = 0;
int ffCount = 0, flCount = 0, fiCount = 0;
char prec = '\0';
while (cin >> noskipws >> c)
{
if (c == 'a' || c == 'A')
++aCount;
else if (c == 'e' || c == 'E')
++eCount;
else if (c == 'i')
if (prec == 'f')
++fiCount;
else
++iCount;
else if (c == 'I')
++iCount;
else if (c == 'o' || c == 'O')
++oCount;
else if (c == 'u' || c == 'U')
++uCount;
else if (c == ' ')
++spaceCount;
else if (c == '\t')
++tabCount;
else if (c == '\n')
++newLineCount;
else if (c == 'f')
{
if (prec == 'f')
++ffCount;
}
else if (c == 'l')
if (prec == 'f')
++flCount;
prec = c;
}
cout << "aCount = " << aCount << endl;
cout << "eCount = " << eCount << endl;
cout << "iCount = " << iCount << endl;
cout << "oCount = " << oCount << endl;
cout << "uCount = " << uCount << endl;
cout << "spaceCount = " << spaceCount << endl;
cout << "tabCount = " << tabCount << endl;
cout << "newLineCount = " << newLineCount << endl;
cout << "ffCount = " << ffCount << endl;
cout << "flCount = " << flCount << endl;
cout << "fiCount = " << fiCount << endl;
return 0;
}
5.14
#include <iostream>
#include <string>
using namespace std;
int main()
{
string str, preStr;
int count = 1;
int maxCount = 1;
string maxStr;
while (cin >> str)
{
if (str == preStr)
++count;
else
count = 1;
if (count > maxCount)
{
maxCount = count;
maxStr = str;
}
preStr = str;
}
if (maxCount == 1)
cout << "任何单词都没有连续出现过" << endl;
else
cout << "单词" << maxStr << "连续出现了" << maxCount << "次" << endl;
}
5.17
#include <iostream>
#include <vector>
using namespace std;
bool isPrefix(vector<int>& v1, vector<int>& v2)
{
if (v1.size() > v2.size())
isPrefix(v2, v1);
for (int i = 0; i < v1.size(); ++i)
{
if (v1[i] != v2[i])
return false;
}
return true;
}
int main()
{
vector<int> ivec1 = { 0, 1, 1, 2 };
vector<int> ivec2 = { 0, 1, 1, 2, 3, 5, 8 };
bool flag = isPrefix(ivec1, ivec2);
cout << (flag ? "yes" : "no") << endl;
return 0;
}
5.19
#include <iostream>
#include <string>
using namespace std;
int main()
{
string s1, s2;
do
{
cout << "请输入两个string对象:" << endl;
cin >> s1 >> s2;
if (s1.size() < s2.size())
cout << s1 << endl;
else
cout << s2 << endl;
} while (1);
return 0;
}
5.20
#include <iostream>
#include <string>
using namespace std;
int main()
{
string str, preStr;
bool flag = false;
while (cin >> str && !str.empty())
{
if (str == preStr)
{
flag = true;
break;
}
else
preStr = str;
}
if (flag)
cout << "重复出现的单词为:" << preStr << endl;
else
cout << "没有任何单词是重复出现的" << endl;
return 0;
}
5.21
#include <iostream>
#include <string>
using namespace std;
int main()
{
string str, preStr;
bool flag = false;
while (cin >> str && !str.empty())
{
if (str == preStr && isupper(str[0]))
{
flag = true;
break;
}
else
preStr = str;
}
if (flag)
cout << "重复出现的以大写字母开头的单词为:" << preStr << endl;
else
cout << "没有任何大写字母开头的单词是重复出现的" << endl;
return 0;
}
5.23
#include <iostream>
using namespace std;
int main()
{
int i, j;
cin >> i >> j;
cout << i / j << endl;
return 0;
}
5.24
#include <iostream>
using namespace std;
int main()
{
int i, j;
cin >> i >> j;
if (j == 0)
throw runtime_error("除数不能为0");
cout << i / j << endl;
return 0;
}
5.25
#include <iostream>
using namespace std;
int main()
{
int i, j;
while (cout << "请输入两个整数:" << endl, cin >> i >> j)
{
try
{
if (j == 0)
throw runtime_error("除数不能为0");
cout << i / j << endl;
}
catch (runtime_error err)
{
cout << err.what() << "\nTry again ? Enter y or n" << endl;
char c;
cin >> c;
if (c == 'n')
break;
}
}
return 0;
}
|