我的任务是使用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;
}
答案 0 :(得分:0)
这不是您的老师所想的,但是(或多或少)符合要求的要求。它等待用户键入R
或S
(更准确地说是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.14159292035398230088
与3.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;
}
进行编译,并依赖于此,在编译和运行时将计算圆形或正方形的面积。
chooser.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
$
更改为首选的编译器名称(两次)。
示例运行:
iex> JOSE.JWA.crypto_supports()
[ciphers: [aes_cbc: 128, aes_cbc: 192, aes_cbc: 256, aes_ecb: 128, aes_ecb: 192,
aes_ecb: 256, aes_gcm: 128, aes_gcm: 192, aes_gcm: 256,
chacha20_poly1305: 256],
hashs: [:md5, :poly1305, :sha, :sha256, :sha384, :sha512, :shake256],
public_keys: [:ec_gf2m, :ecdh, :ecdsa, :ed25519, :ed25519ph, :ed448, :ed448ph,
:rsa, :x25519, :x448], rsa_crypt: [:rsa1_5, :rsa_oaep, :rsa_oaep_256],
rsa_sign: [:rsa_pkcs1_padding, :rsa_pkcs1_pss_padding]]