Experiment report of Nanjing University of Information Science and Technology (4)

The header file, the source file was unclear at first, and then I checked it and found that one article was very good. Here is the link
https://blog.csdn.net/SleepBoyer/article/details/54577848

Question 2
The original one is included in the optional one, and the following is the code for the optional one

graph.cpp

...

include "graph.h"

include

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 symbol
// size and symbol are private member data of class Graph
void Graph::draw() {
for (int i= 1;i<size+1;i++){ //Control the number of lines
for (int j=0;j<size-i;j++){ //Output the space before each line symbol
cout<<' ';
}
for ( int k=0;k<2*i-1;k++){ //output pattern symbol
cout<<symbol;
}
cout<<endl; //change to next line
}

}
// Complement the code to implement the graphic style shown in the "Experiment 4.pdf" document
void Graph::redraw()
{
while(1){
cin>>symbol>>size;
draw();
}

}
...

graph.h

...

ifndef GRAPH_H

define GRAPH_H

// declaration of
class Graph class Graph {
public:
Graph(char ch, int n); // constructor with parameters
void draw(); // drawing graph
void redraw();
private:
char symbol;
int size;
};

endif

...

Main.cpp

...

include

include "graph.h"

using namespace std;

int main() {
Graph graph1('*',5), graph2('$',7) ; // Define the Graph class objects graph1, graph2
graph1.draw(); // Call the public interface draw() through the object graph1 Draw the graph on the screen
graph2.draw(); // Call the public interface draw() on the screen through the object graph2 to draw the graph on the screen
graph1.redraw();
return 0;
}
...

The algorithm of draw: output line by line, the outermost loop controls the number of lines, the first loop inside outputs spaces (regularly), the next loop outputs the specified number of symbols (regular), and then wraps, a line is completed, can proceed to the next line.

Regarding the second way to change the foreground color and background color of the console, Baidu took a look and found that there are two kinds, one is to use graphics.h, but this seems to be in a special software, and the other is Windows.h Although the two I don't understand orz

The third option is to change the position. I have a little idea, but I haven't tried it, that is, getchar() a character, and then redefine the output soft, but I have another doubt, if it is the up or left button at the beginning, how to move the pattern ?

The truth of the third question has not been made. There is a problem with the program. It keeps getting an error saying that my loading is ambiguous, but I don't know how to change it. I hope someone can help me take a look. Thank you~

fraction.cpp
...

include

include "fraction.h"

using namespace std;

Fraction::Fraction(int t,int b):top(t),botton(b){
}
Fraction::Fraction(){ //function overload
top=0;
botton=1;
}
Fraction::Fraction( int t):top(t){ //function overloaded
bottom=1;
}
Fraction::~Fraction()
{}
void Fraction::fenshu(){
cout<<top<<"/"<<botton;
}

void Fraction::add(Fraction &f){
cout<<topf.botton+topf.botton<<"/"<<botton*f.botton<<endl; //加法 //加法
}

void Fraction::minus(Fraction &f){
cout<<topf.botton-f.topbotton<<"/"<<botton*f.botton<<endl; //减法
}

void Fraction::multiplication(Fraction &f){
cout<<topf.top<<"/"<<bottonf.botton<<endl; //乘法
}

void Fraction::division(Fraction &f){
cout<<topf.botton<<"/"<<bottonf.top<<endl; //除法
}

void Fraction::compare(Fraction &f){
int m=top,n=botton;
int p=f.top,q=f.botton;
double w=top/botton;
double j=f.top/f.botton;
if (w>j){
cout<<"b较大"<<endl;
}
if (w<j){
cout<<"b较小"<<endl;
}
else {
cout<<"一样大"<<endl;
}

}

void Fraction::refenshu(){ //fraction input and output
while(1){
cin>>top>>botton;
fenshu();
}

}
...

fraction.h

...

ifndef FRACTION_H

define FRACTION_H

class Fraction{
public:
Fraction(int t=0,int b=1);
Fraction();
Fraction(int t);
~Fraction();
Fraction(Fraction &f);
void fenshu();
void add(Fraction &f);
void minus(Fraction &f);
void multiplication(Fraction &f);
void division(Fraction &f);
void compare(Fraction &f);
void refenshu();
private:
int top;
int botton;

};

endif

...

main.cpp

...

include

include "fraction.h"

using namespace std;
int main(){
Fraction a;
Fraction b(3,4);
Fraction c(5);
b.add(c);
b.minus(c);
b.multiplication(c);
b.division(c);
b.compare(c);
b.refenshu();
return 0;
}
...

There are many problems exposed when doing this question:
1. The use of the copy constructor, when I used it, I did not put it into the function member parameters, which resulted in an error.
2. Forgot to put the class in front of the function when implementing the function. The output of the functions and objects in the class is not enough to determine how to type the code, and the mastery is not good.
3. I forgot to use the overloaded function at the beginning, and I am not familiar with it.
4. There is also the problem of how to write the functions that require functions in some topics. I don't know how to write them to meet the requirements.

Finally, I still have to remind myself to re-read the contents of Chapters 3 and 4. There are many details of the contents of these two chapters that need to be paid attention to in these assignments.

Guess you like

Origin http://43.154.161.224:23101/article/api/json?id=324730720&siteId=291194637