新手求大神帮忙解决一下,拜谢!!!

用C++写一个*号组成的等腰三角形,打印在控制台上可以通过键盘控制使它上下左右移动。要求用类写,注释随意。

6个回答

共3个文件

1) MobileTriangle.cpp 类定义文件

#include <iostream>
using namespace std;

#include "MobileTriangle.h"
#include <stdio.h>

MobileTriangle::MobileTriangle(int init_row, int init_leftMargin, int init_topMargin)
{
    row = init_row;
    leftMargin = init_leftMargin;
    topMargin = init_topMargin;
}

void MobileTriangle::Show(int size, int left, int top)
{
    system("CLS");

    int space = size + top;
    for (int i = 0; i < size + top; i++) {
        for (int w = 0; w < left; w++)
        {
            cout << " ";
        }

        for (int j = space; j > 0; j--) {

            cout << " ";
        }
        for (int star = 0; star <= (i - top) * 2; star++) {
            cout << "*";
        }
        space--;
        cout << endl;
    }
}

2) MobileTriangle.h 类头文件

class MobileTriangle
{
public:
    MobileTriangle(int init_row, int init_leftMargin, int init_topMargin);
    void Show(int size,int left, int top);
private:
    int row;
    int leftMargin;
    int topMargin;
};

3) main.c 主程序

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

#include <stdio.h>
#include <conio.h>

#define KB_UP 72
#define KB_DOWN 80
#define KB_LEFT 75
#define KB_RIGHT 77
#define KB_ESCAPE 27

void main() {
    int size;
    int left;
    int top;
    cout << "Enter the size = ";
    cin >> size;
    cout << "Enter the leftMargin = ";
    cin >> left;
    cout << "Enter the topMargin = ";
    cin >> top;

    MobileTriangle mobileTriangle(size, left, top);
    mobileTriangle.Show(size, left, top);

    cout << "请按方向键移动三角形,按q键退出程序" << endl;

    int KB_code = 0;

    while (KB_code != KB_ESCAPE)
    {
        if (_kbhit())
        {
            KB_code = _getch();

            switch (KB_code)
            {
            case KB_LEFT:
                left--;
                if (left >= 0)
                {
                    mobileTriangle.Show(size, left, top);
                } 
                else
                {
                    cout << "cant't move left";
                }

                break;

            case KB_RIGHT:
                left++;
                mobileTriangle.Show(size, left, top);
                break;

            case KB_UP:
                top--;
                if (top >= 0)
                {
                    mobileTriangle.Show(size, left, top);
                }
                else
                {
                    cout << "cant't move top";
                }
                break;

            case KB_DOWN:
                top++;
                mobileTriangle.Show(size, left, top);
                break;
            }
        }
    }
}

用心回答每个问题,如果对您有帮助,请采纳答案好吗,谢谢!

fight_in_dl
战在春秋 有些着急,应该是按Escape键退出程序~~
2 年多之前 回复

#include
#include
#include
using namespace std;

class Triangle
{
public:
Triangle();
~Triangle();
bool ReadKey();//读取键盘的按键
void Refesh();//当键盘按下时,刷新
private:
int X, Y;

void DrawTriangle(int x, int y);//根据位移量画三角形

};

Triangle::Triangle()
{
//初始化
X = 0;
Y = 0;
DrawTriangle(X,Y);
}

Triangle::~Triangle()
{
//什么都不用做
}

void Triangle::DrawTriangle(int x,int y)
{
//纵向位移
for (int i = y;i > 0;i--)cout << "\n";

//画第一行
for (int j = x + 2;j > 0; j--)cout << " ";
cout << "*"<<endl;

//画第二行
for (int j = x + 1;j > 0;j--)cout << " ";
cout << "***" << endl;

//画第三行
for (int j = x;j > 0;j--)cout << " ";
cout << "*****" << endl;

}

void Triangle::Refesh()
{
//隐藏控制台光标
CONSOLE_CURSOR_INFO cursor;
cursor.bVisible =FALSE;
cursor.dwSize = 20;
SetConsoleCursorInfo(GetStdHandle(STD_OUTPUT_HANDLE), &cursor);

//清空控制台
system("cls");

//重画三角形
DrawTriangle(X, Y);

}

bool Triangle::ReadKey()
{
HANDLE hStdin;
DWORD cNumRead;
INPUT_RECORD irInBuf;
bool IsKeyDown=FALSE;//键是否被按下,初始化为false

//获取控制台句柄
hStdin = GetStdHandle(STD_INPUT_HANDLE);

//读取键盘输入
ReadConsoleInput(hStdin, &irInBuf, 1, &cNumRead);

if (irInBuf.Event.KeyEvent.bKeyDown)//当键按下时
{
    IsKeyDown = TRUE;
    if (irInBuf.Event.KeyEvent.wVirtualKeyCode == VK_LEFT&&X > 0)X--;//如果按键为左方向键,水平位置后退一格
    if (irInBuf.Event.KeyEvent.wVirtualKeyCode == VK_RIGHT)X++;//如果按键为右方向键,水平位置前进一格
    if (irInBuf.Event.KeyEvent.wVirtualKeyCode == VK_UP&&Y> 0)Y--;//如果按键为上方向键,垂直位置上移一格
    if (irInBuf.Event.KeyEvent.wVirtualKeyCode == VK_DOWN)Y++;//如果按键为下方向键,垂直位置下移一格
}
return IsKeyDown;

}

int main()
{
Triangle triangle;
while (true)
{
if (triangle.ReadKey())triangle.Refesh();//如果有键按下,就刷新一下
}
return 0;
}

头文件是iostream,windows.h和Windows.system.h

#include
#include
#include
using namespace std;

class Triangle
{
public:
Triangle();
~Triangle();
bool ReadKey();//读取键盘的按键
void Refesh();//当键盘按下时,刷新
private:
int X, Y;

void DrawTriangle(int x, int y);//根据位移量画三角形

};

Triangle::Triangle()
{
//初始化
X = 0;
Y = 0;
DrawTriangle(X,Y);
}

Triangle::~Triangle()
{
//什么都不用做
}

void Triangle::DrawTriangle(int x,int y)
{
//纵向位移
for (int i = y;i > 0;i--)cout << "\n";

//画第一行
for (int j = x + 2;j > 0; j--)cout << " ";
cout << "*"<<endl;

//画第二行
for (int j = x + 1;j > 0;j--)cout << " ";
cout << "***" << endl;

//画第三行
for (int j = x;j > 0;j--)cout << " ";
cout << "*****" << endl;

}

void Triangle::Refesh()
{
//隐藏控制台光标
CONSOLE_CURSOR_INFO cursor;
cursor.bVisible =FALSE;
cursor.dwSize = 20;
SetConsoleCursorInfo(GetStdHandle(STD_OUTPUT_HANDLE), &cursor);

//清空控制台
system("cls");

//重画三角形
DrawTriangle(X, Y);

}

bool Triangle::ReadKey()
{
HANDLE hStdin;
DWORD cNumRead;
INPUT_RECORD irInBuf;
bool IsKeyDown=FALSE;//键是否被按下,初始化为false

//获取控制台句柄
hStdin = GetStdHandle(STD_INPUT_HANDLE);

//读取键盘输入
ReadConsoleInput(hStdin, &irInBuf, 1, &cNumRead);

if (irInBuf.Event.KeyEvent.bKeyDown)//当键按下时
{
    IsKeyDown = TRUE;
    if (irInBuf.Event.KeyEvent.wVirtualKeyCode == VK_LEFT&&X > 0)X--;//如果按键为左方向键,水平位置后退一格
    if (irInBuf.Event.KeyEvent.wVirtualKeyCode == VK_RIGHT)X++;//如果按键为右方向键,水平位置前进一格
    if (irInBuf.Event.KeyEvent.wVirtualKeyCode == VK_UP&&Y> 0)Y--;//如果按键为上方向键,垂直位置上移一格
    if (irInBuf.Event.KeyEvent.wVirtualKeyCode == VK_DOWN)Y++;//如果按键为下方向键,垂直位置下移一格
}
return IsKeyDown;

}

int main()
{
Triangle triangle;
while (true)
{
if (triangle.ReadKey())triangle.Refesh();//如果有键按下,就刷新一下
}
return 0;
}


#include
#include
using namespace std;

class Trangle{

public:
Trangle(int n);
~Trangle();
void printTrangle();
void controlLocation();

private:
int line; //三角形行数
int up;
int left;
};

Trangle::Trangle(int n){
line = n;
up = 0;
left = 0;
}

Trangle::~Trangle(){
}

void Trangle::printTrangle(){
for(int i = 0; i < up; ++i){
cout<<endl;
}

for(int i = 0; i < line; ++i)
{
    for(int j =0; j < left; ++j){
        cout<<" ";
    }
    for(int j = line-1; j > i; --j)//打印行前的空格
        cout<<" ";
    for(int j = 0; j < 2*i+1; ++j)//输出图像
        cout<<"*";
  cout<<endl;
}

}

void Trangle::controlLocation(){
int ch;
while( (ch=getch())!=0x1B ) /* Press ESC to quit... */
{
system("cls");
switch(ch)
{
case 72: //up

if(up > 0){
--up;
}
break;
case 80: //down
++up;
break;
case 75: // left
if(left > 0){
--left;
}
break;
case 77: //right
++left;
break;
default:
break;
}
printTrangle();
}
}

void main(){
cout<<"请输入行数:";
int n;
cin>>n;

Trangle *trangle = new Trangle(n);
trangle->printTrangle();
trangle->controlLocation();

}

u014167893
冷咖啡15 如果满足你的要求请采纳,如果没有满足你的要求请说明
2 年多之前 回复

已经有大神给出答案了,不求赏金,如果觉得还行就赞一个吧

Csdn user default icon
上传中...
上传图片
插入图片
抄袭、复制答案,以达到刷声望分或其他目的的行为,在CSDN问答是严格禁止的,一经发现立刻封号。是时候展现真正的技术了!