实验四
#pragma once
#include <iostream>
#include <string>
#include <iomanip>
#include <cmath>
using std::cout;
using std::endl;
class Complex
{
public:
Complex();
Complex(double a0,double b0);
Complex(double a0);
Complex(Complex& other);
double get_real() const {return a;};
double get_imag() const;
void show() const;
Complex add(const Complex &c2);
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 a,b;
};
Complex::Complex(double a0,double b0)
{
a = a0;b = b0;
}
Complex::Complex()
{
a = 0;b = 0;
}
Complex::Complex(double a0)
{
a = a0;b = 0;
}
double Complex::get_imag()const
{
return b;
}
void Complex::show() const
{
if(b) cout << a << (b > 0 ? "+" : "-") << (b > 0 ? b :(-b) ) << "i" << endl;
else cout << a << endl;
}
Complex::Complex(Complex &other)
{
a = other.a;
b = other.b;
}
Complex Complex::add(const Complex &c2)
{
Complex c;
c.a += a + c2.a;
c.b += b + c2.b;
return c;
}
Complex add(const Complex &c1,const Complex &c2)
{
Complex c3;
c3.a = c1.a + c2.a;
c3.b = c1.b + c2.b;
return c3;
}
bool is_equal(const Complex &c1,const Complex &c2)
{
return (c1.a == c2.a) && (c1.b == c2.b);
}
double abs(const Complex &c)
{
return sqrt(c.a * c.a + c.b * c.b);
}
#include "Complex.hpp"
#include <iostream>
// 类测试
void test() {
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;
}
int main() {
test();
}
#include "User.hpp"
#include <iostream>
// 测试User类
void test() {
using std::cout;
using std::endl;
cout << "testing 1......\n";
User user1("Jonny", "92197", "[email protected]");
user1.printf_info();
cout << endl
<< "testing 2......\n\n";
User user2("Leonard");
user2.change_passwd();
user2.set_email();
user2.printf_info();
cout << endl;
User::print_n();
}
int main() {
test();
}
#pragma once
#include<iostream>
#include<string>
using namespace std;
class User
{
public:
User(string name0);
User(string name0,string password0,string email0);
void set_email();
void change_passwd();
void printf_info();
static int print_n();
private:
string name,password,email;
};
User::User(string name0)
{
name = name0;
password = "111111";
email = ' ';
}
User::User(string name0,string password0,string email0)
{
name = name0;
password = password0;
email = email0;
}
void User::set_email()
{
cout << "please enter your email address:" << endl;
cin >> email;
cout << "email is set sucessfully..." << endl;
}
void User::change_passwd()
{
cout << "Please Enter your old passord:";
int cnt = 3;
while(cnt --)
{
string s0;
cin >> s0;
if(s0 != password)
{
cout << (cnt == 1 ? "password input error.Please try again later" : "password input error.Please re-enter again!" ) << endl;
}
else
{
cout << "Enter new password:";
cin >> password;
cout << "new password is set successfully..." << endl;
break;
}}}
void User::printf_info()
{
string s1(password.length(),'*');
cout << "name:" << name << endl;
cout <<"password:" << s1 << endl;
cout << "Email:" << email << endl;
}
int User::print_n()
{
static int x = 1;
x ++;
cout << "there are " << x << " users." << endl;
return x;
}
标签:const,cout,Complex,实验,User,c2,c1 From: https://www.cnblogs.com/lxyyyds/p/16799798.html