【解決方法】浮動小数点数を受け入れるようにするには、wait_time を取得するにはどうすればよいですか?


C
int main(int argc, char *argv[]) {
    if (argc != 3) {
        printf("Error: Invalid number of arguments\n");
        return 1;
    }

    int waiting_time = atof(argv[1]); // get waiting time from command line argument
    int num_steps = atoi(argv[2]); // get number of steps from command line argument
    FILE *input_file = fopen("input.txt", "r");
    if (input_file == NULL) {
        printf("Error: Unable to open input file\n");
        return 1;
    }

    int rows, cols, ant1_row, ant1_col, ant2_row, ant2_col;

    if (fscanf(input_file, "%d %d %d %d %d %d", &rows, &cols, &ant1_row, &ant1_col, &ant2_row, &ant2_col) != 6) {
        printf("Error: Invalid input format\n");
        return 1;
    }

    char **map = (char**)malloc(rows * sizeof(char*));

    for (int i = 0; i < rows; i++) {
        map[i] = (char*)malloc(cols * sizeof(char));
        for (int j = 0; j < cols; j++) {
            if (i == 0 || i == rows - 1 || j == 0 || j == cols - 1) {
                map[i][j] = ' ';
            } else {
                map[i][j] = ' ';
            }
        }
    }

    // Initialize the first ant
    struct ant langton_ant = { ant1_row, ant1_col, UP };

    // Initialize the second ant
    struct ant random_ant = { ant2_row, ant2_col, DOWN };

    srand(time(NULL)); // Seed the random number generator with the current time

    int wait_time = 1; //default wait time between each step is 1 second
    if (argc > 1) {
        wait_time = atof(argv[1]);
    }

    // Run the simulation
    for (int step = 0; step < num_steps; step++) {
        // Update the direction of the Langton ant based on the color of the cell
        int langton_ant_row = langton_ant.row;
        int langton_ant_col = langton_ant.col;

     if (map[langton_ant_row][langton_ant_col] == '*') {
            langton_ant.dir = (langton_ant.dir + 1) % 4;
        } else {
            langton_ant.dir = (langton_ant.dir - 1 + 4) % 4;
        }

     // Update the color of the cell and move the Langton ant
     if (map[langton_ant_row][langton_ant_col] == '*') {
         map[langton_ant_row][langton_ant_col] = ' ';
     } else {
          map[langton_ant_row][langton_ant_col] = '*';
      }
        if (langton_ant.dir == UP) {
            langton_ant.row--;
        } else if (langton_ant.dir == RIGHT) {
            langton_ant.col++;
        } else if (langton_ant.dir == LEFT) {
            langton_ant.col--;
        } else if (langton_ant.dir == DOWN) {
            langton_ant.row++;
        }

        // Update the direction of the random ant based on a random number
        int random_number = rand() % 4;
        if (random_number == 0) {
           random_ant.dir = UP;
        } else if (random_number == 1) {
            random_ant.dir = RIGHT;
        } else if (random_number == 2) {
            random_ant.dir = LEFT;
        } else if (random_number == 3) {
            random_ant.dir = DOWN;
        }

        // Move the random ant
        if (random_ant.dir == UP) {
            random_ant.row--;
        } else if (random_ant.dir == RIGHT) {
            random_ant.col++;
    } else if (random_ant.dir == LEFT) {
            random_ant.col--;
        } else if (random_ant.dir == DOWN) {
            random_ant.row++;
        }

        // Print the current state of the map
        print_map(map, rows, cols, langton_ant, random_ant);

        // Wait for some time
    sleep(wait_time);
    }

    // Free the memory used by the map
    for (int i = 0; i < rows; i++) {
            free(map[i]);
    }
    free(map);

    return 0;
}

私が試したこと:

プログラムを実行するとき、指定されたステップ数の間、アリが任意の速度 (1 秒未満またはそれ以上) で移動できるようにしたいと考えています。 プログラムを実行すると、1 秒以上の待機時間であれば問題なく動作します。 しかし、1 秒未満の待機時間でプログラムを実行しようとすると、プログラムは即座に終了し、セグメンテーション エラー (コア ダンプ) メッセージを出力します。

これは、メイン関数の先頭に追加しようとした変更の 1 つです。

C
float wait_time = 1.0; //default wait time between each step is 1 second
if (argc > 1) {
    wait_time = atof(argv[1]);
    if (wait_time < 0.005) {
        wait_time = 0.005; //set minimum wait time to 0.005 seconds
    } else if (wait_time > 10.0) {
        wait_time = 10.0; //set maximum wait time to 10 seconds
    }
}

しかし、それはプログラムに何の変化ももたらしませんでした。 スリープを usleep に変更してみましたが、それでも変化はありませんでした。

解決策 1

これは同じ質問の 3 回目の投稿ですが、最初の質問で指摘したのと同じ間違いをまだ犯しています。 最初に次のコードがあります。

C++
int waiting_time = atof(argv[1]); // get waiting time from command line argument

次に、さらに下にあります:

C++
int wait_time = 1; //default wait time between each step is 1 second
if (argc > 1) {
    wait_time = atof(argv[1]);
}

しかし、どちらの場合でも、変数を次のように宣言します int 型、呼び出し atof 意味がありません。 そして以来、 sleep 関数は整数値のみを受け入れ、値を float としてキャプチャしようとしても意味がありません。 そのため、直後の最初の行を変更します main に:

C++
int wait_time = atoi(argv[1]); // get wait_ime from command line argument

もう一度キャプチャしようとする他の行を削​​除します。

解決策 2

実際に使うかもしれません usleepただし、1 秒を超える待機時間は処理できません (関連するマニュアル ページを参照してください)。 代替案は ナノスリープ[^]、 試す

C
#include <stdio.h>
#include <stdlib.h>
#include <time.h>

int main(int argc, char *argv[])
{
    if (argc != 3)
    {
        printf("Error: Invalid number of arguments\n");
        return 1;
    }

    double waiting_time = atof(argv[1]); // get waiting time from command line argument
    int num_steps = atoi(argv[2]); // get number of steps from command line argument

    if (waiting_time < 0.005)
      waiting_time = 0.005;
    else if (waiting_time > 10.0)
      waiting_time = 10.0;

    struct timespec wt;

    wt.tv_sec = (time_t) waiting_time; // getting the integer part of the waiting time
    wt.tv_nsec = (long) ( (waiting_time - wt.tv_sec) * 1E9); // getting the fractional part of the wating time, converting it to nanoseconds

    for ( int step = 0; step < num_steps; ++step)
    {
      nanosleep(&wt, NULL);
    }
  return 0;
}

非常に短い間隔でのスリープは、(非リアルタイム) オペレーティング システムでは正確ではないことに注意してください。

コメント

タイトルとURLをコピーしました