#include<iostream>#include<string>
using namespace std;intmain(){
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");return0;}
练习1.6
#include<iostream>#include<string>#include<vector>usingnamespace std;intmain(){int count =1;int test;int sum1 =0;int sum2 =0;
vector<int> vec;constint 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");return0;}
练习1.7
#include<iostream>#include<string>#include<vector>#include<fstream>#include<algorithm>usingnamespace std;intmain(){
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");return0;}
练习1.8
#include<iostream>#include<string>#include<cstdlib>usingnamespace std;intmain(){
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~4int num =rand()%4;switch(num){case1:
cout << arr[0]<< endl;break;case2:
cout << arr[1]<< endl;break;case3:
cout << arr[2]<< endl;break;case4:
cout << arr[4]<< endl;break;}system("pause");return0;}