C++ C++;两个碰撞对象不工作的控制台应用程序 #包括“stdafx.h” #包括 #包括 #包括 使用名称空间std; struct spaceship{//创建飞船 int x,y; 字符呼号[51]; }; void shiprandloc(宇宙飞船*飞船,int maxrange){//随机化位置 ship->x=rand()%maxrange; ship->y=rand()%maxrange; } int shipdetcol(飞船*ship1,飞船*ship2,float colrange){//如果它们碰撞,返回1 colrangenumloops; for(int i=0;i呼号,“红色1”); strcpy_s(船舶2->呼号,“蓝色1”); shiprandloc(ship1,maxloc); shiprandloc(ship2,maxloc); d=sqrt((ship1->x-ship2->x)*(ship1->y-ship2->y));//查找两艘船之间的距离。 而(!shipdetcol(ship1、ship2、maxcol)){ ++环碳纳米管; } 删除ship1、ship2; } 返回0; }

C++ C++;两个碰撞对象不工作的控制台应用程序 #包括“stdafx.h” #包括 #包括 #包括 使用名称空间std; struct spaceship{//创建飞船 int x,y; 字符呼号[51]; }; void shiprandloc(宇宙飞船*飞船,int maxrange){//随机化位置 ship->x=rand()%maxrange; ship->y=rand()%maxrange; } int shipdetcol(飞船*ship1,飞船*ship2,float colrange){//如果它们碰撞,返回1 colrangenumloops; for(int i=0;i呼号,“红色1”); strcpy_s(船舶2->呼号,“蓝色1”); shiprandloc(ship1,maxloc); shiprandloc(ship2,maxloc); d=sqrt((ship1->x-ship2->x)*(ship1->y-ship2->y));//查找两艘船之间的距离。 而(!shipdetcol(ship1、ship2、maxcol)){ ++环碳纳米管; } 删除ship1、ship2; } 返回0; },c++,collision,math.h,C++,Collision,Math.h,用于检查距离的平方根函数不起作用。碰撞如果命中,则返回1;如果未命中,则返回0。我错过了什么 #include "stdafx.h" #include <iostream> #include <string.h> #include <math.h> using namespace std; struct spaceship { // create the ship int x, y; char callsign[51]; }; void s

用于检查距离的平方根函数不起作用。碰撞如果命中,则返回1;如果未命中,则返回0。我错过了什么

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

struct spaceship { // create the ship
    int x, y;
    char callsign[51];
};

void shiprandloc(spaceship *ship, int maxrange) { //randomize location
    ship->x = rand() % maxrange;
    ship->y = rand() % maxrange;
}

int shipdetcol(spaceship *ship1, spaceship *ship2, float colrange) { //if they collide return a 1
    colrange < 10;
    return 1;
}

int main()
{
    int maxloc = 100, maxcol = 10;
    int numloops;
    cout << "Enter the Number of Collisions to Simulate: ";
    cin >> numloops;
    for (int i = 0; i < numloops; i++) {
        int loopcnt = 0;
        spaceship *ship1, *ship2;
        ship1 = new spaceship;
        ship2 = new spaceship;
        strcpy_s(ship1->callsign, "Red1");
        strcpy_s(ship2->callsign, "Blue1");
        shiprandloc(ship1, maxloc);
        shiprandloc(ship2, maxloc);
        d = sqrt((ship1->x - ship2->x)*(ship1->y - ship2->y)); //find distance between the two ships.
        while (!shipdetcol(ship1, ship2, maxcol)) {
            ++loopcnt;
        }
        delete ship1, ship2;
    }
    return 0;
}
您的sqrt将尝试取一个数字的平方根,但是如果是负数,那么您应该检查任何减法是否会导致负数,因为它可能会使乘法结果也是负数,从而弄乱结果

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

struct spaceship { // create the ship
    int x, y;
    char callsign[51];
};

void shiprandloc(spaceship *ship, int maxrange) { //randomize location
    ship->x = rand() % maxrange;
    ship->y = rand() % maxrange;
}

int shipdetcol(spaceship *ship1, spaceship *ship2, float colrange) { //if they collide return a 1
    colrange < 10;
    return 1;
}

int main()
{
    int maxloc = 100, maxcol = 10;
    int numloops;
    cout << "Enter the Number of Collisions to Simulate: ";
    cin >> numloops;
    for (int i = 0; i < numloops; i++) {
        int loopcnt = 0;
        spaceship *ship1, *ship2;
        ship1 = new spaceship;
        ship2 = new spaceship;
        strcpy_s(ship1->callsign, "Red1");
        strcpy_s(ship2->callsign, "Blue1");
        shiprandloc(ship1, maxloc);
        shiprandloc(ship2, maxloc);
        d = sqrt((ship1->x - ship2->x)*(ship1->y - ship2->y)); //find distance between the two ships.
        while (!shipdetcol(ship1, ship2, maxcol)) {
            ++loopcnt;
        }
        delete ship1, ship2;
    }
    return 0;
}
在这里:

sqrt((ship1->x - ship2->x)*(ship1->y - ship2->y));
colrange<10;
返回1;
您的代码没有检查,如果colrange小于10,它只是在编写一个表达式。应该是:

colrange < 10;
return 1;

if(colrange这个人类想象中的野兽

if(colrange<10){
return 1;
}
else
{
return 0;
}
删除
ship2
,但不删除
ship1
。此处逗号被视为序列(逗号)运算符,该表达式的结果是最后一个子表达式的结果

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

struct spaceship { // create the ship
    int x, y;
    char callsign[51];
};

void shiprandloc(spaceship *ship, int maxrange) { //randomize location
    ship->x = rand() % maxrange;
    ship->y = rand() % maxrange;
}

int shipdetcol(spaceship *ship1, spaceship *ship2, float colrange) { //if they collide return a 1
    colrange < 10;
    return 1;
}

int main()
{
    int maxloc = 100, maxcol = 10;
    int numloops;
    cout << "Enter the Number of Collisions to Simulate: ";
    cin >> numloops;
    for (int i = 0; i < numloops; i++) {
        int loopcnt = 0;
        spaceship *ship1, *ship2;
        ship1 = new spaceship;
        ship2 = new spaceship;
        strcpy_s(ship1->callsign, "Red1");
        strcpy_s(ship2->callsign, "Blue1");
        shiprandloc(ship1, maxloc);
        shiprandloc(ship2, maxloc);
        d = sqrt((ship1->x - ship2->x)*(ship1->y - ship2->y)); //find distance between the two ships.
        while (!shipdetcol(ship1, ship2, maxcol)) {
            ++loopcnt;
        }
        delete ship1, ship2;
    }
    return 0;
}
你的函数总是返回1。你的意思可能是这样的

delete ship1, ship2;
注意,您需要坐标之间差值的绝对值

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

struct spaceship { // create the ship
    int x, y;
    char callsign[51];
};

void shiprandloc(spaceship *ship, int maxrange) { //randomize location
    ship->x = rand() % maxrange;
    ship->y = rand() % maxrange;
}

int shipdetcol(spaceship *ship1, spaceship *ship2, float colrange) { //if they collide return a 1
    colrange < 10;
    return 1;
}

int main()
{
    int maxloc = 100, maxcol = 10;
    int numloops;
    cout << "Enter the Number of Collisions to Simulate: ";
    cin >> numloops;
    for (int i = 0; i < numloops; i++) {
        int loopcnt = 0;
        spaceship *ship1, *ship2;
        ship1 = new spaceship;
        ship2 = new spaceship;
        strcpy_s(ship1->callsign, "Red1");
        strcpy_s(ship2->callsign, "Blue1");
        shiprandloc(ship1, maxloc);
        shiprandloc(ship2, maxloc);
        d = sqrt((ship1->x - ship2->x)*(ship1->y - ship2->y)); //find distance between the two ships.
        while (!shipdetcol(ship1, ship2, maxcol)) {
            ++loopcnt;
        }
        delete ship1, ship2;
    }
    return 0;
}

最后,它是C++,所以不要使用:

int shipdetcol(spaceship &ship1, spaceship &ship2, float colrange) 
{
    return  colrange > sqrt(abs(((ship1.x - ship2.x)*(ship1.y - ship2.y)));
}
使用


你所说的“检查距离的平方根函数不起作用”是什么意思?你计算一些距离,将其存储在
d
中,然后再也不使用
d
做任何事情……这应该如何“起作用”?
shipdetcol()<代码> >除了<代码> >返回1 。这里确实有很多错误的逻辑。您需要在程序中跟踪程序的含义,直到您得到它正在做的事情,然后使它做任何您希望它做的事情。<代码>删除SIP1,SIP2;<代码> -尝试在现代C++中不做手动内存管理。使用智能指针裸
new
/
delete
在现代代码中是一种非常常见的代码味道,除了在实现智能指针和容器时(以及其他一些罕见的角落).99%以上的程序不需要在任何地方编写
删除
。关于
删除ship1,ship2;
,它*真的*应该
返回colrange<10;
还值得注意的是,可能根本没有理由动态分配
太空船
号。
太空船{0,0,“Red1”}
可能就足够了。@user4581301 true,但如果我继续挖掘,整个程序将剩下大约10行代码:P还有一个非常糟糕的RNG使用,类可能有一些成员函数,yadayada。
#include <string>


std::string callsign;
ship1 = new spaceship { 0, 0, "Red1"};