博客
关于我
强烈建议你试试无所不能的chatGPT,快点击我
实验三 类和对象
阅读量:7123 次
发布时间:2019-06-28

本文共 4717 字,大约阅读时间需要 15 分钟。

一、实验内容

 

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.h
// 类graph的实现 #include "graph.h" #include 
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; }}
graph.cpp
#include 
#include "graph.h"using namespace std;int main() { Graph graph1('*',5); graph1.draw(); system("pause"); system("cls"); Graph graph2('$',7); graph2.draw(); return 0; }
main.cpp

 效果如下:

              

 

 

2、分数类Fraction加减乘除及比较

#ifndef FRA_H#define FRA_Hclass 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
Fraction.h
#include"Fraction.h"#include
#include
#include
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";}//输出
Fraction.cpp
#include
#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;}
main.cpp

效果如下:

 

 

二、实验反思

 

  • graph实验需要将图形转化为代数,发现行列间的规律。
  • Fraction.h中需要考虑是否将函数设为友元函数,是否需要带参数,前前后后修改了很多次。
  • Fraction.cpp中多处需要分类讨论,后来修改的时间用的比一开始的框架时间长,考虑要周全。
  • 最后提出疑问,友元函数不能在主函数中引用吗?还是我使用的格式不对?

 

转载于:https://www.cnblogs.com/zuiyankh/p/10737504.html

你可能感兴趣的文章
检查mysql错误日志并发邮件通知
查看>>
【linux+C】通过几个实例温习指针
查看>>
华为刀片网卡漂移问题
查看>>
搜索专题:Balloons
查看>>
使用shell脚本采集系统cpu、内存、磁盘、网络等信息
查看>>
数据包的分类和调度-Linux TC的另一种解释
查看>>
Nginx服务器平滑升级
查看>>
yum [Errno 256] No more mirrors to try 解决方法
查看>>
第 3 章 Keystone - 018 - 理解 Keystone 核心概念
查看>>
Top 10 Methods for Java Arrays
查看>>
Ex2010-14 Access Ex2013 ECP/OWA in a co-existence scenario
查看>>
Linux命令详解 -- iptables
查看>>
IOS开发知识(四)
查看>>
张涵20160401作业
查看>>
(GeoTrust 企业(OV)型 增强版(EV) SSL证书
查看>>
CentOS 7 用户账户配置
查看>>
PHP
查看>>
常用Python数据分析库详解
查看>>
Java之品优购课程讲义_day14(2)
查看>>
python数据结构与算法(8)
查看>>