首页 > 编程语言 >实验3 数组、指针与现代C++标准库

实验3 数组、指针与现代C++标准库

时间:2022-10-30 03:44:05浏览次数:41  
标签:std cout int C++ 数组 using include string 指针

task1

代码:

 1 #include <iostream>
 2 
 3 using std::cout;
 4 using std::endl;
 5 
 6 // 绫籄鐨勫畾涔?
 7 class A{
 8 public:
 9     A(int x0, int y0): x{ x0 }, y{ y0 } {}
10     void show() const { cout << x << ", " << y << endl; }
11 private:
12     int x, y;
13 };
14 
15 // 绫籅鐨勫畾涔?
16 class B{
17 public:
18     B(double x0, double y0): x{ x0 }, y{ y0 } {}
19     void show() const { cout << x << ", " << y << endl;}
20 private:
21     double x, y;
22 };
23 
24 int main() {
25     A a(3, 4);
26     a.show();
27 
28     B b(3.2, 5.6);
29     b.show();
30 }
 1 #include <iostream>
 2 #include <string>
 3 
 4 using std::cout;
 5 using std::endl;
 6 
 7 // 瀹氫箟绫绘ā鏉縓
 8 template<typename T>
 9 class X{
10 public:
11     X(T x0, T y0): x{x0}, y{y0} {}
12     void show() const { cout << x << ", " << y << endl;}
13 private:
14     T x, y;
15 };
16 
17 int main() {
18     X<int> x1(3, 4);
19     x1.show();
20 
21     X<double> x2(3.2, 5.6);
22     x2.show();
23 
24     X<std::string> x3("hello", "c plus plus");
25     x3.show();
26 }

 

测试结果:

 

 

 

 

 

 

task2

代码:

 1 #include <iostream>
 2 #include <string>
 3 
 4 int main() {
 5     using namespace std;
 6 
 7     string s1, s2;
 8     s1 = "nuist";                                 // 璧嬪€?
 9     s1[0] = 'N';                                  // 鏀寔閫氳繃[]鍜岀储寮曟柟寮忚闂?
10     s1.at(1) = 'U';                               // 鏀寔閫氳繃xx.at()鏂规硶璁块棶
11     cout << boolalpha << (s1 == "nuist") << endl; // 瀛楃涓叉瘮杈?
12     cout << s1.length() << endl;                  // 瀛楃涓查暱搴?
13     cout << s1.size() << endl;                    // 瀛楃涓查暱搴?
14     s2 = s1 + ", 2050";                           // 瀛楃涓茶繛鎺?
15     cout << s2 << endl;
16 
17     string email{"[email protected]"};
18     auto pos = email.find("@"); // 鏌ユ壘瀛愪覆"@"绗竴娆″嚭鐜扮殑绱㈠紩浣嶇疆锛屽鏋滃け璐ワ紝杩斿洖string::npos
19     if (pos == string::npos)
20         cout << "illegal email address";
21     else {
22         auto s1 = email.substr(0, pos);  // 鍙栧瓙涓? 浠庣储寮? ~ pos-1
23         auto s2 = email.substr(pos + 1); // 鍙栧瓙涓诧紝浠巔os+1鍒版湯灏?
24         cout << s1 << endl;
25         cout << s2 << endl;
26     }
27 
28     string phone{"15216982937"};
29     cout << phone.replace(3, 5, string(5, '*')) << endl; // 鎶婁粠绱㈠紩浣嶇疆涓?寮€濮嬬殑杩炵画5涓瓧绗︽浛鎹㈡垚*
30 
31     string s3{"cosmos"}, s4{"galaxy"};
32     cout << "s3: " + s3 + " s4: " + s4 << endl;
33     s3.swap(s4); // 浜ゆ崲
34     cout << "s3: " + s3 + " s4: " + s4 << endl;
35 
36     string s5{"abc"};
37     const char *pstr = s5.c_str(); // 鏂规硶c_str()鎶妔tring绫诲瓧绗︿覆缁勮浆鎹㈡垚C椋庢牸鐨勫瓧绗︿覆
38     cout << pstr << endl;
39 
40     string s6{"12306"};
41     int x1 = stoi(s6); // 鎶妔tring杞崲鎴恑nt
42     cout << x1 << endl;
43 
44     int x2 = 12306;
45     string s7 = to_string(x2);  // 鎶奿nt杞崲鎴恠tring
46     cout << s7 << endl;
47 
48     double x3 = 123.06;
49     string s8 = to_string(x3); // 鎶奷ouble杞崲鎴恠tring
50     cout << s8 << endl;
51 }
 1 #include <iostream>
 2 #include <string>
 3 #include <limits>
 4 
 5 int main() {
 6     using namespace std;
 7 
 8     const int n = 10;
 9     string prompt = string(n, '*') + "Enter a string: " + string(n, '*') + '\n';
10   
11     cout << prompt;
12     string s1;
13     cin >> s1;  // 浠庤緭鍏ユ祦涓彁鍙栧瓧绗︿覆缁檚1锛岀鍒扮┖鏍笺€佸洖杞︺€乀ab閿嵆缁撴潫
14     cout << "s1: " << s1 << endl;
15     cin.ignore(numeric_limits<streamsize>::max(), '\n');  // 娓呯┖杈撳叆缂撳啿鍖?
16 
17     cout << prompt;
18     getline(cin, s1);  // 浠庤緭鍏ユ祦涓彁鍙栦竴琛屽瓧绗︿覆缁檚1锛岀洿鍒版崲琛?
19     cout << "s1: " << s1 << endl;
20 
21     string s2, s3;
22     cout << prompt;
23     getline(cin, s2, ' ');  // 浠庤緭鍏ユ祦涓彁鍙栧瓧绗︿覆缁檚2锛岀洿鍒版寚瀹氬垎闅旂绌烘牸
24     getline(cin, s3);
25     cout << "s2: " << s2 << endl;
26     cout << "s3: " << s3 << endl;
27 }
 1 #include <iostream>
 2 #include <string>
 3 
 4 int main() {
 5     using namespace std;
 6 
 7     string s;
 8 
 9     cout << "Enter words: \n";
10     // 閲嶅褰曞叆瀛楃涓诧紝灏嗗皬鍐欏瓧绗﹁浆鎹㈡垚澶у啓锛岀洿鍒版寜涓婥trl+Z
11     while( getline(cin, s) ) {
12         for(auto &ch: s)
13             ch = toupper(ch);
14         cout << s << "\n";
15     }
16 }
 1 #include <iostream>
 2 #include <string>
 3 #include <algorithm>
 4 
 5 int main() {
 6     using namespace std;
 7 
 8     string s1;
 9 
10     cout << "Enter words: \n";
11     getline(cin, s1);
12     cout << "original words: \n";
13     cout << s1 <<endl;
14     cout << "to uppercase: \n";
15     transform(s1.begin(), s1.end(), s1.begin(), ::toupper);
16     cout << s1 << endl;
17 }

 

测试结果:

 

 

 

 

 

 

 

 

 

 

 

 

task3

代码:

 1 #include <iostream>
 2 #include <vector>
 3 
 4 
 5 template<typename T>
 6 void output(const T &obj) {
 7     for(auto &item: obj)
 8         std::cout << item << ", ";
 9     std::cout << "\b\b \n";
10 }
11 
12 int main() {
13     using namespace std;
14 
15     vector<int> v1;                // 鍒涘缓涓€涓獀ector瀵硅薄v1, 鏈寚瀹氬ぇ灏? 鍏冪礌鏄痠nt鍨? 鏈垵濮嬪寲
16     vector<int> v2(5);             // 鍒涘缓涓€涓獀ector瀵硅薄v2, 鍖呭惈5涓厓绱狅紝鍏冪礌鏄痠nt鍨嬶紝鍒濆鍊兼槸榛樿鍊?
17     vector<int> v3(5, 42);         // 鍒涘缓涓€涓獀ector瀵硅薄v3, 鍖呭惈5涓厓绱狅紝鍏冪礌鏄痠nt鍨嬶紝鎸囧畾鍒濆鍊兼槸42
18     vector<int> v4{1, 9, 8, 4}; // 鍒涘缓涓€涓獀ector瀵硅薄v4, 鍏冪礌鏄痠nt鍨嬶紝浣跨敤鍒濆鍖栧垪琛ㄦ柟寮?
19     vector<int> v5{v4};            // 鍒涘缓涓€涓獀ector瀵硅薄v5, 浣跨敤宸茬粡瀛樺湪鐨勫璞4鍒涘缓
20 
21     output(v2);
22     output(v3);
23     output(v4);
24     output(v5);
25 }
 1 #include <iostream>
 2 #include <vector>
 3 
 4 using std::cout;
 5 using std::endl;
 6 using std::vector;
 7 
 8 void output(const vector<int> &v) {
 9     cout << "v.size() = " << v.size() << endl;
10     cout << "v.capacity() = " << v.capacity() << endl;
11     cout << endl;
12 }
13 
14 int main() {
15     vector<int> v{42};
16     output(v);
17 
18     v.push_back(55);
19     v.push_back(90);
20     output(v);
21 
22     for(auto i = 0; i < 8; ++i)
23         v.push_back(i);
24     output(v);
25 
26     v.pop_back();
27     output(v);
28 }

 

测试结果:

 

 

 

 

 

 

task4

代码:

 1 #include <iostream>
 2 #include <vector>
 3 #include <array>
 4 #include <string>
 5 #include <algorithm>
 6 
 7 using namespace std;
 8 
 9 // 鍑芥暟妯℃澘
10 // 閫氳繃绱㈠紩鏂瑰紡杈撳嚭瀵硅薄鍊?
11 template<typename T>
12 void output1(const T &obj) {
13     for(auto i = 0; i < obj.size(); ++i)
14         cout << obj.at(i) << ", ";
15     cout << "\b\b \n";
16 }
17 
18 // 鍑芥暟妯℃澘
19 // 閫氳繃杩唬鍣ㄦ柟寮忚緭鍑哄璞″€?
20 template<typename T>
21 void output2(const T &obj) {
22     for(auto p = obj.cbegin(); p != obj.cend(); ++p)
23         cout << *p << ", ";
24     cout << "\b\b \n";
25 }
26 
27 // 鍑芥暟妯℃澘
28 // 閫氳繃auto for鏂瑰紡杈撳嚭瀵硅薄鍊?
29 template<typename T>
30 void output3(const T &obj) {
31     for(auto &item: obj) 
32         cout << item << ", ";
33     cout << "\b\b \n";
34 }
35 
36 // 娴嬭瘯string绫诲璞?
37 void test1() {
38     string s1{"cplus"};
39 
40     output1(s1);
41 
42     reverse(s1.begin(), s1.end());  // 瀵瑰璞1涓殑鏁版嵁椤硅繘琛岀炕杞?
43     output2(s1);
44 
45     sort(s1.begin(), s1.end());   // 瀵瑰璞1涓殑鏁版嵁椤规帓搴?榛樿鍗囧簭)
46     output3(s1);
47 }
48 
49 // 娴嬭瘯array<int>绫诲璞?
50 void test2() {
51     array< array<int, 4>, 3> x{1, 9, 8, 4, 2, 0, 2, 2, 2, 0, 4, 9 };
52 
53     output1( x.at(0) );
54     output2( x.at(1) );
55     output3( x.at(2) );
56 }
57 
58 // 娴嬭瘯vector<string>绫诲璞?
59 void test3() {
60     vector<string> v1 {"Sheldon", "Leonard", "Howard", "Raj"};
61 
62     v1.push_back("Penny");
63     v1.push_back("Amy");
64 
65     output1(v1);
66 
67     sort(v1.begin(), v1.end(), std::greater<string>());  // 瀵箆1瀵硅薄涓殑瀛楃涓叉寜闄嶅簭鎺掑簭
68     output2(v1);
69 
70     reverse(v1.begin(), v1.end());  // 瀵箆1瀵硅薄涓殑瀛楃涓茬炕杞?
71     output3(v1);
72 }
73 
74 int main() {
75     cout << "test1: " << endl;
76     test1();
77 
78     cout << "test2: " << endl;
79     test2();
80 
81     cout << "test3: " << endl;
82     test3();
83 }

 

测试结果:

 

 

 

task5

代码:

 1 #include "info.hpp"
 2 #include<iostream>
 3 #include<cstring>
 4 #include<vector>
 5 using namespace std;
 6 int main()
 7 {
 8     const int capacity=200;
 9     vector<Info> audience_info_list;
10     string a,b,c;
11     int d;
12     int t=0;
13     while((cin>>a>>b>>c>>d)&&d!=0&&t<=capacity)
14     {
15         t+=d;
16         audience_info_list.push_back(Info(a,b,c,d));
17     }
18     for(int i=0;i<audience_info_list.size();i++)
19     audience_info_list[i].print();
20     while(cin>>a>>b>>c>>d)
21     {
22         if(d+t>capacity)
23         {
24             cout<<"位置不够"<<endl;
25         }
26         else
27         {
28             cout<<"位置够"<<endl;
29         }
30         t+=d;
31         char q=' ';
32             cout<<"输入q退出u更新"<<endl;
33             cin>>q;
34             if(q=='q') break;
35             if(q=='u'&&t<=capacity)  audience_info_list.push_back(Info(a,b,c,d));
36     }
37     for(int i=0;i<audience_info_list.size();i++)
38     audience_info_list[i].print();
39     return 0;
40 }
 1 #include<cstring>
 2 #include<iostream>
 3 using namespace std;
 4 class Info
 5 {
 6     public:
 7         Info(string a="",string b="",string c="",int d=0)
 8         {
 9             nickname=a;
10             contact=b;
11             city=c;
12             n=d;
13         }
14         void print()
15         {
16             cout<<"昵称:"<<nickname<<endl;
17             cout<<"联系方式:"<<contact<<endl;
18             cout<<"所在城市:"<<city<<endl; 
19             cout<<"预定人数:"<<n<<endl;
20         }
21     private:
22         string nickname,contact,city;
23         int n;
24  } ;

 

测试结果:

 

 

 

task6

代码:

#include "textcoder.hpp"
#include <iostream>
#include <string>

void test() {
    using namespace std;

    string text, encoded_text, decoded_text;

    cout << "输入: ";
    while (getline(cin, text)) {
        encoded_text = TextCoder(text).get_ciphertext();  // 杩欓噷浣跨敤鐨勬槸涓存椂鏃犲悕瀵硅薄
        cout << "加密\t" << encoded_text << endl;

        decoded_text = TextCoder(encoded_text).get_deciphertext(); // 杩欓噷浣跨敤鐨勬槸涓存椂鏃犲悕瀵硅薄
        cout << "解密\t" << decoded_text << endl;
        cout << "输入: ";
    }
}

int main() {  
    test(); 
}
 1 #include<cstring>
 2 #include<iostream>
 3 using namespace std;
 4 class TextCoder
 5 {
 6     public:
 7         TextCoder(string a)
 8         {
 9             text=a;
10         }
11         string get_ciphertext(){
12             encoder();
13             return text;
14         }
15         string get_deciphertext(){
16             decoder();
17             return text;
18         }
19     private:
20         void encoder()
21         {
22             for(int i=0;i<text.size();i++)
23             if((text[i]>='a'&&text[i]<='z')||(text[i]>='A'&&text[i]<='Z'))
24             {
25                 text[i]+=5;
26                 if(text[i]>'z'||(text[i]<'a'&&text[i]>'Z')) text[i]-=26;
27             }
28         }
29         void decoder()
30         {
31             for(int i=0;i<text.size();i++)
32             if((text[i]>='a'&&text[i]<='z')||(text[i]>='A'&&text[i]<='Z'))
33             {
34                 text[i]-=5;
35                 if(text[i]<'A'||(text[i]<'a'&&text[i]>'Z')) text[i]+=26;
36             }
37         }
38         string text;
39  }; 

 

测试结果:

 

标签:std,cout,int,C++,数组,using,include,string,指针
From: https://www.cnblogs.com/git-porn-hub/p/16840409.html

相关文章

  • c++,真有趣啊
    由于笔者的水平不太行,在这个贴里记录一些自己犯过的不太容易被发现的错误20221029基类classCBase{public:virtual~CBase(){}private:virtualbool__......
  • C++11 unistring 类(编码转换)
    C++11 的编码转换程序: #ifndefUNISTRING_HPP#defineUNISTRING_HPP#include<algorithm>#include<codecvt>#include<cstdio>#include<cstdarg>#include<i......
  • java 二维数组
    publicclassArrayTwo{publicstaticvoidmain(String[]args){int[][]arr={{1,2,3},{11,223,44}};......
  • jhava 数组的Arrys工具
    importjava.util.Arrays;publicclassCopyDemo{publicstaticvoidmain(String[]args){int[]arr={10,20,30,40,50,60,70,80};System.ou......
  • C++ Primer Plus学习笔记之复合类型(上)
    前言个人觉得学习编程最有效的方法是阅读专业的书籍,通过阅读专业书籍可以构建更加系统化的知识体系。一直以来都很想深入学习一下C++,将其作为自己的主力开发语言。现在为......
  • C++求高精度pi(1)BBP公式
    C++求高精度pi(1)前言(之后再写)BBP公式由arctan1展开得到的莱布尼茨级数是一个交错级数,并且条件收敛而不绝对收敛,这注定了莱布尼兹级数方法会非常低效而BBP公式$$\sum......
  • C++ STL
    概述STL主要有container,algorithm和iterator三大部分构成容器用于存放数据对象算法用于操作容器中的数据对象迭代器是算法和容器之间的中介STL容器STL容器是一种数据结构......
  • 907. 子数组的最小值之和 : 常规「单调栈 + 数学」运用题
    题目描述这是LeetCode上的​​908.最小差值I​​,难度为中等。Tag:「数学」、「单调栈」给定一个整数数组​​arr​​​,找到​​min(b)​​​ 的总和,其中​​b......
  • 1822. 数组元素积的符号 : 简单模拟题
    题目描述这是LeetCode上的​​1822.数组元素积的符号​​,难度为简单。Tag:「模拟」已知函数 ​​signFunc(x)​​​将会根据​​x​​的正负返回特定值:如果​......
  • 862. 和至少为 K 的最短子数组 : 前缀和 + 离散化 + 权值树状数组
    题目描述这是LeetCode上的​​863.二叉树中所有距离为K的结点​​,难度为困难。Tag:「前缀和」、「离散化」、「二分」、「树状数组」给你一个整数数组​​nums​......