<output id="qn6qe"></output>

    1. <output id="qn6qe"><tt id="qn6qe"></tt></output>
    2. <strike id="qn6qe"></strike>

      亚洲 日本 欧洲 欧美 视频,日韩中文字幕有码av,一本一道av中文字幕无码,国产线播放免费人成视频播放,人妻少妇偷人无码视频,日夜啪啪一区二区三区,国产尤物精品自在拍视频首页,久热这里只有精品12

      實驗1 現代C++編程初體驗

      實驗任務1

      程序源代碼:

      #include <iostream>
      #include <string>
      #include <vector>
      #include <algorithm>
      
      template<typename T>
      void output(const T& c);
      void test1();
      void test2();
      void test3();
      int main() {
      	std::cout << "測試1: \n";
      	test1();
      	std::cout << "\n測試2: \n";
      	test2();
      	std::cout << "\n測試3: \n";
      	test3();
      }
      template <typename T>
      void output(const T& c) {
      	for (auto& i : c)
      		std::cout << i << ' ';
      	std::cout << '\n';
      }
      void test1() {
      	using namespace std;
      	string s0{ "0123456789" };
      	cout << "s0 = " << s0 << endl;
      	string s1(s0);
      	reverse(s1.begin(), s1.end());
      	cout << "s1 = " << s1 << endl;
      	string s2(s0.size(), ' ');
      	reverse_copy(s0.begin(), s0.end(), s2.begin());
      	cout << "s2 = " << s2 << endl;
      }
      void test2() {
      	using namespace std;
      	vector<int> v0{ 2, 0, 4, 9 };
      	cout << "v0: "; output(v0);
      	vector<int> v1{ v0 };
      	reverse(v1.begin(), v1.end());
      	cout << "v1: "; output(v1);
      	vector<int> v2{ v0 };
      	reverse_copy(v0.begin(), v0.end(), v2.begin());
      	cout << "v2: "; output(v2);
      }
      void test3() {
      	using namespace std;
      	vector<int> v0{ 0, 1, 2, 3, 4, 5, 6, 7, 8, 9 };
      	cout << "v0: "; output(v0);
      	vector<int> v1{ v0 };
      	rotate(v1.begin(), v1.begin() + 1, v1.end());
      	cout << "v1: "; output(v1);
      	vector<int> v2{ v0 };
      	rotate(v2.begin(), v2.begin() + 2, v2.end());
      	cout << "v2: "; output(v2);
      	vector<int> v3{ v0 };
      	rotate(v3.begin(), v3.end() - 1, v3.end());
      	cout << "v3: "; output(v3);
      	vector<int> v4{ v0 };
      	rotate(v4.begin(), v4.end() - 2, v4.end());
      	cout << "v4: "; output(v4);
      }
      

      運行測試截圖:
      image

      回答問題
      問題1:reverse直接反轉原容器元素順序,reverse_copy是把原容器反轉后的元素拷貝到新容器。
      問題2:rotate通過將[first,middle)區間元素移到[middle,last)之后改變元素順序,三個參數代表待旋轉區間起始迭代器,新起始位置迭代器,區間末尾迭代器。

      實驗任務2

      程序源代碼:

      #include <iostream>
      #include <vector>
      #include <algorithm>
      #include <numeric>
      #include <iomanip>
      #include <cstdlib>
      #include <ctime>
      template<typename T>
      void output(const T& c);
      int generate_random_number();
      void test1();
      void test2();
      int main() {
      	std::srand(std::time(0)); 
      	std::cout << "測試1: \n";
      	test1();
      	std::cout << "\n測試2: \n";
      	test2();
      }
      
      template <typename T>
      void output(const T& c) {
      	for (auto& i : c)
      		std::cout << i << ' ';
      	std::cout << '\n';
      }
      
      int generate_random_number() {
      	return std::rand() % 101;
      }
      
      void test1() {
      	using namespace std;
      	vector<int> v0(10); 
      	generate(v0.begin(), v0.end(), generate_random_number); 
      	cout << "v0: "; output(v0);
      	vector<int> v1{ v0 };
      	sort(v1.begin(), v1.end());
      	cout << "v1: "; output(v1);
      	vector<int> v2{ v0 };
      	sort(v2.begin() + 1, v2.end() - 1); 
      	cout << "v2: "; output(v2);
      }
      
      void test2() {
      	using namespace std;
      	vector<int> v0(10);
      	generate(v0.begin(), v0.end(), generate_random_number);
      	cout << "v0: "; output(v0);
      
      	auto min_iter = min_element(v0.begin(), v0.end());
      	auto max_iter = max_element(v0.begin(), v0.end());
      	cout << "最小值: " << *min_iter << endl;
      	cout << "最大值: " << *max_iter << endl;
      
      	auto ans = minmax_element(v0.begin(), v0.end());
      	cout << "最小值: " << *(ans.first) << endl;
      	cout << "最大值: " << *(ans.second) << endl;
      
      	double avg1 = accumulate(v0.begin(), v0.end(), 0.0) / v0.size();
      	cout << "均值: " << fixed << setprecision(2) << avg1 << endl;
      	sort(v0.begin(), v0.end());
      	double avg2 = accumulate(v0.begin() + 1, v0.end() - 1, 0.0) / (v0.size() - 2);
      	cout << "去掉最大值、最小值之后,均值: " << avg2 << endl;
      }
      

      運行測試截圖:
      image

      回答問題
      問題1:generate按規則為容器元素生成新值以填充容器
      問題2:minmax_element一次遍歷同時找最大最小元素,效率更高,代碼更簡潔。
      問題3:lambda適臨時簡單邏輯,需捕獲局部變量,替代輕量級函數對象的場景。

      實驗任務3

      程序源代碼:

      #include <iostream>
      #include <string>
      #include <algorithm>
      #include <cctype>
      unsigned char func(unsigned char c);
      void test1();
      void test2();
      int main() {
      	std::cout << "測試1: 字符串大小寫轉換\n";
      	test1();
      	std::cout << "\n測試2: 字符變換\n";
      	test2();
      }
      unsigned char func(unsigned char c) {
      	if (c == 'z')
      		return 'a';
      
      	if (c == 'Z')
      		return 'A';
      
      	if (std::isalpha(c))
      		return static_cast<unsigned char>(c + 1);
      
      	return c;
      }
      void test1() {
      	std::string s1{ "Hello World 2049!" };
      	std::cout << "s1 = " << s1 << '\n';
      	std::string s2;
      	for (auto c : s1)
      		s2 += std::tolower(c);
      	std::cout << "s2 = " << s2 << '\n';
      	std::string s3;
      	for (auto c : s1)
      		s3 += std::toupper(c);
      	std::cout << "s3 = " << s3 << '\n';
      }
      void test2() {
      	std::string s1{ "I love cosmos!" };
      	std::cout << "s1 = " << s1 << '\n';
      
      	std::string s2(s1.size(), ' ');
      	std::transform(s1.begin(), s1.end(),
      		s2.begin(),
      		func);
      	std::cout << "s2 = " << s2 << '\n';
      }
      

      運行測試截圖:
      image
      回答問題
      問題1:將字母z轉換成a,字母Z轉換成A,其他字母轉換成下一個字母,非字母字符保持不變。
      問題2:tolower把大寫字母轉換成小寫,非大寫保持不變。toupper把小寫字母轉大寫,非小寫不變。
      問題3:四個參數依次是輸入起始,輸入結束,輸出起始,處理函數;改后會覆蓋輸入序列S1原有內容。

      實驗任務4

      程序源代碼:

      #include <iostream>
      #include <string>
      #include <algorithm>
      bool is_palindrome(const std::string& s);
      bool is_palindrome_ignore_case(const std::string& s);
      int main() {
      	using namespace std;
      	string s;
      	while (cin >> s) {
      		cout << boolalpha
      			<< "區分大小寫: " << is_palindrome(s) << "\n"
      			<< "不區分大小寫: " << is_palindrome_ignore_case(s) << "\n\n";
      	}
      }
      bool is_palindrome(const std::string& s)
      {
      	using namespace std;
      	string m=s;
      	reverse(m.begin(),m.end());
      	return m == s;
      }
      bool is_palindrome_ignore_case(const std::string& s)
      {
      	using namespace std;
      	string m,n;
      	for (auto c : s)
      	{
      		m += tolower(static_cast<unsigned char>(c));
      	}
      	n = m;
      	reverse(m.begin(), m.end());
      	return n == m;
      }
      

      運行測試截圖:
      image
      回答問題
      問題1:使用getline(cin,s)進行多組輸入

      實驗任務5

      程序源代碼:

      #include <iostream>
      #include <string>
      #include <algorithm>
      std::string dec2n(int x, int n = 2);
      int main() {
      	int x;
      	while (std::cin >> x) {
      		std::cout << "十進制: " << x << '\n'
      			<< "二進制: " << dec2n(x) << '\n'
      			<< "八進制: " << dec2n(x, 8) << '\n'
      			<< "十二進制: " << dec2n(x, 12) << '\n'
      			<< "十六進制: " << dec2n(x, 16) << '\n'
      			<< "三十二進制: " << dec2n(x, 32) << "\n\n";
      	}
      }
      std::string dec2n(int x, int n)
      {
      	std::string sum;
      	std::string chars = "0123456789ABCDEFGHIJKLMNOPQRSTUVWXYZ";
      	while (x)
      	{
      		sum.insert(0,1,chars[(x % n)]);
      		x /= n;
      	}
      	return sum;
      }
      

      運行測試截圖:
      image

      實驗任務6

      程序源代碼:

      #include <iostream>
      #include <string>
      #include <algorithm>
      #define N 26
      
      char func( char c,int n);
      int main() {
      	std::string x = "abcdefghijklmnopqrstuvwxyz";
      	int y[N];
      	for (int i = 0; i < N; i++)
      		y[i] = i + 1;
      	std::cout << "  ";
      	for (auto i : x)
      	{
      		std::cout << " " << i;
      	}
      	std::cout << std::endl;
      	for (auto j : y)
      	{
      		if (j < 10)
      			std::cout << " " << j;
      		else
      			std::cout << j;
      			for (auto i : x)
      			{
      				std::cout << " "<<static_cast<char>(toupper(func(i, j)));
      			}
      			std::cout << std::endl;
      	}
      	return 0;
      
      }
      char func(char c,int n)
      {
      	if (c >= 'a' && c <= 'z')
      	{
      		return 'a' + (c - 'a' + n) % 26;
      	}
      	if (c >= 'A' && c <= 'Z')
      	{
      		return 'A' + (c - 'A' + n) % 26;
      	}
      	return c;
      }
      

      運行測試截圖:
      image

      實驗任務7

      程序源代碼:

      #include <iostream>
      #include <string>
      #include <algorithm>
      #define N 10
      
      int main() {
      	using namespace std;
      	srand(static_cast<unsigned int>(time(0)));
      	string x = "+-*/";
      	int result[N+1];
      	int answer[N+1];
      	int num1 = 0,num2=0;
      	double rate = 0.0;
      	for (int i = 0; i < 10; i++)
      	{
      		string f;
      		f.insert(0, 1, x[rand() % 4]);
      		if (f == "-")
      		{
      			int b = rand() % 10;
      			int a = rand() % 10;
      			while (a < b)
      			{
      				a = rand() % 10;
      			}
      			cout << a << "-" << b << "=" ;
      			result[num1++] = a - b;
      			cin >> answer[num2++];
      		}
      		if (f == "/")
      		{
      			int a = rand() % 10;
      			int b = rand() % 10;
      			while (a == 0)
      			{
      				a = rand() % 10;
      			}
      			while (b == 0)
      			{
      				b = rand() % 10;
      			}
      			while (a % b != 0)
      			{
      				a = rand() % 10;
      				b = rand() % 10;
      			}
      			cout << a << "/" << b << "=" ;
      			result[num1++] = a / b;
      			cin >> answer[num2++];
      		}
      		if (f == "*")
      		{
      			int a = rand() % 10;
      			int b = rand() % 10;
      			cout << a << "*" << b << "=" ;
      			result[num1++] = a * b;
      			cin >> answer[num2++];
      		}
      		if (f == "+")
      		{
      			int a = rand() % 10;
      			int b = rand() % 10;
      			cout << a << "+" << b << "=" ;
      			result[num1++] = a + b;
      			cin >> answer[num2++];
      		}
      	}
      	for (int i = 0; i < 10; i++)
      	{
      		if (answer[i] == result[i])  rate+=1;
      	}
      	rate = rate * 10;
      	printf("正確率:%.2f%%\n", rate);
      	return 0;
      }
      

      運行測試截圖:
      image

      posted @ 2025-10-15 18:55  l栗l  閱讀(15)  評論(1)    收藏  舉報
      主站蜘蛛池模板: 国产成人精品18| 久久人妻国产精品| 亚洲欧美日韩综合一区在线| 精品乱人伦一区二区三区| 亚洲高清日韩专区精品| 亚洲精品动漫一区二区三| 国产SUV精品一区二区四| 久久发布国产伦子伦精品| 国产亚洲欧洲AⅤ综合一区| 国产成人精品亚洲高清在线| 国产熟女精品一区二区三区| gogogo在线播放中国| 抚顺县| 性色av无码不卡中文字幕| 日本熟妇乱一区二区三区| 图片区偷拍区小说区五月| 国产精品视频一区二区噜噜| 亚洲精品国产无套在线观| 日本一区二区久久人妻高清| 人人妻人人狠人人爽| 四虎永久精品免费视频| 久久亚洲中文无码咪咪爱| 精品国偷自产在线视频99| 91超碰在线精品| 一区二区三区不卡国产| 亚洲国产中文字幕在线视频综合| 午夜av高清在线观看| b站永久免费看片大全| 国产精品线在线精品| 色综合人人超人人超级国碰| 久久精品女人的天堂av| 晋州市| 伊人中文在线最新版天堂| 伊人久久大香线蕉AV网禁呦| 人妻av无码一区二区三区| 国产精品亚洲一区二区z| 成人亚洲一区二区三区在线| 国产成人av大片大片| 国产精品国三级国产av| 中国丰满少妇人妻xxx性董鑫洁 | 国产精品久久久久久久久久直播|