task1
代码:
1 #include <iostream> 2 #include <complex> 3 int main() 4 { 5 using namespace std; 6 complex<double> c1{3, 4}, c2{4.5}; 7 const complex<double> c3{c2}; 8 cout << "c1 = " << c1 << endl; 9 cout << "c2 = " << c2 << endl; 10 cout << "c3 = " << c3 << endl; 11 cout << "c3.real = " << c3.real() << endl; 12 cout << "c3.imag = " << c3.imag() << endl; 13 cout << "c1 + c2 = " << c1 + c2 << endl; 14 cout << "c1 - c2 = " << c1 - c2 << endl; 15 cout << "abs(c1) = " << abs(c1) << endl; // abs()是标准库中数学函数,对复数进行取模 16 cout << boolalpha; // 设置bool型值以true/false方式输出 17 cout << "c1 == c2 : " << (c1 == c2) << endl; 18 cout << "c3 == c2 : " << (c3 == c2) << endl; 19 complex<double> c4 = 2; 20 cout << "c4 = " << c4 << endl; 21 c4 += c1; 22 cout << "c4 = " << c4 << endl; 23 }
测试结果:
task2
代码:
1 ing> 2 3 // 妯℃澘鍑芥暟 4 // 瀵规弧瓒崇壒瀹氭潯浠剁殑搴忓垪绫诲瀷T锛屼娇鐢ㄨ寖鍥磃or杈撳嚭 5 template<typename T> 6 void output1(const T &obj) { 7 for(auto i: obj) 8 std::cout << i << ", "; 9 std::cout << "\b\b \n"; 10 } 11 12 // 妯℃澘鍑芥暟 13 // 瀵规弧瓒崇壒瀹氭潯浠剁殑搴忓垪绫诲瀷T锛屼娇鐢ㄨ凯浠e櫒杈撳嚭 14 template<typename T> 15 void output2(const T &obj) { 16 for(auto p = obj.cbegin(); p != obj.cend(); ++p) 17 std::cout << *p << ", "; 18 std::cout << "\b\b \n"; 19 } 20 21 int main() { 22 using namespace std; 23 24 array<int, 5> x1; // 鍒涘缓涓€涓猘rray瀵硅薄锛屽寘鍚?涓猧nt绫诲瀷鍏冪礌锛屾湭鍒濆鍖? 25 cout << "x1.size() = " << x1.size() << endl; // 杈撳嚭鍏冪礌涓暟 26 x1.fill(42); // 灏唜1鐨勬墍鏈夊厓绱犲€奸兘璧嬪€间负42 27 x1.at(0) = 999; // 鎶婁笅鏍囦负0鐨勫厓绱犱慨鏀逛负999 28 x1.at(4) = -999; // 鎶婁笅鏍囦负4鐨勫厓绱犱慨鏀逛负-999 29 x1[1] = 777; // 鎶婁笅鏍囦负1鐨勫厓绱犱慨鏀逛负777 30 cout << "x1: "; 31 output1(x1); 32 cout << "x1: "; 33 output2(x1); 34 35 36 array<double, 10> x2{1.5, 2.2}; // 鍒涘缓瑕佺粰array瀵硅薄锛屽寘鍚?0涓猟ouble绫诲瀷鍏冪礌锛屽垵濮嬪寲閮ㄥ垎鍏冪礌 37 cout << "x2.size() = " << x2.size() << endl; 38 cout << "x2: "; 39 output1(x2); 40 41 array<int, 5> x3{x1}; 42 cout << boolalpha << (x1 == x3) << endl; 43 x3.fill(22); 44 cout << "x3: "; 45 output1(x3); 46 47 swap(x1, x3); // 浜ゆ崲x1鍜寈3鐨勬暟鎹? 48 cout << "x1: "; 49 output1(x1); 50 cout << "x3: "; 51 output1(x3); 52 }
测试结果:
task3
代码:
1 #include "Employee.hpp" 2 #include <iostream> 3 4 void test() { 5 using std::cout; 6 using std::endl; 7 8 cout << Employee::doc << endl << endl; 9 10 Employee employee1; 11 employee1.set_info("Sam", 30000, 2015, 1, 6); 12 employee1.update_hire_date(2017, 6, 30); 13 employee1.update_salary(35000); 14 employee1.display_info(); 15 cout << endl << endl; 16 17 Employee employee2{"Tony", 20000, 2020, 3, 16}; 18 employee2.raise_salary(15); // 鎻愭垚15% 19 employee2.display_info(); 20 cout << endl << endl; 21 22 Employee::display_count(); 23 } 24 25 int main() { 26 test(); 27 }
测试结果:
task4
代码:
1 #include<cmath> 2 class Complex 3 { 4 public: 5 Complex(){ 6 real=0; 7 imag=0; 8 } 9 Complex(float x){ 10 real=x; 11 imag=0; 12 } 13 Complex(float x,float y){ 14 real=x; 15 imag=y; 16 } 17 Complex(const Complex &xx){ 18 real=xx.get_real(); 19 imag=xx.get_imag(); 20 } 21 float get_real() const; 22 float get_imag() const; 23 void show() const; 24 friend Complex add (const Complex,const Complex); 25 friend bool is_equal (const Complex,const Complex); 26 friend float abs (const Complex); 27 private: 28 float real,imag; 29 } ; 30 Complex::float get_real() const{ 31 return real; 32 } 33 Complex::float get_imag() const{ 34 return imag; 35 } 36 Complex::void show() const{ 37 printf("%f+%fi\n",real,imag); 38 } 39 Complex::void add(Complex &xx){ 40 real+=xx.get_real(); 41 imag+=xx.get_imag(); 42 } 43 Complex add(Complex a,Complex b) 44 { 45 Complex c(a.get_real()+b.get_real(),a.get_imag()+b.get_imag()); 46 return c; 47 } 48 bool is_equal(Complex a,Complex b) 49 { 50 if(a.get_imag()==b.get_imag()&&a.get_real()==b.get_real()) 51 return true; 52 else return false; 53 } 54 float abs(Complex a) 55 { 56 return sqrt(a.get_real()*a.get_real()+a.get_imag()*a.get_imag()); 57 }
测试结果:
task5
代码:
1 #include<iostream> 2 #include<cstring> 3 using namespace std; 4 class User 5 { 6 public: 7 User(string a,string b="111111",string c="") 8 { 9 name=a; 10 password=b; 11 email=c; 12 n++; 13 } 14 ~User(){ 15 n--; 16 } 17 static int n; 18 void set_email() 19 { 20 printf("请输入邮箱:"); 21 cin>>email; 22 } 23 void change_passwd(){ 24 string f=""; 25 int t=0; 26 while(f!=password&&t<3) 27 { 28 printf("输入旧密码:"); 29 cin>>f; 30 t++; 31 } 32 if(t==3) printf("稍后再试"); 33 else 34 { 35 printf("输入新密码:"); 36 cin>>f; 37 password=f; 38 } 39 } 40 void print_info(){ 41 cout<<"用户名:"<<name<<endl; 42 cout<<"密码:"; 43 for(int i=0;i<password.length();i++) cout<<'*'; 44 cout<<endl; 45 cout<<"邮箱:"<<email; 46 } 47 static void print_n(){ 48 cout<<"n="<<n; 49 } 50 private: 51 string name,password,email; 52 } ; 53 int User::n=0;
测试结果:
标签:real,const,cout,get,对象,imag,Complex,实验 From: https://www.cnblogs.com/git-porn-hub/p/16840394.html