通过命令提示符 C 语言提供浮点参数

Supplying Float Arguments through Command Prompt C Language

我在使用命令提示符获取变量(在命令提示符中写入)并在程序中使用它们时遇到困难。我的想法是我使用以下方法编译程序:

gcc main.c

并通过 运行 执行它, a.exe 创建后跟参数。

如下:

C:\Users\Pc\Desktop\resistance>gcc main.c

C:\Users\Pc\Desktop\resistance>a.exe 0.0005 1.5 160

48.000000 49.000000 49.000000 提供的参数不足

最后一行是我从代码中收到的值,从我提供的参数来看,这些值是不正确的。

以及代码由 "Not enough arguments supplied" if 语句终止的事实。这再次令人困惑,因为 *argv[] 从 0 *argv[0] 开始,它应该等于 "a.exe" 因此后面的参数应该 = *argv[1]... ect

我的代码:

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

float areaOfcircle(float radius_circle)
{
    float area_circle;
    area_circle = M_PI * radius_circle * radius_circle;

    return area_circle;
}
void resitance_current(float length, float area_circle, float voltage, float* resistance, float* current)
{
    float resistivity;
    resistivity = 1.782*pow(10, -8);
    *resistance = ((resistivity*length) / area_circle);
    *current = (voltage / *resistance);
}
int main(int argc, char *argv[])
{
    float radius, voltage, length, current, resistance;
    float length_u, length_l;
    voltage = *argv[1];
    length = *argv[2];
    radius = *argv[3];
    printf("%f %f %f", voltage, length, radius);
    //char response, yes;
    // take radius as input
    //printf("Enter the radius of wire : ");
    //scanf("%f", &radius);
    if (argc != 3)
    {
        printf("Not enough arguments supplied");
    }
    else
    {
        if (radius < 0)
        {
            exit(1);
        }
        else
        {
            //printf("Enter the Voltage of circuit : ");
            //scanf("%f", &voltage);
            if (voltage < 0)
            {
                exit(1);
            }
            else
            {
                //printf("Enter the Length of Wire : ");
                //scanf("%f", &length);
                if (length < 0)
                {
                    exit(1);
                }
                else
                {
                    resitance_current(length, areaOfcircle(radius), voltage, &resistance, &current);
                    printf("Resistance = %f , Current = %f\n", resistance, current);
                    printf("\nEnter the Upper Length of Wire : ");
                    scanf("%f", &length_u);
                    printf("\nEnter the Lower Length of Wire : ");
                    scanf("%f", &length_l);
                    if ((length_l < 0) || (length_l >= length_u))
                    {
                        exit(1);
                    }
                    else
                    {
                        for(length_l = length_l; length_l<=length_u; length_l++)
                        {
                            length = (length_l + 1);
                            resitance_current(length, areaOfcircle(radius), voltage, &resistance, &current);
                            printf("\nLength = %0.3f Resistance = %0.3f , Current = %0.3f", length, resistance, current);
                        }
                    }

                }
            }
        }
    }
    return 0;
}

在第 23-25 行,我尝试为提供的参数分配变量名。使通过代码阅读它们更容易。

我试图寻求帮助的主要问题是如何从 program.exe 名称后的命令行中获取 (Integer/Float 数字 read/entered 并在代码中正确使用.

*代码是预先编写的,这是最后一步,所以如果我遗漏了代码中的任何内容,请也提供帮助,在此先感谢。希望你能帮忙:)

警告程序的路径名是第一个参数,因此在您的情况下 argc 必须与 4 进行比较,这就是您有这种行为的原因

另一条评论:您需要在之前检查argc才能访问argv

这是做什么的?

  voltage = *argv[1];
    length = *argv[2];
    radius = *argv[3]

您正在尝试获取字符串值并将其分配给浮点数。这是行不通的。您需要使用类似 atof() 函数 (https://www.tutorialspoint.com/c_standard_library/c_function_atof.htm) 的函数来转换字符串。

ex;   voltage = atof(argv[1])

同样编译时出现完整警告。

您可以尝试打印出 argc 和所有 argv 字符串的值,看看它们是什么。你可能会感到惊讶。将以下函数添加到您的代码中:

void dumpargs(int argc, char *argv[])
  {
  int i;

  printf("argc = %d\n", argc);

  for(i = 0 ; i < argc ; ++i)
    printf("argv[%d] = '%s'\n", i, argv[i]));
  }

然后在main中的变量声明后的第一行添加以下内容:

dumpargs(argc, argv);