#ifndef GRAPH_H #define GRAPH_H // 类Graph的声明 class Graph { public: Graph(char ch, int n); // 带有参数的构造函数 void draw(); // 绘制图形 private: char symbol; int size; }; #endif
// 类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」文档中展示的图形样式 int i, j, k; for (i = 1; i <= size; i++) { for (j = (size + 1) / 2; j >= 1; j--) { cout << " " << endl; } for (k = 1; k = i; k++) { cout << symbol; } cout << endl; } }
#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; }
#ifndef FRACTION_H_INCLUDED #define FRACTION_H_INCLUDED #pragma once class Fraction { public: Fraction(); Fraction(int t, int b); Fraction(int t); void show(); void add(Fraction &f1);//加 void sub(Fraction &f1);//减 void mul(Fraction &f1);//乘 void div(Fraction &f1);//除 void compare(Fraction f1, Fraction f2);//比较 private: int top; int bottom; }; fraction.h
#include<iostream> #include"Fraction.h" using namespace std; int main() { Fraction f1; Fraction f2(2); Fraction f3(2, 4); Fraction f4(-3, 4); Fraction f5(3, -4); Fraction f6(-3, -4); f1.show(); f2.show(); f3.show(); f4.show(); f5.show(); f6.show();//不同形式的分数显示 f3.sub(f4); f3.add(f4); f5.mul(f6); f5.div(f6); //分数的加减乘除 f1.compare(f2, f4); //分数比较 return 0; }
F #include<iostream> #include"Fraction.h" using namespace std; int sim(int t, int j) { //sim函数,功能为找出分子与分母的最大公约数 int a, b, i, m, c, n; a = t; b = j; if (a >= b)m = b; else m = a; for (i = 1; i <= m; i++) { if (a%i == 0 && b%i == 0)c = i; } return c; } Fraction::Fraction() { top = 0; bottom = 1; } Fraction::Fraction(int t, int b) { top = t; bottom = b; } Fraction::Fraction(int t) { top = t; bottom = 1; } void Fraction::add(Fraction &f1) { //加法add函数的实现 Fraction f2; f2.top = top * f1.bottom + f1.top*bottom; f2.bottom = bottom * f1.bottom; f2.show(); } void Fraction::sub(Fraction &f1) { //减法sub函数的实现 Fraction f2; f2.top = top * f1.bottom - f1.top*bottom; f2.bottom = bottom * f1.bottom; f2.show(); } void Fraction::mul(Fraction &f1) { //乘法mul函数的实现 Fraction f2; f2.top = top * f1.top; f2.bottom = bottom * f1.bottom; f2.show(); } void Fraction::div(Fraction &f1) { //除法div函数的实现 Fraction f2; f2.top = top * f1.bottom; f2.bottom = bottom * f1.top; f2.show(); } void Fraction::show() { //show函数的实现 if (top > 0 && bottom > 0) { int c; c = sim(top, bottom); cout << top / c << "/" << bottom / c << endl;//化简 } if (top < 0 && bottom>0) { int c; c = sim(-top, bottom); cout << top / c << "/" << bottom / c << endl;//化简 } if (top > 0 && bottom < 0) { int c; c = sim(top, -bottom); cout << -top / c << "/" << -bottom / c << endl;//将分母的负号移动至分子上 } if (top < 0 && bottom < 0) { int c; c = sim(-top, -bottom); cout << -top / c << "/" << -bottom / c << endl;//分子分母同为负数时去除负号 } if (top == 0) cout << top << "/" << bottom << endl; } void Fraction::compare(Fraction f1, Fraction f2) { //分数比较compare函数的实现 float a = float(f1.top) / float(f1.bottom); float b = float(f2.top) / float(f2.bottom); if (a <= b) { cout << f1.top << "/" << f1.bottom << "<="; f2.show(); cout << endl; } if (a > b) { cout << f1.top << "/" << f1.bottom << ">"; f2.show(); cout << endl; } } fraction.cpp raction.cpp

不知道问题出在哪里。。。询问了@buluguy也没有解决
浙公网安备 33010602011771号