枚举类型的应用举例
例二:一家水果店出售4种水果,每千克价格分别是:苹果1.15元,桔子1.20元,香蕉0.95元,菠萝0.85元。编一程序使售货员主要从键盘上打入货品的代码及重量,计算机将显示货品名、单价、重量及总价。货品代码为苹果1,桔子2,香蕉3,菠萝4。
CONST PA=1.15;PO=1.20;PB=0.95;PP=0.85;
TYPE FRUITTYPE =(APPLE,ORANGE,BANANA,PINEAPPLE);
VAR TOTAL,WEIGHT,P:REAL; { 重量和价格 }
CODE:INTEGER; { 代码 }
FRUIT:FRUITTYPE;
BEGIN
READLN(CODE,WEIGHT);
WHILE (CODE>=1)AND (CODE<=4)DO
BEGIN
CASE CODE OF
1 : FRUIT:=APPLE;
2 : FRUIT:=ORANGE;
3 : FRUIT:=BANANA;
4 : FRUIT:=PINEAPPLE;
END;
CASE FRUIT OF
APPLE : BEGIN WRITE(‘APPLE’); P:=PA END;
ORANGE: BEGIN WRITE(‘ORANGE’); P:=PO END;
BANANA : BEGIN WRITE(‘BANANA’); P:=PB END;
PINEAPPLE : BEGIN WRITE(‘PINEAPPLE’); P:=PP END;
END;
WRITE(P:6:2, ‘ * ’, WEIGHT:6:2 , ‘ = ’);
WRITELN(p*WEIGHT:8:2);
READLN(CODE,WEIGHT);
END;
END.
C中示例:
typedef enum {
DEMO_LABEL_A = 0,
DEMO_LABEL_B,
DEMO_LABEL_C
DEMO_LABEL_D
} demo_label_t;
demo_label_t demo_label;
/* 对demo 的赋值操作 */
if (demo_label == DEMO_LABEL_C)
printf (the label is C);