C++ 速查手冊 V1.00 - 單元 15 範例




u1501.cpp


#include <iostream>
#include <iomanip>
#include <ctime>

using namespace std;

int main() {
   time_t t = time(nullptr);
   cout << "UTC:   "
     << put_time(gmtime(&t), "%c %Z")
     << endl;
   cout << "Local: "
     << put_time(localtime(&t), "%c %Z")
     << endl;
}
 
/* Kaiching Chang 
   u1501.cpp
   2014-02 */

u1502.cpp


#include <iostream>
#include <string>

using namespace std;

int main() {
   string s = "";
   s += "123";
   cout << s
        << endl;
   s += "*()";
   cout << s
        << endl;

   int i = stoi(s);
   cout << i
        << endl;
}
 
/* Kaiching Chang 
   u1502.cpp
   2014-02 */

u1503.cpp


#include <iostream>
#include <vector>

using namespace std;

int main() {
   vector<int> v;

   v.push_back(1);
   v.push_back(2);
   v.push_back(3);
   v.push_back(4);
   v.push_back(5);

   for (int i = 0; i < v.size(); i++) {
      cout << v[i]
           << endl;
   }

   return 0;
}
 
/* Kaiching Chang 
   u1503.cpp
   2014-02 */

u1504.cpp


#include <iostream>
#include <vector>
#include <algorithm>

using namespace std;

int main() {
   vector<int> v = {12, 11, 5, -98, 22};

   cout << "Before: ";
   for (int i = 0; i < v.size(); i++) {
      cout << v[i]
           << ", ";
   }
   cout << endl;
   
   sort(v.begin(), v.end());

   cout << "After:  ";   
   for (int i = 0; i < v.size(); i++) {
      cout << v[i]
           << ", ";
   }
   cout << endl;

   return 0;
}
 
/* Kaiching Chang 
   u1504.cpp
   2014-02 */

u1505.cpp


#include <iostream>
#include <cmath>

using namespace std;

int main() {
   cout << abs(-33.22)
        << endl;
   
   cout << sqrt(16)
        << endl;

   cout << pow(11, 3)
        << endl;

   return 0;
}
 
/* Kaiching Chang 
   u1505.cpp
   2014-02 */

u1506.cpp


#include <iostream>

using namespace std;

int main() {
   int a;
   int sum = 0;
   
   cin >> a;
   sum += a;

   cin >> a;
   sum += a;

   cin >> a;
   sum += a;
 
   cout << "Sum: "
        << sum
        << endl;

   return 0;
}
 
/* Kaiching Chang 
   u1506.cpp
   2014-02 */

u1507.cpp


#include <iostream>
#include <string>
#include <regex>

using namespace std;
 
int main() {
   string s1 = "Hello world!";
   cout << s1
        << endl;

   regex r("Hello");
   string t = "Goodbye";
   string s2 = regex_replace(s1, r, t);

   cout << s2
        << endl;
}
 
/* Kaiching Chang 
   u1507.cpp
   2014-02 */

the end

沒有留言: