CPrimerPlus第六版中文版习题答案_第1页
CPrimerPlus第六版中文版习题答案_第2页
CPrimerPlus第六版中文版习题答案_第3页
CPrimerPlus第六版中文版习题答案_第4页
CPrimerPlus第六版中文版习题答案_第5页
已阅读5页,还剩171页未读 继续免费阅读

下载本文档

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

文档简介

/CPrimerPlus第六版中文版习题答案Github:Email:第一章1.#include<stdio.h>intmain(void){doubleinch,cm;printf("Pleaseinputtheinches:");scanf("%lf",&inch);cm=inch*2.54;printf("%gcm\n",cm);return0;}第二章3。#include〈stdio.h>intmain(void){intdays,years=21;days=years*365;printf("我的年龄是%d岁,%d天\n”,years,days);return0;}4.#include<stdio.h>voidjolly(void);voiddeny(void);intmain(void){jolly();jolly();deny();return0;}voidjolly(void){printf("Forhe'sajollygoodfellow!\n");}voiddeny(void){printf(”Whichnobodycandeny!\n");}5.#include〈stdio.h〉voidbr(void);voidic(void);intmain(void){br();printf(”,”);ic();printf("\n”);ic();printf(”\n");br();printf("\n");return0;}voidbr(void){printf(”Brazil,Russia");}voidic(void){printf("India,China”);}6.#include<stdio.h>intmain(void){inttoes=10;inttoes_2,toes2;toes_2=2*toes;toes2=toes*toes;printf("toes是%d,toes的两倍是%d,toes的平方是%d\n",toes,toes_2,toes2);return0;}8.#include〈stdio.h〉voidone_three(void);voidtwo(void);intmain(void){printf("startingnow\n”);one_three();}voidone_three(void){printf("one\n");two();printf("three\n”);printf(”done!\n”);}voidtwo(void){printf("two\n");}第三章2。#include〈stdio.h>intmain(void){charch;printf("pleaseinputanumber:”);scanf("%d",&ch);printf(”%c\n",ch);return0;}4。#include<stdio.h>intmain(void){floata;printf("Enterafloating—pointvalue:”);scanf(”%f",&a);printf("fixed-pointnotation:%f\n",a);printf("exponentialnotation:%e\n",a);return0;}5。#include〈stdio.h>intmain(void){intage;doubleseconds;printf("pleaseinputyourage:");scanf(”%d",&age);seconds=age*3。156e7;printf(”thecorrespondingsecondsare:%e\n",seconds);return0;}7。#include<stdio。h>intmain(void){floatinches,cms;printf("inputyourheight(inch):”);scanf(”%f”,&inches);cms=inches*2.54;printf("yourheight(cm):%f\n",cms);return0;}8。#include<stdio。h>intmain(void){floatpint,ounce,soupspoon,teaspoon,cup;printf("inputthenumberofcups:");scanf(”%f”,&cup);pint=cup/2;ounce=cup*8;soupspoon=ounce*2;teaspoon=soupspoon*3;printf(”theyareequivalentof:\n%fpint\n%founce\n%fsoupspoons\n%fteaspoons\n",pint,ounce,soupspoon,teaspoon);return0;}第四章1.#include<stdio。h〉intmain(void){charfirstname[40],lastname[40];printf(”Inputyourfirstname:");scanf(”%s",firstname);printf("Inputyourlastname:”);scanf(”%s",lastname);printf("Yournameis%s,%s\n",firstname,lastname);return0;}2.#include<stdio。h〉#include<string.h〉intmain(void){charname[40];intwidth;printf("Inputyourname:");scanf("%s",name);width=strlen(name)+3;printf("%*s\n",width,name);//输入的名和姓中间不能分隔return0;}4.#include<stdio.h>intmain(void){floatheight;charname[40];printf(”Inputyourheight(cm)andname:");scanf(”%f%s",&height,name);height=height/100;printf("%s,youare%.3fmtall\n",name,height);return0;}5.#include<stdio。h〉intmain(void){floatspeed,size,time;printf("Inputthedownloadspeed(Mb/s)andthe(MB):\n");scanf("%f%f”,&speed,&size);time=size/speed*8.0;printf(”At%.2fmegabitspersecond,a%.2fmegabytes\n",speed,size);printf(”downloadsin%。2fseconds.\n”,time);return0;}6。#include〈stdio.h〉#include<string.h〉intmain(void){charfirstname[40],lastname[40];printf("Inputyourfirstname:”);scanf("%s",firstname);printf("Inputyourlastname:");scanf("%s",lastname);printf("%s%s\n",firstname,lastname);printf("%*d%*d\n",strlen(firstname),strlen(firstname),strlen(lastname),strlen(lastname));printf(”%s%s\n",firstname,lastname);printf("%*d%*d\n",-strlen(firstname),strlen(firstname),-strlen(lastname),strlen(lastname));return0;}7.#include〈stdio。h>#include<float.h>intmain(void){doublea=1.0/3.0;floatb=1。0/3.0;printf(”%.6f%.6f\n”,a,b);//左侧double型右侧float型printf(”%.12f,%.12f\n",a,b);printf("%。16f,%。16f\n”,a,b);printf("DBL_DIG:%d\n",DBL_DIG);printf("FLT_DIG:%d\n”,FLT_DIG);return0;}8.#include<stdio.h〉#defineGALLON3.758//1gallon=3.785liters#defineMILE1.609//1mile=1。609kilometersintmain(void){floatgallon,mile;printf(”Inputmilesandgallons:”);scanf(”%f%f",&mile,&gallon);printf("Milespergallon:%.1f\n”,mile/gallon);printf(”Litreper100kilometers:%.1f\n”,gallon*GALLON/(mile*MILE)*100);return0;}第五章1.#include<stdio。h〉#defineH_P_M60//1h=60minintmain(void){inthour,min,left;printf(”Enterthenumberofminutes:");scanf("%d",&min);while(min>0){hour=min/H_P_M;left=min%H_P_M;printf(”%dminutesis%dhoursand%dminutes。\n",min,hour,left);printf("Enteryournextvalue:");scanf("%d",&min);}printf(”Goodbye!\n");return0;}2.#include〈stdio.h>intmain(void){intnum,count;printf(”Inputainteger:");scanf(”%d",&num);count=0;while(count++<11){printf("%d",num);num++;}printf("\n");return0;}3。#include<stdio.h>#defineDAYS_PER_WEEK7//一周7天intmain(void){intday,week,left;printf("Inputthenumberofdays:");scanf(”%d",&day);while(day>0){week=day/DAYS_PER_WEEK;left=day%DAYS_PER_WEEK;printf("%ddaysare%dweeks,%ddays。\n",day,week,left);printf("Nextinput:”);scanf(”%d”,&day);}return0;}4.#include<stdio。h〉#defineCM_PER_FEET30.48//1feet=30.48cm#defineCM_PER_INCH2.54//1inch=2.54cmintmain(void){intfeet;floatcm,inch;printf("Enteraheightincentimeters:");scanf(”%f”,&cm);while(cm>0){feet=(int)(cm/CM_PER_FEET);inch=(cm-feet*CM_PER_FEET)/CM_PER_INCH;printf(”%。1fcm=%dfeet,%.1finches\n”,cm,feet,inch);printf("Enteraheightincentimeters(<=0toquit):");scanf("%f",&cm);}printf("bye\n");return0;}5。#include〈stdio.h>intmain(void){intcount,sum,days;printf("Inputthenumberofdays:");scanf("%d",&days);count=sum=0;while(count++<days)sum=sum+count;printf("Themoneyyouearned:%d\n",sum);return0;}6.#include<stdio。h〉intmain(void){intcount,sum,days;printf("Inputthenumberofdays:");scanf(”%d",&days);count=sum=0;while(count++<days)sum=sum+count*count;printf("Themoneyyouearned:%d\n”,sum);return0;}7.#include<stdio.h〉voidcube(doublen);intmain(void){doublenum;printf("Inputanumber:");scanf("%lf",&num);cube(num);}voidcube(doublen){printf(”Thecubeof%fis%f\n",n,n*n*n);}8.#include〈stdio.h>intmain(void){intnum1,num2;printf("Thisprogramcomputesmoduli。\n");printf("Enteranintegertoserveasthesecondoperand:");scanf(”%d",&num1);printf("Nowenterthefirstoperand:”);scanf(”%d”,&num2);while(num2〉0){printf("%d%%%dis%d\n”,num2,num1,num2%num1);printf("Enternextnumberforfirstoperand(〈=0toquit):”);scanf("%d",&num2);}printf("Done\n");}9.#include〈stdio。h〉voidTemperatures(doublefah);intmain(void){doublefah,cel,kel;//华氏温度,摄氏温度,开氏温度printf("InputtheFahrenheittemperature:”);while(scanf("%lf",&fah)==1){Temperatures(fah);printf("Nextinput:");}printf(”Done。\n”);}voidTemperatures(doublefah){constdoublea=5。0,b=9.0,c=32.0,d=276。13;printf("%.2f℉is%.2f℃,%.2fK.\n",fah,a/b*(fah—c),a/b*(fah-c)+d);}第六章1.#include<stdio.h>#defineSIZE26intmain(void){charch[SIZE];intindex;for(index=0;index〈SIZE;index++){ch[index]='a'+index;printf("%c",ch[index]);}printf(”\n”);return0;}2。#include<stdio。h〉intmain(void){inti,j;for(i=1;i〈=5;i++){for(j=1;j〈=i;j++)printf(”$");printf("\n”);}return0;}3.#include<stdio.h>intmain(void){inti,j;for(i=1;i<=6;i++){for(j=0;j〈i;j++)printf(”%c",'F'—j);printf("\n");}return0;}4.#include<stdio.h〉#defineROWS6intmain(void){charch;inti,j;for(ch='A',i=0;i<ROWS;i++){for(j=0;j<=i;j++)printf(”%c",ch++);printf(”\n");}return0;}5.#include<stdio.h〉#defineROWS5intmain(void){charch='A’;inti,j;for(i=1;i〈=ROWS;i++){for(j=1;j<=ROWS-i;j++)printf("");for(j=0;j〈i;j++)printf("%c",ch+j);for(j=i—2;j〉=0;j——)printf("%c”,ch+j);printf("\n”);}return0;}6。#include<stdio.h>intmain(void){intmax,min,num;printf("Inputtheminandmax:");scanf("%d%d”,&min,&max);printf(”%10s%10s%10s\n","number",”square","cube”);for(num=min;num<=max;num++)printf("%10d%10d%10d\n”,num,num*num,num*num*num);return0;}7。//与题目不同打印的是句子#include<stdio.h>#include〈string.h>#defineSIZE40intmain(void){inti,index=-1;charch[SIZE];printf(”Inputaword:");do{ index++;scanf(”%c",&ch[index]);}while(ch[index]!='\n');for(i=index+1;i<=40;i++)ch[i]=’\0';for(index=strlen(ch);index>=0;index-—)printf("%c”,ch[index]);printf("\n");return0;}8。#include<stdio.h〉intmain(void){doublen1,n2;printf("Inputtwonumbers:”);while(2==scanf(”%lf%lf",&n1,&n2)){printf("%f\n”,(n1-n2)/n1*n2);printf("Inputyournextpairofnumbers:");}printf("Bye!\n");return0;}9。#include〈stdio.h>doublecalculate(doublen1,doublen2);intmain(void){doublenum1,num2;printf("Inputtwonumbers:");while(2==scanf(”%lf%lf",&num1,&num2))//输入两个浮点数{printf(”%f\n",calculate(num1,num2));//函数调用printf("Inputyournextpairofnumbers:”);}printf("Bye!\n”);return0;}doublecalculate(doublen1,doublen2){return((n1-n2)/(n1*n2));//返回运算结果}10.#include〈stdio。h>intmain(void){intlower,upper;intnum,sum;printf(”Enterlowerandupperintegerlimits:”);scanf(”%d%d",&lower,&upper);while(lower<upper){for(sum=0,num=lower;num<=upper;num++)sum=sum+num*num;//计算平方和printf("Thesumsofthesquaresfrom%dto%dis%d\n”,lower*lower,upper*upper,sum);//输出结果printf("Enternextsetoflimits:");scanf("%d%d”,&lower,&upper);//下一次输入}printf("Done\n");return0;}11.#include〈stdio。h〉#defineSIZE8intmain(void){intnum[SIZE];intindex;printf("Enter8integers:");for(index=0;index〈SIZE;index++)//输入8个整数scanf(”%d",&num[index]);for(index=SIZE-1;index〉=0;index--)ﻩ//倒序输出printf(”%d”,num[index]);printf("\n”);return0;}12.#include<stdio.h>intmain(void){doublesum1=0,sum2=0;intcount,items,sign;printf(”Entertheitems:");scanf(”%d",&items);//输入序列的项数for(count=1,sign=1;count<=items;count++,sign*=-1){sum1+=1.0/count;sum2+=1.0*sign/count;} ﻩﻩﻩ ﻩﻩ ﻩ ﻩ//分别计算两序列的和printf("1.0+1。0/2.0+1.0/3.0+1.0/4。0+...=%f\n",sum1);printf(”1。0-1。0/2.0+1.0/3.0—1.0/4.0+。。.=%f\n",sum2);return0;}13。#include〈stdio.h>#defineSIZE8intmain(void){intindex,count,num[SIZE];for(index=0,count=1;index<SIZE;index++){count*=2;num[index]=count;} ﻩﻩﻩ ﻩ //for循环将数组元素设为2的前8次幂index=0;ﻩﻩ ﻩﻩﻩ//初始化index的值doprintf(”%d",num[index++]);while(index〈SIZE); ﻩ//dowhile循环显示数组元素的值printf("\n");return0;}14。#include<stdio。h>#defineSIZE8intmain(){doublenum1[SIZE],num2[SIZE];intindex1,index2,index;printf(”Enter8numberstothefirstarray:\n”);for(index1=0;index1〈SIZE;index1++)scanf(”%lf",&num1[index1]); ﻩ //向第一个数组输入8个数num2[0]=num1[0];for(index1=1,index2=1;index1<SIZE;index1++,index2++)num2[index2]=num2[index2-1]+num1[index1];//为第二个数组赋值(是第一个数组对应的元素之和)printf(”Thefirstarray:");for(index=0;index<SIZE;index++){printf("%6.2f",num1[index]);} ﻩ ﻩ ﻩﻩﻩﻩ//输出第一个数组的内容printf("\nThesecondarray:”);for(index=0;index<SIZE;index++){printf("%6.2f",num2[index]); ﻩ //输出第二个数组的内容}printf(”\n");return0;}15。#include<stdio.h>#include<string.h〉#defineSIZE255intmain(void){intindex;charch[SIZE];printf(”Enteraline:”);for(index=0,scanf("%c”,&ch[0]);ch[index]!='\n';){index++;scanf("%c”,&ch[index]);} ﻩ ﻩﻩ //输入内容到字符数组中,回车时结束for(index+=1;index〈SIZE;index++)ch[index]=’\0';ﻩ ﻩ ﻩ//将数组剩余空间补充为’\0'for(index=strlen(ch);index〉=0;index--)printf("%c”,ch[index]); ﻩ //倒序输出内容printf("\n");return0;}16.#include<stdio。h>#defineRATE_DAPHNE0。1#defineRATE_DEIRDRE0.05ﻩ ﻩﻩ//两人的利率#defineﻩMONEY100intmain(void){intyear;doubledaphne=MONEY,deirdre=MONEY; //两人的初始投资额相同for(year=1;daphne>=deirdre;year++){daphne+=MONEY*RATE_DAPHNE;deirdre+=deirdre*RATE_DEIRDRE;}//计算Deirdre投资额超过Daphne需要的年数和当时的金额printf("After%dyear,Deirdre'sinvestmentwillbemorethanDaphne’s,\n""Daphne'sinvestmentwillbe$%lf,\nandDeirdre'sinvestmentwillbe$%lf。\n”,year,daphne,deirdre);ﻩﻩﻩ //输出结果return0;}17。#include〈stdio.h〉#defineINITIAL_MONEY100 ﻩ//账户初始金额为100万元#defineANNUAL_RATE0.08 //年利率为8%intmain(void){intyear;doublemoney;for(year=1,money=INITIAL_MONEY;money〉0;year++)money+=money*ANNUAL_RATE-10; //计算每年年终的账户余额printf("After%dyears,Chuckiewilldrawallmoneyfromhisaccount.\n",year);return0;}18.#include<stdio。h>#defineﻩINITIAL_NUMBER5//初始朋友数为5人#defineDUNBAR_NUMBER150//邓巴数intmain(void){intweek;intnumber=INITIAL_NUMBER;for(week=1;number<=DUNBAR_NUMBER;week++){number=(number-week)*2;ﻩﻩ//计算每周的朋友数量printf(”After%dweek,thenumberofRabnud'sfriendsis%d\n”,week,number);}return0;}第七章1.#include<stdio。h>intmain(void){charch;intn_space=0; ﻩ//空格数intn_newline=0;ﻩﻩ//换行数intn_others=0; ﻩ//其他字符数printf("Entersometext;Enter#toquit。\n");while((ch=getchar())!='#’){if(ch=='')n_space++;elseif(ch==’\n')n_newline++;elsen_others++;}printf(”Spaces:%d,newlines:%d,others:%d\n",n_space,n_newline,n_others);return0;}2.#include<stdio。h〉#defineCHARS_PER_LINE8ﻩ//每行字符数intmain(void){charch;intn_chars=1;ﻩﻩ //字符数printf("Entersomecharacters(#toquit):\n”);while((ch=getchar())!=’#'){printf("%3c(%3d)",ch,ch);if(n_chars++%CHARS_PER_LINE==0)printf("\n");}printf("\n”);return0;}3.#include<stdio.h>intmain(void){intnum;intn_even=0,n_odd=0; ﻩ//偶数和奇数个数intsum_even=0,sum_odd=0;ﻩ//偶数和奇数和printf("Entersomeintegers(0toquit):\n”);scanf("%d”,&num);while(num!=0){if(num%2==0){n_even++;sum_even+=num;}ﻩﻩ ﻩﻩ //计算偶数个数和偶数和else{n_odd++;sum_odd+=num;}ﻩﻩ ﻩ//计算奇数个数和奇数和scanf("%d",&num);}printf(”Thenumberofevennumbersis%d,""andtheeverageofevennumbersis%.2f\n",n_even,(n_even==0)?0:(float)sum_even/n_even);printf(”Thenumberofoddnumbersis%d,""andtheeverrageofoddnumersis%.2f\n”,n_odd,(n_odd==0)?0:(float)sum_odd/n_odd);return0;}4.#include<stdio.h>intmain(void){charch;intn_repl=0;//替换次数printf(”Entersometexts(#toquit):\n");while((ch=getchar())!=’#'){if(ch==’.'){ch=’!';n_repl++;} ﻩ//替换句号elseif(ch=='!’){printf(”!");n_repl++;} ﻩ ﻩ //替换感叹号printf("%c”,ch);}printf("\n%dsubstitutionsweremade。\n”,n_repl);return0;}5.#include<stdio.h>intmain(void){charch;intn_repl=0;ﻩ ﻩﻩ//替换次数printf("Entersometexts(#toquit):\n”);while((ch=getchar())!='#'){switch(ch){case'。':ch=’!';n_repl++;break;case’!’:printf(”!");n_repl++;break;default:break;}ﻩ ﻩﻩﻩ//利用switch语句进行替换printf(”%c”,ch);}printf("\n%dsubstitutionsweremade.\n”,n_repl);return0;}6。#include<stdio。h>intmain(void){charch;charlast_ch=0;ﻩﻩ//前一个字符intcount=0;printf("Entersometexts(#toquit):\n”);while((ch=getchar())!='#’){if((ch==’i')&&(last_ch==’e'))count++;last_ch=ch; ﻩ//出现ei时,计数+1}printf("\”ei\"appeared%dtimes。\n",count);return0;}7。#include<stdio.h〉#defineBASE1000 ﻩ//基本工资100美元/h#defineTIME40 ﻩﻩ//超过40h为加班#defineﻩMULﻩ1.5 ﻩ//加班时间算作平时的1。5倍#define RATE10。15 ﻩ//前300美元的税率#defineRATE20.2ﻩﻩ//300-450美元的税率#defineRATE30.25 ﻩ//大于450美元的税率#defineBREAK1300ﻩﻩ//税率的第一个分界点#defineBREAK2450ﻩﻩ//税率的第二个分界点intmain(void){doublehour,tax,gross;printf(”Inputyourworkhoursinaweek:");scanf("%lf”,&hour);if(hour<=TIME)gross=hour*BASE;elsegross=TIME*BASE+(hour-TIME)*MUL*BASE;//计算总收入if(gross〈=BREAK1)tax=gross*RATE1;elseif(gross<=BREAK2)tax=BREAK1*RATE1+(gross-BREAK1)*RATE2;elsetax=BREAK1*RATE1+(BREAK2—BREAK1)*RATE2+(gross—BREAK2)*RATE3;//计算税金printf(”Yourgrossincomeis$%.2lf\nYourtaxis$%.2lf\n""Yournetincomeis$%。2lf\n",gross,tax,(gross—tax));return0;8.#include〈stdio。h〉#defineBASE18。75#defineBASE29.33#defineBASE310.00#defineBASE411。20//四种等级的基本工资#defineTIME40 //超过40h为加班#define MUL 1。5ﻩ ﻩ//加班时间算作平时的1.5倍#defineﻩRATE10.15 ﻩ//前300美元的税率#defineRATE20。2 //300-450美元的税率#defineRATE30。25 ﻩ//大于450美元的税率#defineBREAK1300ﻩ //税率的第一个分界点#defineBREAK2450 ﻩ//税率的第二个分界点intmain(void){doublebase,hour,tax,gross;intcount,num;constintLENGTH=65; //*的长度printpart:for(count=0;count<LENGTH;count++)printf("*”);printf(”\nEnterthenumbercorrespondingtothedesiredpayrateoraction:\n");printf("%-36s%s","1)$8.75/hr”,"2)$9.33/hr\n”);printf("%—36s%s",”3)$10.00/hr","4)$11。20/hr\n”);printf(”%s\n",”5)quit");for(count=0;count<LENGTH;count++)printf("*");printf("\n”);//打印表格while(scanf("%d",&num)==1){switch(num){case1:ﻩbase=BASE1;break;case2:base=BASE2;break;case3:base=BASE3;break;case4:base=BASE4;break;case5:printf("quit.\n");return0;default:printf("Pleaseinputtherightoption.\n");gotoprintpart;} //选择基本工资等级printf(”Inputyourworkhoursinaweek:”);scanf("%lf",&hour);if(hour〈=TIME)gross=hour*base;elsegross=TIME*base+(hour-TIME)*MUL*base;//计算总收入if(gross〈=BREAK1)tax=gross*RATE1;elseif(gross〈=BREAK2)tax=BREAK1*RATE1+(gross-BREAK1)*RATE2;elsetax=BREAK1*RATE1+(BREAK2—BREAK1)*RATE2+(gross-BREAK2)*RATE3;//计算税金printf("Yourgrossincomeis$%.2lf\nYourtaxis$%.2lf\n""Yournetincomeis$%。2lf\n”,gross,tax,(gross-tax));printf("\nYournextchoice:\n");}return0;}9.#include〈stdio.h>intmain(void){intdiv,prime;intnum,count;intflag;printf("Inputapositiveinteger:");scanf("%d",&num);printf("Theprimenumbersinrange:\n");for(prime=2;prime<=num;prime++) //外层循环显示所有素数{flag=1;for(div=2;(div*div)<=prime;div++){if(prime%div==0)flag=0;} ﻩﻩﻩﻩ//内层循环检验是否为素数if(flag)ﻩ ﻩﻩ ﻩ//利用标记flag判断printf(”%d”,prime);}printf(”\n”);return0;}10.#include<stdio。h>#defineRATE10.15#defineRATE20.28#defineSINGLE17850 //单身人群的税率分界点#defineHOST23900 //户主人群的税率分界点#defineMAR_SHA 29750 //已婚共有人群的分界点#defineMAR_DEVﻩ14875 //已婚离异人群的分界点intmain(void){intnum;doubleincome,tax_break,tax;printpart:printf(”PleaseenterCorresponding""figurestoselectthetype\n”);printf("1single,2host,3marriedandshared,”"4marriedbutdevocedand5toquit.\n");scanf("%d”,&num);switch(num){case1:tax_break=SINGLE;break;case2:tax_break=HOST;break;case3:tax_break=MAR_SHA;break;case4:tax_break=MAR_DEV;break;case5:ﻩprintf(”quit.\n");return0;default:printf("Pleaseinputrightnumber.");gotoprintpart; ﻩ//回到输入阶段}printf("Enteryourincome:”);ﻩﻩ//指定种类和收入while(scanf("%lf”,&income)==1){if(income<=tax_break)tax=income*RATE1;elsetax=tax_break*RATE1+(income—tax_break)*RATE2;//计算税金printf("Thetaxis$%。2lf.\n",tax);printf(”Yournextinput:\n");gotoprintpart; ﻩ //回到输入阶段}return0;}11.#include〈stdio.h>#include<ctype.h>#defineARTICHOKE2.05 ﻩ//洋蓟2.05美元/磅#defineBEET1.15 //甜菜1。15美元/磅#defineCARROT1.09 ﻩﻩ//胡萝卜1.09美元/磅#define DISCOUNT_LIMIT100//包装费和运费打折要求订单100美元#defineDISCOUNT_RATE0.05 //折扣为%5#defineBREAK15#defineBREAK220ﻩ //装运费的分界点#defineﻩFEE16。5#define FEE214#defineFEE3_RATE0。5//不同重量区间的装运费,其中超过20磅的每续重一磅//增加0.5元intmain(void){doubleweight;doubleweight_artichoke=0;doubleweight_beet=0;doubleweight_carrot=0;ﻩ//购买三种蔬菜的重量doubletotal_weight;ﻩ//总重量doubleveg_cost; //三种蔬菜总共花费doubleorder_cost; ﻩ//订单总额doubletotal_cost; //费用总额doublepack_tran_fee; //装运费doublediscount;intcount=0;charch;printf(”Pleaseselectthevegetablesyouwanttobuy:\n");printf(”a:artichoke$%.2f/lb\n”,ARTICHOKE);printf(”b:beet$%.2f/lb\n”,BEET);printf("c:carrot$%.2f/lb\n",CARROT);printf("q:quit.\n”);//打印选择信息while((ch=tolower(getchar()))!='q’){//ﻩif(ch=='\n')// continue;ﻩ ﻩ//滤掉回车switch(ch){case'a’:printf("Inputtheweightofartichokeinpound:");scanf("%lf",&weight);weight_artichoke+=weight;count++;printf(”Continueenteringa,b,corq:”);break;case’b’:printf("Inputtheweightofbeetinpound:");scanf(”%lf",&weight);weight_beet+=weight;count++;printf("Continueenteringa,b,corq:”);break;case'c':printf(”Inputtheweightofcarrotinpound:");scanf(”%lf",&weight);weight_carrot+=weight;count++;printf(”Continueenteringa,b,corq:");break;default:printf("Pleaseentertherightcharacter。");}while(getchar()!='\n')continue; ﻩﻩ//滤掉输入重量后面的所有字符}if(!count){printf(”Bye.\n”);return0;}ﻩﻩﻩ ﻩﻩ ﻩ//开始输出q,直接退出total_weight=weight_artichoke+weight_beet+weight_carrot;veg_cost=weight_artichoke*ARTICHOKE+weight_beet*BEET+weight_carrot*CARROT;discount=0;if(veg_cost>=DISCOUNT_LIMIT){discount=veg_cost*DISCOUNT_RATE;order_cost=veg_cost—discount;}elseorder_cost=veg_cost; ﻩﻩﻩ//折扣计算if(total_weight<=BREAK1)pack_tran_fee=FEE1;elseif(total_weight〈=BREAK2)pack_tran_fee=FEE2;elsepack_tran_fee=FEE2+(total_weight-BREAK2)*FEE3_RATE;//装运费计算total_cost=order_cost+pack_tran_fee;printf(”\nHereiswhatyouchoose:\n”);if(weight_artichoke){printf("artichokePrice:$%。2f/lbweight:%.2fpoundscost:$%.2f\n",ARTICHOKE,weight_artichoke,weight_artichoke*ARTICHOKE);}if(weight_beet){printf("beetPrice:$%。2f/lbweight:%.2fpoundscost:$%.2f\n”,BEET,weight_beet,weight_beet*BEET);}if(weight_carrot){printf("carrotPrice:$%.2f/lbweight:%。2fpoundscost:$%.2f\n",CARROT,weight_carrot,weight_carrot*CARROT);}printf("Theordercost:$%。2f\n”,veg_cost);if(discount)printf("Youhave%%%。fdiscount,andthereducedmoneyis$%.2f\n”,DISCOUNT_RATE*100,discount);printf("Thepackingandtranspotingfeeis$%.2f\n",pack_tran_fee);printf(”Thetotalcostis$%。2f\n",total_cost);//输出订单信息return0;}第八章1.#include〈stdio.h>intmain(void){intch;intcount=0;while((ch=getchar())!=EOF)count++;printf(”Thenumberofcharactersis%d\n",count);return0;}2。#include<stdio。h>#defineSPACE32#defineCTRL64#defineCOL10 ﻩﻩ//每行打印10对intmain(void){charch;intcount=0;while((ch=getchar())!=EOF){count++;//打印字符输出if(ch〉=SPACE)printf("%c",ch);//换行符和制表符的输出elseif(ch=='\n'||ch==’\t')printf("%s",ch=='\n'?”\\n”:"\\t”);//一般控制字符的输出elseprintf("^%c",ch+CTRL);printf("%-5d",ch);if(ch=='\n')count=0;if(count%COL==0)ﻩ//10对换行printf("\n");}return0;}3.#include<stdio.h>#include<ctype.h>intmain(void){intlower=0,upper=0;charch;printf("Inputsometexts:\n”);while((ch=getchar())!=EOF){if(islower(ch))lower++;if(isupper(ch))upper++;}printf("lower:%d,upper:%d\n",lower,upper);return0;}4.#include<stdio.h>#include<ctype.h〉intmain(void){charch;intinword=0;ﻩ ﻩ//在单词中为1否则为0intwords=0; ﻩ //单词数intchars=0;ﻩ //字符数printf("Pleaseentersomewords:\n”);while((ch=getchar())!=EOF){if(!isspace(ch)&&!ispunct(ch))chars++;//开始一个新的单词if(!isspace(ch)&&!ispunct(ch)&&!in

温馨提示

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

评论

0/150

提交评论