【问题标题】:Supplying Float Arguments through Command Prompt C Language通过命令提示符 C 语言提供浮点参数
【发布时间】:2019-06-07 19:48:31
【问题描述】:

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

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 提供的参数不足

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

以及代码被“提供的参数不足” 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 名称后面的命令行读取/输入并在代码中正确使用的整数/浮点数。

*代码是预先编写的,这是最后一步,所以如果我错过了代码中的任何内容,请帮忙解决,提前致谢。希望你能帮忙:)

【问题讨论】:

  • 您是否期望电压为 0.0005 (V?)、长度为 1.5 (m?)、半径为 160 (m??)?
  • 是的,我是。鲍勃_
  • 难道不是相反(160 V 和 0.0005m 的半径或 1.5 V 和 160m 的长度)?也许我想太多或使用了错误的单位,但是,这似乎并不是真正的 wire...
  • OT: about: gcc main.c 这是编译/链接程序的糟糕方式。建议:gcc -c -Wall -Wextra -Wconversion -pedantic -std=gnu11 main.c -o main.o,当它编译没有错误或警告时,然后gcc main.o -o main
  • OT: about: printf("Not enough arguments supplied"); 这告诉用户(几乎)除了他们犯了错误之外什么都没有。更好用:fprintf( stderr, "USAGE: %s voltage length radius\n", argv[0] );,因为它输出到stderr(应该这样)并告诉用户命令行上应该有什么,

标签: c command-line-arguments command-prompt


【解决方案1】:

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

另外备注:你需要检查argc before才能访问argv

【讨论】:

    【解决方案2】:

    这是做什么的?

      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])
    

    同时编译时会出现完整的警告。

    【讨论】:

      【解决方案3】:

      您可以尝试打印出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);
      

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 1970-01-01
        • 2012-10-05
        • 2018-12-08
        • 1970-01-01
        • 2018-11-26
        • 2012-05-22
        • 2020-08-18
        • 1970-01-01
        相关资源
        最近更新 更多