C++ Primer Plus 第2章_开始学习C++_代码和练习题

程序清单

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;
}

  • 1
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 1
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值