Part 1 画布小球试验

程序源码

 #include <iostream>
#include "canvas.h"
#include "ball.h" int main() {
Canvas canvas; //创建默认画布,黑底绿色 Ball ball1(,);
system("pause"); ball1.left();
system("pause"); ball1.up();
system("pause"); canvas.changeCanvasFg("E"); // 更新画布前景色
system("pause"); canvas.changeCanvasBg("D"); // 更新画布背景色
system("pause"); return ;
}

main.cpp

 #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

canvas.h

 #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());
}

canvas.cpp

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

ball.h

 #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 Ball::Ball(int x0, int y0):x(x0), y(y0) { // 打印y0-1行空行
for(int line=; line <= y0-; line++)
cout << endl;
// 打印x0-1个空格
for(int col=; col <= x0-; col++)
cout << " "; // 打印小球
cout << "O" << endl;
} void Ball::left(int step) {
x = x-step;
if(x <= )
x=;
// 清屏 system("cls");
// 打印y-1行空行
for(int line=; line <= y-; line++)
cout << endl;
// 打印x-1个空格
for(int col=; col <= x-; 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=; line <= y-; line++)
cout << endl;
// 打印x-1个空格
for(int col=; col <= x-; col++)
cout << " "; // 打印小球
cout << "O" << endl;
} void Ball::up(int step) {
y = y-step;
if(y <= )
y=; // 清屏
system("cls");
// 打印y-1行空行
for(int line=; line <= y-; line++)
cout << endl;
// 打印x-1个空格
for(int col=; col <= x-; 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=; line <= y-; line++)
cout << endl;
// 打印x-1个空格
for(int col=; col <= x-; col++)
cout << " ";
// 打印小球
cout << "O" << endl;
}

ball.cpp

运行截图

PS:system()在Xcode中运行并不是动画,而且不会改变前景色和背景色,也没有清屏,如图,还在研究中

Part 2 GRAGH

程序源码

 #include <iostream>
#include "graph.h"
using namespace std; int main() { Graph graph1('*',);
graph1.draw(); system("pause");
system("cls"); Graph graph2('$',);
graph2.draw(); return ;
}

main.cpp

 #ifndef GRAPH_H
#define GRAPH_H class Graph {
public:
Graph(char ch, int n);
void draw();
private:
char symbol;
int size;
}; #endif

graph.h

 // 类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=;i<=size;i++) //利用行数控制
{ for(j=size-i;j>=;j--) //控制空格的输入
cout<<" ";
for(k=;k<=*i-;k++) //控制graph的输入
cout<<symbol;
cout<<endl;
}
}

graph.cpp

运行截图

PS:依旧和上个程序一样system()的问题

Part 3 Fraction 类

程序源码

 #include <iostream>
#include "Fraction.hpp"
using namespace std; int main(){
Fraction a;
printf("a:");
a.printFraction();
a.compareFraction(, );
a.addFraction(,);
a.printFraction();
cout<<endl<<"b:";
Fraction b(,);
b.printFraction();
b.compareFraction(, );
b.divideFraction(, );
b.printFraction();
cout<<endl<<"c:";
Fraction c();
c.printFraction();
c.compareFraction(, );
c.minusFraction(,);
c.printFraction();
cout<<endl<<"d:";
Fraction d(,);
d.printFraction();
d.compareFraction(, );
d.timeFraction(, );
d.printFraction();
cout<<endl<<"e:";
Fraction e(,);
e.printFraction();
e.compareFraction(, );
e.addFraction(, );
e.printFraction();
return ;
}

main.cpp

 #ifndef Fraction_hpp
#define Fraction_hpp #include <stdio.h>
class Fraction{
public:
Fraction(int x0=,int y0=);
void addFraction(int x1,int y1); //两分数相加
void minusFraction(int x1,int y1); //两分数相减
void timeFraction(int x1,int y1); //两分数相乘
void divideFraction(int x1,int y1); //两分数相除
void printFraction(); //输出分数
void compareFraction(int x1,int y1); //两分数相比较
private:
int top;
int bottom; };
#endif /* Fraction_hpp */

fraction.h

 #include "Fraction.hpp"
#include <iostream>
using namespace std; Fraction::Fraction(int x0,int y0):top(x0),bottom(y0){
} void Fraction::addFraction(int x1,int y1){
int m,n,r,t;
if(y1==) //控制分母为零的情况
cout<<"error!"<<endl;
else
{
if(bottom==y1) //将分数化为最简形式
{ top=top+x1;
if(top%bottom==)
{ top=top/bottom;
bottom=;}
else
{ m=top;
n=bottom;
t=;
r=;
if(m<n)
{t=m;m=n;n=t;}
r=m%n;
while(r!=)
{m=n;n=r;r=m%n;}
top=top/n;
bottom=bottom/n;
}
}
else
{ top=top*y1;
x1=x1*bottom;
top=top+x1;
bottom=bottom*y1;
m=top;
n=bottom;
t=;
r=;
if(m<n)
{t=m;m=n;n=t;}
r=m%n;
while(r!=)
{m=n;n=r;r=m%n;}
top=top/n;
bottom=bottom/n;
}
}
} void Fraction::minusFraction(int x1,int y1){
int m,n,r,t;
if(y1==) //控制分母为零的情况
cout<<"error!"<<endl;
else{
if(bottom==y1) //将分数化为最简形式
{ top=top-x1;
if(top%bottom==)
{ top=top/bottom;
bottom=;}
else
{ m=top;
n=bottom;
t=;
r=;
if(m<n)
{t=m;m=n;n=t;}
r=m%n;
while(r!=)
{m=n;n=r;r=m%n;}
top=top/n;
bottom=bottom/n;
}
}
else
{ top=top*y1;
x1=x1*bottom;
top=top-x1;
bottom=bottom*y1;
m=top;
n=bottom;
t=;
r=;
if(m<n)
{t=m;m=n;n=t;}
r=m%n;
while(r!=)
{m=n;n=r;r=m%n;}
top=top/n;
bottom=bottom/n;
}
}
} void Fraction::timeFraction(int x1,int y1){
if(y1==) //控制分母为零的情况
cout<<"error!"<<endl;
else{
int m,n,r,t;
top=top*x1;
bottom=bottom*y1;
if(top%bottom==) //将分数化为最简形式
{ top=top/bottom;
bottom=;}
else
{
m=top;
n=bottom;
t=;
r=;
if(m<n)
{t=m;m=n;n=t;}
r=m%n;
while(r!=)
{m=n;n=r;r=m%n;}
top=top/n;
bottom=bottom/n;
}
}
} void Fraction::divideFraction(int x1,int y1){
int m,n,r,t;
if(y1==) //控制分母为零的情况
cout<<"error!"<<endl;
else{
top=top*y1;
bottom=bottom*x1;
if(top%bottom==) //将分数化为最简形式
{ top=top/bottom;
bottom=;}
else
{
m=top;
n=bottom;
t=;
r=;
if(m<n)
{t=m;m=n;n=t;}
r=m%n;
while(r!=)
{m=n;n=r;r=m%n;}
top=top/n;
bottom=bottom/n;
}
}
} void Fraction::printFraction(){
if(bottom==) //控制分母为零的情况
cout<<"error!"<<endl;
else
cout<<top<<"/"<<bottom<<endl;
} void Fraction::compareFraction(int x1,int y1){
double x,y;
if(y1==) //控制分母为零的情况
cout<<"error!"<<endl;
else{
x=top*1.0/bottom;
y=x1*1.0/y1;
if(x>y)
cout<<top<<"/"<<bottom<<" > "<<x1<<"/"<<y1<<endl;
else if(x<y)
cout<<top<<"/"<<bottom<<" < "<<x1<<"/"<<y1<<endl;
else cout<<top<<"/"<<bottom<<" = "<<x1<<"/"<<y1<<endl;
}
}

fraction.cpp

运行截图

实验总结与体会

1.学会运用多文件结构,将类的定义、实现、使用部分分开,便于错误排查,使得程序更加便于管理。

2.了解了system("pause),system("color ××")函数的使用,但在Xcode中貌似有点问题,还在研究中,待完善。

3.C与C++的一些语法规则有些混淆了,说明对C++的基础语法规则掌握的还不熟练,需多敲多练。

实验二评论链接

https://www.cnblogs.com/zuiyankh/p/10587674.html#4219118

https://www.cnblogs.com/qsxsc/p/10583875.html#4219112

https://www.cnblogs.com/yfwg/p/10594280.html#4219099

 
 

C++(实验三)的更多相关文章

  1. FPGA与simulink联合实时环路系列——实验三 按键key

    实验三 按键key 实验内容 在FPGA的实验中,经常涉及到按键的使用,按键是必不可少的人机交互的器件之一,在这些实验中,有时将按键的键值读取显示到数码管.LCD或者是通过串口传送到PC的串口助手上进 ...

  2. Java实验三

    20145113 20145102实验三 实验步骤 编码标准 编程标准包含:具有说明性的名字.清晰的表达式.直截了当的控制流.可读的代码和注释,以及在追求这些内容时一致地使用某些规则和惯用法的重要性 ...

  3. Verilog HDL那些事_建模篇笔记(实验三:按键消抖)

    实验三:按键消抖 首先将按键消抖功能分成了两个模块,电平检查模块和10ms延迟模块.电平检测模块用来检测按键信号的变化(是否被按下),10ms延迟模块用来稳定电平检查模块的输入,进而稳定按键信号,防止 ...

  4. 20145229&20145316 《信息安全系统设计基础》实验三 实时系统的移植

    实验封面 实验内容 1.安装ADS(安装文件在00-ads1.2目录下,破解方法00-ads1.2\Crack目录下) 2.安装GIVEIO驱动(安装文件在01-GIVEIO目录下) 3.把整个GIV ...

  5. 20145301&20145321&20145335实验三

    20145301&20145321&20145335实验三 这次实验我的组员为:20145301赵嘉鑫.20145321曾子誉.20145335郝昊 实验内容详见:实验三

  6. 20145212 实验三《敏捷开发与XP实践》

    20145212 实验三<敏捷开发与XP实践> 实验内容 使用git上传代码 与20145223同学一组,使用git相互更改代码 同组实验报告链接:http://www.cnblogs.c ...

  7. 20145213《Java程序设计》实验三敏捷开发与XP实践

    20145213<Java程序设计>实验三敏捷开发与XP实践 实验要求 1.XP基础 2.XP核心实践 3.相关工具 实验内容 1.敏捷开发与XP 软件工程是把系统的.有序的.可量化的方法 ...

  8. 20145206《Java程序设计》实验三实验报告

    20145206<Java程序设计>实验三实验报告 实验内容 XP基础 XP核心实践 相关工具 实验步骤 (一)敏捷开发与XP 软件工程是把系统的.有序的.可量化的方法应用到软件的开发.运 ...

  9. 20145308刘昊阳 《Java程序设计》实验三 敏捷开发与XP实践 实验报告

    20145308刘昊阳 <Java程序设计>实验三 敏捷开发与XP实践 实验报告 实验名称 敏捷开发与XP实践 实验内容 XP基础 XP核心实践 相关工具 统计的PSP(Personal ...

  10. 20145337实验三实验报告——敏捷开发与XP实践

    20145337实验三实验报告--敏捷开发与XP实践 实验名称 敏捷开发与XP实践 实验内容 XP基础 XP核心实践 相关工具 ** 实验步骤**### 敏捷开发与XP 软件工程包括下列领域:软件需求 ...

随机推荐

  1. 主线程 RunLoop 学习笔记

    以下为主RunLoop 的输出,能够看到不同的source0,source1,observer ---------------------------------- CFRunLoop{wakeup ...

  2. 删除打开方式里的wine

    title: "删除打开方式里的wine" date: 2018-05-27T13:54:28+08:00 tags: ["wine"] categories: ...

  3. 极致21点开发DAY4

    完成的内容:1.修改上一篇博文中的Bug  2.完成任务窗口逻辑 using System; using System.Collections.Generic; using UnityEngine; ...

  4. c# 调用RDP和SSH实现远程登陆

    1.ssh的登陆实现: windows平台可以安装OpenSSHforWindows 后,可以通过cmd 执行ssh的指令. 也可以在c#编程中实现ssh的登陆: var p = new System ...

  5. JS文件写法操作,DOM基本操作

     js文件写法.规范 // 定义全局变量 var num = 0;//这个是用来记数的. // 页面加载完成 window.onload = funtion(){ intVar();//初始化变量 s ...

  6. postman抓包工具与kap项目部署(新猿旺学习总结)

    postman抓包工具 1.post请求在哪里输入数据:选择请求方法-post--->在body里面如图位置输入参数和值,如果是json格式在raw出填写 get请求在哪里输入数据:选择请求方法 ...

  7. bloc控制读写文件

    import 'package:flutter/material.dart'; import 'dart:io'; import 'package:path_provider/path_provide ...

  8. Prometheus监控学习笔记之Prometheus 2.0 告警规则介绍

    0x00 变化 Prometheus 2.0 已经发布一段时间了,从今天开始我将分几篇文章为大家介绍其中的一些变化. 此篇文章主要介绍 2.0 的告警规则声明的新写法. 从 1.x 到 2.0 规则声 ...

  9. chocolatey使用

    chocolatey使用 安装 使用管理员权限打开CMD,输入: @powershell -NoProfile -ExecutionPolicy Bypass -Command "iex ( ...

  10. Bugku-CTF之never give up

    Day23 never give up http://123.206.87.240:8006/test/hello.php   本题要点:url编码,base64编码,代码审计,php函数       ...