标签:graph oat char space .com turn .cpp div std
编译环境:vs2017
#pragma once //Graph.h class Graph { public: Graph(char s, int l); void draw(); private: char sign; int line; }; //Graph.cpp #include"graph.h" #include<iostream> using namespace std; Graph::Graph(char s, int l) { sign = s; line = l; } void Graph::draw() { int i, j; for (i = 1; i <= line; i++) { for (j = 1; j <= line - i; j++) cout << " "; for (j = 1; j <= i * 2 - 1; j++) cout << sign; for (j = 1; j <= line - 1; j++) cout << " "; cout << endl; } } //main.cpp #include"graph.h" #include<iostream> using namespace std; int main() { Graph graph1(‘*‘, 5); graph1.draw(); Graph graph2(‘$‘, 7); graph2.draw(); return 0; }
//Fraction.h部分 #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.cpp #include<iostream> #include"Fraction.h" using namespace std; 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) { Fraction f2; f2.top = top * f1.bottom + f1.top*bottom; f2.bottom = bottom * f1.bottom; f2.show(); } void Fraction::sub(Fraction &f1) { Fraction f2; f2.top = top * f1.bottom - f1.top*bottom; f2.bottom = bottom * f1.bottom; f2.show(); } void Fraction::mul(Fraction &f1) { Fraction f2; f2.top = top * f1.top; f2.bottom = bottom * f1.bottom; f2.show(); } void Fraction::div(Fraction &f1) { Fraction f2; f2.top = top * f1.bottom; f2.bottom = bottom * f1.top; f2.show(); } void Fraction::show() { cout << top << "/" << bottom << endl; } void Fraction::compare(Fraction f1, Fraction f2) { 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; } } //main.cpp部分 #include<iostream> #include"Fraction.h" using namespace std; int main() { Fraction f1; Fraction f2(5); Fraction f3(3, 6); Fraction f4(4, 6); Fraction f5(3, 8); Fraction f6(6, 7); 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; }
标签:graph oat char space .com turn .cpp div std
原文地址:https://www.cnblogs.com/nuist20178303037/p/8922493.html