一、实验内容
1、graph类内容补充
代码如下:
#ifndef GRAPH_H #define GRAPH_H // 类Graph的声明 class Graph { public: Graph(char ch, int n); // 带有参数的构造函数 void draw(); // 绘制图形 private: char symbol; int size; }; #endif
// 类graph的实现 #include "graph.h" #include <iostream> using namespace std; // 带参数的构造函数的实现 Graph::Graph(char ch, int n): symbol(ch), size(n) { } // 成员函数draw()的实现 // 功能:绘制size行,显示字符为symbol的指定图形样式 void Graph::draw() { int i, j, k; for (i = 1;i <= size;i++) { for (j = 1;j <= size - i;j++) cout << ' '; for (k = 1;k <= 2 * i - 1;k++) cout << symbol; cout << endl; } }
#include <iostream> #include "graph.h" using namespace std; int main() { Graph graph1('*',5); graph1.draw(); system("pause"); system("cls"); Graph graph2('$',7); graph2.draw(); return 0; }
效果如下:
2、分数类Fraction加减乘除及比较
#ifndef FRA_H #define FRA_H class Fraction { public: Fraction(int t0 = 0, int b0 = 1); friend Fraction add(Fraction &f1, Fraction &f2);//加法计算 friend Fraction sub(Fraction &f1, Fraction &f2);//减法计算 friend Fraction mul(Fraction &f1, Fraction &f2);//乘法计算 friend Fraction div(Fraction &f1, Fraction &f2); //除法计算 friend void com(Fraction &f1, Fraction &f2); //比较大小 void sim( ); //化简 void transform(); //转化 void input(); //输入 void output(); //输出 private: int top; int bottom; }; #endif
#include"Fraction.h" #include<iostream> #include<iomanip> #include<math.h> using std::cout; using std::cin; using std::endl; Fraction::Fraction(int t0, int b0):top(t0),bottom(b0){ if (b0 = 0) { cout << "worrong fraction!"; exit(0); } } Fraction add(Fraction &f1, Fraction &f2) { Fraction ad; ad.bottom = f1.bottom*f2.bottom; ad.top = f1.top*f2.bottom + f2.top*f1.bottom; ad.sim(); return ad; }//加法 Fraction sub(Fraction &f1, Fraction &f2) { Fraction su; su.bottom = f1.bottom*f2.bottom; su.top = f1.top*f2.bottom - f2.top*f1.bottom; su.sim(); return su; }//减法 Fraction mul(Fraction &f1, Fraction &f2) { Fraction mu; mu.bottom = f1.bottom*f2.bottom; mu.top = f1.top*f2.top; mu.sim(); return mu; }//乘法 Fraction div(Fraction &f1, Fraction &f2) { Fraction di; di.bottom = f1.bottom*f2.top; di.top = f1.top*f2.bottom; di.sim(); return di; }//除法 void com(Fraction &f1, Fraction &f2) { f1.sim(); f2.sim(); if (f1.top*f2.bottom > f1.bottom*f2.top) cout << f1.top << "/" << f1.bottom << " > " << f2.top << "/" << f2.bottom << endl; else if (f1.top*f2.bottom < f1.bottom*f2.top) cout << f1.top << "/" << f1.bottom << " < " << f2.top << "/" << f2.bottom << endl; else cout << f1.top << "/" << f1.bottom << " = " << f2.top << "/" << f2.bottom << endl; }//比较 void Fraction::sim() { int i; if (top != 0) { for (i = fabs(top);i >= 1;i--) { if (top%i == 0 && bottom%i == 0) break; } top /= i; bottom /= i; } if (bottom*top < 0) { top = -fabs(top); bottom = fabs(bottom); } else if (bottom*top > 0) { top = fabs(top); bottom = fabs(bottom); } }//化简 void Fraction::transform() { double p; p = top*1.0 / bottom; cout << std::setprecision(3) << p << endl; }//转化 void Fraction::input() { cin >> top >> bottom; }//输入 void Fraction::output() { if (top != 0) { if (bottom != 1) cout << top << "/" << bottom; else if (bottom == 1) cout << top; } else cout << "0"; }//输出
#include<iostream> #include"Fraction.h" using std::cout; using std::endl; int main() { Fraction a,b; Fraction c1, c2,c3,c4; cout << "Enter two Fraction:"; a.input(); b.input(); cout << "a= "; //输出a的分数和小数形式 a.output(); cout << "= "; a.transform(); cout << endl; cout << "b= "; //输出b的分数和小数形式 b.output(); cout << "= "; b.transform(); cout << endl; c1=add(a, b); //a,b相加 cout << "a+b= "; c1.output(); cout << endl; c2=sub(a, b); //a,b相减 cout << "a-b= "; c2.output(); cout << endl; c3=mul(a, b); //a,b相乘 cout << "a*b= "; c3.output(); cout << endl; c4=div(a, b); //a,b相除 cout << "a/b= "; c4.output(); cout << endl; com(a, b); //比较a,b大小 system("pause"); return 0; }
效果如下:
二、实验反思
- graph实验需要将图形转化为代数,发现行列间的规律。
- Fraction.h中需要考虑是否将函数设为友元函数,是否需要带参数,前前后后修改了很多次。
- Fraction.cpp中多处需要分类讨论,后来修改的时间用的比一开始的框架时间长,考虑要周全。
- 最后提出疑问,友元函数不能在主函数中引用吗?还是我使用的格式不对?