南京信息工程大学实验报告(四)

头文件,源文件一开始分不清,然后查了一下,发现一篇讲的挺好的,下面是链接
https://blog.csdn.net/SleepBoyer/article/details/54577848

第二题
原来的包含在选做一里面了,下面是选做一的代码

graph.cpp

...

include "graph.h"

include

using namespace std;

// 带参数的构造函数的实现
Graph::Graph(char ch, int n): symbol(ch), size(n) {
}

// 成员函数draw()的实现
// 功能:绘制size行,显示字符为symbol的指定图形样式
// size和symbol是类Graph的私有成员数据
void Graph::draw() {
for (int i=1;i<size+1;i++){ //控制行数
for (int j=0;j<size-i;j++){ //输出每行符号前的空格
cout<<' ';
}
for (int k=0;k<2*i-1;k++){ //输出图案符号
cout<<symbol;
}
cout<<endl; //换至下一行
}

}
// 补足代码,实现「实验4.pdf」文档中展示的图形样式
void Graph::redraw()
{
while(1){
cin>>symbol>>size;
draw();
}

}
...

graph.h

...

扫描二维码关注公众号,回复: 53091 查看本文章

ifndef GRAPH_H

define GRAPH_H

// 类Graph的声明
class Graph {
public:
Graph(char ch, int n); // 带有参数的构造函数
void draw(); // 绘制图形
void redraw();
private:
char symbol;
int size;
};

endif

...

Main.cpp

...

include

include "graph.h"

using namespace std;

int main() {
Graph graph1('*',5), graph2('$',7) ; // 定义Graph类对象graph1, graph2
graph1.draw(); // 通过对象graph1调用公共接口draw()在屏幕上绘制图形
graph2.draw(); // 通过对象graph2调用公共接口draw()在屏幕上绘制图形
graph1.redraw();
return 0;
}
...

draw的算法:一行一行输出,最外层循环控制行数,里面第一个循环输出空格(有规律),紧接着的循环输出指定符号个数(有规律),接着换行,一行就完成了,可以进行下一行。

关于第二道改变控制台前景颜色和背景颜色的,百度了一下,发现有两种,一种是利用graphics.h 但是这个好像要在专门的一个软件里面,另一种是Windows.h 虽然两种都没有弄明白orz

第三道选做改变位置,有一点想法,但是没试过,就是getchar()一个字符,然后去重新定义一下输出软,但是又个疑惑,如果一开始是上或者是左键,图案怎么移动?

第三题实话没有做出来,程序有问题,一直报错说我的载入模糊不清,但是我不知道怎么改了,希望有大神帮我看一下,谢谢惹~

fraction.cpp
...

include

include "fraction.h"

using namespace std;

Fraction::Fraction(int t,int b):top(t),botton(b){
}
Fraction::Fraction(){ //函数的重载
top=0;
botton=1;
}
Fraction::Fraction(int t):top(t){ //函数的重载
botton=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(){ //分数输入输出
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;
}
...

做这题的时候有很多很多的问题暴露出来:
1.复制构造函数的运用,我运用他的时候,并没有将它放入函数成员参数中,导致了报错。
2.实现函数的时候忘记将类放在前面,对类中函数与对象的输出还不够确定代码怎么敲,掌握的不好。
3.重载函数一开始忘了使用,运用的不熟。
4.还有就是一些题目中要求功能的函数如何编写的问题了,不知道如何编写使得满足要求。

最后还是要提醒一下自己再把第三四章内容重新看看,在这几次作业中都有很多这两章内容的细节需要注意。

猜你喜欢

转载自www.cnblogs.com/zxy666/p/8922054.html