命令行和用户输入
#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 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
argc 是:
更改逻辑
嗯,OP 正在改变代码 - 很难回答移动的目标。
argc
is:Change logic
Hmmm, OP's changing code - hard to answer a moving target.