实 验 3:

part 1:验证

part 2:graph

#include <iostream>
#include "graph.h"
using namespace std; int main() {
Graph graph1('*',);
graph1.draw(); system("pause");
system("cls"); Graph graph2('$',);
graph2.draw();
system("pause"); return ;
}

main.cpp

// 类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() {
for(int i=;i<=size;i++)
{for(int j=;j<=size-i;j++)
cout<<" ";
for(int k=;k<*i-;k++)
cout<<symbol;
cout<<endl;
}
}

graph.cpp

#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

运行结果:

part 3:分数的加减乘除

#ifndef Fraction_H
#define Fraction_H
// 类Fraction的声明
class Fraction {
public:
Fraction(int t=, int b=):top(t),bottom(b){
}// 带有参数的构造函数
void add(Fraction a, Fraction b);
void sub(Fraction a, Fraction b);
void mul(Fraction a, Fraction b);
void div(Fraction a, Fraction b);
void compare(Fraction a, Fraction b);
void show();
private:
int top;
int bottom;
}; #endif

fraction.h

#include <iostream>
#include "fraction.h"
using namespace std;
int main()
{int x,y,m,n;
Fraction a;
a.show();
Fraction b(,);
b.show();
Fraction c();
c.show();
cout<<"Input two score: "<<endl;
cin>>x>>y;
cin>>m>>n;
while(y==||n==)
{cout<<"ERROR!Please try again!"<<endl;
cin>>x>>y;
cin>>m>>n;
}
Fraction d(x,y);
Fraction e(m,n);
a.add(d,e);
a.sub(d,e);
a.mul(d,e);
a.div(d,e);
a.compare(d,e);
return ;
}

main.cpp

#include"fraction.h"
#include<iostream>
using namespace std;
void Fraction::show(){
if (top == ) cout << <<endl;
else if (bottom == ) cout << top << endl;
else cout << top << "/" << bottom << endl;
}
void Fraction::add(Fraction a, Fraction b){
int gbs,m=a.bottom,n=b.bottom,t,r,fz,fm;
if(m<n)
{t=m;m=n;n=t;}
r=m%n;
while(r!=)
{m=n;n=r;r=m%n;}
gbs=a.bottom*b.bottom/n;
fz=a.top*(gbs/a.bottom)+b.top*(gbs/b.bottom);
fm=gbs;
cout<<"add: "<<fz<<"/"<<fm<<endl;
}
void Fraction::sub(Fraction a, Fraction b){
int gbs,m=a.bottom,n=b.bottom,t,r,fz,fm;
if(m<n)
{t=m;m=n;n=t;}
r=m%n;
while(r!=)
{m=n;n=r;r=m%n;}
gbs=a.bottom*b.bottom/n;
fz=a.top*(gbs/a.bottom)-b.top*(gbs/b.bottom);
fm=gbs;
cout<<"sub: "<<fz<<"/"<<fm<<endl;
}
void Fraction::mul(Fraction a, Fraction b){
int fz,fm;
fz=a.top*b.top;
fm=a.bottom*b.bottom;
cout<<"mul: "<<fz<<"/"<<fm<<endl;
}
void Fraction::div(Fraction a, Fraction b){
int fz,fm;
fz=a.top*b.bottom;
fm=b.top*a.bottom;
cout<<"div: "<<fz<<"/"<<fm<<endl;
} void Fraction::compare(Fraction a, Fraction b){
int gbs,m=a.bottom,n=b.bottom,t,r,fz,fm;
if(m<n)
{t=m;m=n;n=t;}
r=m%n;
while(r!=)
{m=n;n=r;r=m%n;}
gbs=a.bottom*b.bottom/n;
fz=a.top*(gbs/a.bottom)-b.top*(gbs/b.bottom);
if(fz<)
cout<<"compare: "<<a.top<<"/"<<a.bottom<<"<"<<b.top<<"/"<<b.bottom<<endl;
else if(fz>)
cout<<"compare: "<<a.top<<"/"<<a.bottom<<">"<<b.top<<"/"<<b.bottom<<endl;
else
cout<<"compare: "<<a.top<<"/"<<a.bottom<<"="<<b.top<<"/"<<b.bottom<<endl;
}

fraction.cpp

运行结果:

总结:

1、part3中有许多代码是重复的,目前还想不到好的解决方案。

2、在做题时,还是会翻阅书,参考样例,不够熟练。

3、学习了用项目。

评论:

1、https://www.cnblogs.com/shenqidetao/p/10742384.html

2、https://www.cnblogs.com/qsxsc/p/10742704.html

3、https://www.cnblogs.com/csc13813017371/p/10743961.html

最新文章

  1. JavaScript性能优化
  2. 最新版CentOS6.5上安装部署ASP.NET MVC4和WebApi
  3. php5.3 fastcgi方式 安装以及和nginx整合
  4. Jenkins搭建
  5. Servlet中的GET和POST之间的区别
  6. Android小项目之三 splash界面
  7. Java Concurrency - Callable &amp; Future
  8. WEB项目(B/S系统)打包安装(总结篇)
  9. 【Android】 分享一个完整的项目,适合新手!
  10. Python字符串 --Python3
  11. ECMA262学习笔记(一)
  12. AngularJS 关于ng-model和ng-bind还有{{}}
  13. mysql触发器,视图,游标
  14. 【tmos】SpringBoot项目IDEA不识别依赖,但是能够运行时什么鬼?
  15. Java 导出 CSV
  16. Zabbix,Nagios,OneAPM Servers 安装部署大比拼
  17. Java中创建String的两种方式差异
  18. &quot;android.uid.systemandroid.view.InflateException: Binary XML file line #7: Error inflating class android.webkit.WebView
  19. ubuntu16.04下安装文献管理工具mendelay
  20. h5 的 audio 标签知识点

热门文章

  1. 不同系统里同一Customizing activity的显示差异分析
  2. websocket常见错误
  3. IOS 录音(AVAudioRecorder)
  4. MySQL30条规范解读
  5. Android(java)学习笔记35:如何改变Spinner系统自带的字体和颜色
  6. HBuilder实现WiFi调试Android
  7. intellij idea下载安装以及创建项目(输出Hello World)
  8. 【luogu P2194 HXY烧情侣】 题解
  9. UVA - 136 Ugly Numbers(丑数,STL优先队列+set)
  10. C# ==、Equals、ReferenceEquals 区别与联系 (转载)