实验一
complex.h
#include "complex.h" #include <iostream> int main() { using namespace std; complex c1(3, -4); const complex c2(4.5); 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; }
complex.cpp
//类的实现 #include "complex.h" #include<iostream> #include<math.h> using namespace std; complex::complex(double a) :real(a), imag(0) {} complex::complex(double a, double b) : real(a), imag(b) {} complex::complex() : real(0), imag(0) {} complex::complex(complex& c){ real = c.real; imag = c.imag; } void complex::show(){ if (imag > 0){ cout << real << '+' << imag << 'i'; }else if (imag < 0){ cout << real << imag << 'i'; }else { cout << real; } } void complex::show() const{ if (imag > 0)cout << real << '+' << imag << 'i'; else if (imag < 0)cout << real << imag << 'i'; else { cout << real; } } void complex::add(const complex& d){ real = real + d.real; imag = imag + d.imag; } complex add(complex& c1, const complex& c2){ complex cc; cc.real = c1.real + c2.real; cc.imag = c1.imag + c2.imag; return cc; } bool is_equal(complex& c3, complex& c4){ if ((c3.real == c4.real) && (c3.imag == c4.imag)) { return true; } else { return false; } } bool is_equal(complex& c3, const complex& c4){ if ((c3.real == c4.real) && (c3.imag == c4.imag)){ return true; } else{ return false; } } double abs(complex& c5){ return sqrt(c5.real * c5.real + c5.imag * c5.imag); }
task3
#include "complex.h" #include <iostream> int main() { using namespace std; complex c1(3, -4); const complex c2(4.5); 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; }

user.h
#pragma once #include<iostream> using namespace std; class User { public: User(string a, string b="1111111", string c="") :name(a), passwd(b), email(c) { n++; } void set_email(); void change_passwd(); void print_info(); static void print_n(); private: string name, passwd, email; static int n; };
user.cpp
#include<iostream> #include <string> #include"user.h" using namespace std; int User::n = 0; void User::set_email(){ string s; cout << "请输入邮箱" << endl; cin >> s; email = s; } void User::change_passwd(){ int n = 3; while (n--){ cout << "请输入旧密码" << endl; string s; cin >> s; if (s == passwd){ string t; cin >> t; email = t; break; } } } void User::print_info(){ cout << name << email << "******"; } void User::print_n(){ cout<<n<<endl; }
task4
#include "user.h" #include <iostream> void in() { 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(); }


浙公网安备 33010602011771号