程序设计实验2
运行结果1
问题1:
不能运行,因为func函数未声明
问题2:
普通构造函数可以初始化,复制构造函数是对同类对象的引用,移动构造函数是对参数右值的引用,编译周期结束时,自动调用析构函数
问题3:
不能运行
实验任务2
1 //Complex.h 2 #pragma once 3 #include <string> 4 #include<iostream> 5 #include<cmath> 6 // 类Complex: 声明 7 class Complex { 8 public: 9 Complex(); 10 Complex(double r); 11 Complex(double r, double i); 12 Complex(const Complex& c); 13 double get_real()const; 14 double get_imag()const; 15 void add(const Complex& c); 16 //~Complex(); // 析构函数 17 // void output(const Complex& c); 18 void output()const; 19 //void Complex add(const Complex& c); 20 static const std::string doc; 21 friend Complex add(const Complex& c1, const Complex& c2); 22 friend bool is_equal(const Complex& c1, const Complex& c2); 23 friend bool is_not_equal(const Complex& c1, const Complex& c2); 24 friend double abs(const Complex& c); 25 //friend Complex output(const Complex& c); 26 friend std::ostream& operator<<(std::ostream& os, const Complex& c); 27 28 private: 29 double real; 30 double imag; 31 32 };
1 //Complex.cpp 2 #include "Complex.h" 3 // 普通函数实现 4 #include <iostream> 5 #include <string> 6 using std::cout; 7 using std::endl; 8 using std::string; 9 // static成员数据类外初始化 10 const std::string Complex::doc = " a simplified complex class"; 11 12 13 // 对象方法 14 Complex::Complex() :real(0), imag(0) {} 15 Complex::Complex(double r) :real(r), imag(0) {} 16 Complex::Complex(double r, double i) :real(r), imag(i) {} 17 Complex::Complex(const Complex& c) : real(c.real), imag(c.imag) {} 18 19 20 // 类方法 21 double Complex::get_real()const { 22 return real; 23 } 24 double Complex::get_imag() const { 25 return imag; 26 } 27 void Complex::add(const Complex& c) { 28 real += c.real; 29 imag += c.imag; 30 }//将复数加到自身 31 std::ostream& operator<<(std::ostream& os, const Complex& c) { 32 os << c.real << (c.imag >= 0 ? "+" : "") << c.imag << "i"; 33 return os; 34 }// 输出流重载 35 Complex add(const Complex& c1, const Complex& c2) { 36 return Complex(c1.real + c2.real, c1.imag + c2.imag); 37 }//加法友元 38 bool is_equal(const Complex& c1, const Complex& c2) { 39 if ((c1.real == c2.real) && (c1.imag == c2.imag)) 40 return true; 41 return false; 42 }//判等友元 43 bool is_not_equal(const Complex& c1, const Complex& c2) { 44 return !is_equal(c1, c2); 45 }//判不等友元 46 double abs(const Complex& c) { 47 return std::sqrt(c.real * c.real + c.imag * c.imag); 48 }//取模友元 49 void Complex::output() const { 50 if (imag >= 0) 51 std::cout << real << " + " << imag << "i" << std::endl; 52 else 53 std::cout << real << " - " << imag * (-1) << "i" << std::endl; 54 }//输出
1 //task2.cpp 2 #include "Complex.h" 3 #include <iostream> 4 5 using std::cout; 6 using std::endl; 7 using std::boolalpha; 8 9 void test() { 10 cout << "类成员测试: " << endl; 11 cout << Complex::doc << endl; 12 13 cout << endl; 14 15 cout << "Complex对象测试: " << endl; 16 Complex c1; 17 Complex c2(3, -4); 18 const Complex c3(3.5); 19 Complex c4(c3); 20 21 cout << "c1 = "; c1.output(); cout << endl; 22 cout << "c2 = "; c2.output(); cout << endl; 23 cout << "c3 = "; c3.output(); cout << endl; 24 cout << "c4 = "; c4.output(); cout << endl; 25 cout << "c4.real = " << c4.get_real() << ", c4.imag = " << c4.get_imag() << endl; 26 27 cout << endl; 28 29 cout << "复数运算测试: " << endl; 30 cout << "abs(c2) = " << abs(c2) << endl; 31 c1.add(c2); 32 cout << "c1 += c2, c1 = "; c1.output(); cout << endl; 33 cout << boolalpha; 34 cout << "c1 == c2 : " << is_equal(c1, c2) << endl; 35 cout << "c1 != c3 : " << is_not_equal(c1, c3) << endl; 36 c4 = add(c2, c3); 37 cout << "c4 = c2 + c3, c4 = "; c4.output(); cout << endl; 38 39 } 40 41 int main() { 42 test(); 43 44 }
运行结果2
实验任务3
提供Complex<返回值类型> Complex+名称+real/imag直接返回虚部实部的值,虚数运算c1+c2,判等直接比较
在虚数计算和判等时直接用+-代替而不是另外编写函数
实验任务4
1 #pragma once 2 #include<string> 3 4 class Fraction{ 5 public: 6 static const std::string doc; 7 Fraction(int up = 0, int down = 1); 8 Fraction(const Fraction& f); 9 int get_up()const; 10 int get_down()const; 11 int gcd(int m,int n)const; 12 Fraction negative()const; 13 friend Fraction add(const Fraction& f1, const Fraction& f2); 14 friend Fraction sub(const Fraction& f1,const Fraction& f2); 15 friend Fraction mul(const Fraction& f1,const Fraction& f2); 16 friend Fraction div(const Fraction& f1, const Fraction& f2); 17 friend void output(const Fraction& f); 18 void simplify(); 19 private: 20 int up, down; 21 int m, n; 22 };
1 #include "Fraction.h" 2 #include<iostream> 3 #include<string> 4 #include <numeric> 5 6 using std::cout; 7 using std::endl; 8 using std::string; 9 10 const std::string Fraction::doc = "Fraction类 v 0.01版.\n目前仅支持分数对象的构造、输出、加/减/乘/除运算."; 11 12 13 Fraction::Fraction(int u, int d) : up(u), down(d) { 14 simplify(); 15 } 16 17 Fraction::Fraction(const Fraction& f) :up(f.up), down(f.down) {} 18 19 int gcd(int a, int b) { 20 while (b != 0) { 21 int temp = b; 22 b = a % b; 23 a = temp; 24 } 25 return abs(a); // 返回绝对值 26 } 27 28 void Fraction::simplify() { 29 if (down < 0) { // 保证分母为正 30 up = -up; 31 down = -down; 32 } 33 int divisor = gcd(abs(up), abs(down)); 34 up /= divisor; 35 down /= divisor; 36 } 37 38 39 int Fraction::get_up()const { 40 return up; 41 } 42 int Fraction::get_down()const { 43 return down; 44 } 45 Fraction Fraction::negative()const { 46 return Fraction(-up, down); 47 } 48 49 //友元 50 void output(const Fraction& f) 51 { 52 if (f.get_up() == 0 && f.get_down() == 0) 53 printf("分母不能为0\n"); 54 else if (f.get_up() == 0 && f.get_down() != 0) 55 std::cout << "0" << std::endl; 56 else 57 std::cout << f.get_up() << "/" << f.get_down() << std::endl; 58 } 59 Fraction add(const Fraction& f1, const Fraction& f2) 60 { 61 return Fraction(f1.get_up() * f2.get_down() + f2.get_up() * f1.get_down(), f1.get_down() * f2.get_down()); 62 } 63 Fraction sub(const Fraction& f1, const Fraction& f2) 64 { 65 return Fraction(f1.get_up() * f2.get_down() - f2.get_up() * f1.get_down(), f1.get_down() * f2.get_down()); 66 } 67 Fraction mul(const Fraction& f1, const Fraction& f2) 68 { 69 return Fraction(f1.get_up() * f2.get_up(), f1.get_down() * f2.get_down()); 70 } 71 Fraction div(const Fraction& f1, const Fraction& f2) { 72 if (f2.get_up() == 0) { 73 //throw std::invalid_argument("除数不能为零"); 74 printf("分母不能为0\n"); 75 Fraction f(0, 0); 76 return f; 77 } 78 return Fraction(f1.get_up() * f2.get_down(), f1.get_down() * f2.get_up()); 79 }
1 #include "Fraction.h" 2 #include <iostream> 3 4 using std::cout; 5 6 using std::endl; 7 8 void test1() { 9 cout << "Fraction类测试: " << endl; 10 cout << Fraction::doc << endl << endl; 11 Fraction f1(5); 12 Fraction f2(3, -4), f3(-18, 12); 13 Fraction f4(f3); 14 cout << "f1 = "; output(f1); cout << endl; 15 cout << "f2 = "; output(f2); cout << endl; 16 cout << "f3 = "; output(f3); cout << endl; 17 cout << "f4 = "; output(f4); cout << endl; 18 Fraction f5(f4.negative()); 19 cout << "f5 = "; output(f5); cout << endl; 20 cout << "f5.get_up() = " << f5.get_up() << ", f5.get_down() = " << 21 22 f5.get_down() << endl; 23 cout << "f1 + f2 = "; output(add(f1, f2)); cout << endl; 24 cout << "f1 - f2 = "; output(sub(f1, f2)); cout << endl; 25 cout << "f1 * f2 = "; output(mul(f1, f2)); cout << endl; 26 cout << "f1 / f2 = "; output(div(f1, f2)); cout << endl; 27 cout << "f4 + f5 = "; output(add(f4, f5)); cout << endl; 28 } 29 30 void test2() { 31 Fraction f6(42, 55), f7(0, 3); 32 cout << "f6 = "; output(f6); cout << endl; 33 cout << "f7 = "; output(f7); cout << endl; 34 cout << "f6 / f7 = "; output(div(f6, f7)); cout << endl; 35 } 36 int main() { 37 cout << "测试1: Fraction类基础功能测试\n"; 38 test1(); 39 cout << "\n测试2: 分母为0测试: \n"; 40 test2(); 41 }
运行结果4
实验任务5
1 #pragma once 2 class SavingsAccount { 3 private: 4 int id; 5 double balance; 6 double rate; 7 int lastDate; 8 double accumulation; 9 static double total; 10 void record(int date, double amount); 11 double accumulate(int date)const { 12 return accumulation + balance * (date - lastDate); 13 } 14 public: 15 SavingsAccount(int date, int id, double rate); 16 int getId()const { return id; } 17 double getBalance()const { return balance;} 18 double getRate()const { return rate;} 19 static double getTotal() { return total; } 20 void deposit(int date, double amount); 21 void withdraw(int date, double amount); 22 void settle(int date); 23 void show()const; 24 };
1 #include"account.h" 2 #include<cmath> 3 #include<iostream> 4 using namespace std; 5 6 double SavingsAccount::total = 0; 7 SavingsAccount::SavingsAccount(int date, int id, double rate) : 8 id(id), balance(0), rate(rate), lastDate(date), accumulation(0) { 9 cout << date << "\t#" << id << "is created" << endl; 10 } 11 void SavingsAccount::record(int date, double amount) { 12 accumulation = accumulate(date); 13 lastDate = date; 14 amount = floor(amount * 100 + 0.5) / 100; 15 balance += amount; 16 total += amount; 17 cout << date << "\t#" << id << "\t" << amount << "\t" << balance << endl; 18 } 19 void SavingsAccount::deposit(int date, double amount) { 20 record(date, amount); 21 } 22 void SavingsAccount::withdraw(int date, double amount) { 23 if (amount > getBalance()) 24 cout << "Error:not enough money" << endl; 25 else 26 record(date, -amount); 27 } 28 void SavingsAccount::settle(int date) { 29 double interest = accumulate(date) * rate / 365; 30 if (interest != 0) 31 record(date, interest); 32 accumulation = 0; 33 } 34 void SavingsAccount::show()const { 35 cout << "#" << id << "\tBalance:" << balance; 36 }
1 #include"account.h" 2 #include<iostream> 3 using namespace std; 4 int main() 5 { 6 SavingsAccount sa0(1, 21325302, 0.015); 7 SavingsAccount sa1(1, 58320212, 0.015); 8 sa0.deposit(5, 5000); 9 sa1.deposit(25, 10000); 10 sa0.deposit(45, 5500); 11 sa1.withdraw(60,4000); 12 sa0.settle(90); 13 sa1.settle(90); 14 sa0.show(); cout << endl; 15 sa1.show(); cout << endl; 16 cout << "Total:" << SavingsAccount::getTotal() << endl; 17 return 0; 18 }
运行结果5