//pe2-2.cpp
#include<iostream>
intmain(void)
{
usingnamespacestd;
cout<<"Enteradistanceinfurlongs:";
doublefurlongs;
cin>>furlongs;
doublefeet;
feet=220*furlongs;
cout<<furlongs<<"furlongs="
<<feet<<"feet";
return0;
}
//pe2-3.cpp
#include<iostream>
usingnamespacestd;
voidmice();
voidrun();
intmain()
{
mice();
mice();
run();
run();
return0;
}
voidmice()
{
cout<<"Threeblindmice";
}
voidrun()
{
cout<<"Seehowtheyrun";
}
//pe2-4.cpp
#include<iostream>
doubleC_to_F(double);
intmain()
{
usingnamespacestd;
cout<<"EnteratemperatureinCelsius:";
doubleC;
cin>>C;
doubleF;
F=C_to_F(C);
cout<<C<<"degreesCelsius="
<<F<<"degreesFahrenheit\n";
return0;
}
doubleC_to_F(doubletemp)
{
return1.8*temp+32.0;
}
//pe3-1.cpp
#include<iostream>
constintInch_Per_Foot=12;
intmain(void)
{
usingnamespacestd;
//Note:someenvironmentsdon'tsupportthebackspacecharacter
cout<<"Pleaseenteryourheightininches:___/b/b/b";
intht_inch;
cin>>ht_inch;
intht_feet=ht_inch/Inch_Per_Foot;
intrm_inch=ht_inch%Inch_Per_Foot;
cout<<"Yourheightis"<<ht_feet<<"feet,";
cout<<rm_inch<<"inch(es).\n";
return0;
}
//pe3-3.cpp
#include<iostream>
constdoubleMINS_PER_DEG=60.0;
constdoubleSECS_PER_MIN=60.0;
intmain()
{
usingnamespacestd;
intdegrees;
intminutes;
intseconds;
doublelatitude;
cout<<"Enteralatitudeindegrees,minutes,andseconds:\n";
cout<<"First,enterthedegrees:";
cin>>degrees;
cout<<"Next,entertheminutesofarc:";
cin>>minutes;
cout<<"Finally,enterthesecondsofarc:";
cin>>seconds;
latitude=degrees+(minutes+seconds/SECS_PER_MIN)/MINS_PER_DEG;
cout<<degrees<<"degrees,"<<minutes<<"minutes,"
<<seconds<<"seconds="<<latitude<<"degrees\n";
return0;
}
!
//pe3-5.cpp
#include<iostream>
intmain(void)
{
usingnamespacestd;
cout<<"Howmanymileshaveyoudrivenyourcar?";
floatmiles;
cin>>miles;
cout<<"Howmanygallonsofgasolinedidthecaruse?";
floatgallons;
cin>>gallons;
cout<<"Yourcargot"<<miles/gallons;
cout<<"milespergallon.\n";
return0;
}
//pe3-6.cpp
#include<iostream>
constdoubleKM100_TO_MILES=62.14;
constdoubleLITERS_PER_GALLON=3.875;
intmain(void)
{
usingnamespacestd;
doubleeuro_rating;
doubleus_rating;
cout<<"Enterfuelconsumptioninlitersper100km:";
cin>>euro_rating;
//dividebyLITER_PER_GALLONtogetgallonsper100-km
//dividebyKM100_TO_MILEStogetgallonspermile
//invertresulttogetmilespergallon
us_rating=(LITERS_PER_GALLON*KM100_TO_MILES)/euro_rating;
cout<<euro_rating<<"litersper100kmis";
cout<<us_rating<<"milespergallon.\n";
return0;
}
//pe4-2.cpp--storingstringsinstringobjects
#include<iostream>
#include<string>
intmain()
{
usingnamespacestd;
stringname;
stringdessert;
cout<<"Enteryourname:\n";
getline(cin,name);//readsthroughnewline
cout<<"Enteryourfavoritedessert:\n";
getline(cin,dessert);
cout<<"Ihavesomedelicious"<<dessert;
cout<<"foryou,"<<name<<".\n";
return0;
}
//pe4-3.cpp--storingstringsinchararrays
#include<iostream>
#include<cstring>
constintSIZE=20;
intmain()
{
usingnamespacestd;
charfirstName[SIZE];
charlastName[SIZE];
charfullName[2*SIZE+1];
cout<<"Enteryourfirstname:";
cin>>firstName;
cout<<"Enteryourlastname:";
cin>>lastName;
strncpy(fullName,lastName,SIZE);
strcat(fullName,",");
strncat(fullName,firstName,SIZE);
fullName[SIZE-1]='\0';
cout<<"Here'stheinformationinasinglestring:"
<<fullName<<endl;
return0;
}
//pe4-5.cpp
//acandybarstructure
structCandyBar{
charbrand[40];
doubleweight;
intcalories;
};
#include<iostream>
intmain()
{
usingnamespacestd;//introducesnamespacestd
CandyBarsnack={"MochaMunch",2.3,350};
cout<<"Brandname:"<<snack.brand<<endl;
cout<<"Weight:"<<snack.weight<<endl;
cout<<"Calories:"<<snack.calories<<endl;
return0;
}
//pe4-7.ccp
#include<iostream>
constintSlen=70;
structpizza{
charname[Slen];
floatdiameter;
floatweight;
};
intmain(void)
{
usingnamespacestd;
pizzapie;
cout<<"Whatisthenameofthepizzacompany?";
cin.getline(pie.name,Slen);
cout<<"Whatisthediameterofthepizzaininches?";
cin>>pie.diameter;
cout<<"Howmuchdoesthepizzaweighinounces?";
cin>>pie.weight;
cout<<"Company:"<<pie.name<<"\n";
cout<<"Diameter:"<<pie.diameter<<"inches\n";
cout<<"Weight:"<<pie.weight<<"ounces\n";
return0;
}
//pe5-2.cpp
#include<iostream>
intmain(void)
{
usingnamespacestd;
doublesum=0.0;
doublein;
cout<<"Enteranumber(0toterminate):";
cin>>in;
while(in!=0){
sum+=in;
cout<<"Runningtotal="<<sum<<"\n";
cout<<"Enternextnumber(0toterminate):";
cin>>in;
}
cout<<"Bye!\n";
return0;
}
//pe5-4.cpp
//booksales
#include<iostream>
constintMONTHS=12;
constchar*months[MONTHS]={"January","February","March","April",
"May","June","July","August","September",
"October","November","December"};
intmain()
{
usingnamespacestd;//introducesnamespacestd
intsales[MONTHS];
intmonth;
cout<<"Enterthemonthlysalesfor\"C++forFools\":\n";
for(month=0;month<MONTHS;month++)
{
cout<<"Salesfor"<<months[month]<<":";
cin>>sales[month];
}
- 1
- 2
前往页