c++精解和程序设计.doc
文本预览下载声明
********************** 清单2-4 ****************************
// File: coins.cpp
// Determines the values of a coin collection
#include iostream
#include string
using namespace std;
int main()
{
string name; // input: nieces first name
int pennies; // input: count of pennies
int nickels; // input: count of nickels
int dollars; // output: value of coins in dollars
int change; // output: value of coins in cents
int totalCents; // total cents represented
// Read in your nieces first name.
cout Enter your first name: ;
cin name;
// Read in the count of nickels and pennies.
cout Enter the number of nickels: ;
cin nickels;
cout Enter the number of pennies: ;
cin pennies;
// Compute the total value in cents.
totalCents = 5 * nickels + pennies;
// Find the value in dollars and change.
dollars = totalCents / 100;
change = totalCents % 100;
// Display the value in dollars and change
cout Good work name ! endl;
cout You collection is worth dollars dollars and
change cents. endl;
return 0;
}
****************将英里转化为千米的程序的批处理版本*********************
// File: milesBatch.cpp
// Converts distance in miles to kilometers.
#include iostream
using namespace std;
int main()
{
const float KM_PER_MILE = 1.609; // 1.609 km in a mile
float miles, // input: distance in miles
kms; // output: distance in kilometers
// Get the distance in miles.
cin miles;
cout The distance in miles is miles endl;
// Convert the distance to kilometers.
kms = KM_PER_MILE * miles;
// Display the distance in kilometers.
cout The distance in kilometers is kms endl;
return 0;
}
********************string操作说明***********************
// File: stringOperations.cpp
// Illustrates string operations
#include iostream
#include string
using namespace std;
int main()
{
string firstName, lastName; // inputs - first and last names
string wholeName; // inputs - whole name
string greeting = Hello ; // output - a greeting string
// Read
显示全部