簡體   English   中英

使用命令行參數 (argc, argv) 的 C 計算器程序

[英]Calculator program in C using Command Line Arguments (argc, argv)

我編寫了一個程序來從命令行獲取輸入參數並在 C 中創建一個基本的計算器。代碼如下:

#include <stdio.h>
#include <stdlib.h>
#include <ctype.h>


void CheckArgumentCount(int argc);
int CheckOperands(char *argv[]);
long Calculate(long nr1, long nr2, char operation);

int main(int argc, char *argv[])
{
    long nr1 = CheckOperands(&argv[1]);
    long nr2 = CheckOperands(&argv[3]);
    int result;

    CheckArgumentCount(argc);
    result = Calculate(nr1, nr2, *argv[2]);
    printf("Result: %d", result);

    return 0;
}


void CheckArgumentCount(int argc)
{
    if (argc > 3 || argc < 3)
    {
        puts("Wrong number of arguments to perform the calculation.\n");
    }
}

int CheckOperands(char *argv[])
{
        int i = 1;
        
        while (*argv[i] != '\0')
        {
            if (*argv[i] < '0' || *argv[i] > '9')
            {
                return *argv[i];
            }
            argv++;
        }
        return atoi(argv[i]);
}


long Calculate(long nr1, long nr2, char operation)
{
    long result = 0;

    switch (operation)
    {
        case '+':
            result = nr1 + nr2;
            break;
        case '-':
            result = nr1 - nr2;
            break;
        case '*':
            result = nr1 * nr2;
            break;
        case '/':
            if (nr2 == 0)
            {
                puts("Error! Divion by zero.");
            }
            else
            {
                result = nr1 / nr2;
            }
            break;
        default:
            puts("Operator invalid.");
            break;
    }
    return result;
}

哪個編譯得很好,因為編譯器沒有顯示任何錯誤。 但是,當我提供 3 個參數時,我在運行時在 zsh 終端上收到此錯誤,例如:

zsh: segmentation fault ./calc 1 + 3

我認為它可能與我的 CheckOperands 函數有關? 任何建議將被認真考慮。

使用CheckOperands之前,或者更確切地說,使用 n > 1 取消引用任何argc[n]之前,您需要檢查參數計數。

你要這個:

int main(int argc, char* argv[])
{
  if (!CheckArgumentCount(argc))
    exit(1);   // if argument count differenmt from 3 exit program
  ...

和這個:

int CheckArgumentCount(int argc)
{
  if (argc != 4)   // argc is one more because if the command 
                      invocation which is argv[0]
  {
    puts("Wrong number of arguments to perform the calculation.\n");
    return 0;
  }

  return 1;
}

CheckOperands還有更多錯誤。

整體更正代碼:

#include <stdio.h>
#include <stdlib.h>
#include <ctype.h>

int CheckArgumentCount(int argc);
int CheckOperands(char* operand);
long Calculate(long nr1, long nr2, char operation);

int main(int argc, char* argv[])
{
  if (!CheckArgumentCount(argc))
    exit(1);

  long nr1 = CheckOperands(argv[1]);
  long nr2 = CheckOperands(argv[3]);
  int result;


  result = Calculate(nr1, nr2, argv[2][0]);
  printf("Result: %d", result);

  return 0;
}


int CheckArgumentCount(int argc)
{
  if (argc != 4)
  {
    puts("Wrong number of arguments to perform the calculation.\n");
    return 0;
  }

  return 1;
}

int CheckOperands(char* operand)
{
  int i = 0;

  while (operand[i] != '\0')
  {
    if (operand[i] < '0' || operand[i] > '9')
    {
      return operand[i];
    }

    i++;
  }
  return atoi(operand);
}


long Calculate(long nr1, long nr2, char operation)
{
  long result = 0;

  switch (operation)
  {
  case '+':
    result = nr1 + nr2;
    break;
  case '-':
    result = nr1 - nr2;
    break;
  case '*':
    result = nr1 * nr2;
    break;
  case '/':
    if (nr2 == 0)
    {
      puts("Error! Divion by zero.");
    }
    else
    {
      result = nr1 / nr2;
    }
    break;
  default:
    puts("Operator invalid.");
    break;
  }
  return result;
}

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM