一、实验结论
1.实验任务3
#include "Complex.hpp" #include <iostream> int main() { using namespace std; Complex c1(5, -3); const Complex c2(2.8); Complex c3(c1); cout << "c1 = "; c1.show(); cout << endl; cout << "c2 = "; c2.show(); cout << endl; cout << "c2.imag = " << c2.get_imag() << endl; cout << "c3 = "; c3.show(); cout << endl; cout << "abs(c1) = "; cout << abs(c1) << endl; cout << boolalpha; cout << "c1 == c3 : " << is_equal(c1, c3) << endl; cout << "c1 == c2 : " << is_equal(c1, c2) << endl; Complex c4; c4 = add(c1, c2); cout << "c4 = c1 + c2 = "; c4.show(); cout << endl; c1.add(c2); cout << "c1 += c2, " << "c1 = "; c1.show(); cout << endl; }
#ifndef COMPLEX_HPP #define COMPLEX_HPP #include <iostream> #include <cmath> class Complex{ public: Complex():real(0),imag(0){} Complex(double r,double i=0){ real=r; imag=i; } Complex(const Complex &c){ real=c.real; imag=c.imag; } double get_real() const; double get_imag() const; void show() const; void add(const Complex &c); friend Complex add(const Complex &c1,const Complex &c2); friend bool is_equal(const Complex &c1,const Complex &c2); friend double abs(Complex &c1); ~Complex(){} private: double real,imag; }; double Complex::get_real() const{ return real; } double Complex::get_imag() const{ return imag; } void Complex::show() const{ using namespace std; if (imag>0) cout << real << " + " << imag << "i"; else if (imag<0) cout << real << " - " << -imag << "i"; else cout << real; } void Complex::add(const Complex &c){ real=real+c.get_real(); imag=imag+c.get_imag(); } Complex add(const Complex &c1,const Complex &c2){ Complex c(c1.real+c2.real,c1.imag+c2.imag); return c; } bool is_equal(const Complex &c1,const Complex &c2){ if(c1.real==c2.real && c1.imag==c2.imag){ return true; }else return false; } double abs(Complex &c){ return sqrt(c.real*c.real+c.imag*c.imag); } #endif
2.实验任务4
#include "User.hpp" #include <iostream> int main() { using namespace std; cout << "testing 1......" << endl; User user1("Jonny", "92197", "xyz@hotmail.com"); user1.print_info(); cout << endl << "testing 2......" << endl << endl; User user2("Leonard"); user2.change_passwd(); user2.set_email(); user2.print_info(); User::print_n(); }
#ifndef USER_HPP #define USER_HPP #include <iostream> #include <string> using namespace std; class User{ public: User(string nn,string p="111111",string e=""){ name = nn; passwd = p; email = e; n++; } void set_email(); void change_passwd(); void print_info(); static void print_n(){ cout << "there are " << n << " users" <<endl; } private: string name; string passwd; string email; static int n; }; void User::set_email(){ string a; cout << "Please input e_mail:"; cin >> a; if(a.find('@')!=string::npos){ cout << "Set e_mail success!" << endl; email = a; }else { cout << "e_mail is illegal!" << endl; } } void User::change_passwd(){ cout << "Please input your past password:"; int flag=0; for (int i=0;i<3;i++){ string a; cin >> a; if(a==passwd){ flag=1; break; }else if(i<2){ cout << "Error!Please re_input your past password:"; } } if(flag){ cout << "Passwd right!Please input your new password:"; cin >> passwd; cout << "Change success!" << endl; }else { cout << "Error!This window will close" <<endl; } } void User:: print_info(){ cout << "name: " << name <<endl; cout << "passwd: " << "******" << endl; cout << "email: " << email <<endl; } int User::n = 0; #endif

二、实验总结
1.string的相关操作中,find() 函数返回符合搜索条件的字符区间内的第一个字符的索引,没有找到目标就返回npos ,npos类似于size_type;
2.可以用 if(s.find("jia")==string::nops)) 来判断字符串中是否含有某子串;
3.(碎碎念) const 的用法真的好容易搞混。。。