使用命令行参数(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] != '[=10=]')
        {
            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之前检查参数计数,或者更确切地说您取消引用任何[=14]之前=] n > 1.

你想要这个:

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] != '[=12=]')
  {
    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;
}