C中的switch语句中的case

时间:2017-08-05 23:23:48

标签: c switch-statement

library(jsonlite)
rep<-c("{\"status\":\"OK\",\"responseTime\":10,\"message\":[\"No Block results found\"],\"Results\":{\"block\":[]}}", "{\"status\":\"OK\",\"responseTime\":9,\"message\":[],\"Results\":{\"block\":[{\"envelope\":{\"maxy\":00.0000000000,\"minx\":-00.000000000,\"maxx\":-00.00000000,\"miny\":00.0000000000000},\"geographyType\":\"BLOCK2010\",\"FIPS\":\"111111111121\"}]}}"  )

## jsonlite seems to choke on the number format 00.0000,
rep_fixed <- gsub("\\:\\-?00\\.", "\\:0.", rep)

# multiple results are returned each time, to apply to each structure 
rep1 <- lapply(rep_fixed, fromJSON)
str(rep1)

rep1[[2]][['Results']][['block']]

# envelope.maxy envelope.minx envelope.maxx envelope.miny geographyType         FIPS
#             0             0             0             0     BLOCK2010 111111111121

我知道案例后只允许一个整数。我也知道在单个引号内写的任何字符都会写成一个ascii interger。

在这里,switch语句中的参数是一个字符, #include<stdio.h> void main(){ char operator; double a, b; printf("enter an operand(+,-,*,/)"); scanf("%c",&operator); printf("enter two operands"); scanf("%lf %lf",&a,&b); switch(operator){ case '+': printf("%lf is the output of a & b",(a+b)); break; case '-': printf("%lf is the output of a & b",(a-b)); break; case '*': printf("%lf is the output of a & b",(a*b)); break; case '/': printf("%lf is the output of a & b",(a/b)); break; } }

这将如何与案例中的整数等同? switch(operator);

2 个答案:

答案 0 :(得分:3)

switch - 语句中的条件不需要完全是int类型,而是&#34;整数类型的任何表达式(char,signed或unsigned integer,或enumeration)&#34; (参见,例如,switch statement documentation at cppreference)。因此,使用operator类型的变量char作为switch - 语句中的条件是可以的。

每个case: - 标签中的常量表达式将转换为提升类型的表达式(在您的情况下为char),然后将评估条件的结果与(转换)值进行比较表达方式。因此,像'+''-'这样的char类型的常量表达式也可以。

最后,条件的提升类型为charcase: - 语句中的每个常量都为char类型,因此charchar进行比较'+'

请注意char - &#34;值&#34;常量43是8位ASCII值,即+。此外,如果在使用scanf("%c",&operator)时在控制台中输入operator,则&#34;值&#34; +的{​​8}也将是43的8位ASCII值,即43。在这种情况下,switch语句将8位43与8位public class LoadInfo implements Serializable { // your code, } 进行比较......

希望它有所帮助。

答案 1 :(得分:0)

char只是8位(大部分)整数

所以case '+':相当于to case 43:,因为43是'+'的ascii代码