5.3:
#include <iostream>
using namespace std;
int main()
{
int sum = 0, val = 1;
while (val <= 10)sum += val, val++;
cout << sum << endl;
system("pause");
return 0;
}
5.5:
#include <iostream>
using namespace std;
#include<vector>
int main()
{
vector<string> scores = { "F","D","C","B","A","A++" };
cout << "请输入你的成绩: " << endl;
int m_score = 0;
cin >> m_score;
string level;
if (m_score < 60)
{
level = scores[0];
}
else
{
level = scores[(m_score - 50) / 10];
if (m_score != 100)
{
if (m_score % 10 <= 3)
{
level += "-";
}
else if (m_score % 10 >= 7)
{
level += "+";
}
}
}
cout << level << endl;
system("pause");
return 0;
}
5.6:
第一次修改:
#include <iostream>
using namespace std;
#include<vector>
int main()
{
vector<string> scores = { "F","D","C","B","A","A++" };
cout << "请输入你的成绩: " << endl;
int m_score = 0;
cin >> m_score;
string level;
if (m_score < 60)
{
level = scores[0];
}
else
{
level = scores[(m_score - 50) / 10];
if (m_score != 100)
{
level += m_score % 10 <= 3 ? "-" : m_score % 10 >= 7 ? "+" : "";
}
}
cout << level << endl;
system("pause");
return 0;
}
第二次修改:
#include <iostream>
using namespace std;
#include<vector>
int main()
{
vector<string> scores = { "F","D","C","B","A","A++" };
cout << "请输入你的成绩: " << endl;
int m_score = 0;
cin >> m_score;
string level;
level = (m_score < 60 ? scores[0] : scores[(m_score - 50) / 10]);
level += (m_score == 100 || m_score < 60) ? "" : m_score % 10 <= 3 ? "-" : m_score % 10 >= 7 ? "+" : "";
cout << level << endl;
system("pause");
return 0;
}
5.9:
#include <iostream>
using namespace std;
int main()
{
char c1;
cout << " 请输入一段英文字母: " << endl;
int num = 0;
while (cin >> c1)
{
if (c1 == 'a' || c1 == 'e' || c1 == 'i' || c1 == 'o' || c1 == 'u')
{
++num;
}
}
cout << "元音字母个数: " << num << endl;
system("pause");
return 0;
}
5.10:
#include <iostream>
using namespace std;
int main()
{
unsigned aCnt = 0, eCnt = 0, iCnt = 0, oCnt = 0, uCnt = 0;
char ch;
while (cin >> ch)
{
switch (ch)
{
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;
default:
break;
}
}
cout << "Number of vowel a: \t" << aCnt << '\n'
<< "Number of vowel e: \t" << eCnt << '\n'
<< "Number of vowel i: \t" << iCnt << '\n'
<< "Number of vowel o: \t" << oCnt << '\n'
<< "Number of vowel u: \t" << uCnt << endl;
system("pause");
return 0;
}
5.11:
#include <iostream>
using namespace std;
int main()
{
unsigned aCnt = 0, eCnt = 0, iCnt = 0, oCnt = 0, uCnt = 0,
spaceCnt = 0, tabCnt = 0, newlineCnt = 0;
char ch;
while (cin >> std::noskipws >> ch)
{
switch (ch)
{
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':
++tabCnt;
break;
case '\n':
++newlineCnt;
break;
default:
break;
}
}
cout << "Number of vowel a: \t" << aCnt << '\n'
<< "Number of vowel e: \t" << eCnt << '\n'
<< "Number of vowel i: \t" << iCnt << '\n'
<< "Number of vowel o: \t" << oCnt << '\n'
<< "Number of vowel u: \t" << uCnt << '\n'
<< "Number of vowel space: \t" << spaceCnt << '\n'
<< "Number of vowel table: \t" << tabCnt << '\n'
<< "Number of vowel enter: \t" << newlineCnt << endl;
system("pause");
return 0;
}
5.12:
#include <iostream>
using namespace std;
int main()
{
int ffCnt = 0, flCnt = 0, fiCnt = 0;
char ch, prech = '\0';
while (cin >> std::noskipws >> ch)
{
switch (ch)
{
case 'f':
if (prech == 'f')
{
++ffCnt;
}
break;
case 'i':
if (prech == 'f')
{
++fiCnt;
}
break;
case 'l':
if (prech == 'f')
{
++fiCnt;
}
break;
default:
break;
}
prech = ch;
}
cout << "Number of vowel ff: \t" << ffCnt << '\n'
<< "Number of vowel fl: \t" << flCnt << '\n'
<< "Number of vowel fi: \t" << fiCnt << endl;
system("pause");
return 0;
}
5.14:
第一稿,再看看题目我应该漏掉了一些细节QAQ
#include <iostream>
using namespace std;
int main()
{
cout << "请输入一组单词,并以Ctrl+z结束:" << endl;
string currVal, val;
if (cin >> currVal)
{
int cnt = 1;
while (cin >> val)
{
if (val == currVal)
{
cnt++;
}
else
{
cout << currVal << "重复了" << cnt << "次" << endl;
currVal = val;
cnt = 1;
}
}
cout << currVal << "重复了" << cnt << "次" << endl;
}
return 0;
}
5.17:
#include <iostream>
using namespace std;
#include<vector>
int main()
{
vector<int>v1 = { 0,1,1,2 ,3,5,8,9};
vector<int>v2 = { 0,1,1,2,3,5,8 };
if (v1.size() < v2.size())
{
auto it1 = v1.begin(), it2 = v2.begin();
for (; it1 != v1.end() && (*it1) == (*it2); ++it1, ++it2);
if (it1 == v1.end())
{
cout << "是前缀" << endl;
}
else
{
cout << "不是" << endl;
}
}
else
{
auto it1 = v2.begin(), it2 = v1.begin();
for (; it1 != v2.end() && (*it1) == (*it2); ++it1, ++it2);
if (it1 == v2.end())
{
cout << "是前缀" << endl;
}
else
{
cout << "不是" << endl;
}
}
return 0;
}
5.19:
#include <iostream>
using namespace std;
int main()
{
string str1, str2, str3, rsp;
do
{
cout << "请输入两个string对象:" << endl;
cin >> str1 >> str2;
str3 = (str1.size() > str2.size() ? str2 : str1);
cout << "较短的那个对象是: " << str3 << endl;
cout << "More? Enter yes or no: " << endl;
cin >> rsp;
} while (!rsp.empty() && rsp[0] != 'n');
return 0;
}
5.20:
#include <iostream>
using namespace std;
int main()
{
string str1, str2;
cin >> str1;
while (cin >> str2)
{
if (str2 == str1)
{
break;
}
else
{
str1 = str2;
}
}
if (cin.eof())//如果输入文件结束符
{
cout << "没有重复的" << endl;
}
else
{
cout << "重复的词语是: " << str1 << endl;
}
return 0;
}
5.21:
#include <iostream>
using namespace std;
int main()
{
string str1, str2;
cin >> str1;
while (cin >> str2)
{
if (str2 == str1)
{
if (!isupper(str2[0]))
{
continue;
}
break;
}
else
{
str1 = str2;
}
}
if (cin.eof())//如果输入文件结束符
{
cout << "没有重复的" << endl;
}
else
{
cout << "重复的词语是: " << str1 << endl;
}
return 0;
}
5.22:
for (int sz = get_size(); sz <=0; sz = get_size())
5.25:
#include <iostream>
using namespace std;
int main()
{
double a = 0, b = 0;
while (cin >> a >> b)
{
try{
if (b == 0)
{
throw runtime_error("除数不能为0!");
}
cout << "a / b = " << a / b << endl;
}
catch (runtime_error err)
{
cout << err.what() << "\nTry again? Enter y or n" << endl;
char c;
cin >> c;
if (!cin || c == 'n')
{
break;
}
}
}
return 0;
}