南京信息工程大學實驗報告(四)
阿新 • • 發佈:2018-04-23
private include 聲明 har 分享 window 屏幕 rac 開始
頭文件,源文件一開始分不清,然後查了一下,發現一篇講的挺好的,下面是鏈接
https://blog.csdn.net/SleepBoyer/article/details/54577848
第二題
原來的包含在選做一裏面了,下面是選做一的代碼
graph.cpp
#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() { 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
#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 <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()在屏幕上繪制圖形 graph1.redraw(); return 0; }
draw的算法:一行一行輸出,最外層循環控制行數,裏面第一個循環輸出空格(有規律),緊接著的循環輸出指定符號個數(有規律),接著換行,一行就完成了,可以進行下一行。
關於第二道改變控制臺前景顏色和背景顏色的,百度了一下,發現有兩種,一種是利用graphics.h 但是這個好像要在專門的一個軟件裏面,另一種是Windows.h 雖然兩種都沒有弄明白orz
第三道選做改變位置,有一點想法,但是沒試過,就是getchar()一個字符,然後去重新定義一下輸出軟,但是又個疑惑,如果一開始是上或者是左鍵,圖案怎麽移動?
第三題實話沒有做出來,程序有問題,一直報錯說我的載入模糊不清,但是我不知道怎麽改了,希望有大神幫我看一下,謝謝惹~
fraction.cpp
# include <iostream>
# 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<<top*f.botton+top*f.botton<<"/"<<botton*f.botton<<endl; //加法 //加法
}
void Fraction::minus(Fraction &f){
cout<<top*f.botton-f.top*botton<<"/"<<botton*f.botton<<endl; //減法
}
void Fraction::multiplication(Fraction &f){
cout<<top*f.top<<"/"<<botton*f.botton<<endl; //乘法
}
void Fraction::division(Fraction &f){
cout<<top*f.botton<<"/"<<botton*f.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 <iostream>
# 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.還有就是一些題目中要求功能的函數如何編寫的問題了,不知道如何編寫使得滿足要求。
最後還是要提醒一下自己再把第三四章內容重新看看,在這幾次作業中都有很多這兩章內容的細節需要註意。
南京信息工程大學實驗報告(四)