程序清单
2.1 myfirst.cpp
// myfirst.cpp--displays a message
#include <iostream>
int main(void)
{
using namespace std;
cout << "Come up and C++ me some time.";
cout << endl;
cout << "You won't regret it!" << endl;
return 0;
}
2.2 carrots.cpp
// carrots.cpp -- food processing program
// uses and displays a variable
#include <iostream>
int main(void)
{
using namespace std;
int carrots;
carrots = 25;
cout << "I have ";
cout << carrots;
cout << " carrots.";
cout << endl;
carrots = carrots - 1;
cout << "Crunch, crunch. Now I have " << carrots << " carrots" << endl;
return 0;
}
2.3 getinfo.cpp
// getinfo.cpp -- input and output
#include <iostream>
int main(void)
{
using namespace std;
int carrots;
cout << "How many carrots do you have?" << endl;
cin >> carrots;
cout << "Here are two more. ";
carrots = carrots + 2;
cout << "Now you have " << carrots << " carrots." << endl;
return 0;
}
2.4 sqrt.cpp
// ourfunc.cpp -- defining your own function
#include <iostream>
void simon(int n);
int main(void)
{
using namespace std;
simon(3);
cout << "Pick an integer: ";
int count;
cin >> count;
simon(count);
cout << "Done!" << endl;
return 0;
}
void simon(int n)
{
using namespace std;
cout << "Simon says touch yout toes " << n << " times." << endl;
}
2.6 convert.cpp
// convert.cpp -- converts stone to pounds
#include <iostream>
int stonetolb(int sts);
int main(void)
{
using namespace std;
int stones;
cout << "Enter the weight in stone: ";
cin >> stones;
int pounds = stonetolb(stones);
cout << stones << " stone = ";
cout << pounds << " pounds." << endl;
return 0;
}
int stonetolb(int sts)
{
return sts * 14;
}
编程练习
第1题
#include <iostream>
int main(void)
{
using namespace std;
cout << "Rocky" << endl;
cout << "I live in China." << endl;
return 0;
}
第2题
#include <iostream>
int main(void)
{
using namespace std;
double distance;
cout << "Enter the distance (in long):";
cin >> distance;
cout << "the distance " << distance << " long" << " equals " << distance * 20 << " yard." << endl;
return 0;
}
第3题
#include <iostream>
using namespace std;
void print_mice(void);
void print_run(void);
int main(void)
{
print_mice();
print_mice();
print_run();
print_run();
return 0;
}
void print_mice(void)
{
cout << "Three blind mice" << endl;
}
void print_run(void)
{
cout << "See how they run" << endl;
}
第4题
#include <iostream>
using namespace std;
int main(void)
{
int years;
cout << "Enter your ages:";
cin >> years;
cout << "You are " << years << " years old, equals = " << years * 12 << " months old." << endl;
return 0;
}
第5题
#include <iostream>
double convert(double c);
using namespace std;
int main(void)
{
double c_degree, f_degree;
cout << "Please enter a Celsius value: ";
cin >> c_degree;
f_degree = convert(c_degree);
cout << c_degree << " degrees Celsius is " << f_degree << " degrees Fahrenheit." << endl;
return 0;
}
double convert(double c)
{
return 1.8 * c + 32.0;
}
第6题
#include <iostream>
double convert(double light);
using namespace std;
int main(void)
{
double light_years, astro_years;
cout << "Enter the number of light years: ";
cin >> light_years;
astro_years = convert(light_years);
cout << light_years << " light years = " << astro_years << " astronomical units." << endl;
return 0;
}
double convert(double light)
{
return 63240 * light;
}
第7题
#include <iostream>
void display_time(int h, int m);
using namespace std;
int main(void)
{
int hours, minutes;
cout << "Enter the number of hours: ";
cin >> hours;
cout << "Enter the number of minutes: ";
cin >> minutes;
display_time(hours, minutes);
return 0;
}
void display_time(int h, int m)
{
cout << "Time: " << h << ":" << m << endl;
}