天才吃鸡少女

导航

实验3

第一题:

#include "ball.h"
#include <iostream>
#include <cstdlib>  
using std::cout;
using std::endl;

const int SIZE_X=50;   
const int SIZE_Y=50;   
Ball::Ball(int x0, int y0):x(x0), y(y0) {
	for(int line=1; line <= y0-1; line++)
		cout << endl;
	
	for(int col=1; col <= x0-1; col++) 
		cout << " ";
	
	cout << "O" << endl; 
	
}

void Ball::left(int step) {
	x = x-step;
	if(x <= 0)
		x=0;
	

	system("cls");
	

	for(int line=1; line <= y-1; line++)
		cout << endl;

	for(int col=1; col <= x-1; col++) 
		cout << " ";

	cout << "O" << endl; 
	
}

void Ball::right(int step) {
	x = x+step;
	if(x >= SIZE_X)
		x=SIZE_X;
	
	// 锟斤拷锟斤拷 
	system("cls");
	
	// 锟斤拷印y-1锟叫匡拷锟斤拷
	for(int line=1; line <= y-1; line++)
		cout << endl;
	
	// 锟斤拷印x-1锟斤拷锟秸革拷
	for(int col=1; col <= x-1; col++) 
		cout << " ";
	
	// 锟斤拷印小锟斤拷
	cout << "O" << endl; 
	
} 

void Ball::up(int step) {
	y = y-step;
	if(y <= 0)
		y=0;
	
	// 锟斤拷锟斤拷 
	system("cls");
	
	// 锟斤拷印y-1锟叫匡拷锟斤拷
	for(int line=1; line <= y-1; line++)
		cout << endl;
	
	// 锟斤拷印x-1锟斤拷锟秸革拷
	for(int col=1; col <= x-1; col++) 
		cout << " ";
	
	// 锟斤拷印小锟斤拷
	cout << "O" << endl; 
	
}

void Ball::down(int step) {
	y = y+step;
	if(y >= SIZE_Y)
		y = SIZE_Y;
	
	// 锟斤拷锟斤拷 
	system("cls");
	
	// 锟斤拷印y-1锟叫匡拷锟斤拷
	for(int line=1; line <= y-1; line++)
		cout << endl;
	
	// 锟斤拷印x-1锟斤拷锟秸革拷
	for(int col=1; col <= x-1; col++) 
		cout << " ";
	
	// 锟斤拷印小锟斤拷
	cout << "O" << endl; 	
}

  

#ifndef BALL_H
#define BALL_H

class Ball {
	public:
		Ball(int x0=0, int y0=0);   // 在坐标(x,y)处构造一个小球(小球用字符O表示) 
		void left(int step=1);		// 左移step 
		void right(int step=1);		// 右移step 
		void up(int step=1);		// 上移step 
		void down(int step=1); 		// 下移step 
	private:
		int x;	// x坐标
		int y;  // y坐标 
};

#endif

  

#include "canvas.h"
#include <cstdlib>

Canvas::Canvas(string bg0, string fg0):bg(bg0), fg(fg0) {
	string color = "color ";
	color += bg0;
	color += fg0;
	system(color.c_str());
} 
void Canvas::changeCanvasBg(string bg0) {
	bg = bg0; // 更新画布背景色 
	
	string color = "color ";
	color += bg;
	color += fg;
	system(color.c_str());
	
}
void Canvas::changeCanvasFg(string fg0) {
	fg = fg0; // 更新画布前景色 
	
	string color = "color ";
	color += bg;
	color += fg;
	system(color.c_str());
	
}
void Canvas::changeCanvasColor(string bg0, string fg0){
	bg = bg0;  // 更新画布背景色
	fg = fg0;   // 更新画布前景色 
	
	string color = "color ";
	color += bg;
	color += fg;
	system(color.c_str());	
}

  

#ifndef CANVAS_H
#define CANVAS

#include <string>
using std::string;

class Canvas {
	public:
		Canvas(string bg0="0", string fg0="A");
		void changeCanvasBg(string bg0);
		void changeCanvasFg(string fg0);
		void changeCanvasColor(string bg0, string fg0); 
	private:
		string bg; // background color
		string fg; // foreground color 
};

#endif

  

#include <iostream>
#include "canvas.h"
#include "ball.h"
int main(){
	Canvas canvas;
	Ball ball(10,10);
	system("pause");
	ball.left(5);
	system("pause");
	ball.up(20);
	system("pause");
	canvas.changeCanvasFg("E");
	system("pause");
    canvas.changeCanvasBg("D");
	system("pause");
return 0;
}

  

第二题:

 

// 类graph的实现
 
#include "graph.h" 
#include <iostream>
using namespace std;

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


// 成员函数draw()的实现
// 功能:绘制size行,显示字符为symbol的指定图形样式 
void Graph::draw() {
	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;
		cout<<endl;
  }
	
}

 

  

#ifndef GRAPH_H
#define GRAPH_H

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


#endif

  

#include <iostream>
#include "graph.h"
using namespace std;

int main() {
	Graph graph1('*',5);
    graph1.draw();
    
    system("pause");
    system("cls");
    
    Graph graph2('$',7);
    graph2.draw();
    system("pause");
	return 0; 
} 

  

 第三题:

 

#include <iostream>
#include<cstdlib>
#include "fraction.h"
using std::cout;
using std::endl;

int main(){
    Fraction m,n;
    Fraction c1,c2,c3,c4;
    
    cout<<"输入两个分数:";
    m.input();
    n.input();
    
    cout<<"m=";
    m.output();
    cout<<endl;
    cout<<"n=";
    n.output();
    cout<<endl;
    
    c1=Fraction::add(m,n);
    cout<<"相加";
    c1.output();
    cout<<endl;
    
    c2=Fraction::sub(m,n);
    cout<<"相减";
    c2.output();
    cout<<endl;
    
    c3=Fraction::mul(m,n);
    cout<<"相乘";
    c3.output();
    cout<<endl;
    
    c4=Fraction::div(m,n);
    cout<<"相除";
    c4.output();
    cout<<endl;
    
    Fraction::compare(m,n);
    
    system("pause");
    return 0;
}

 

  

#include "fraction.h"
#include <iostream>
#include <cmath>
using std::cin;
using std::cout;
using std::endl;

Fraction::Fraction(int top0,int bottom0):top(top0),bottom(bottom0){
    if(bottom0=0){
        cout<<"Wrong!";
        exit(0);
    }
}

Fraction::Fraction(){}

Fraction Fraction::add(Fraction a,Fraction b){
    Fraction add1;
    add1.bottom=a.bottom*b.bottom;
    add1.top=a.top*b.bottom+b.top*a.bottom;
    add1.sim(); 
    return add1;
}

Fraction Fraction::sub(Fraction a,Fraction b){
    Fraction sub1;
    sub1.bottom=a.bottom*b.bottom;
    sub1.top=a.top*b.bottom-b.top*a.bottom;
    sub1.sim();
    return sub1;
} 

Fraction Fraction::mul(Fraction a,Fraction b){
    Fraction mul1;
    mul1.bottom=a.bottom*b.bottom;
    mul1.top=a.top*b.top;
    mul1.sim();
    return mul1;
} 

Fraction Fraction::div(Fraction a,Fraction b){
    Fraction div1;
    div1.bottom=a.bottom*b.top;
    div1.top=a.top*b.bottom;
    div1.sim();
    return div1;
} 

void Fraction::compare(Fraction a,Fraction b){
    a.sim();
    b.sim();
    if(a.top*b.bottom>a.bottom*b.top)
       cout<<a.top<<"/"<<a.bottom<<">"<<b.top<<"/"<<b.bottom<<endl;
    else if(a.top*b.bottom<a.bottom*b.top)
       cout<<a.top<<"/"<<a.bottom<<"<"<<b.top<<"/"<<b.bottom<<endl;
    else
       cout<<a.top<<"/"<<a.bottom<<"="<<b.top<<"/"<<b.bottom<<endl;
}

void Fraction::sim(){
    int i;
    if(top!=0){
        for(i=fabs(top);i>=1;i--){
            if(top%i==0&&bottom%i==0)
               break;
        }
        top/=i;
        bottom/=i;
    }
    if(bottom*top<0){
        top=-fabs(top);
        bottom=fabs(bottom);
    }
    else if(bottom*top>0){
        top=fabs(top);
        bottom=fabs(bottom);
    }
} 

void Fraction::input(){
    cin>>top>>bottom;
} 

void Fraction::output(){
    if(top!=0){
        if(bottom!=1)
           cout<<top<<"/"<<bottom;
        else if(bottom==1)
           cout<<top;
    }
    else
        cout<<"0";
} 

  

#ifndef FRACTION_H
#define FRACTION_H
class Fraction{
    public:
        Fraction(int top0,int bottom0);
        Fraction(); 
        static Fraction add(Fraction a,Fraction b);
        static Fraction sub(Fraction a,Fraction b);
        static Fraction mul(Fraction a,Fraction b);
        static Fraction div(Fraction a,Fraction b);
        static void compare(Fraction a,Fraction b);
        void sim();
		void input();
        void output();
    private:
        int top;
        int bottom;
};

#endif 

  

 

总结:

1:   在课上做的第一题存入U盘回来打开了运行不了,重新导入的才可以。

2:最后一题花了好久好久,一言难尽,自己电脑的DEV运行不出来,让学长检查错误,然后截图是让学长帮忙运行的截图,不知道为啥我的软件不好运行,心态爆炸。

 

posted on 2019-04-22 23:37  天才吃鸡少女  阅读(224)  评论(1编辑  收藏  举报