版权说明:本文档由用户提供并上传,收益归属内容提供方,若内容存在侵权,请进行举报或认领
文档简介
第二章:开始学习C++//ex2.1--displayyournameandaddressintmain(void){usingnamespacestd;cout<<"MynameisliaochunguangandIliveinhunanchenzhou.\n”;}//ex2.2--convertthefurlongunitstoyarduints-把浪单位换位码单位#include<iostream>doublefur2yd(double);intmain(){usingnamespacestd;cout<<"enterthedistancemeasuredbyfurlongunits:";doublefur;cin>>fur;cout<<"convertthefurlongtoyard"<<endl;doubleyd;yd=fur2yd(fur);cout<<fur<<"furlongis"<<yd<<"yard"<<endl;return0;}doublefur2yd(doublet){return220*t;}//ex2.3-每个函数都被调用两次voidmice();voidsee();usingnamespacestd;intmain(){mice();mice();see();see();return0;}voidmice(){cout<<"threeblindmice"<<endl;}voidsee(){cout<<"seehowtheyrun"<<endl;}//ex2.4intmain(){usingnamespacestd;cout<<"Enteryourage:";intage;cin>>age;intmonth;month=age*12;cout<<age<<"yearsis"<<month<<"months"<<endl;return0;}//ex2.5---converttheCelsiusvalvetoFahrenheitvalue#include<iostream>doubleC2F(double);intmain(){usingnamespacestd;cout<<"pleaseenteraCelsiusvalue:";doubleC;cin>>C;doubleF;cout<<C<<"degreesCelsiusis"<<F<<"degreesFahrenheit."<<endl;return0;}doubleC2F(doublet){return1.8*t+32;}//ex2.6---convertthelightyearsvalvetoastronomicalunits--把光年转换为天文单位#include<iostream>doubleconvert(double);//函数原型intmain(){usingnamespacestd;cout<<"Enterthenumberoflightyears:";doublelight_years;cin>>light_years;doubleastro_units;astro_units=convert(light_years);cout<<light_years<<"light_years="<<astro_units<<"astronomicalunits."<<endl;return0;}t){return63240*t;//1光年=63240天文单位}//ex2.7--显示用户输入的小时数和分钟数voidshow();main(){usingnamespacestd;show();return0;}voidshow(){usingnamespacestd;inth,m;cout<<"enterthenumberofhours:";cin>>h;cout<<"enterthenumberofminutes:";cin>>m;cout<<"Time:"<<h<<":"<<m<<endl;}第三章:处理数据//ex3.1—将身高用英尺(feet)和英寸(inch)表示#include<iostream>constintinch_per_feet=12;// const常量--1feet=12inches--1英尺=12英寸intmain(){usingnamespacestd;\bintht_inch;intrm_inch=ht_inch%inch_per_feet;//取余cout<<"yourheightis"<<ht_feet<<"feet,and"<<rm_inch<<"inches\n";return0;}//ex3.2--计算相应的bodymassindex(体重指数)#include<iostream>constintinch_per_feet=12;constdoublemeter_per_inch=0.0254;constdoublepound_per_kilogram=2.2;intmain(){usingnamespacestd;cout<<"Pleaseenteryourheight:"<<endl;cout<<"First,enteryourheightoffeetpart(输入你身高的英尺部分):_\b";intht_feet;cin>>ht_feet;cout<<"Second,enteryourheightofinchpart(输入你身高的英寸部分):_\b";intht_inch;cin>>ht_inch;cout<<"Now,pleaseenteryourweightinpound: \b\b\b";doublewt_pound;intinch;doubleht_meter;ht_meter=inch*meter_per_inch;doublewt_kilogram;cout<<endl;cout<<"Yourpensonalbodyinformationasfollows:"<<endl;cout<<"身高:"<<inch<<"(英尺inch)\n"<<"身高:"<<ht_meter<<"(米meter)\n"<<"体重:"<<wt_kilogram<<"(千克kilogram)\n";doubleBMI;BMI=wt_kilogram/(ht_meter*ht_meter);cout<<"yourBodyMassIndex(体重指数)is"<<BMI<<endl;return0;}//ex3.3以度,分,秒输入,以度输出#include<iostream>constintminutes_per_degree=60;constintseconds_per_minute=60;intmain(){usingnamespacestd;cout<<"Enteralatitudeindegrees,minutes,andseconds:\n";cout<<"First,enterthedegrees:";intdegree;cout<<"Next,entertheminutesofarc:";intminute;cin>>minute;cout<<"Fianlly,enterthesecondsofarc:";intsecond;cin>>second;doubleshow_in_degree;nutes_per_degree/seconds_per_minute;="<<show_in_degree<<"degrees\n";return0;}//ex3.4#include<iostream>constinthours_per_day=24;constintminutes_per_hour=60;constintseconds_per_minute=60;intmain(){usingnamespacestd;cout<<"Enterthenumberofseconds:";longseconds;cin>>seconds;intDay=seconds/seconds_per_minute/minutes_per_hour/hours_per_day;Minute=seconds/seconds_per_minute%minutes_per_hour;Second=seconds%seconds_per_minute;= minutes,"<<Second<<"seconds\n";return0;}//ex3.5intmain(){usingnamespacestd;cout<<"Entertheworldpopulation:";longlongworld_population;cin>>world_population;cout<<"EnterthepopulationoftheUS:";longlongUS_population;cin>>US_population;doublepercentage;cout<<"ThepopulationoftheUSis"<<percentage<<"%oftheworldpopulation.\n";return0;}//ex3.6汽车耗油量-美国(mpg)or欧洲风格(L/100Km)intmain(){usingnamespacestd;cout<<"Enterthemilesofdistanceyouhavedriven:";doublem_distance;cin>>m_distance;cout<<"Enterthegallonsofgasolineyouhaveused:";doublem_gasoline;cin>>m_gasoline;carcanrun"<<m_distance/m_gasoline<<"milespergallon\n";cout<<"ComputingbyEuropeanstyle:\n";cout<<"Enterthedistanceinkilometers:";doublek_distance;cin>>k_distance;cout<<"Enterthepetrolinliters:";doublek_gasoline;cin>>k_gasoline;cout<<"InEuropeanstyle:"<<"yourcanused"<<100*k_gasoline/k_distance<<"litersofpetrolper100kilometers\n";return0;}intmain(){usingnamespacestd;cout<<"Entertheautomobilegasolineconsumptionfigurein\n"<<"Europeanstyle(litersper100kilometers):";doubleEuro_style;cin>>Euro_style;cout<<"ConvertstoU.S.style(milespergallon):"<<endl;cout<<Euro_style<<"L/100Km="<<62.14*3.875/Euro_style<<"mpg\n";return0;}//Notethat100kilometersis62.14miles,and1gallonis3.875liters.//Thus,19mpgisabout12.4L/100Km,and27mpgisabout8.7L/100Km.EntertheautomobilegasolineconsumptionfigureinEuropeanstyle(litersper100kilometers):12.4ConvertstoU.S.style(milespergallon):12.4L/100Km=19.4187mpgPressanytocontinue//intmain(){usingnamespacestd;cout<<"Entertheautomobilegasolineconsumptionfigurein\n"<<"U.S.style(milespergallon):";doubleUS_style;cin>>US_style;cout<<"ConvertstoEuropeanstyle(milespergallon):"<<endl;cout<<US_style<<"mpg="<<62.14*3.875/US_style<<"L/100Km\n";return0;}//EntertheautomobilegasolineconsumptionfigureinU.S.style(milespergallon):19ConvertstoEuropeanstyle(milespergallon):19mpg=12.6733L/100KmPressanytocontinue第四章复合类型ofstudent#include<iostream>constintAsize=20;usingnamespacestd;structstudent//定义结构描述{charfirstname[Asize];charlastname[Asize];chargrade;intage;};voiddisplay(student);//函数原型放在结构描述后intmain(){cout<<"whatisyourfirstname?"<<endl;studentlcg;//创建结构变量(结构数据对象)cin.getline(lcg.firstname,Asize);cout<<"whatisyourlastname?"<<endl;cin.getline(lcg.lastname,Asize);cout<<"whatlettergradedoyoudeserve?"<<endl;cin>>lcg.grade;cout<<"whatisyourage?"<<endl;cin>>lcg.age;return0;}voiddisplay(studentname){cout<<"Grade:"<<char(name.grade+1)<<endl;cout<<"Age:"<<name.age<<endl;}//ex4.2usethestring-classinsteadofchar-array#include<iostream>intmain(){usingnamespacestd;stringname,dessert;cout<<"Enteryourname:\n";getline(cin,name);cout<<"Enteryourfavoritedessert:\n";getline(cin,dessert);cout<<"Ihavesomedelicious"<<dessert;cout<<"foryou,"<<name<<".\n";return0;}//有时候会遇到需要按下两次回车键才能正确的显示结果,这是vc++6.0的一个BUG,更改如下:elseif(_Tr::eq((_E)_C,_D)){_Chg=true;break;}ex4.3输入其名和姓,并组合显示#include<iostream>#include<cstring>constintAsize=20;intmain(){usingnamespacestd;charfname[Asize];charlname[Asize];charfullname[2*Asize+1];cout<<"Enteryourfirstname:";//输入名字,存储在fname[]数组中cin.getline(fname,Asize);cout<<"Enteryourlastname:";//输入姓,存储在lname[]数组中cin.getline(lname,Asize);strncpy(fullname,lname,Asize);//把姓lname复制到fullname空数组中strcat(fullname,",");//把“,”附加到上述fullname尾部strncat(fullname,fname,Asize);//把fname名字附加到上述fullname尾部fullname[2*Asize]='\0';//为防止字符型数组溢出,在数组结尾添加结束符cout<<"Here'stheinformationinasinglestring:"<<fullname<<endl;//显示组合结果return0;}#define_CRT_SECURE_NO_WARNINGS#include<iostream>#include<cstring>constintAsize=20;intmain(){usingnamespacestd;charfirstname[Asize];charlastname[50];cout<<"Enteryourfirstname:";cin.getline(firstname,Asize);cout<<"Enteryourlastname:cin.getline(lastname,50);strcat(lastname,",");cout<<"Here'stheinformationinasinglestring:"<<lastname<<endl;return0;}//ex4.4使用string对象存储、显示组合结果#include<iostream>intmain(){usingnamespacestd;stringfname,lname,attach,fullname;cout<<"Enteryourfirstname:";getline(cin,fname);//note:将一行输入读取到string类对象中使用的是getline(cin,str)cout<<"Enteryourlastname:";attach=",";fullname=lname+attach+fname;cout<<"Here'stheinformationinasinglestring:"<<fullname<<endl;return0;}//ex4.5declareastructandinitializeit声明结果并创建一个变量constintAsize=20;structCandyBar{charbrand[Asize];doubleweight;intcalory;};intmain(){usingnamespacestd;CandyBarsnack={"MochaMunch",2.3,350};cout<<"Here'stheinformationofsnack:\n";cout<<"brand:"<<snack.brand<<endl;cout<<"weight:"<<snack.weight<<endl;cout<<"calory:"<<snack.calory<<endl;return0;}//ex4.6结构数组的声明及初始化constintAsize=20;structCandyBar{charbrand[Asize];doubleweight;intcalory;};intmain(){usingnamespacestd;CandyBarsnack[3]={{"MochaMunch",2.3,350},{"XuFuJi",1.1,300},{"Alps",0.4,100}};for(inti=0;i<3;i++)//利用for循环来显示snack变量的内容{cout<<snack[i].brand<<endl<<snack[i].weight<<endl<<snack[i].calory<<endl<<endl;}return0;}//ex4.7pizza披萨饼#include<iostream>#include<string>constintSize=20;structpizza//声明结构{charcompany[Size];doublediameter;doubleweight;};intmain(){usingnamespacestd;pizzapie;//创建一个名为pie的结构变量cout<<"What'sthe nameofpizzacompany:";cin.getline(pany,Size);cout<<"What'sthediameterofpizza:";cin>>pie.diameter;thecin>>pie.weight;cout<<"company:"<<pany<<endl;cout<<"weight:"<<pie.weight<<"ounches"<<endl;return0;}//ex4.8pizzapie披萨饼使用new创建动态结构#include<iostream>#include<string>constintSize=20;structpizza//声明结构{charcompany[Size];doublediameter;doubleweight;};intmain(){usingnamespacestd;pizza*pie=newpizza;//使用new创建动态结构cout<<"What'sthediameterofpizza:";cin>>pie->diameter;cin.get();//读取下一个字符cout<<"What'sthe nameofpizzacompany:";cin.get(pie->company,Size);thecin>>pie->weight;cout<<"company:"<<pie->company<<endl;cout<<"weight:"<<pie->weight<<"ounches"<<endl;deletepie;//delete释放内存return0;}//ex.4.9使用new动态分配数组—方法1#include<iostream>#include<string>usingnamespacestd;structCandyBar{stringbrand;intcalory;};intmain(){CandyBar*snack=newCandyBar[3];snack[0].brand="A";//单个初始化由new动态分配的内存snack[0].weight=1.1;snack[1].brand="B";snack[2].brand="C";for(inti=0;i<3;i++){cout<<"brand:"<<snack[i].brand<<endl;cout<<"weight:"<<snack[i].weight<<endl;cout<<"calorie:"<<snack[i].calory<<endl<<endl;}delete[]snack;return0;}//ex.4.10数组—方法1#include<iostream>intmain(){usingnamespacestd;constintSize=3;intsuccess[Size];cout<<"Enteryoursuccessofthethreetimes40metersrunning:\n";cin>>success[0]>>success[1]>>success[2];cout<<"success1:"<<success[0]<<endl;cout<<"success2:"<<success[1]<<endl;cout<<"success3:"<<success[2]<<endl;doubleaverage=(success[0]+success[1]+success[2])/3;cout<<"average:"<<average<<endl;return0;}//ex.4.10array—方法2#include<iostream>#include<array>intmain(){usingnamespacestd;cout<<"Enteryoursuccessofthethreetimes40metersrunning:\n";cin>>ad[0]>>ad[1]>>ad[2];ad[3]=(ad[0]+ad[1]+ad[2])/3;cout<<"average:"<<ad[3]<<endl;return0;}#include<iostream>intmain(){
第五章循环和关系表达式usingnamespacestd;cout<<"Pleaseentertwointegers:";intnum1,num2;intsum=0;for(inttemp=num1;temp<=num2;++temp)//ortemp++sum+=temp;cout<<"Thesumfrom"<<num1<<"to"<<num2<<"is"<<sum<<endl;return0;}//ex.5.2#include<iostream>#include<array>intmain(){usingnamespacestd;array<longdouble,101>ad={0};ad[1]=ad[0]=1L;for(inti=2;i<101;i++)ad[i]=i*ad[i-1];for(inti=0;i<101;i++)cout<<i<<"!="<<ad[i]<<endl;return0;}#include<iostream>#include<array>usingnamespacestd;intmain(){array<longdouble,101>multiply;multiply[0]=multiply[1]=1LL;for(inti=2;i<=100;i++)multiply[i]=multiply[i-1]*i;cout<<multiply[100];return0;}//ex.5.3#include<iostream>intmain(){usingnamespacestd;cout<<"Pleaseenteraninteger:";intsum=0,num;while((cin>>num)&&num!=0){sum+=num;cout<<"Sofar,thesumis"<<sum<<endl;cout<<"Pleaseenteraninteger:";}return0;}//ex.5.4#include<iostream>intmain(){usingnamespacestd;doublesum1,sum2;sum1=sum2=0.0;intyear=0;{++year;sum2=(100+sum2)*0.05+sum2;}cout<<"经过"<<year<<"年后,Cleo的投资价值才能超过Daphne的投资价值。"<<endl;cout<<"此时,Cleo的投资价值为"<<sum1<<",而Daphne的投资价值为"<<sum2<<endl;return0;}#include<iostream>usingnamespacestd;intmain(){doubleDaphne=100.0;doubleCleo=100.0;intyear=0;while(Cleo<=Daphne){Daphne+=10;Cleo*=1.05;year++;}cout<<year<<endl;return0;}#include<iostream>constintMONTHS=12;const char*ember","October","November","December"};intmain(){usingnamespacestd;intsales[MONTHS],sum=0;for(inti=0;i<MONTHS;i++){cout<<"请输入在"<<months[i]<<"的C++ForFools的销售量:";cin>>sales[i];sum+=sales[i];}cout<<"这一年中的C++ForFools的总销售量为:"<<sum<<endl;return0;}//ex.5.6#include<iostream>constintMONTHS=12;const char*ember","October","November","December"};constchar*years[3]={"第一年","第二年","第三年"};intmain(){usingnamespacestd;intyear_sale[3],sum=0,sales[3][MONTHS];for(inti=0;i<3;i++){inttemp=0;for(intj=0;j<MONTHS;j++){cin>>sales[i][j];temp+=sales[i][j];}}for(inti=0;i<3;i++)cout<<"这三年的总销售量为:"<<sum<<endl;return0;}#include<iostream>usingnamespacestd;constintYears=3,Months=12;constchar*months[Months]={"January","February","March","April","May","June",intmain(){intsale[Years][Months]={0};for(inti=0;i<Years;i++){intsum=0;for(intj=0;j<Months;j++){cout<<"Enterthesalesmentof"<<months[j]<<":";cin>>sale[i][j];sum+=sale[i][j];}cout<<"Salesmentforthisyear:"<<sum<<endl<<endl;}return0;}//ex.5.7#include<iostream>#include<string>usingnamespacestd;structcar{stringname;intyear;};intmain(){cout<<"Howmanycarsdoyouwishtocatalog?";intnum;(cin>>num).get();car*ps=newcar[num];for(inti=0;i<num;++i){cout<<"Car#"<<i+1<<":\n";cout<<"Pleaseenterthemake:";getline(cin,ps[i].name);cout<<"Pleaseentertheyearmade:";(cin>>ps[i].year).get();}cout<<"Hereisyourcollection:\n";for(inti=0;i<num;++i)cout<<ps[i].year<<""<<ps[i].name<<endl;delete[]ps;return0;}#include<iostream>#include<string>usingnamespacestd;structcar{stringmaker;intyear;};intmain(){intnumber;cout<<"Howmanycarsdoyouwishtocatalog?cin>>number;car*a=newcar[number];for(inti=0;i<number;i++){cout<<"Car#"<<i+1<<":"<<endl;cout<<"Pleaseenterthemaker:cin.get();getline(cin,a[i].maker);cout<<"Pleaseentertheyearmade:cin>>a[i].year;}cout<<"Hereisyourcollection:"<<endl;for(inti=0;i<number;i++)cout<<a[i].year<<""<<a[i].maker<<endl;delete[]a;return0;}#include<iostream>usingnamespacestd;structcar{charmaker[20];intyear;};intmain(){intnumber;cout<<"Howmanycarsdoyouwishtocatalog?cin>>number;car*a=newcar[number];for(inti=0;i<number;i++){cout<<"Car#"<<i+1<<":"<<endl;cout<<"Pleaseenterthemaker:cin.get();cin.getline(a[i].maker,20);cout<<"Pleaseentertheyearmade:cin>>a[i].year;}cout<<"Hereisyourcollection:"<<endl;for(inti=0;i<number;i++)cout<<a[i].year<<""<<a[i].maker<<endl;delete[]a;return0;}//ex.5.8#include<iostream>#include<cstring>intmain(){usingnamespacestd;charword[20];intsum=0;cout<<"Enterwords(tostop,typetheworddone):\n";cin>>word;while(strcmp(word,"done")){sum++;}cout<<"Youenteredatotalof"<<sum<<"words.\n";return0;}//ex.5.9#include<iostream>#include<string>intmain(){usingnamespacestd;stringword;intsum=0;cout<<"Enterwords(tostop,typetheworddone):\n";cin>>word;while(word!="done"){sum++;}cout<<"Youenteredatotalof"<<sum<<"words.\n";return0;}和ex.5.8的区别是:word!="done",因为当word=done一样时,返回值为1,不一样时才是返回0.//ex.5.10#include<iostream>intmain(){usingnamespacestd;cout<<"Enternumberofrows:";intnum;cin>>num;for(inti=0;i<num;i++){for(intj=num-i;j>1;j--)cout<<".";for(intk=0;k<=i;++k)cout<<"*";cout<<endl;}return0;}第六章分支语句和逻辑运算符#include<iostream>#include<cctype>intmain(){usingnamespacestd;charch;while(ch!='@'){cin.get(ch);else{}}
elsecout<<ch;cin.get(ch);return0;}#include<iostream>#include<cctype>usingnamespacestd;intmain(){charch;cout<<"Pleaseenter:\n";while(cin.get(ch)&&ch!='@'){if(islower(ch)){ch=toupper(ch);cout<<ch;}elseif(isupper(ch)){ch=tolower(ch);cout<<ch;}elsecout<<ch;}return0;}//ex.6.2--数组#include<iostream>intmain(){usingnamespacestd;doublesum=0,average=0;doublenum[10];inti=0,total=0;doubletemp;while(cin>>temp&&i<10&&!isdigit(temp)){++i;}if(i!=0)average=sum/i;for(intj=0;j<i;++j)if(num[j]>average)++total;cout<<"这些数字的平均值为"<<average<<endl;return0;}#include<iostream>usingnamespacestd;constintNum=10;intmain(){doubledonation[Num];inti=0;intcount=0;doublesum=0.0;cout<<"Pleaseenter:\n";while(i<Num&&cin>>donation[i]){sum+=donation[i++];}if(i==0)cout<<"Nodata--bye\n";else{doubleaverage=sum/i;for(intj=0;j<i;j++){if(donation[j]>average)++count;}cout<<"Theaverage="<<average<<endl<<"Thenumbersbiggerthantheaverage:"<<count<<endl;}return0;}//ex.6.2--array#include<iostream>#include<cctype>intmain(){usingnamespacestd;doublesum=0,average=0;array<double,10>ad={0};inti=0,total=0;doubletemp;{++i;}if(i!=0)average=sum/i;for(intj=0;j<i;++j)if(ad[j]>average)++total;cout<<"这些数字的平均值为"<<average<<endl;return0;}#include<iostream>intmain(){usingnamespacestd;cout<<"Pleaseenteroneofthefollowingchoices:\n"<<"c)carnivorep)pianist\n"<<"t)treeg)game\nf\n";//书上的这个f个人认为是打印错误cout<<"Pleaseenterac,p,t,org:";charch;while(ch!='c'&&ch!='p'&&ch!='t'&&ch!='g'){cout<<"Pleaseenterac,p,t,org:";cin>>ch;}switch(ch){case'c':cout<<"Amapleisacarnivore.\n";break;case'p':cout<<"Amapleisapianist.\n";break;case't':cout<<"Amapleisatree.\n";break;case'g':}return0;}
cout<<"Amapleisagame.\n";#include<iostream>usingnamespacestd;voidshow();intmain(){show();charchoice;while(cin>>choice){switch(choice){case'c':cout<<"It'sacarnivore.\n";break;case'p':cout<<"It'sapianist.\n";break;case't':cout<<"Amapleisatree.\n";break;case'g':cout<<"It'sagame.\n";break;default:cout<<"Pleaseenterac,p,t,org:";}}return0;}voidshow(){cout<<"Pleaseenteroneofthefollowingchoices:\n""c)carnivore p)pianist\n""t)tree g)game\n";}//ex.6.4#include<iostream>constintstrsize=20;structbop{
charfullname[strsize];chartitle[strsize];};intmain(){
charbopname[strsize];intpreference;usingnamespacestd;cout<<"BenevolentOrderofProgrammersReport\n"<<"a.displaybyname b.displaybytitle\n"<<"c.displaybybopname d.diplaybypreference\n"<<"q.quit\n";charch;bopmember[5]={{"WimpMacho","EnglishTeacher","DEMON",0},{"RakiRhodes","JuniorProgrammer","BOOM",1},{"CeliaLaiter","SuperStar","MIPS",2},{"HoppyHipman","AnalystTrainee","WATEE",1},{"PatHand","Police","LOOPY",2}};cout<<"Enteryourchoice:";while(cin>>ch&&ch!='q'){switch(ch){case'a':for(inti=0;i<5;i++)break;case'b':for(inti=0;i<5;i++)break;case'c':for(inti=0;i<5;i++)break;case'd':for(inti=0;i<5;i++){if(member[i].preference==0)cout<<member[i].fullname<<endl;elseif(member[i].preference==1)cout<<member[i].title<<endl;elseif(member[i].preference==2)cout<<member[i].bopname<<endl;}break;}cout<<"Nextchoice:";}return0;}#include<iostream>usingnamespacestd;constintstrsize=30;structbop{charfullname[strsize];chartitle[strsize];charbopname[strsize];intpreference;};voidshow();intmain(){bopA[5]={{"WimpMacho","Teacher","HAHA",0},{"RakiRhodes","JuniorProgrammer","LIAR",1},{"Celia","engineer","MIPS",2},{"HoppyHipman","AnalystTrainee","WAHU",1},{"PatHand","LOOPY",2}};cout<<"BenevolentOrderofProgrammersReport\n";show();cout<<"Enteryourchoice:";charchoice;cin>>choice;while(choice!='q'){switch(choice){case'a':cout<<A[0].fullname<<endl<<A[1].fullname<<endl<<A[2].fullname<<endl<<A[3].fullname<<endl<<A[4].fullname<<endl;break;case'b':cout<<A[0].title<<endl<<A[1].title<<endl<<A[2].title<<endl<<A[3].title<<endl<<A[4].title<<endl;break;case'c':cout<<A[0].bopname<<endl<<A[1].bopname<<endl<<A[2].bopname<<endl<<A[3].bopname<<endl<<A[4].bopname<<endl;break;case'd':cout<<A[0].fullname<<endl<<A[1].title<<endl<<A[2].bopname<<endl<<A[3].title<<endl<<A[4].bopname<<endl;break;default:cout<<"That'snottheproperchoice.\n";}cout<<"Nextchoice:";cin>>choice;}cout<<"Bye!\n";return0;}voidshow(){cout<<"a.displaybyname b.displaybytitle\n"<<"c.displaybybopname d.displaybypreference\n"<<"q.quit\n";}//ex.6.5#include<iostream>intmain(){usingnamespacestd;doubleincome,revenue;cout<<"请输入你的收入:";{revenue=0.0;elseif(income<=15000)elseif(income<=35000)elserevenue=0.1*(15000-5000)+0.15*(35000-15000)+0.2*(income-35000);cout<<"你的所得税为"<<revenue<<endl;cout<<"请输入你的收入:";}return0;}#include<iostream>usingnamespacestd;intmain(){doubleincome,tax;cout<<"Pleaseenteryourincome:while(cin>>income&&income>=0){if(income<=5000)tax=0;elseif(income<=15000)tax=0.1*(income-5000);elseif(income<=35000)tax=10000*0.1+0.15*(income-15000);elsetax=10000*0.1+0.15*20000+0.2*(income-35000);cout<<"Yourtaxis:"<<tax<<endl;cout<<"Pleaseenteryourincome:";}cout<<"Bye!\n";return0;}//ex.6.6#include<iostream>#include<string>usingnamespacestd;structpatron{stringname;doublemoney;};intmain(){intnum,temp=0;cin>>num;cin.get();patron*ps=newpatron[num];for(inti=0;i<num;++i){cout<<"请输入第"<<i+1<<"位捐款人的名字:";getline(cin,ps[i].name);cin>>ps[i].money;cin.get();}cout<<"GrandPatrons:\n";for(inti=0;i<num;++i)if(ps[i].money>10000){cout<<ps[i].name<<"\n"<<ps[i].money<<endl;++temp;}if(temp==0)cout<<"none\n";cout<<"Patrons:\n";for(inti=0;i<num;++i){cout<<ps[i].name<<"\n"<<ps[i].money<<endl;++temp;}if(temp==0)delete[]ps;return0;}#include<iostream>#include<string>usingnamespacestd;structcharity{stringname;doublemoney;};intmain(){intnumber;intcount=0;cout<<"Pleaseenterthenumberofdonator:";cin>>number;charity*pt=newcharity[number];for(inti=0;i<number;i++){cout<<"Pleaseenteryourname:cin.get();getline(cin,pt[i].name);cout<<"Pleaseenterthemoneyyouaregoingtodonate:";cin>>pt[i].money;if(pt[i].money>10000)count++;}if(count==0)cout<<"None(money>10000)";else{cout<<"GrandPatron\n";for(inti=0;i<number;i++){if(pt[i].money>10000)cout<<pt[i].name<<""<<pt[i].money<<endl;}}cout<<endl;if(10-count==0)cout<<"None(money<10000)";else{cout<<"Patron\n";for(inti=0;i<number;i++){if(pt[i].money<10000)cout<<pt[i].name<<""<<pt[i].money<<endl;}}return0;}//ex.6.7#include<iostream>#include<cctype>intmain(){usingnamespacestd;intvowel=0,consonant=0,other=0;charword[15];cout<<"Enterwords(qtoquit):\n";while(cin>>word){if(isalpha(word[0])){break;elseif(word[0]=='a'||word[0]=='i'||else++consonant;}else++other;}cout<<vowel<<"wordsbeginningwithvowels\n";cout<<consonant<<"wordsbeginningwithconsonants\n";cout<<other<<"others\n";return0;}#include<iostream>#include<cctype>usingnamespacestd;intmain(){charword[20];intvow=0,consonant=0,other=0;cout<<"Enterwords(qtowhile(cin>>word){if(isalpha(word[0])){if(word[0]=='a'||word[0]=='e'||word[0]=='i'||word[0]=='o'||word[0]=='u'||word[0]=='A'||word[0]=='E'||word[0]=='I'||word[0]=='O'||word[0]=='U')vow++;elseif(word[0]=='q'&&strlen(word)==1)break;elseconsonant++;}elseother++;}cout<<vow<<"wordsbeginningwithvowels\n"<<consonant<<"wordsbeginningwithconsonants\n"<<other<<"others\n";return0;}//ex.6.8#include<iostream>#include<fstream>#include<cstdlib>intmain(){usingnamespacestd;charch;intsum=0;ifstreaminFile;if(!inFile.is_open()){cout<<"Couldnotopenthefile\n";cout<<"Programterminating.\n";exit(EXIT_FAILURE);}inFile>>ch;{++sum;}if(inFile.eof())cout<<"Endoffilereached.\n";elseif(inFile.fail())cout<<"Inputterminatedbydatamismatch.\n";elsecout<<"Inputterminatedforunkonwnreason.\n";return0;}#include<iostream>#include<fstream>#include<cstdlib>usingnamespacestd;constintSize=20;intmain(){charfilename[Size];ifstreaminfile;cout<<"Enternameofdatafile:";cin.getline(filename,Size);infile.open(filename);if(!infile.is_open()){cout<<"Couldnotopenthefile"<<filename<<endl;cout<<"Programterminating.\n";exit(EXIT_FAILURE);}chara;intcount=0;infile>>a;while(infile.good()){++count;infile>>a;}if(infile.eof())cout<<"Endoffilereached.\n";elseif(infile.fail())cout<<"Inputterminatedbydatamismatch.\n";elsecout<<"Inputterminatedforunknownreason.\n";if(count==0)cout<<"Nodataelsecout<<"Thetextcontains"<<count<<"character(s)"<<endl;return0;}//ex.6.9#include<iostream>#include<fstream>#include<cstdlib>structmember{charname[20];doubledonation;};intmain(){usingnamespacestd;intnum,count1=0,count2=0;ifstreamfin;charfile[20];cout<<"Enternameofdatafile:";cin.getline(file,20);fin.open(file);if(!fin.is_open()){cout<<"Couldnotopenthefile-"<<file<<endl;cout<<"Programterminating.\n";exit(EXIT_FAILURE);}fin>>num;member*pd=newmember[num];for(inti=0;i<num;i++){fin>>pd[i].donation;fin.get();}cout<<"GrandPatrons:\n";for(inti=0;i<num;i++){count1++;}if(count1==0)cout<<"none\n";cout<<"Patrons:\n";for(inti=0;i<num;i++){count2++;}if(count2==0)delete[]pd;return0;}#include<iostream>#include<fstream>#include<cstdlib>#include<string>usingnamespacestd;structcharity{stringname;doublemoney;};intmain(){stringfilename;ifstreaminfile;cout<<"Enternameofdatafile:";getline(cin,filename);infile.open(filename);if(!infile.is_open()){cout<<"Couldnotopenthefile"<<filename<<endl;cout<<"Programterminating.\n";exit(EXIT_FAILURE);}intnumber,count=0;infile>>number;charity*pt=newcharity[number];for(inti=0;i<number;i++){infile.get();getline(infile,pt[i].name);infile>>pt[i].money;if(pt[i].money>10000)count++;}if(count==0)cout<<"None(money>10000)";else{cout<<"GrandPatron:\n";for(inti=0;i<number;i++){if(pt[i].money>10000)cout<<pt[i].name<<""<<pt[i].money<<endl;}}if(10-count==0)cout<<"None(money<10000)";else{cout<<"Patron:\n";for(inti=0;i<number;i++){if(pt[i].money<10000)cout<<pt[i].name<<""<<pt[i].money<<endl;}}delete[]pt;return0;}第7章函数——C++的编程模块//ex7.1#include<iostream>doublet_av(doublex,doubley);intmain(){usingnamespacestd;doublex,y;doubleresult;cout<<"Pleaseentertwonumbers(0tostop):";while((cin>>x>>y)&&x!=0&&y!=0){result=t_av(x,y);cout<<"调和平均数="<<result<<endl;cout<<"Pleaseentertwonumbers(0tostop):";}return0;}doublet_av(doublex,doubley){return2.0*x*y/(x+y);}#include<iostream>usingnamespacestd;voiddouble);intmain(){doubleA,B;cout<<"Pleaseentertwonumbers:while(cin>>A>>B){if(A==0||B==0)elseaverage(A,B);cout<<"Pleaseentertwonumbers:";}cout<<"Bye!\n";return0;}voidaverage(doublex,doubley){cout<<"Theaverageis:"<<2.0*x*y/+y)<<endl;}//ex7.2#include<iostream>constintMAX=10;usingnamespacestd;intfill_ar(doublear[],intlimit);voidshow_ar(constdoublear[],intn);doubleaverage(constdoublear[],intn);intmain(){doublescores[MAX];intsize=fill_ar(scores,MAX);show_ar(scores,size);if(size>0)cout<<"Theaverageofscoresis:"<<average(scores,size)<<endl;return0;}intfill_ar(doublear[],intlimit){doubletemp;inti;for(i=0;i<limit;i++){cout<<"Ent
温馨提示
- 1. 本站所有资源如无特殊说明,都需要本地电脑安装OFFICE2007和PDF阅读器。图纸软件为CAD,CAXA,PROE,UG,SolidWorks等.压缩文件请下载最新的WinRAR软件解压。
- 2. 本站的文档不包含任何第三方提供的附件图纸等,如果需要附件,请联系上传者。文件的所有权益归上传用户所有。
- 3. 本站RAR压缩包中若带图纸,网页内容里面会有图纸预览,若没有图纸预览就没有图纸。
- 4. 未经权益所有人同意不得将文件中的内容挪作商业或盈利用途。
- 5. 人人文库网仅提供信息存储空间,仅对用户上传内容的表现方式做保护处理,对用户上传分享的文档内容本身不做任何修改或编辑,并不能对任何下载内容负责。
- 6. 下载文件中如有侵权或不适当内容,请与我们联系,我们立即纠正。
- 7. 本站不保证下载资源的准确性、安全性和完整性, 同时也不承担用户因使用这些下载资源对自己和他人造成任何形式的伤害或损失。
最新文档
- 代工oem包装合同范例
- 内衣合作合同模板
- 吊篮工程合同范例
- 2024年师傅带徒弟合同协议
- 厨师租聘合同范例
- 制作app合同范例
- 2024年工程咨询居间合同 with 咨询效果及佣金结算
- 部编版语文七年级上册 5 秋天的怀念 教案
- 国有钢厂转让合同范例
- 仓库纸箱配送合同范例
- 提升员工营销能力的企业教育培训
- 学院(部)国际交流与合作工作考核指标体系与评分标准
- 国际贸易法与跨境业务合规的风险管理与应对策略
- 大学生社团对大学生的影响的社会调查报告
- 麻醉科临床诊疗指南2020版
- 供应商QSA-QPA评鉴表
- 餐券模板完整
- 英语48个国际音标课件(单词带声、附有声国际音标图)
- (完整文本版)货物验收单
- 广东省深圳市2023一2024学年三年级上学期科学期中核心素养提升试卷
- 分子的空间结构(第二课时)杂化轨道理论教学设计高二下学期化学人教版(2019)选择性必修2
评论
0/150
提交评论