C语言程序设计现代方法第16章答案_第1页
C语言程序设计现代方法第16章答案_第2页
C语言程序设计现代方法第16章答案_第3页
已阅读5页,还剩3页未读 继续免费阅读

下载本文档

版权说明:本文档由用户提供并上传,收益归属内容提供方,若内容存在侵权,请进行举报或认领

文档简介

Chapter16AnswerstoSelectedExercisesAnswerstoSelectedExercises2.[was#2;modified](a)struct{doublereal,imaginary;}c1,c2,c3;(b)struct{doublereal,imaginary;}c1={0.0,1.0},c2={1.0,0.0},c3;(c)Onlyonestatementisnecessary:c1=c2;(d)c3.real=c1.real+c2.real;c3.imaginary=c1.imaginary+c2.imaginary;4.[was#4;modified](a)typedefstruct{doublereal,imaginary;}Complex;(b)Complexc1,c2,c3;(c)Complexmake_complex(doublereal,doubleimaginary){Complexc;c.real=real;c.imaginary=imaginary;returnc;}(d)Complexadd_complex(Complexc1,Complexc2){Complexc3;c3.real=c1.real+c2.real;c3.imaginary=c1.imaginary+c2.imaginary;returnc3;}11.[was#10;modified]Theamemberwilloccupy8bytes,theunionewilltake8bytes(thelargestmember,c,is8byteslong),andthearrayfwillrequire4bytes,sothetotalspaceallocatedforswillbe20bytes.14.[was#12;modified](a)doublearea(structshapes){if(s.shape_kind==RECTANGLE)returns.u.rectangle.height*s.u.rectangle.width;elsereturn3.14159*s.u.circle.radius*s.u.circle.radius;}(b)structshapemove(structshapes,intx,inty){structshapenew_shape=s;new_shape.center.x+=x;new_shape.center.y+=y;returnnew_shape;}(c)structshapescale(structshapes,doublec){structshapenew_shape=s;if(new_shape.shape_kind==RECTANGLE){new_shape.u.rectangle.height*=c;new_shape.u.rectangle.width*=c;}elsenew_shape.u.circle.radius*=c;returnnew_shape;}15.[was#14]enumweek_days{MON,TUE,WED,THU,FRI,SAT,SUN};typedefenum{MON,TUE,WED,THU,FRI,SAT,SUN}Week_days;17.[was#16]Allthestatementsarelegal,sinceCallowsintegersandenumerationvaluestobemixedwithoutrestriction.Only(a),(d),and(e)aresafe.(b)isnotmeaningfulifihasavalueotherthan0or1.(c)willnotyieldameaningfulresultifbhasthevalue1.1.[was#6;1.[was#6;modified]#include<stdio.h>#defineCOUNTRY_COUNT\((int)(sizeof(country_codes)/sizeof(country_codes[0])))structdialing_code{char*country;intcode;};conststructdialing_code{{"Argentina",country_codes[]=54},{"Bangladesh",880},{"Brazil",55},{"Burma(Myanmar)",95},{"China",86},{"Colombia",57},{"Congo,Dem.Rep.of",243},{"Egypt",20},{"Ethiopia",251},{"France",33},{"Germany",49},{"India",91},{"Indonesia",62},{"Iran",98},{"Italy",39},{"Japan",81},{"Mexico",52},{"Nigeria",234},{"Pakistan",92},{"Philippines",63},{"Poland",48},{"Russia",7},{"SouthAfrica",27},{"SouthKorea",82},{"Spain",34},{"Sudan",249},{"Thailand",66},{"Turkey",90},{"Ukraine",380},{"UnitedKingdom",44},{"UnitedStates",1},{"Vietnam",84}};intmain(void){intcode,i;printf("Enterdialingcode:");scanf("%d",&code);for(i=0;i<COUNTRY_COUNT;i++)if(code==country_codes[i].code){printf("Thecountrywithdialingcode%dis%s\n",code,country_codes[i].country);return0;}printf("Nocorrespondingcountryfound\n");return0;}3.[was#8]#include<stdio.h>#include"readline.h"#defineNAME_LEN25#defineMAX_PARTS100structpart{intnumber;charname[NAME_LEN+1];inton_hand;};intfind_part(intnumber,conststructpartinv[],intvoidinsert(structpartinv[],int*np);voidsearch(conststructpartinv[],intvoidupdate(structpartinv[],intnp);voidprint(conststructpartinv[],intnp);/**********************************************************main:Promptstheusertoenteranoperationcode, *thencallsafunctiontoperformtherequested *action.Repeatsuntiltheuserentersthe *command'q'.Printsanerrormessageiftheuser*entersanillegalcode. ***********************************************************/intmain(void){charcode;structpartinventory[MAX_PARTS];intnum_parts=0;for(;;){printf("Enteroperationcode:");scanf("%c",&code);while(getchar()!='\n') /*skipstoendofline*/;switch(code){case'i':insert(inventory,break;case's':search(inventory,num_parts);break;case'u':update(inventory,num_parts);break;case'p':print(inventory,num_parts);break;case'q':return0;default: printf("Illegalcode\n");}printf("\n");}}/**********************************************************find_part:Looksupapartnumberintheinvarray. *Returnsthearrayindexifthepartnumber *isfound;otherwise,returns-1. ***********************************************************/intfind_part(intnumber,conststructpartinv[],intnp){inti;for(i=0;i<np;i++)if(inv[i].number==number)returni;return-1;}/***********************************************************insert:Promptstheuserforinformationaboutanew**partandtheninsertsthepartintotheinv**array.Printsanerrormessageandreturns**prematurelyifthepartalreadyexistsorthe**arrayisfull.***********************************************************/voidinsert(structpartinv[],int*np){intpart_number;if(*np==MAX_PARTS){printf("Databaseisfull;can'taddmoreparts.\n");return;}printf("Enterpartnumber:");scanf("%d",&part_number);if(find_part(part_number,inv,*np)>=0){printf("Partalreadyexists.\n");return;}inv[*np].number=part_number;printf("Enterpartname:");read_line(inv[*np].name,NAME_LEN);printf("Enterquantityonhand:scanf("%d",&inv[*np].on_hand);(*np)++;}/***********************************************************search:Promptstheusertoenterapartnumber,then**looksupthepartintheinvarray.Ifthe**partexists,printsthenameandquantityon**hand;ifnot,printsanerrormessage.***********************************************************/voidsearch(conststructpartinv[],intnp){inti,number;printf("Enterpartnumber:");scanf("%d",&number);i=find_part(number,inv,np);if(i>=0){printf("Partname:%s\n",inv[i].name);printf("Quantityonhand:%d\n",inv[i].on_hand);}elseprintf("Partnotfound.\n");}/**********************************************************update:Promptstheusertoenterapartnumber. *Printsanerrormessageifthepartcan'tbe *foundintheinvarray;otherwise,promptsthe*usertoenterchangeinquantityonhandand *updatesthearray. ***********************************************************/voidupdate(structpartinv[],intnp){inti,number,change;printf("Enterpartnumber:scan

温馨提示

  • 1. 本站所有资源如无特殊说明,都需要本地电脑安装OFFICE2007和PDF阅读器。图纸软件为CAD,CAXA,PROE,UG,SolidWorks等.压缩文件请下载最新的WinRAR软件解压。
  • 2. 本站的文档不包含任何第三方提供的附件图纸等,如果需要附件,请联系上传者。文件的所有权益归上传用户所有。
  • 3. 本站RAR压缩包中若带图纸,网页内容里面会有图纸预览,若没有图纸预览就没有图纸。
  • 4. 未经权益所有人同意不得将文件中的内容挪作商业或盈利用途。
  • 5. 人人文库网仅提供信息存储空间,仅对用户上传内容的表现方式做保护处理,对用户上传分享的文档内容本身不做任何修改或编辑,并不能对任何下载内容负责。
  • 6. 下载文件中如有侵权或不适当内容,请与我们联系,我们立即纠正。
  • 7. 本站不保证下载资源的准确性、安全性和完整性, 同时也不承担用户因使用这些下载资源对自己和他人造成任何形式的伤害或损失。

评论

0/150

提交评论