版权声明:瞎几把写 https://blog.csdn.net/weixin_43184615/article/details/82777945
练习1.5
#include <iostream>
#include <string>
using namespace std;
int main()
{
string name;
cout << "please enter your name with english:" ;
while (cin >> name)
{
if (name.size() <= 2){
cout << "please enter your name with english:";
continue;
}
else
cout << "welcome come back " << name << endl;
break;
}
system("pause");
return 0;
}
练习1.6
#include <iostream>
#include <string>
#include <vector>
using namespace std;
int main()
{
int count = 1;
int test;
int sum1 = 0;
int sum2 = 0;
vector<int> vec;
const int size = 10;
int arr[size];
for (int i = 0; i < size; ++i)
{
cout << "please enter the No. " << count << " number:";
cin >> test;
vec.push_back(test);
arr[i] = test;
count += 1;
}
for (int j = 0; j < size; ++j)
{
sum1 += arr[j];
sum2 += vec[j];
}
cout << "The number sum of array is " << sum1 << " and the number sum of array is "
<< sum2 << "and the average value is " << (sum1 / size) << endl;
system("pause");
return 0;
}
练习1.7
#include <iostream>
#include <string>
#include <vector>
#include <fstream>
#include <algorithm>
using namespace std;
int main()
{
ofstream outfile1("practice1_7.txt");
string str1;
cout << "please enter some string(enter 'over' to over) : " << endl;
int count = 0;
while (true)
{
cin >> str1;
if (str1=="over")
break;
outfile1 << str1 << endl;
count += 1;
}
ifstream infile("practice1_7.txt");
vector<string> vec;
string str2;
for(int i=0;i<count;++i)
{
infile >> str2;
vec.push_back(str2);
}
for (int i = 0; i < vec.size(); ++i)
cout <<"the str is "<< vec[i] << endl;
sort(vec.begin(), vec.end());
ofstream outfile2("practice1_7_sort.txt");
for (int i = 0; i < vec.size(); ++i)
outfile2 << vec[i] << endl;
system("pause");
return 0;
}
练习1.8
#include <iostream>
#include <string>
#include <cstdlib>
using namespace std;
int main()
{
string arr[4] = { "Your just answer fail once,go on and you will success!",
"This is your second answer fail,hold on!", "just go on!", "do you want guess again?" };
//假设已经得到答错的次数为随机1~4
int num = rand()%4;
switch (num)
{
case 1:
cout << arr[0] << endl;
break;
case 2:
cout << arr[1] << endl;
break;
case 3:
cout << arr[2] << endl;
break;
case 4:
cout << arr[4] << endl;
break;
}
system("pause");
return 0;
}