内容三
1 #include <iostream>
2 using namespace std;
3 class Franction{
4 public:
5 Franction(int t=0,int b=1);
6 void draw();
7 private:
8 int top;
9 int bottom;
10 };
11 Franction::Franction(int t,int b):top(t),bottom(b){
12 }
13 void Franction::draw(){
14 int t1,t2,n,b1;
15 if(bottom<0){
16 bottom=-bottom;
17 top=-top;
18 }
19 if(top==0){
20 cout<<0<<endl;
21 return;
22 }
23 else if(top<0)
24 t1=-top;
25 else
26 t1=top;
27 b1=bottom;
28 t2=t1;
29 while(t1%b1>0){
30 n=t1%b1;
31 t1=b1;
32 b1=n;
33 }
34 if(t2%bottom==0)
35 cout<<top/bottom<<endl;
36 else
37 cout<<top/b1<<"/"<<bottom/b1<<endl;
38 }
39 int main() {
40 int n,m;
41 Franction a;
42 a.draw();
43 Franction b(3,4);
44 b.draw();
45 Franction c(5);
46 c.draw();
47 cout<<"请输入分子 分母,用空格隔开,分母不为0"<<endl;
48 cin>>n>>m;
49 if(m==0)
50 cout<<"不听话,不和你玩了!再见!"<<endl;
51 Franction d(n,m);
52 d.draw();
53 return 0;
54 }
内容二
// 类graph的实现
#include "graph.h"
#include <iostream>
using namespace std;
// 带参数的构造函数的实现
Graph::Graph(char ch, int n): symbol(ch), size(n) {
}
// 成员函数draw()的实现
// 功能:绘制size行,显示字符为symbol的指定图形样式
// size和symbol是类Graph的私有成员数据
void Graph::draw() {
// 补足代码,实现「实验4.pdf」文档中展示的图形样式
for(int i=1;i<=size;i++){
for(int j=1;j<=size-i;j++)
cout<<" ";
for(int k=1;k<=2*i-1;k++)
cout<<symbol;
cout<<endl;
}
}
#ifndef GRAPH_H
#define GRAPH_H
// 类Graph的声明
class Graph {
public:
Graph(char ch, int n); // 带有参数的构造函数
void draw(); // 绘制图形
private:
char symbol;
int size;
};
#endif
#include <iostream>
#include "graph.h"
using namespace std;
int main() {
Graph graph1('*',5), graph2('$',7) ; // 定义Graph类对象graph1, graph2
graph1.draw(); // 通过对象graph1调用公共接口draw()在屏幕上绘制图形
graph2.draw(); // 通过对象graph2调用公共接口draw()在屏幕上绘制图形
return 0;
}
![]()