作者:ftwinkle | 来源:互联网 | 2023-10-11 11:48
实验4
complex.hpp
#pragma once
#include
#include
using namespace std;
class Complex
{
public:
Complex(double x0, double y);
Complex(const Complex& obj) : x{ obj.x }, y{ obj.y } {}
double get_real()const { return x; }
double get_imag()const { return y; }
void show()const;
void add(Complex obj);
friend Complex add(Complex c1, Complex c2);
friend bool is_equal(Complex c1, Complex c2);
friend double abs (Complex obj);
double x, y;
};
Complex::Complex(double x0 = 0.0, double y0 = 0.0)
{
x = x0;
y = y0;
}
void Complex::show() const {
if (y > 0)
{
cout <"+" <"i";
}
else if (y <0)
{
cout <"-" <"i";
}
else
{
cout << x;
}
}
void Complex::add(Complex obj) {
x += obj.x;
y += obj.y;
}
Complex add(Complex c1, Complex c2) {
Complex c3;
c3.x = c1.x + c2.x;
c3.y = c1.y + c2.y;
return c3;
}
bool is_equal(Complex c1, Complex c2)
{
if (c1.x == c2.x && c1.y == c2.y)
return true;
else
return false;
}
double abs( Complex obj )
{
double ab;
ab = sqrt(obj.x * obj.x + obj.y * obj.y);
return ab;
}task4.cpp
#include "Complex.hpp"
#include
void test() {
using namespace std;
Complex c1(1, -5);
const Complex c2(1.5);
Complex c3(c1);
cout <<"c1 = ";
c1.show();
cout << endl;
cout <<"c2 = ";
c2.show();
cout << endl;
cout <<"c2.imag = " < endl;
cout <<"c3 = ";
c3.show();
cout << endl;
cout <<"abs(c1) = ";
cout < endl;
cout << boolalpha;
cout <<"c1 == c3 : " < endl;
cout <<"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();
}实验五
user.hpp
#pragma once
#include
#include<string>
#include
using namespace std;
class User
{
public:
User(string name0, string password = "111111", string email0 = ""):name{name0}, passwd{ password }, email{ email0 }
{n++;}
void set_email();
void change_passwd();
void print_info();
static void print_n() { cout <<"there are" <"users"; }
private:
string name, passwd, email;
static int n;
};
int User::n = 0;
void User::set_email() {
cout <<"Enter email address :";
cin >> email;
cout <<"email is set successfully..." << endl;
}
void User::change_passwd() {
cout <<"Enter old passwd:";
int i;
for (i = 0; i <3; i++)
{
string b;
cin >> b;
if (b.compare(passwd) != 0)
{
if (i == 2)
cout <<"password input error.please try after a while." << endl;
else
cout <<"password input error.please re-enter again:";
}
else
{
cout <<"Enter new passwd:";
cin >> passwd;
cout <<"new passwd os set successfully...";
break;
}
}
}
void User::print_info()
{
cout <<"name:" <"passwd:";
string s1(passwd.length(), '*');
cout < endl;
cout <<"email:" < endl;
}task5.cpp
#include "User.hpp"
#include
void test() {
using std::cout;
using std::endl;
cout <<"testing 1......\n";
User user1("John", "114514", "qwe@hotmail.com");
user1.print_info();
cout << endl
<<"testing 2......\n\n";
User user2("Lion");
user2.change_passwd();
user2.set_email();
user2.print_info();
cout << endl;
User::print_n();
}
int main() {
test();
}