一个函数奇怪的行为

时间:2017-10-03 15:56:26

标签: c function

在练习中,我需要比较一个distances数组并告诉最短路径。为此,我需要阅读一个带有城市的输入文件。坐标(x,y)并找到所有可能的路线并计算每条路线的总距离。这个练习的第一部分我已经做到了。我做了以下功能来打印所有可能的路线:

void Troca(int *x, int *y)
{
    int aux;
    aux = *x;
    *x = *y;
    *y = aux;
}

void Permuta(FILE *saida, Cidade *C, int *sequencia, int inicio, int termino, int totalViagens, float **distanciasAux)
{
    int i, j;
    totalViagens = TotalViagens(termino);
    if(inicio == termino)
    {
        for(i = 0; i < termino; i++)
            fprintf(saida, "%d\t", sequencia[i]+1);
        fprintf(saida, "= %f\n", Distancia(C, termino, sequencia));

        *distanciasAux = (float *) malloc(totalViagens*sizeof(float));
        float *distancias = *distanciasAux;
        distancias[i] = Distancia(C, termino, sequencia);
        printf("\n%f\n", distancias[i]);
    }
    else
    {
        for(j = inicio; j < termino; j++)
        {
            Troca((sequencia+inicio), (sequencia+j));
            Permuta(saida, C, sequencia, inicio+1, termino, totalViagens, distanciasAux);
            Troca((sequencia+inicio), (sequencia+j));
        }
    }
}

我在输出文件中得到以下结果:

120
1   2   3   4   5   = 47.063416
1   2   3   5   4   = 61.486000
1   2   4   3   5   = 46.907471
1   2   4   5   3   = 62.644718
1   2   5   4   3   = 58.522675
1   2   5   3   4   = 57.208015
1   3   2   4   5   = 51.513924
1   3   2   5   4   = 61.814468
1   3   4   2   5   = 47.235947
1   3   4   5   2   = 58.522675
1   3   5   4   2   = 62.644718
1   3   5   2   4   = 61.658531
1   4   3   2   5   = 46.077225
1   4   3   5   2   = 57.208015
1   4   2   3   5   = 50.199272
1   4   2   5   3   = 61.658527
1   4   5   2   3   = 61.814472
1   4   5   3   2   = 61.485996
1   5   3   4   2   = 46.907475
1   5   3   2   4   = 50.199272
1   5   4   3   2   = 47.063412
1   5   4   2   3   = 51.513927
1   5   2   4   3   = 47.235943
1   5   2   3   4   = 46.077229
2   1   3   4   5   = 58.522675
2   1   3   5   4   = 62.644714
2   1   4   3   5   = 57.208019
2   1   4   5   3   = 61.486000
2   1   5   4   3   = 47.063416
2   1   5   3   4   = 46.907475
2   3   1   4   5   = 61.814472
2   3   1   5   4   = 51.513927
2   3   4   1   5   = 46.077225
2   3   4   5   1   = 47.063412
2   3   5   4   1   = 61.485996
2   3   5   1   4   = 50.199272
2   4   3   1   5   = 47.235947
2   4   3   5   1   = 46.907475
2   4   1   3   5   = 61.658527
2   4   1   5   3   = 50.199268
2   4   5   1   3   = 51.513927
2   4   5   3   1   = 62.644714
2   5   3   4   1   = 57.208015
2   5   3   1   4   = 61.658531
2   5   4   3   1   = 58.522675
2   5   4   1   3   = 61.814472
2   5   1   4   3   = 46.077225
2   5   1   3   4   = 47.235947
3   2   1   4   5   = 61.486000
3   2   1   5   4   = 47.063416
3   2   4   1   5   = 50.199272
3   2   4   5   1   = 51.513927
3   2   5   4   1   = 61.814472
3   2   5   1   4   = 46.077225
3   1   2   4   5   = 62.644714
3   1   2   5   4   = 58.522675
3   1   4   2   5   = 61.658531
3   1   4   5   2   = 61.814472
3   1   5   4   2   = 51.513927
3   1   5   2   4   = 47.235947
3   4   1   2   5   = 57.208015
3   4   1   5   2   = 46.077225
3   4   2   1   5   = 46.907471
3   4   2   5   1   = 47.235943
3   4   5   2   1   = 58.522675
3   4   5   1   2   = 47.063412
3   5   1   4   2   = 50.199272
3   5   1   2   4   = 46.907475
3   5   4   1   2   = 61.485996
3   5   4   2   1   = 62.644714
3   5   2   4   1   = 61.658531
3   5   2   1   4   = 57.208015
4   2   3   1   5   = 51.513927
4   2   3   5   1   = 50.199272
4   2   1   3   5   = 62.644714
4   2   1   5   3   = 46.907471
4   2   5   1   3   = 47.235943
4   2   5   3   1   = 61.658527
4   3   2   1   5   = 47.063416
4   3   2   5   1   = 46.077225
4   3   1   2   5   = 58.522675
4   3   1   5   2   = 47.235947
4   3   5   1   2   = 46.907475
4   3   5   2   1   = 57.208015
4   1   3   2   5   = 61.814468
4   1   3   5   2   = 61.658531
4   1   2   3   5   = 61.486000
4   1   2   5   3   = 57.208015
4   1   5   2   3   = 46.077225
4   1   5   3   2   = 50.199272
4   5   3   1   2   = 62.644714
4   5   3   2   1   = 61.485996
4   5   1   3   2   = 51.513927
4   5   1   2   3   = 47.063412
4   5   2   1   3   = 58.522675
4   5   2   3   1   = 61.814472
5   2   3   4   1   = 46.077225
5   2   3   1   4   = 61.814468
5   2   4   3   1   = 47.235947
5   2   4   1   3   = 61.658531
5   2   1   4   3   = 57.208015
5   2   1   3   4   = 58.522675
5   3   2   4   1   = 50.199272
5   3   2   1   4   = 61.486000
5   3   4   2   1   = 46.907471
5   3   4   1   2   = 57.208015
5   3   1   4   2   = 61.658531
5   3   1   2   4   = 62.644714
5   4   3   2   1   = 47.063416
5   4   3   1   2   = 58.522675
5   4   2   3   1   = 51.513924
5   4   2   1   3   = 62.644714
5   4   1   2   3   = 61.486000
5   4   1   3   2   = 61.814472
5   1   3   4   2   = 47.235943
5   1   3   2   4   = 51.513924
5   1   4   3   2   = 46.077225
5   1   4   2   3   = 50.199268
5   1   2   4   3   = 46.907471
5   1   2   3   4   = 47.063416

其中120代表总体可能性(这是针对五个城市),下面的行显示每个城市的指数+ 1,最后是路线的总距离。

在这个函数中,我创建了一个数组distancias,因此我可以存储所有总距离值。我要求它在屏幕上打印每条路线的所有总距离,它给了我正确的价值。但是为了比较这些值,我需要在另一个函数中进行,因为函数Permuta调用自身,我无法找到比较其中的值的方法。所以我做了下一个功能:

void CriaSequencia(FILE *saida, Cidade *C, int *sequencia, int numeroCidade, int totalViagens)
{
    int i;
    totalViagens = TotalViagens(numeroCidade);
    float *distancias;
    for(i = 0; i < numeroCidade; i++)
    {
        sequencia[i] = i;
    }
    Permuta(saida, C, sequencia, 0, numeroCidade, TotalViagens(numeroCidade), &distancias);
    for(i = 0; i < totalViagens; i++)
        printf("\n%f\n", distancias[i]);
}

我没有比较它们,因为我想确定这些值是否正确,但当我要求它打印时,它并没有给我与其他函数相同的值我。它有一个奇怪的行为:

47.063416
47.063416
47.063416
47.063416
47.063416
0.000000
0.000000
0.000000
0.000000
0.000000
0.000000
0.000000
0.000000
0.000000
0.000000
0.000000
0.000000
0.000000
0.000000
0.000000
0.000000
0.000000
0.000000
0.000000
0.000000
0.000000
0.000000
0.000000
0.000000
0.000000
0.000000
0.000000
0.000000
0.000000
0.000000
0.000000
0.000000
0.000000
0.000000
0.000000
0.000000
0.000000
0.000000
0.000000
0.000000
0.000000
0.000000
0.000000
0.000000
0.000000
0.000000
0.000000
0.000000
0.000000
0.000000
0.000000
0.000000
0.000000
0.000000
0.000000
0.000000
0.000000
0.000000
0.000000
0.000000
0.000000
0.000000
0.000000
0.000000
0.000000
0.000000
0.000000
0.000000
0.000000
0.000000
0.000000
0.000000
0.000000
0.000000
0.000000
0.000000
0.000000
0.000000
0.000000
0.000000
0.000000
0.000000
0.000000
0.000000
0.000000
0.000000
0.000000
0.000000
0.000000
0.000000
0.000000
0.000000
0.000000
0.000000
0.000000
0.000000
0.000000
0.000000
0.000000
0.000000
0.000000
0.000000
0.000000
0.000000
0.000000
0.000000
0.000000
0.000000
0.000000
0.000000
0.000000
0.000000
0.000000
0.000000
0.000000

我想知道为什么它没有打印正确的值以及我的功能中可能存在的问题。

其他信息:

城市的结构:

typedef struct
{
    int x;
    int y;
} Cidade;

Distancia功能:

float Distancia(Cidade *C, int numeroCidade, int *sequencia)
{
    int i;
    float total = 0;
    float distancia;
    for (i = 0; i < numeroCidade; i++)
    {
        /* distance from one city in the sequence to the next = ... ? */
        if(i == numeroCidade-1)
        {
            int distanciaX = pow(C[sequencia[i]].x - C[sequencia[0]].x, 2);
            int distanciaY = pow(C[sequencia[i]].y - C[sequencia[0]].y, 2);
            distancia = sqrt(distanciaX + distanciaY);
            total = total + distancia;
        }
        else
        {
            int distanciaX = pow(C[sequencia[i]].x - C[sequencia[i+1]].x, 2);
            int distanciaY = pow(C[sequencia[i]].y - C[sequencia[i+1]].y, 2);
            distancia = sqrt(distanciaX + distanciaY);
            total = total + distancia;
        }
    }
    return total;
}

TotalViagens功能

int TotalViagens(int numeroCidade)
{
    int i;
    int total = 1;
    for(i = 1; i <= numeroCidade; i++)
        total = total*i;
    return total;
}

0 个答案:

没有答案