C++实验三

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

}
}

C++实验三

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++实验三

上一篇:规则html表单对象赋值


下一篇:【JSP实例】指定用户计数器