gpt4 book ai didi

c++ - 为什么我的程序的轴关闭了

转载 作者:行者123 更新时间:2023-11-28 03:44:47 26 4
gpt4 key购买 nike

    //============================================================================
// Name : Assignment.cpp
// Author : Tim Bialecki
// Version :
//============================================================================

#include <iostream>
#include <math.h>
using namespace std;

void circle(int x, int y, int radius);
void line(int a, int b, int c, int d);
bool buffer[26][81];
char drawSpace[26][81];

int main() {
int a = 75;
int b = 5;
int c = 4;
int d = 26;
/*cout << "please enter an x coordinate for the center of the circle";
cin >> x;
cout << "please enter a y coordinate for the center of the circle";
cin >> y;
cout << "please enter a value for the radius of the circle";
cin >> radius;*/

circle(a, b, c);
for (int col = 80; col >= 0; col--) {

for (int row = 25; row >= 0; row--) {
cout << drawSpace[row][col];
}
cout << "\n";
}
return 0;
}

void circle(int x, int y, int radius){
/*if (x + radius >= 81 || y + radius >= 26 || y - radius <= 26){
cout << "the coordinates provided for the circle will not fit on the screen" << endl;
return;
}*/

for (int i = 0; i < 26; i++) {
for(int j = 0; j < 81; j++) {
int a = abs (x - j);
int b = abs (y - i);
int distance = pow(a, 2) + pow(b, 2);
int realDistance = pow(radius, 2);
if (abs(realDistance - distance) <= 3){
buffer[i][j] = true;
}
}
}

for (int m = 0; m < 26; m++){
for(int n = 0; n < 81; n++){
if (buffer[m][n]){
drawSpace[m][n] = 42;
}
else
drawSpace[m][n] = 32;
}
}
}

void line(int a, int b, int c, int d){
int intercept = 0;
double rise = d - b;
double run = c - a;
double slope = rise/run;
intercept = b - (slope*a);
for (int i = 0; i < 26; i++) {
for(int j = 0; j < 81; j++) {
int newIntercept = i - (slope*j);
int test = abs (intercept - newIntercept);
if (test <= 0)
buffer[i][j] = true;
else
buffer[i][j] = false;
}

}

for (int m = 0; m < 26; m++){
for(int n = 0; n < 81; n++){
if (buffer[m][n])
drawSpace[m][n] = 42;
else
drawSpace[m][n] = 32;
}
}
}

这段代码是一个正在进行的工作,但我正在尝试编写一个程序,它接受直线和圆的坐标和尺寸的输入,并在终端窗口中将它们打印出来,就像它是一个 81x26 图形一样.我刚刚提供了示例输入来对此进行测试,但出于某种原因,形状没有以适当的方向打印到 x 轴和 y 轴。我尝试了很多不同的方法来解决这个问题,但没有成功。希望有人能帮忙。谢谢

最佳答案

我觉得不错:

               ***    
** **
* *
* *
* *
* *
* *
** **
***

它不是完美的圆形,因为字符的高度大于宽度。

编辑:这只是我输出的前几行。根据评论和再次查看代码,我认为行和列混淆了。

 for (int col = 80; col >= 0; col--) {
for (int row = 25; row >= 0; row--) {
cout << drawSpace[row][col];
}
cout << "\n";
}

每个“列”后都有一个换行符。交换两个 for 行可能会产生您想要的结果。

关于c++ - 为什么我的程序的轴关闭了,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/7988469/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com