命令行和用户输入

发布于 2025-01-11 06:48:55 字数 1790 浏览 0 评论 0原文

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

int main(int argc, char *argv[] )
{
    float out_radius,in_radius,area, perimeter, in_radius2, out_radius2;
    
    if (argc == 1) {
        printf("Enter inner radius of ring: ");
        scanf("%f",&in_radius);
        printf("Enter outer radius of ring: ");
        scanf("%f",&out_radius);
        perimeter= (2 * 3.14) * (in_radius + out_radius);
        area= 3.14 * ((in_radius * in_radius) + (out_radius * out_radius));
    }
    
    else if (argc > 1 || argc == 2) {
        in_radius2 = atof(argv[1]);
        out_radius2 = atof(argv[2]);
        perimeter = (2 * 3.14) * (in_radius2 + out_radius2);
        area = 3.14 * ((in_radius2 * in_radius2) + (out_radius2 * out_radius2));
    }
    
    printf("Area of circle: %.2f \nPerimeter of circle: %.2f\n",area,perimeter);
}

这是一个简单的程序,当给定内半径和外半径作为用户输入时,它可以计算周长和半径。我试图做到这一点,以便用户可以选择通过用户输入或命令行(无论用户决定使用哪个)输入半径。我尝试创建 if 和 else if 语句来检查用户是否在命令行中输入数据。我的问题是,当我通过命令行输入半径时,我得到 0 或有时错误的答案作为我的周长和面积。我需要做什么来解决这个问题?

编辑固定代码:

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

int main(int argc, char *argv[] )
{
    float out_radius,in_radius,area, perimeter, in_radius2, out_radius2;
  
    if (argc == 3) {
        in_radius2 = atof(argv[1]);
        out_radius2 = atof(argv[2]);
        
    }
   
    else {
        printf("Enter inner radius of ring: ");
        scanf("%f",&in_radius);
        printf("Enter outer radius of ring: ");
        scanf("%f",&out_radius);
    }
    perimeter = (2 * 3.14) * (in_radius2 + out_radius2);
    area = 3.14 * ((in_radius2 * in_radius2) + (out_radius2 * out_radius2));
    
    printf("Area of circle: %.2f \nPerimeter of circle: %.2f\n",area,perimeter);
}
    #include <stdlib.h>
#include <stdio.h>

int main(int argc, char *argv[] )
{
    float out_radius,in_radius,area, perimeter, in_radius2, out_radius2;
    
    if (argc == 1) {
        printf("Enter inner radius of ring: ");
        scanf("%f",&in_radius);
        printf("Enter outer radius of ring: ");
        scanf("%f",&out_radius);
        perimeter= (2 * 3.14) * (in_radius + out_radius);
        area= 3.14 * ((in_radius * in_radius) + (out_radius * out_radius));
    }
    
    else if (argc > 1 || argc == 2) {
        in_radius2 = atof(argv[1]);
        out_radius2 = atof(argv[2]);
        perimeter = (2 * 3.14) * (in_radius2 + out_radius2);
        area = 3.14 * ((in_radius2 * in_radius2) + (out_radius2 * out_radius2));
    }
    
    printf("Area of circle: %.2f \nPerimeter of circle: %.2f\n",area,perimeter);
}

This is a simple program that calculates the perimeter and radius when given the inner and outer radius as user input. I'm trying to make it so the user has the option to either enter the radius via user input or command line (whichever the user decides to use). I tried to create and if and else if statement to check whether the user enters data in the command line or not. My problem is when I enter the radius via the command line, I get 0 or sometimes the wrong answer as my perimeter and area. What would I need do to fix this?

Edit fixed code:

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

int main(int argc, char *argv[] )
{
    float out_radius,in_radius,area, perimeter, in_radius2, out_radius2;
  
    if (argc == 3) {
        in_radius2 = atof(argv[1]);
        out_radius2 = atof(argv[2]);
        
    }
   
    else {
        printf("Enter inner radius of ring: ");
        scanf("%f",&in_radius);
        printf("Enter outer radius of ring: ");
        scanf("%f",&out_radius);
    }
    perimeter = (2 * 3.14) * (in_radius2 + out_radius2);
    area = 3.14 * ((in_radius2 * in_radius2) + (out_radius2 * out_radius2));
    
    printf("Area of circle: %.2f \nPerimeter of circle: %.2f\n",area,perimeter);
}

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

ι不睡觉的鱼゛ 2025-01-18 06:48:55

argc 是:

如果argc的值大于零,则数组成员argv[0]argv[argc-1]应包括在内包含指向字符串的指针,... C17dr § 5.1.2.2.1 2

如果argc的值大于零,则argv[0]指向的字符串代表程序名称...

更改逻辑

if (argc == 3) {
  // Don't need to read inputs. Use argv[1], argv[2] as pointers to strings
} else {
  // Read inputs
}

嗯,OP 正在改变代码 - 很难回答移动的目标。

argc is:

If the value of argc is greater than zero, the array members argv[0] through argv[argc-1] inclusive shall contain pointers to strings, ... C17dr § 5.1.2.2.1 2

If the value of argc is greater than zero, the string pointed to by argv[0] represents the program name ....

Change logic

if (argc == 3) {
  // Don't need to read inputs. Use argv[1], argv[2] as pointers to strings
} else {
  // Read inputs
}

Hmmm, OP's changing code - hard to answer a moving target.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文