码迷,mamicode.com
首页 > 编程语言 > 详细

C++实验三

时间:2019-04-21 15:56:44      阅读:129      评论:0      收藏:0      [点我收藏+]

标签:src   tip   namespace   ott   技术   c++   rap   图形   http   

part2

graph.h

#ifndef GRAPH_H
#define GRAPH_H
// 类Graph的声明
class Graph {
public:
Graph(char ch, int n); // 带有参数的构造函数
void draw(); // 绘制图形
private:
char symbol;
int size;
};
#endif

main.cpp

#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;
}

graph.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=1;i<=size;i++)
{
for(int j=1;j<=size-i;j++)cout<<"";
for(int j=1;j<=2*i-1;j++)cout<<symbol

}
}

技术图片

part3

fraction.h

#ifndef FRACTION_H
#define FRACTION_H
class Fraction
{
public:
Fraction(int top0=0,int bottom0=1):top(top0),bottom(bottom0) {}
friend void add(Fraction a,Fraction b);
friend void subtract(Fraction a,Fraction b);
friend void multiply(Fraction a,Fraction b);
friend void divide(Fraction a,Fraction b);
friend void compare(Fraction a,Fraction b);
private:
int top;
int bottom;
};
#endif

fraction.cpp

#include "fraction.h"
#include <iostream>
using namespace std;
void add(Fraction a,Fraction b)
{
int i,j;
i=a.top*b.bottom+a.bottom*b.top;
j=a.bottom*b.bottom;
cout<<i<<‘/‘<<j<<endl;
}
void subtract(Fraction a,Fraction b)
{
int i,j;
i=a.top*b.bottom-a.bottom*b.top;
j=a.bottom*b.bottom;
cout<<x<<‘/‘<<y<<endl;
}
void multiply(Fraction a,Fraction b)
{
int i,j;
i=a.top*b.top;
j=a.bottom*b.bottom;
cout<<i<<‘/‘<<j<<endl;
}
void divide(Fraction a,Fraction b)
{
int i,j;
i=a.top*b.bottom;
j=a.bottom*a.top;
cout<<i<<‘/‘<<j<<endl;
}
void compare(Fraction a,Fraction b)
{
int i;
i=a.top*b.bottom-a.bottom*b.top;
if(i>0)
{
cout<<‘>‘<<endl;
}
else if(i<0)
{
cout<<‘<‘<<endl;
}
else if(i=0)
{
cout<<‘=‘<<endl;
}
}

#include "fraction.h"
#include <iostream>
int main()
{
Fraction a;
Fraction b(3,4);
Fraction c(5);
add(b,c);
subtract(b,c);
multiply(b,c);
divide(b,c);
compare(b,c);
return 0;
}

技术图片

 

 

 


 

C++实验三

标签:src   tip   namespace   ott   技术   c++   rap   图形   http   

原文地址:https://www.cnblogs.com/csc13813017371/p/10743961.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!