c++第三次实验
第一题:
先把代码贴上来
main.cpp
#include <iostream>
#include <cstdlib>
#include<conio.h>
#include "canvas.h"
#include "Ball.h"
using namespace std;
int main() {
Canvas canvas;
char ch; Ball ball1(,);
system("pause"); /*ball1.left(5);
system("pause"); ball1.up(20);
system("pause");*/
while(cin>>ch)
{ cout<<"如果想退出请按q";
switch(ch)
{ case 'w':ball1.up();break;
case 's':ball1.down();break;
case 'a':ball1.left();break;
case 'd':ball1.right();break;
default :break; }
if(ch=='q')break;
} canvas.changeCanvasFg("E"); // 更新画布前景色
system("pause"); canvas.changeCanvasBg("D"); // 更新画布背景色
system("pause"); return ;
}
canvas.h
#ifndef CANVAS_H
#define CANVAS_H #include <string>
using std::string; class Canvas {
public:
Canvas(string bg0="", 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
ball.h
#ifndef BALL_H
#define BALL_H class Ball {
public:
Ball(int x0=, int y0=); // 在坐标(x,y)处构造一个小球(小球用字符O表示)
void left(int step=); // 左移step
//void left(char step);
void right(int step=); // 右移step
// void right(int step=1);
void up(int step=); // 上移step
//void up(int step=1);
void down(int step=); // 下移step
//void down(int step=1);
private:
int x; // x坐标
int y; // y坐标
}; #endif
ball.cpp
#include "ball.h"
#include <iostream>
#include <cstdlib> // 因为使用了system("cls"); 所以需要包含这个头文件
using std::cout;
using std::endl; const int SIZE_X=; // 小球x轴移动范围0~SIZE_X
const int SIZE_Y=; // 小球y轴移动范围0~SIZE_Y void printline(int n)
{
int i;
for(i=; i <= n-; i ++)
cout << endl;
}
void printspace(int n)
{
int i;
for(int i=; i <= n-; i ++)
cout << " ";
}
Ball::Ball(int x0, int y0):x(x0), y(y0) {
// 打印y0-1行空行
printline( y0);
//for(int line=1; line <= y0-1; line++)
//cout << endl; // 打印x0-1个空格
printspace( x0);
//for(int col=1; col <= x0-1; col++)
//cout << " "; printspace(int y0); // 打印小球
cout << "O" << endl; } void Ball::left(int step) {
x = x-step;
if(x <= )
x=; // 清屏
system("cls"); // 打印y-1行空行
printline( y);
//for(int line=1; line <= y-1; line++)
//cout << endl; // 打印x-1个空格
printspace( x);
//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行空行
printline( y);
//for(int line=1; line <= y-1; line++)
//cout << endl; // 打印x-1个空格
printspace( x);
//for(int col=1; col <= x-1; col++)
//cout << " "; // 打印小球
cout << "O" << endl; } void Ball::up(int step) {
y = y-step;
if(y <= )
y=; // 清屏
system("cls"); // 打印y-1行空行
printline( y);
//for(int line=1; line <= y-1; line++)
//cout << endl; // 打印x-1个空格
printspace( x);
//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行空行
printline( y);
//for(int line=1; line <= y-1; line++)
//cout << endl; // 打印x-1个空格
printspace( x);
//for(int col=1; col <= x-1; col++)
//cout << " "; // 打印小球
cout << "O" << endl;
} // 思考:
// Ball类的成员函数实现中,包含大量重复的代码
// 利用所学知识对代码改进优化,使代码更简洁,同时,保持逻辑清晰
canvas.cpp
#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());
} // 1. 说明
// system("color ××");
// 可以用于改变屏幕的前景色和背景色
// 这里画布类Canvas的默认画布颜色及修改就是利用这个函数实现的 // 由于sysmtem()要求参数必须是const char*类型
// 因此,这里借助string类成员函数c_str完成从string类到const char*类型的转换
// c++标准库中tring类虽然是对char *类的封装,以此实现对字符串更便捷、丰富的操作,但是,仍然是有区别的。 // 2. 思考
// Canvas类成员函数的实现中,有大量重复的代码
// 思考如何进一步优化代码,同时又能保持代码的可读性和简洁、逻辑清晰
我对canvas中的重复代码利用函数简化。
添加了基于WASD控制上下左右的功能。
图片有点糊 但是还是能看出来。
第二题:
还是先贴代码为敬
main.cpp
#include <iostream>
#include <cstdlib>
#include <string>
#include "graph.h"
#include "canvas2.h"
using namespace std; int main() {
char ss,ch;
int flag,ll;
string bg0,fg0;
Canvas canvas;
Graph graph1('*',);
graph1.draw(); system("pause");
system("cls"); Graph graph2('$',);
graph2.draw();
system("pause");
system("cls");
while()
{
cout<<"重新设置一个你喜欢的字符和长度:";
cin>>ss>>ll;
//cout<<ll;
Graph graph3(ss,ll);
graph3.draw();
cout<<"重新设置一个你喜欢的背景色和前景色:";
cin>>bg0>>fg0;
canvas.changeCanvasColor(bg0,fg0);
cout<<"如果想退出请按q";
while(cin>>ch)
{ switch(ch)
{ case 'w':graph3.up();break;
case 's':graph3.down();break;
case 'a':graph3.left();break;
case 'd':graph3.right();break;
default :break; }
if(ch=='q')break;
}
system("pause");
system("cls");
cout<<"继续请按1,退出请按2";
cin>>flag;
if(flag==)continue;
else if(flag==)break;
} return ;
}
graph.h
#ifndef GRAPH_H
#define GRAPH_H // 类Graph的声明
class Graph {
public:
Graph(char ch, int n); // 带有参数的构造函数
void draw(); // 绘制图形
void left(int step=);
void right(int step=);
void up(int step=);
void down(int step=); private:
char symbol;
int size;
int x;
int y;
}; #endif
canvas2.h
#ifndef CANVAS2_H
#define CANVAS2_H #include <string>
using std::string; class Canvas {
public:
Canvas(string bg0="", 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
graph.cpp
// 类graph的实现 #include "graph.h"
#include <iostream>
#include <cstdlib> // 因为使用了system("cls"); 所以需要包含这个头文件
using namespace std;
const int SIZE_X=; // 小球x轴移动范围0~SIZE_X
const int SIZE_Y=; // 小球y轴移动范围0~SIZE_Y // 带参数的构造函数的实现
Graph::Graph(char ch, int n): symbol(ch), size(n) {
x=;
y=;
} // 成员函数draw()的实现
void Graph::draw()
{
int i,j,k,sum;
sum=*size-; //计算列数
for(i=;i<=size;i++)
{
for(j=;j<=size-i;j++)
cout<<' ';//每一行先打印出(size-行数)个空格
for(k=size-i+;k<=sum-j+;k++)
cout<<symbol;//再打印出剩下的字符
cout<<endl;
}
}
// 功能:绘制size行,显示字符为symbol的指定图形样式 void printline(int n)
{
int i;
for(i=; i <= n-; i ++)
cout << endl;
} void printspace(int n)
{
int i;
for( i=; i <= n-; i ++)
cout << " ";
} void Graph::left(int step)
{
int i,j,k,sum;
x = x-step;
if(x <= )
x=;
system("cls");
printline(y);
sum=*size-;
for(i=;i<=size;i++)
{
printspace(x);
for(j=;j<=size-i;j++)
cout<<' ';
for(k=size-i+;k<=sum-j+;k++)
cout<<symbol;
cout<<endl;
}
}
void Graph::right(int step)
{
int i,j,k,sum;
x = x+step;
if(x >= SIZE_X)
x=SIZE_X;
system("cls");
printline(y);
sum=*size-;
for(i=;i<=size;i++)
{
printspace(x);
for(j=;j<=size-i;j++)
cout<<' ';
for(k=size-i+;k<=sum-j+;k++)
cout<<symbol;
cout<<endl;
}
}
void Graph::up(int step)
{
int i,j,k,sum;
y = y-step;
if(y <= )
y=;
system("cls");
printline(y);
sum=*size-;
for(i=;i<=size;i++)
{
printspace(x);
for(j=;j<=size-i;j++)
cout<<' ';
for(k=size-i+;k<=sum-j+;k++)
cout<<symbol;
cout<<endl;
}
}
void Graph::down(int step)
{
int i,j,k,sum;
y = y+step;
if(y >= SIZE_Y)
y = SIZE_Y;
system("cls");
printline(y);
for(i=;i<=size;i++)
{
printspace(x);
for(j=;j<=size-i;j++)
cout<<' ';
for(k=size-i+;k<=sum-j+;k++)
cout<<symbol;
cout<<endl;
}
}
canvas2.cpp
#include "canvas2.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());
} // 1. 说明
// system("color ××");
// 可以用于改变屏幕的前景色和背景色
// 这里画布类Canvas的默认画布颜色及修改就是利用这个函数实现的 // 由于sysmtem()要求参数必须是const char*类型
// 因此,这里借助string类成员函数c_str完成从string类到const char*类型的转换
// c++标准库中tring类虽然是对char *类的封装,以此实现对字符串更便捷、丰富的操作,但是,仍然是有区别的。 // 2. 思考
// Canvas类成员函数的实现中,有大量重复的代码
// 思考如何进一步优化代码,同时又能保持代码的可读性和简洁、逻辑清晰
程序运行之后,先显示 Graph graph1('*',5);和Graph graph2('$',7);这两条语句下的图案。按任意键继续后可以输入自定义的字符和行数
然后可以选择前背景色和后背景色。然后想要退出可以按q,不退出就按其他的键进入图形移动界面。
如果想要退出按q即可。如果想再次设置会弹出选择,按1继续,按2退出整个程序。









第三题:
main.cpp
#include <iostream>
#include "fraction.h"
using namespace std; int main ()
{
Fraction a(,);
cout<<"a:";
a.print(a);
Fraction b(,);
cout<<"b:";
b.print(b);
Fraction c();
cout<<"c:";
a.print(c);
Fraction d;
d=d.add(a,b);
cout<<"a+b=";
a.print(d);
a.mytransform(d);
d=d.minus(a,b);
cout<<"a-b=";
a.print(d);
d=d.product(a,b);
cout<<"a*b=";
a.print(d);
d=d.division(a,b);
cout<<"a/b=";
a.print(d); cout<<"当分数分母为0时:";
Fraction e(,);
cout<<"e:";
a.print(e);
cout<<"比较b和c:";
a.compare(b,c);
return ;
}
fraction.h
#ifndef FRACTION_H
#define FRACTION_H class Fraction
{
public:
Fraction(int a=,int b=);
Fraction(Fraction &p);
Fraction add(Fraction p1,Fraction p2);
Fraction minus(Fraction p1,Fraction p2);
Fraction product(Fraction p1,Fraction p2);
Fraction division(Fraction p1,Fraction p2);
void compare(Fraction p1,Fraction p2);
void print(Fraction p);
void standard(int &top,int &bottom);
void mytransform(Fraction p);
int yueshu(int a,int b);
int jueduizhi(int a);
private:
int top;
int bottom;
}; #endif FRACTION_H
fraction.cpp
#include "fraction.h"
#include <iostream>
using namespace std;
void Fraction::mytransform(Fraction p)
{
cout<<"转换成小数输出:"<<static_cast<double>(p.top)/static_cast<double>(p.bottom)<<endl;
}
int Fraction::yueshu(int a,int b)
{
if(b==)return a;
else
return yueshu(b,a%b);
}
int Fraction::jueduizhi(int a)
{
if(a<)return -a;
else return a;
}
void Fraction::standard(int &top,int &bottom)
{
int gongyueshu;
if(top<&&bottom<)
{
top=-top;
bottom=-bottom;
}
else if(top<||bottom<)
{
if(top>)
{
top=-top;
bottom=-bottom;
}
}
gongyueshu=yueshu(jueduizhi(top),jueduizhi(bottom));
top/=gongyueshu;
bottom/=gongyueshu;
} Fraction::Fraction (int a,int b):top(a),bottom(b)
{
if(b==)
{
cout<<"分母不能为零!分数默认为0/1"<<endl;
top=;
bottom=;
}
}
Fraction::Fraction (Fraction &p)
{
top=p.top;
bottom=p.bottom;
}
Fraction Fraction::add(Fraction p1,Fraction p2)
{
Fraction p3;
p3.bottom=p1.bottom*p2.bottom;
p3.top=p1.top*p2.bottom+p2.top*p1.bottom;
//cout<<p3.top<<p3.bottom<<endl;
standard(p3.top,p3.bottom);
// cout<<p3.top<<p3.bottom<<endl;
return p3;
}
Fraction Fraction::minus(Fraction p1,Fraction p2)
{
Fraction p3;
p3.bottom=p1.bottom*p2.bottom;
p3.top=p1.top*p2.bottom-p2.top*p1.bottom;
standard(p3.top,p3.bottom);
return p3;
}
Fraction Fraction::product(Fraction p1,Fraction p2)
{
Fraction p3;
p3.bottom=p1.bottom*p2.bottom;
p3.top=p1.top*p2.top;
standard(p3.top,p3.bottom);
return p3;
}
Fraction Fraction::division(Fraction p1,Fraction p2)
{
Fraction p3;
p3.bottom=p1.bottom*p2.top;
p3.top=p1.top*p2.bottom;
standard(p3.top,p3.bottom);
return p3;
}
void Fraction::compare(Fraction p1,Fraction p2)
{
int m,n;
m=p1.top*p2.bottom;
n=p1.bottom*p2.top;
if(m/n>)cout<<p1.top<<'/'<<p1.bottom<<'>'<<p2.top<<'/'<<p2.bottom<<endl;
else if(m/n==)cout<<p1.top<<'/'<<p1.bottom<<'='<<p2.top<<'/'<<p2.bottom<<endl;
else cout<<p1.top<<'/'<<p1.bottom<<'<'<<p2.top<<'/'<<p2.bottom<<endl;
}
void Fraction::print(Fraction p)
{
cout<<p.top<<'/'
<<p.bottom<<endl;
}
我的程序当创建一个对象没有赋予初值时是0/1,可以进行加减乘除运算,可以将分数转换为小数,当输入的分母为零时提醒错误并改为默认的0/1,可以两个分数进行比较大小。当分数为负数时,负号在分子上。当分子分母不是最简时,进行约分。

实验总结:
1.初步了解了多文件结构的使用方法和运行机制。
2.对类的定义实现和调用加深了理解。
3.了解了简单的用字符控制输出的方法。
c++第三次实验的更多相关文章
- 20145224&20145238 《信息安全系统设计基础》 第三次实验
20145224&20145238 <信息安全系统设计基础>第三次实验 课程:信息安全系统设计基础 班级:1452 姓名:陈颢文 荆玉茗 学号:20145224 20145238 ...
- 20145330Java程序设计第三次实验
20145330<Java程序设计>第三次实验报告 实验三 敏捷开发与XP实践 实验内容 1.使用git上传代码 2.使用git实现代码开发实践 3.实现代码的重载 实验步骤 使用git上 ...
- 20145320《Java程序设计》第三次实验报告
20145320<Java程序设计>第三次实验报告 北京电子科技学院(BESTI)实验报告 课程:Java程序设计 班级:1453 指导教师:娄嘉鹏 实验日期:2016.04.22 15: ...
- Linux第三次实验报告
北京电子科技学院(BESTI) 实 验 报 告 课程:信息安全系统设计基础 班级:201352 姓名:池彬宁 贺邦 学号:20135212 2013520 ...
- 南京邮电大学java第三次实验报告
实 验 报 告 ( 2017 / 2018学年 第2学期) 课程名称 JAVA语言程序设计 实验名称 Java集成开发环境的安装与使用. Java变量.表达式与控制结构 实验时间 2018 年 4 月 ...
- Java第三次实验敏捷开发与XP实验
实验三-1 1.实验要求: 实验三 敏捷开发与XP实践 http://www.cnblogs.com/rocedu/p/4795776.html, Eclipse的内容替换成IDEA 参考 http: ...
- java第三次实验报告
北京电子科技学院(BESTI) 实验报告 课程: Java程序设计 班级: 1352 姓名: 池彬宁 学号: 20135212 成绩: 指导教师: 娄嘉鹏 实验日期: 2015.6.3 实验密级: 无 ...
- 20162327WJH第三次实验——查找与排序2
20162327WJH第三次实验--查找与排序2 实 验 报 告 课程:程序设计与数据结构 班级: 1623 姓名: 王旌含 学号:20162327 成绩: 指导教师:娄嘉鹏 王志强 实验日期:11月 ...
- 20155210 潘滢昊 Java第三次实验
Java第三次实验 实验内容 在IDEA中使用工具(Code->Reformate Code)把代码重新格式化 在码云上把自己的学习搭档加入自己的项目中,确认搭档的项目加入自己后,下载搭档实验二 ...
- 20155217 《Java程序设计》第三次实验报告
20155217 <Java程序设计>第三次实验报告 实验内容 XP基础 XP核心实践 相关工具 实验要求 没有Linux基础的同学建议先学习<Linux基础入门(新版)>&l ...
随机推荐
- gcc 8.2.1 / MCF thread 简介
gcc 8.2.1 下载 地址 https://gcc-mcf.lhmouse.com/ MCF threadhttps://github.lhmouse.com/ MCF thread 简介MCF ...
- c4b和c4f的区别
1. cc.c4b的参数直接填rgba的值. 2 .cc.c4f(r,g,b,透明度),把rgb值填进去,会发现颜色不对,需要把rgb值除以255,所以最终转换公式是: cc.c3b(r,g,b) = ...
- 关于strcpy函数形参类型的解析和指针作为输入型输出型参数的不同
在C语言中,字符串一直都是热点,关于strcpy函数大家都很熟悉,但是真正了解的很少,一旦用到总会报一大堆莫名其妙错误,今天我就来给大家详细剖析一下strcpy函数. 虽然不能看到strcpy的内部实 ...
- SpringBoot图片上传(三)——调用文件上传项目的方法(同时启动两个项目)
简单说明:图片上传有一个专门的工程A,提供了图片的上传和下载预览,工程B涉及到图片上传以及回显,都是调用的工程A的方法,言外之意就是要同时启动两个项目. 代码: //工程B的html代码 <di ...
- freemarker是什么东西?
前言 由于考虑到网站访问量,以及tocmat可能承受的最大访问压力,我们需要引进一些比较好的技术,来解决这个问题.所以在项目快要结束之际又收到消息,我们要考虑到这些问题然后对现在的项目进行改进,于是就 ...
- elasticsearch-5.2.1在windows下的安装方法
elasticsearch-5.2.1安装方法 1. 安装java 下载安装java jdk 1.8 以上 配置java环境变量 右击[我的电脑]---[属性]-----[高级系统设置]---[环境变 ...
- EF执行SQL返回动态类型
using System; using System.Data.Common; using System.Data.Entity.Core.Objects; using System.Data.Ent ...
- 4.17 小发现(dalao勿点)
洛谷上: (1)iso::sync_with_stio(0); 虽然可以提高cin的速度; 但是有时会RE或WA(如果是WA一般提示Too shot on line); (2)函数最好写上return ...
- Round#534 div.2-C Grid game
http://codeforces.com/contest/1104/problem/C 好厉害的题~ 只要把竖着的放在第一第二行,横着的放在第三/第四行就行. 哦吼,大半夜脑子迷糊地看英文的脑筋急转 ...
- window编程_消息分类
Window应用程序利用Windows消息(Message)与应用程序及系统进行信息交换.消息分为:消息号:有事先定义好的消息名标识字节参数(wParam):用于提供消息的附加信息长字节参数(lPar ...