Liny_@NotePad

沉迷ACG中

《Essential C++》第一章练习

YOYO posted @ 2009年7月20日 05:20 in 【C/C++】 with tags Essential C++ 练习 , 2653 阅读

刚看完第一章~做做练习 = = 前4题偷懒不做,从第5题开始……

练习1.5:cstyle的char*和string

  1. #include<iostream>
  2. #include<cstring>
  3. using namespace std;
  4.  
  5. int main()
  6. {
  7.         char cname[15];
  8.  
  9.         cout<<"What's your name? : ";
  10.         cin>>cname;
  11.  
  12.         while(strlen(cname)<=2)
  13.         {
  14.                 cout<<"A name's length must > 2! please input your name again: ";
  15.                 cin>>cname;
  16.         }
  17.  
  18.         cout<<"Hello, "<<cname<<" !"<<endl;
  19.  
  20.         return 0;
  21. }
  1. #include<iostream>
  2. #include<string>
  3. using namespace std;
  4.  
  5. int main()
  6. {
  7.         string name;
  8.  
  9.         cout<<"What's your name? : ";
  10.         cin>>name;
  11.  
  12.         while(name.size() <= 2)
  13.         {
  14.                 cout<<"A name's length must > 2! please input your name again: ";
  15.                 cin>>name;
  16.         }
  17.  
  18.         cout<<"Hello, "<<name<<" !"<<endl;
  19.  
  20.         return 0;
  21. }

练习1.6:array和vector

  1. #include<iostream>
  2. using namespace std;
  3.  
  4. int main()
  5. {
  6.         const int N = 10;
  7.         int number[N];
  8.  
  9.         cout<<"please input "<<N<<" integers: ";
  10.        
  11.         int i = 0;
  12.         for(i=0; i<N; i++) cin>>number[i];
  13.  
  14.         int sum = 0;
  15.         for(i=0; i<N; i++)
  16.         {
  17.                 sum += number[i];
  18.         }
  19.         cout<<"The sum of these numbers is " << sum << endl;
  20.  
  21.         cout<<"The average of these numbers is "<< ((double)sum)/N <<endl;
  22.  
  23.         return 0;
  24. }
  1. #include<iostream>
  2. #include<vector>
  3. using namespace std;
  4.  
  5. int main()
  6. {
  7.         vector<int> vt;
  8.         int i;
  9.  
  10.         cout<<"please input integers: ";
  11.         while(cin>>i){
  12.                 if(i==-1)break;
  13.                 vt.insert(vt.end(), i);
  14.         }
  15.  
  16.         int sum = 0;
  17.         for(i=0; i<vt.size(); i++)
  18.                 sum += vt[i];
  19.         cout<<"The sum of these integers is " << sum << "." << endl;
  20.  
  21.         cout<<"The average of these integers is "<< ((double)sum/vt.size()) <<". "<< endl;
  22.  
  23.         return 0;
  24. }

练习1.7:偷懒 没有做到每个字读,只做了每行读

  1. #include<iostream>
  2. #include<fstream>
  3. #include<algorithm>
  4. #include<string>
  5. #include<vector>
  6. using namespace std;
  7.  
  8. int main()
  9. {
  10.         vector<string> content;
  11.         ifstream infile("ex_1_7_in.txt", ios_base::in);
  12.         ofstream outfile("ex_1_7_out.txt");
  13.  
  14.         if(!infile)
  15.         {
  16.                 cout<<"File Cannot Open!"<<endl;
  17.         }
  18.         else
  19.         {
  20.                 string line;
  21.                
  22.                 while(infile>>line)content.push_back(line+'\n');
  23.  
  24.                 vector<string>::iterator iter;
  25.                 for(iter = content.begin();iter!=content.end(); ++iter)
  26.                 {
  27.                         cout<<(*iter);
  28.                 }
  29.  
  30.                 sort(content.begin(), content.end());
  31.  
  32.                 for(iter = content.begin();iter!=content.end(); ++iter)
  33.                 {
  34.                         outfile<<(*iter);
  35.                 }
  36.         }
  37.  
  38.         return 0;
  39. }

练习1.8:偷懒,让用户自己输入错误次数 囧

  1. #include<iostream>
  2. #include<string>
  3. using namespace std;
  4.  
  5. int main()
  6. {
  7.         string msg[4] = {"You are great!", "Good job!", "25%", "0%"} ;
  8.        
  9.         int user_wrong_value;
  10.  
  11.         cout<<"How many the user wrongs..?(<4): ";
  12.         cin>>user_wrong_value;
  13.  
  14.         cout<<msg[user_wrong_value]<<endl;
  15.  
  16.         return 0;
  17. }

 

Avatar_small
Leek 说:
2009年11月28日 05:37

好好向你学习才是


登录 *


loading captcha image...
(输入验证码)
or Ctrl+Enter