#ifndef GRAPH_H #define GRAPH_H // Declaration of class Graph class Graph { public : Graph( char ch, int n); // Constructor with parameters void draw(); // Draw graph private : char symbol; int size; }; #endif
// The implementation of class graph #include " graph.h " #include <iostream> using namespace std; // Implementation of constructor with parameters Graph::Graph( char ch, int n): symbol(ch), size(n) { } // implementation of member function draw() // Function: draw size line, display the specified graphic style with the character as symbol // size and symbol are private member data of class Graph void Graph::draw() { // Complement code, Implement the graphic style shown in the "Experiment 4.pdf" document int i,j,k; for (i= 1 ;i<=size;i++ ) { for(j=1;j<=size-i;j++) { cout<<' '; } for(k=1;k<=2*i-1;k++) { cout<<symbol; } for(j=1;j<=size-i;j++) { cout<<' '; } cout<<endl; } }
#include <iostream> #include "graph.h" using namespace std; int main() { Graph graph1( ' * ' , 5 ), graph2( ' $ ' , 7 ) ; // Define Graph class objects graph1, graph2 graph1.draw(); // Call the public interface draw() through the object graph1 to draw the graph on the screen graph2.draw(); // Draw the graph on the screen by calling the public interface draw() through the object graph2 return 0; }
// Declaration of class Fraction class Fraction { public : Fraction(); // constructor Fraction( int t, int b); // overload Fraction( int t); // overload void show(); // show function void add(Fraction &f1); // add void min(Fraction &f1); // Subtract void mul(Fraction &f1); // Multiply void div(Fraction &f1); // Division void compare(Fraction f1, Fraction f2); // Compare private : inttop; // numerator int bottom; // denominator };
#include "Fraction.h" #include <iostream> using namespace std; // Fraction function 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::min(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(){ //show函数 cout<<top<<"/"<<bottom<<endl; } void Fraction::compare(Fraction f1, Fraction f2) { //比较 float a,b; a = f1.top * f1.bottom; b = f2.top * 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; } }
#include <iostream> #include "Fraction.h" using namespace std; intmain () { Fraction f1; Fraction f2(5); Fraction f3(3, 4); f1.show(); f2.show(); f3.show(); f2.min(f3); // subtract f2.add(f3); // add f2.mul(f3); // multiply f2.div(f3); // divide f2.compare(f2, f3); / / compare return 0 ; }
Question 1: There are n lines in total, there are ni spaces before and after the i-th line, and 2i-1 symbols.
In this experiment, I can't solve the problem alone, and I am not proficient in the application of classes, so I need to practice more.