简体   繁体   English

c:如何使用 sscanf 读取命令行参数 (x,y) 并将 x 和 y 作为单独的双精度值存储在结构中?

[英]c: How to use sscanf to read the command-line argument (x,y) and store x and y as separate doubles inside a struct?

I'm working on a complex numbers calculator.我正在研究一个复数计算器。 The user must pass the complex operands as arguments to the main function using the command line.用户必须使用命令行将复杂操作数作为 arguments 传递给主 function。 Additionally, the numbers need to be passed exactly in this notation (x,y) , where x is the real part of the number and y the imaginary part.此外,数字需要准确地以这种表示法(x,y)传递,其中 x 是数字的实部,y 是虚部。

I created a struct for complex numbers.我为复数创建了一个结构。 I believe I have to use sscanf to read from argv and store the values into the corresponding real and imaginary parts of my struct complex variables (which will hold the numbers to be operated on), but I haven't been able to achieve it, especially not when the parenthesis formatting is used.我相信我必须使用 sscanf 从 argv 读取并将值存储到我的结构复杂变量的相应实部和虚部(它将保存要操作的数字),但我无法实现它,尤其是在使用括号格式时。

For the moment I am focusing on being able to store at least one complex number from the command line argument.目前,我专注于能够从命令行参数中存储至少一个复数。 I'm hoping someone here can lead me in the right direction.我希望这里有人能引导我朝着正确的方向前进。 Here's the portion of my code that deals with reading from argv and storing in my struct complex variable struct complex c这是我的代码部分,它处理从 argv 读取并存储在我的 struct complex variable struct complex c

#define _CRT_SECURE_NO_WARNINGS
#include <stdio.h>
#include <stdlib.h>
#include <string.h>


struct complex {
    double real;
    double imag;
};

int main(int argc, char *argv[]) {

    struct complex c;
    sscanf(argv, "(%lf,%lf)", &c.real, &c.imag);
    printf("%.2lf %.2lf \n", c.real, c.imag);
    return 0;
}

I've tried using different variations in the parameters of sscanf , but so far I've only succeeded at storing one of the doubles (the one corresponding to the real part of the number) and only when the user doesn't use the parenthesis.我尝试在sscanf的参数中使用不同的变体,但到目前为止,我只成功存储了一个双精度数(对应于数字的实数部分),并且仅在用户不使用括号时. How do I procede?我该如何进行?

I think it better to read the parameter as whole string (%s) and then process it.我认为最好将参数读取为整个字符串(%s)然后处理它。 but if you insist using sscanf you may try to use this.但如果你坚持使用 sscanf 你可以尝试使用它。

but bear in mind that when you pass parameter like (1,2) the command like will think it is one parameter so you need to pass something like (1, 2) - it have space between comma and the 2. also don't forget to check that you have 3 parameters.但请记住,当您传递 (1,2) 之类的参数时,类似的命令会认为它是一个参数,因此您需要传递 (1, 2) 之类的东西 - 它在逗号和 2 之间有空格。也不要忘记检查您是否有 3 个参数。

   struct complex 
   {
        double real;
        double imag;
   };
    
   int main(int argc, char *argv[]) 
   {
    
        struct complex c;
        if(3 == argc)
        {
            sscanf(argv[1], "(%lf,", &c.real);
            sscanf(argv[2], "%lf)",  &c.imag);
            printf("%.2lf %.2lf \n", c.real, c.imag);
        }
        return 0;
    }

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM