#pragma once #include <iostream> #include <iomanip> #include <cmath> using std::cout; using std::endl; using std::setfill; using std::setw; using std::left; using std::right; class Complex { public: Complex(double a = 0.0,double b = 0.0); Complex(const Complex &c); const double get_real const(){ return r; }; const double get_imag const(){ return i; }; void show() const; void add(complex c) const; friend Complex add(const Complex &c1, const Complex &c2); friend bool is_equal(const Complex &c1, const Complex &c2); friend double abs(const Complex c1); private: double r; double i; }; Complex::Complex(double a, double b) : r{a}, i{b} { } Complex::Complex(const Complex &c){ r = c.r; i = c.r; } void Complex::show() const { if(i>0) cout << r << " + " << i << "i" << endl; else if(i==0) cout << r << endl; else cout << r << " + " << -i << "i" << endl; } void Complex::add(const Complex c) const { r = r + c.get_real(); i = i + c.get_imag(); } Complex add(const Complex &c1, const Complex &c2){ Complex c; c.r = c1.r + c2.r; c.i = c1.ri+ c2.i; } bool is_equal(const Complex &c1, const Complex &c2){ if(c1.r == c2.r && c1.i == c2.i ) return true; else return false; } double abs(const Complex &c1){ double s = sqrt(c1.r * c1.r + c1.i * c1.i); return s; }
#include "实验二task4.complex.hpp" #include <iostream> //类测试 void test() { using namespace std; Complex c1(4, -4); Complex c2(8,10); 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; } int main() { test(); }
#pragma once #include <iostream> #include <string> using namespace std; class User{ public: User(string name0); User(string name0, string passwd0, string email0); void set_email(); void change_passwd(); void print_info(); static int print_n(); private: string name,passwd,email; }; User::User(string name0) : name{name0}, passwd{"111111"}, email{""} {}; User::User(string name0, string passwd0, string email0) : name{name0}, passwd{passwd0}, email{email0} {}; void User::set_email(){ cout << "Enter email adress:"; cin >> email; cout << "email sets successfully..." << endl; } void User::change_passwd(){ cout << "enter old password: "; int i=3; for(i;i>0;i--) { string pd; cin >> pd; if(pd == passwd){ cout << "Enter new password: "; cin >> pd; cout << "new password is set successfully..."; passwd = pd; break; } else { if(i>1) cout << "password input error.Please re-enter again: "; else cout << "password input error.Please try after a while."; } } cout << endl; } void User::print_info(){ int k = passwd.length() ; string s1(k,'*'); cout << "name: " << name << endl; cout << "password: " << s1 << endl; cout << "email: " << email << endl; } int User::print_n() { static int n=1; ++n; cout << "There are " << n << " users." << endl; return n; }
#include "实验二task5.User.hpp" #include <iostream> void test() { using std::cout; using std::endl; cout << "testing 1......\n"; User user1("Timi", "123789", "[email protected]"); user1.print_info(); cout << endl << "testing 2......\n\n"; User user2("Jackey"); user2.change_passwd(); user2.set_email(); user2.print_info(); cout << endl; User::print_n(); } int main() { test(); }
标签:std,const,cout,对象,Complex,实验,using,string From: https://www.cnblogs.com/djynb/p/16804581.html