gpt4 book ai didi

c - 如何使用预处理器条件让用户输入带有字符的选择

转载 作者:行者123 更新时间:2023-11-30 21:05:46 24 4
gpt4 key购买 nike

我的作业是使用C预处理器条件来决定是否执行圆形或正方形的面积;其中一项指令强调:当提示用户时,用户将键入一个数字,然后键入“R”代表半径,“S”代表正方形。我不知道这是否有意义

这是我的头文件,其中声明了我的定义

#define RADIUS 'R'
#define PI 3.14
#define CIRCLE(X) (PI*(X)*(X))
#define SIDE 'S'
#define SQ(X) ((X)*(X))

这是我的代码;我对此很陌生,所以我想了解为什么我的代码不接受我的用户输入选择。我将不胜感激任何帮助。

这是我到目前为止的代码

#include<stdio.h>
#include"square_circle.h"

int main(){

int number;
float area1, area2;
char choice;


printf("Please type a whole number: ");
scanf("%d", &number);

printf("What kind of shape do you want type R or S: ");
scanf(" %c", &choice);


#if choice == RADIUS
area1 = CIRCLE(number);
printf("Your shape is a circle and your value is %f", area1);

#elif choice == SIDE
area2 = SQ(number);
printf("Your shape is a square and your value is %f", area2);

#endif



return 0;
}

最佳答案

这不会是你的老师想要的,但它(或多或少)符合要求。它等待用户输入 RS(更准确地说,R 或其他内容),并根据条件预处理来编译一些代码用户输入的内容。

square_circle.h

#define RADIUS 'R'
#define PI (355.0 / 113.0)
#define CIRCLE(X) (PI*(X)*(X))
#define SIDE 'S'
#define SQ(X) ((X)*(X))

这是一个比您想象的更好的 π 近似值 — 它精确到小数点后 7 位,仅在小数点后第 7 位出现 3 个计数错误 — bc -l 报告值为 3.141592920353982300883.14159265358979323844 相比(4*a(1) 的结果)。

area.c

此代码使用或不使用 -DAREA_CIRCLE 进行编译,并且根据情况,在编译和运行时将计算圆形或正方形的面积。

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

int main(int argc, char **argv)
{
if (argc != 2)
{
fprintf(stderr, "Usage: %s length\n", argv[0]);
return 1;
}

double d;
if ((d = strtod(argv[1], 0)) == 0.0)
{
fprintf(stderr, "Invalid argument given to %s\n", argv[0]);
return 1;
}
#ifdef AREA_CIRCLE
printf("Your shape is a circle with radius %g and area %g\n", d, CIRCLE(d));
#else
printf("Your shape is a square with sides %g and area %g\n", d, SQ(d));
#endif
return 0;
}

选择器.c

这是用户直接运行并向其中输入数据的程序。

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

int main(void)
{
double number;
char choice;

printf("Please type a number: ");
if (scanf("%lf", &number) != 1)
{
fprintf(stderr, "That wasn't a number!\n");
return 1;
}

printf("What kind of shape do you want type R or S: ");
if (scanf(" %c", &choice) != 1)
{
fprintf(stderr, "That wasn't recognized\n");
return 1;
}

char cmd[1000];
if (choice == RADIUS)
snprintf(cmd, sizeof(cmd), "gcc -o area area.c -DAREA_CIRCLE");
else
snprintf(cmd, sizeof(cmd), "gcc -o area area.c -DAREA_SQUARE");
system(cmd);
snprintf(cmd, sizeof(cmd), "./area %g", number);
system(cmd);
return 0;
}

请注意,使用 %g 作为格式在这里效果很好;它不会打印多余的尾随零,并且也会合理地格式化非常大和非常小的数字。

该代码确实假设您使用 GCC 作为编译器;您可以将 gcc 更改为您首选的编译器名称(两次)。

运行示例:

$ ./chooser
Please type a number: 2.34
What kind of shape do you want type R or S: R
Your shape is a circle with radius 2.34 and area 17.2021
$ ./chooser
Please type a number: 2.34
What kind of shape do you want type R or S: S
Your shape is a square with sides 2.34 and area 5.4756
$ ./chooser
Please type a number: 9999
What kind of shape do you want type R or S: R
Your shape is a circle with radius 9999 and area 3.14096e+08
$ ./chooser
Please type a number: 0.00007654
What kind of shape do you want type R or S: S
Your shape is a square with sides 7.654e-05 and area 5.85837e-09
$

关于c - 如何使用预处理器条件让用户输入带有字符的选择,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/52794924/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com