命令行和用户输入
Command line and user input
#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);
}
这是一个简单的程序,当给定内半径和外半径作为用户输入时计算周长和半径。我试图做到这一点,以便用户可以选择通过用户输入或命令行(无论用户决定使用哪个)输入半径。我尝试创建 and 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);
}
argc
是:
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 ....
改变逻辑
if (argc == 3) {
// Don't need to read inputs. Use argv[1], argv[2] as pointers to strings
} else {
// Read inputs
}
嗯,OP 的更改代码 - 很难回答移动的目标。
#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);
}
这是一个简单的程序,当给定内半径和外半径作为用户输入时计算周长和半径。我试图做到这一点,以便用户可以选择通过用户输入或命令行(无论用户决定使用哪个)输入半径。我尝试创建 and 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);
}
argc
是:
If the value of
argc
is greater than zero, the array membersargv[0]
throughargv[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 byargv[0]
represents the program name ....
改变逻辑
if (argc == 3) {
// Don't need to read inputs. Use argv[1], argv[2] as pointers to strings
} else {
// Read inputs
}
嗯,OP 的更改代码 - 很难回答移动的目标。