task3
//Complex.hpp
#ifndef COMPLEX_HPP #define COMPLEX_HPP #include <iostream> #include <cmath> using namespace std; class Complex { public: Complex(double real0 = 0,double imag0 = 0):real{real0},imag{imag0}{}; Complex(const Complex &c):real{c.real},imag{c.imag}{}; double get_real() const{return real;} double get_imag() const{return imag;} 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(const Complex &c); private: double real; double imag; }; //输出复数 void Complex::show () const { if(imag < 0) { cout << real << " - " << fabs(imag) << "i \n" << endl; } else if( imag == 0) { cout << real << endl; } else { cout << real << " + " << imag << "i \n" << endl; } } //把一个复数加到自身 void Complex::add(const Complex &c) { real += c.real; imag += c.imag; } //友元函数add Complex add(const Complex &c1,const Complex &c2) { Complex c; c.real = c1.real + c2.real; c.imag = 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(const Complex &c) { return sqrt(pow(c.real,2) + pow(c.imag,2)); } #endif
//main.cpp
#include "Complex.hpp" #include <iostream> int main() { using namespace std; Complex c1(6, -8); const Complex c2(4.1); 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; }
(输出有点丑懒得改了
task4
//User.hpp
#ifndef USER_HPP #define USER_HPP #include<iostream> #include<string> using namespace std; class User{ public: User(string m,string p = "111111",string e = ""):name{m},password{p},email{e}{n++;} User(User &u):name{u.name},password{u.password},email{u.email}{n++;} void set_email(); void change_passwd(); void print_info(); static void print_n(){ cout << n << endl; } ~User() {n--;} private: string name; string password; string email; static int n;//记录用户总数 }; int User::n = 0; void User::set_email() { cout << "请输入邮箱 " << endl; ; cin>>email; } void User::change_passwd() { string p1,p2,p3;//第n次输入的密码 cout << "请输入旧密码" << endl; cin >> p1; string newpassword; if( p1 != password) { std::cout << "密码错误,重新输入" << std::endl; cin >> p2; if( p2 != password) { std::cout << "密码错误,重新输入" << std::endl; cin >> p3; if( p3 != password) { std::cout << "连续三次输入错误,请稍后再试" << std::endl; } else//第三次输入正确 { cout << "请输入新密码 " << endl;; cin >> newpassword; password = newpassword; } } else//第二次输入正确 { cout << "请输入新密码 " << endl ; cin >> newpassword; password = newpassword; } } else//第一次输入正确 { cout << "请输入新密码" << endl; ; cin >> newpassword; password = newpassword; } } void User::print_info() { std::cout << "name " << name << std::endl; std::cout << "password ******" << std::endl; std::cout << "email " << email << std::endl; } #endif
//main.cpp
#include "User.hpp"
#include <iostream>
int main()
{
using namespace std;
cout << "testing 1......" << endl;
User user1("Jiajia", "123456", "xyz@hotmail.com");
user1.print_info();
cout << endl
<< "testing 2......" << endl
<< endl;
User user2("MESSI");
user2.change_passwd();
user2.set_email();
user2.print_info();
User::print_n();
}
实验总结:
#include<sstream> #include<string> using namespace std; 这样命名空间std内定义的所有标识符都有效