作者热门文章
- html - 出于某种原因,IE8 对我的 Sass 文件中继承的 html5 CSS 不友好?
- JMeter 在响应断言中使用 span 标签的问题
- html - 在 :hover and :active? 上具有不同效果的 CSS 动画
- html - 相对于居中的 html 内容固定的 CSS 重复背景?
TL;博士:
是否有一种类型(让我们将其命名为 convert_type
),即使在处理 float
类型时,也可以在不丢失信息的情况下进行类型转换?
使用示例:
float f1 = 12.34;
long i1 = 32;
convert_type tmp1 = (convert_type) f;
convert_type tmp2 = (convert_type) i;
float f2 = (float) tmp1; // should be 12.34
long i2 = (long) tmp2; // should be 32
<小时/>
上下文:
我想创建这样一个函数:
convert_type ask(char* question, val_type input_type)
类型val_type
定义为:
typedef enum {
INT_T,
STR_T,
CHAR_T,
FLOAT_T
} val_type;
参数问题
有一个要打印的字符串,以便询问输入。
此函数的想法是在 stdin
中询问 input_type
类型的输入,并再次询问,直到输入有效。
ask() 的使用示例:
int main(int argc, char const *argv[]) {
int nb = (int) ask("How many tries to you want ?\n --> ", T_INT);
char * message = (char*) ask("What is the victory message ?\n --> ", T_STR);
float prob = (float) ask("What is the probability of winning ?\n --> ", T_FLOAT);
printf("\n\n");
printf("Number of tries : %d\n", nb);
printf("Victory message : %s\n", message);
printf("Probability : %f\n", prob);
return 0;
}
我使用unsigned long
类型实现了它,但我在使用float
类型时遇到了麻烦。
执行示例:
How many tries to you want ?
--> many
The input must be a valid integer.
How many tries to you want ?
--> -2131
What is the victory message ?
--> We are the champions !!!
What is the probability of winning ?
--> 3.4.3
The input must be a valid float value.
What is the probability of winning ?
--> 2.54
Number of tries : -2131
Victory message : We are the champions !!!
Probability : 2.000000
值得注意的是,unsigned long
不适用于 float
类型(2.54
变成了 2.00
因为我猜 unsigned long 包含一个整数值)。
我可以使用什么类型?
最佳答案
如果你足够小心不要导致读/写类型不匹配,你可以使用union
这样做:
#include <stdio.h>
typedef union {
int int_data;
char* str_data;
char char_data;
float float_data;
} convert_type;
typedef enum {
INT_T,
STR_T,
CHAR_T,
FLOAT_T
} val_type;
convert_type ask(char* question, val_type input_type) {
static char[] str = "We are the champions !!!";
convert_type ret;
fputs(question, stdout);
/* this is examples of how to set values. reading is omitted. */
switch (input_type) {
case INT_T:
ret.int_data = -2131;
break;
case STR_T:
ret.str_data = str;
break;
case CHAR_T:
ret.char_data = 'a';
break;
case FLOAT_T:
ret.float_data = 2.54;
break;
}
return ret;
}
int main(int argc, char const *argv[]) {
int nb = ask("How many tries to you want ?\n --> ", INT_T).int_data;
char * message = ask("What is the victory message ?\n --> ", STR_T).str_data;
float prob = ask("What is the probability of winning ?\n --> ", FLOAT_T).float_data;
printf("\n\n");
printf("Number of tries : %d\n", nb);
printf("Victory message : %s\n", message);
printf("Probability : %f\n", prob);
return 0;
}
与struct
不同的是,union
的成员共享一个内存空间。因此,当您向 union
的成员写入一些值时,union
的其他成员失效。
关于c - 在不使用指针的情况下模拟 C 中的多态性,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/59226369/
我来自 Asp.Net 世界,试图理解 Angular State 的含义。 什么是 Angular 状态?它类似于Asp.Net中的ascx组件吗?是子页面吗?它类似于工作流程状态吗? 我听到很多人
我一直在寻找 3 态拨动开关,但运气不佳。 基本上我需要一个具有以下状态的开关: |开 |不适用 |关 | slider 默认从中间开始,一旦用户向左或向右滑动,就无法回到N/A(未回答)状态。 有人
我是一名优秀的程序员,十分优秀!