资源描述:
《(完整版)数据库系统基础教程第九章答案.doc》由会员上传分享,免费在线阅读,更多相关内容在教育资源-天天文库。
1、9.3.1a)Inthefollowing,weusemacroNOT_FOUNDasdefinedinthesection.voidclosestMatchPC(){EXECSQLBEGINDECLARESECTION;charmanf,SQLSTATE[6];inttargetPrice,/*holdspricegivenbyuser*/floattempSpeed,speedOfClosest;chartempModel[4],modelOfClosest[4];inttempPrice,priceOfClosest;/*fortuplejustreadfr
2、omPC&closestpricefoundsofar*/EXECSQLENDDECLARESECTION;EXECSQLDECLAREpcCursorCURSORFORSELECTmodel,price,speedFROMPC;EXECSQLOPENpcCursor;/*askuserfortargetpriceandreadtheanswerintovariabletargetPrice*//*Initially,thefirstPCistheclosesttothetargetprice.IfPCisempty,wecannotanswerthequesti
3、on,andsoabort.*/EXECSQLFETCHFROMpcCursorINTO:modelOfClosest,:priceOfClosest,:speedOfClosest;if(NOT_FOUND)/*printmessageandexit*/;while(1){EXECSQLFETCHpcCursorINTO:tempModel,:tempPrice,:tempSpeed;if(NOT_FOUND)break;if(/*tempPriceclosertotargetPricethanispriceOfClosest*/){modelOfClosest
4、=tempModel;priceOfClosest=tempPrice;speedOfClosest=tempSpeed;}}/*Now,modelOfClosestisthemodelwhosepriceisclosesttotarget.Wemustgetitsmanufacturerwithasingle-rowselect*/EXECSQLSELECTmakerINTO:manfFROMProductWHEREmodel=:modelOfClosest;printf("manf=%s,model=%d,speed=%d",manf,modelOfClo
5、sest,speedOfClosest);EXECSQLCLOSECURSORpcCursor;}b)voidacceptableLaptop(){EXECSQLBEGINDECLARESECTION;intminRam,minHd,minScreen;/*givenbyuser*/floatminSpeed;charmodel[4],maker,floatspeed;intram,hd,screen,price;EXECSQLENDDECLARESECTION;EXECSQLPREPAREquery1FROM‘SELECTmodel,speed,ram,hd,s
6、creen,price,makerFROMLaptopl,ProductpWHEREspeed>=?ANDram>=?ANDhd>=?ANDscreen>=?ANDl.model=p.model’EXECSQLDECLAREcursor1CURSORFORquery1;/*askuserforminimumspeed,ram,hdsize,andscreensize*/EXECSQLOPENcursor1USING:minSpeed,:minRam,:minHd,:minScreen;while(!NOT_FOUND){EXECSQLFETCHcursor1INT
7、O:model,:speed,:ram,:hd,:screen,:price,:maker;if(FOUND){printf("maker:%s,model:%d,speed:%.2f,ram:%d,hd:%d,screen:%d,price:%d",maker,model,speed,ram,hd,screen,price);}}EXECSQLCLOSECURSORcursor1;}c)voidproductsByMaker(){EXECSQLBEGINDECLARESECTION;charmaker,model[4],type[10],color[6]
8、;floa