1Z0-051 QUESTION 10 运算的时候注意括号的位置和隐式转换

QUESTION 10
View the Exhibit; e xamine the structure of the PROMOTIONS table.
Each promotion has a duration of at least seven days .
Your manager has asked you to generate a report, which provides the weekly cost for each promotion
done to l date.

Which query would achieve the required result?


A. SELECT promo_name, promo_cost/promo_end_date-promo_begin_date/7 FROM promotions;
B. SELECT promo_name,(promo_cost/promo_end_date-promo_begin_date)/7 FROM promotions;
C. SELECT promo_name, promo_cost/(promo_end_date-promo_begin_date/7) FROM promotions;
D. SELECT promo_name, promo_cost/((promo_end_date-promo_begin_date)/7) FROM promotions;


答案:D

解析:

SQL> create table promotions(
  2  promo_id number(6) not null,
  3  promo_name varchar2(30) not null,
  4  promo_subcategory varchar(30) not null,
  5  promo_subcategrory_id number not null,
  6  promo_gategory varchar2(30) not null,
  7  promo_gategory_id number not null,
  8  promo_cost number(10,2) not null,
  9  promo_begin_date date not null,
 10  promo_end_date date not null);

表已创建。

SQL> insert into promotions values(100001,'testname','testsubcategory',1,'testca
tegory',1,140000,sysdate-14,sysdate)
  2  ;

已创建 1 行。
--A选项报错
SQL> SELECT promo_name, promo_cost/promo_end_date-promo_begin_date/7 FROM promot
ions;
SELECT promo_name, promo_cost/promo_end_date-promo_begin_date/7 FROM promotions
                              *
第 1 行出现错误:
ORA-00932: 数据类型不一致: 应为 NUMBER, 但却获得 DATE

--B选项报错
SQL> SELECT promo_name,(promo_cost/promo_end_date-promo_begin_date)/7 FROM promo
tions;
SELECT promo_name,(promo_cost/promo_end_date-promo_begin_date)/7 FROM promotions

                              *
第 1 行出现错误:
ORA-00932: 数据类型不一致: 应为 NUMBER, 但却获得 DATE
--C选项报错
SQL> SELECT promo_name, promo_cost/(promo_end_date-promo_begin_date/7) FROM prom
otions;
SELECT promo_name, promo_cost/(promo_end_date-promo_begin_date/7) FROM promotion
s
                                              *
第 1 行出现错误:
ORA-00932: 数据类型不一致: 应为 NUMBER, 但却获得 DATE

--D选项可以执行
SQL> SELECT promo_name, promo_cost/((promo_end_date-promo_begin_date)/7) FROM pr
omotions;

PROMO_NAME                     PROMO_COST/((PROMO_END_DATE-PROMO_BEGIN_DATE)/7)
------------------------------ ------------------------------------------------
testname                                                                  70000


lexical_analyzer.l %option noyywrap %{ #include <stdio.h> #include <stdlib.h> #include <string.h> #include "parse_tree.h" #include "syntax_analyzer.h" int lines = 1; int pos_start = 1; int pos_end = 1; char token_name[20]; void new_node(char *text) { yylval.node = new_parse_tree_node(text); } #define SET_TOKEN(name) do { \ pos_start = pos_end; \ pos_end += yyleng; \ strcpy(token_name, #name); \ new_node(yytext); \ } while(0) %} %x COMMENT DIGIT [0-9] OCTAL [0-7] HEX [0-9a-fA-F] ID [a-zA-Z_][a-zA-Z0-9_]* EXPONENT [Ee][+-]?{DIGIT}+ HEX_EXP [Pp][+-]?{DIGIT}+ DEC_INT 0|([1-9]{DIGIT}*) OCT_INT 0{OCTAL}+ HEX_INT 0[xX]{HEX}+ DEC_FLOAT (({DIGIT}+"."{DIGIT}*)|("."{DIGIT}+)){EXPONENT}? HEX_FLOAT 0[xX]({HEX}+"."{HEX}*|"."{HEX}+){HEX_EXP} %% <INITIAL>{ "/*" { BEGIN(COMMENT); pos_start = pos_end; pos_end += 2; } "//".* { pos_start = pos_end; pos_end += yyleng; } /* 数字常量 */ {DEC_INT} { SET_TOKEN(INTCONST); return INTCONST; } {OCT_INT} { SET_TOKEN(INTCONST); return INTCONST; } {HEX_INT} { SET_TOKEN(INTCONST); return INTCONST; } {DEC_FLOAT} { SET_TOKEN(FLOATCONST); return FLOATCONST; } {HEX_FLOAT} { SET_TOKEN(FLOATCONST); return FLOATCONST; } /* 关键字 */ "int" { SET_TOKEN(INT); return INT; } "float" { SET_TOKEN(FLOAT); return FLOAT; } "void" { SET_TOKEN(VOID); return VOID; } "return" { SET_TOKEN(RETURN); return RETURN; } "if" { SET_TOKEN(IF); return IF; } "else" { SET_TOKEN(ELSE); return ELSE; } "while" { SET_TOKEN(WHILE); return WHILE; } "break" { SET_TOKEN(BREAK); return BREAK; } "continue" { SET_TOKEN(CONTINUE); return CONTINUE; } "const" { SET_TOKEN(CONST); return CONST; } /* 运算符 */ ";" { SET_TOKEN(SEMICOLON); return SEMICOLON; } "," { SET_TOKEN(COMMA); return COMMA; } "=" { SET_TOKEN(ASSIGN); return ASSIGN; } "<" { SET_TOKEN(LT); return LT; } "<=" { SET_TOKEN(LET); return LET; } ">" { SET_TOKEN(GT); return GT; } ">=" { SET_TOKEN(GET); return GET; } "==" { SET_TOKEN(EQ); return EQ; } "!=" { SET_TOKEN(NEQ); return NEQ; } "+" { SET_TOKEN(ADD); return ADD; } "-" { SET_TOKEN(SUB); return SUB; } "*" { SET_TOKEN(MUL); return MUL; } "/" { SET_TOKEN(DIV); return DIV; } "%" { SET_TOKEN(MOD); return MOD; } "&&" { SET_TOKEN(AND); return AND; } "||" { SET_TOKEN(OR); return OR; } "!" { SET_TOKEN(NOT); return NOT; } /* 括号 */ "(" { SET_TOKEN(LPARENTHESIS); return LPARENTHESIS; } ")" { SET_TOKEN(RPARENTHESIS); return RPARENTHESIS; } "[" { SET_TOKEN(LBRACKET); return LBRACKET; } "]" { SET_TOKEN(RBRACKET); return RBRACKET; } "{" { SET_TOKEN(LBRACE); return LBRACE; } "}" { SET_TOKEN(RBRACE); return RBRACE; } /* 标识符 */ {ID} { SET_TOKEN(IDENT); return IDENT; } /* 空白处理 */ [ \t\r]+ { pos_start = pos_end; pos_end += yyleng; } \n { lines++; pos_start = 1; pos_end = 1; } . { SET_TOKEN(ERROR); return ERROR; } } <COMMENT>{ "*/" { BEGIN(INITIAL); pos_end += 2; } \n { lines++; pos_start = 1; pos_end = 1; } . { pos_end++; } <<EOF>> { fprintf(stderr, "Error: Unclosed comment at line %d\n", lines); exit(1); } } %% syntax_analyzer.y %{ #include <stdio.h> #include <stdlib.h> #include <string.h> #include <stdarg.h> #include "parse_tree.h" extern int yylex(); extern int yyparse(); extern int yyrestart(); extern FILE * yyin; extern int lines; extern char * yytext; extern int pos_end; extern int pos_start; parse_tree *gt; void yyerror(const char *s); parse_tree_node *node(const char *node_name, int children_num, ...); %} %union { struct _parse_tree_node *node; } %token <node> // 运算符 EQ NEQ LE GE ADD_ASSIGN SUB_ASSIGN MUL_ASSIGN DIV_ASSIGN MOD_ASSIGN ASSIGN LT LET GT GET ADD SUB MUL DIV MOD AND OR NOT LSHIFT RSHIFT INC DEC BIT_NOT BIT_AND BIT_OR BIT_XOR // 分隔符 LPARENTHESIS RPARENTHESIS LBRACE RBRACE LBRACKET RBRACKET SEMICOLON COMMA DOT ARROW COLON QUESTION // 关键字 IF ELSE WHILE FOR RETURN INT CHAR FLOAT VOID STRUCT TYPEDEF DO BREAK CONTINUE CONST // 标识符常量 IDENT INTCONST FLOATCONST HEX_CONST OCT_CONST STRING_CONST CHAR_CONST // 错误标记 ERROR %type <node> Program %type <node> CompUnit %type <node> Decl %type <node> ConstDecl %type <node> VarDecl %type <node> ConstDefList %type <node> BType %type <node> ConstDef %type <node> ConstExpList %type <node> ConstInitVal %type <node> ConstInitValList %type <node> VarDef %type <node> VarDeclList %type <node> InitVal %type <node> InitValList %type <node> FuncDef %type <node> FuncFParams %type <node> FuncFParam %type <node> ExpList %type <node> Block %type <node> BlockList %type <node> BlockItem %type <node> Stmt %type <node> Exp %type <node> Cond %type <node> LVal %type <node> PrimaryExp %type <node> Number %type <node> UnaryExp %type <node> UnaryOp %type <node> FuncRParams %type <node> MulExp %type <node> AddExp %type <node> RelExp %type <node> EqExp %type <node> LAndExp %type <node> LOrExp %type <node> ConstExp %type <node> Integer %type <node> Floatnum // 指定起始符号 %start Program %% Program : CompUnit { $$ = node("Program", 1, $1); gt->root = $$; } ; CompUnit : CompUnit Decl { $$ = node("CompUnit", 2, $1, $2); } | CompUnit FuncDef { $$ = node("CompUnit", 2, $1, $2); } | Decl { $$ = node("CompUnit", 1, $1); } | FuncDef { $$ = node("CompUnit", 1, $1); } ; Decl : ConstDecl { $$ = node("Decl", 1, $1); } | VarDecl { $$ = node("Decl", 1, $1); } ; ConstDecl : CONST BType ConstDefList SEMICOLON { $$ = node("ConstDecl", 4, $1, $2, $3, $4); } ; ConstDefList : ConstDef { $$ = node("ConstDefList", 1, $1); } | ConstDefList COMMA ConstDef { $$ = node("ConstDefList", 3, $1, $2, $3); } ; BType : INT { $$ = node("BType", 1, $1); } | FLOAT { $$ = node("BType", 1, $1); } ; ConstDef : IDENT ConstExpList ASSIGN ConstInitVal { $$ = node("ConstDef", 4, $1, $2, $3, $4); } ; ConstExpList : /* ε */ { $$ = node("ConstExpList", 0); } | ConstExpList LBRACKET ConstExp RBRACKET { $$ = node("ConstExpList", 4, $1, $2, $3, $4); } ; ConstInitVal : ConstExp { $$ = node("ConstInitVal", 1, $1); } | LBRACE ConstInitValList RBRACE { $$ = node("ConstInitVal", 3, $1, $2, $3); } | LBRACE RBRACE { $$ = node("ConstInitVal", 2, $1, $2); } ; ConstInitValList : ConstInitVal { $$ = node("ConstInitValList", 1, $1); } | ConstInitValList COMMA ConstInitVal { $$ = node("ConstInitValList", 3, $1, $2, $3); } ; VarDecl : BType VarDef VarDeclList SEMICOLON { $$ = node("VarDecl", 4, $1, $2, $3, $4); } ; VarDeclList : /* ε */ { $$ = node("VarDeclList", 0); } | VarDeclList COMMA VarDef { $$ = node("VarDeclList", 3, $1, $2, $3); } ; VarDef : IDENT ConstExpList { $$ = node("VarDef", 2, $1, $2); } | IDENT ConstExpList ASSIGN InitVal { $$ = node("VarDef", 4, $1, $2, $3, $4); } ; InitVal : Exp { $$ = node("InitVal", 1, $1); } | LBRACE InitValList RBRACE { $$ = node("InitVal", 3, $1, $2, $3); } | LBRACE RBRACE { $$ = node("InitVal", 2, $1, $2); } ; InitValList : InitVal { $$ = node("InitValList", 1, $1); } | InitValList COMMA InitVal { $$ = node("InitValList", 3, $1, $2, $3); } ; FuncDef : BType IDENT LPARENTHESIS RPARENTHESIS Block { $$ = node("FuncDef", 5, $1, $2, $3, $4, $5); } | VOID IDENT LPARENTHESIS RPARENTHESIS Block { $$ = node("FuncDef", 5, $1, $2, $3, $4, $5); } | VOID IDENT LPARENTHESIS FuncFParams RPARENTHESIS Block { $$ = node("FuncDef", 6, $1, $2, $3, $4, $5, $6); } | BType IDENT LPARENTHESIS FuncFParams RPARENTHESIS Block { $$ = node("FuncDef", 6, $1, $2, $3, $4, $5, $6); } ; FuncFParams : FuncFParam { $$ = node("FuncFParams", 1, $1); } | FuncFParams COMMA FuncFParam { $$ = node("FuncFParams", 3, $1, $2, $3); } ; FuncFParam : BType IDENT { $$ = node("FuncFParam", 2, $1, $2); } | BType IDENT LBRACKET RBRACKET ExpList { $$ = node("FuncFParam", 5, $1, $2, $3, $4, $5); } ; ExpList : /* ε */ { $$ = node("ExpList", 0); } | ExpList LBRACKET Exp RBRACKET { $$ = node("ExpList", 4, $1, $2, $3, $4); } ; Block : LBRACE BlockList RBRACE { $$ = node("Block", 3, $1, $2, $3); } ; BlockList : /* ε */ { $$ = node("BlockList", 0); } | BlockList BlockItem { $$ = node("BlockList", 2, $1, $2); } ; BlockItem : Decl { $$ = node("BlockItem", 1, $1); } | Stmt { $$ = node("BlockItem", 1, $1); } ; Stmt : LVal ASSIGN Exp SEMICOLON { $$ = node("Stmt", 4, $1, $2, $3, $4); } | Exp SEMICOLON { $$ = node("Stmt", 2, $1, $2); } | SEMICOLON { $$ = node("Stmt", 1, $1); } | Block { $$ = node("Stmt", 1, $1); } | IF LPARENTHESIS Cond RPARENTHESIS Stmt { $$ = node("Stmt", 5, $1, $2, $3, $4, $5); } | IF LPARENTHESIS Cond RPARENTHESIS Stmt ELSE Stmt { $$ = node("Stmt", 7, $1, $2, $3, $4, $5, $6, $7); } | WHILE LPARENTHESIS Cond RPARENTHESIS Stmt { $$ = node("Stmt", 5, $1, $2, $3, $4, $5); } | BREAK SEMICOLON { $$ = node("Stmt", 2, $1, $2); } | CONTINUE SEMICOLON { $$ = node("Stmt", 2, $1, $2); } | RETURN Exp SEMICOLON { $$ = node("Stmt", 3, $1, $2, $3); } | RETURN SEMICOLON { $$ = node("Stmt", 2, $1, $2); } ; Exp : AddExp { $$ = node("Exp", 1, $1); } ; Cond : LOrExp { $$ = node("Cond", 1, $1); } ; LVal : IDENT ExpList { $$ = node("LVal", 2, $1, $2); } ; PrimaryExp : LPARENTHESIS Exp RPARENTHESIS { $$ = node("PrimaryExp", 3, $1, $2, $3); } | LVal { $$ = node("PrimaryExp", 1, $1); } | Number { $$ = node("PrimaryExp", 1, $1); } ; Number : Integer { $$ = node("Number", 1, $1); } | Floatnum { $$ = node("Number", 1, $1); } ; UnaryExp : PrimaryExp { $$ = node("UnaryExp", 1, $1); } | IDENT LPARENTHESIS FuncRParams RPARENTHESIS { $$ = node("UnaryExp", 4, $1, $2, $3, $4); } | IDENT LPARENTHESIS RPARENTHESIS { $$ = node("UnaryExp", 3, $1, $2, $3); } | UnaryOp UnaryExp { $$ = node("UnaryExp", 2, $1, $2); } ; UnaryOp : ADD { $$ = node("UnaryOp", 1, $1); } | SUB { $$ = node("UnaryOp", 1, $1); } | NOT { $$ = node("UnaryOp", 1, $1); } ; FuncRParams : Exp { $$ = node("FuncRParams", 1, $1); } | FuncRParams COMMA Exp { $$ = node("FuncRParams", 3, $1, $2, $3); } ; MulExp : UnaryExp { $$ = node("MulExp", 1, $1); } | MulExp MUL UnaryExp { $$ = node("MulExp", 3, $1, $2, $3); } | MulExp DIV UnaryExp { $$ = node("MulExp", 3, $1, $2, $3); } | MulExp MOD UnaryExp { $$ = node("MulExp", 3, $1, $2, $3); } ; AddExp : MulExp { $$ = node("AddExp", 1, $1); } | AddExp ADD MulExp { $$ = node("AddExp", 3, $1, $2, $3); } | AddExp SUB MulExp { $$ = node("AddExp", 3, $1, $2, $3); } ; RelExp : AddExp { $$ = node("RelExp", 1, $1); } | RelExp LT AddExp { $$ = node("RelExp", 3, $1, $2, $3); } | RelExp LET AddExp { $$ = node("RelExp", 3, $1, $2, $3); } | RelExp GT AddExp { $$ = node("RelExp", 3, $1, $2, $3); } | RelExp GET AddExp { $$ = node("RelExp", 3, $1, $2, $3); } ; EqExp : RelExp { $$ = node("EqExp", 1, $1); } | EqExp EQ RelExp { $$ = node("EqExp", 3, $1, $2, $3); } | EqExp NEQ RelExp { $$ = node("EqExp", 3, $1, $2, $3); } ; LAndExp : EqExp { $$ = node("LAndExp", 1, $1); } | LAndExp AND EqExp { $$ = node("LAndExp", 3, $1, $2, $3); } ; LOrExp : LAndExp { $$ = node("LOrExp", 1, $1); } | LOrExp OR LAndExp { $$ = node("LOrExp", 3, $1, $2, $3); } ; ConstExp : AddExp { $$ = node("ConstExp", 1, $1); } ; Integer : INTCONST { $$ = node("Integer", 1, $1); } ; Floatnum : FLOATCONST { $$ = node("Floatnum", 1, $1); } ; %% void yyerror(const char * s) { fprintf(stderr, "Syntax Error at line %d, column %d: %s. Near '%s'\n", lines, pos_start, s, yytext); exit(1); } parse_tree *parse(const char *input_path) { if (input_path != NULL) { if (!(yyin = fopen(input_path, "r"))) { fprintf(stderr, "[ERR] Open input file %s failed.\n", input_path); exit(1); } } else { yyin = stdin; } lines = pos_start = pos_end = 1; gt = new_parse_tree(); yyrestart(yyin); yyparse(); return gt; } parse_tree_node *node(const char *name, int children_num, ...) { parse_tree_node *p = new_parse_tree_node(name); parse_tree_node *child; if (children_num == 0) { child = new_parse_tree_node("epsilon"); parse_tree_add_child(p, child); } else { va_list ap; va_start(ap, children_num); for (int i = 0; i < children_num; ++i) { child = va_arg(ap, parse_tree_node *); parse_tree_add_child(p, child); } va_end(ap); } return p; }
最新发布
05-30
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值