C++ Primer Plus 第6版 编程练习(第二章)

 

1.

#include <iostream>
int main()
{
 using namespace std;
 cout<<"abc";
 cout<<endl;
 cout<<"abcd";
 return 0;
}

 

2.

#include <iostream>
using namespace std;
int main()
{
 int l,m;
 cout<<"Please enter long:";
 cin>>l;
 m=l*220;
 cout<<"=";
 cout<<m;
 cout<<"ma";
 return 0;
}

 

3.

#include<iostream>
using namespace std;
void function_a();
void function_b();

int main()
{
 function_a();
 function_a();
 function_b();
 function_b();
    return 0;
}

void function_a()
{
 cout<<"Three blind mice"<<endl;
}

void function_b()
{
 cout<<"See how they run"<<endl;
}

 

4.

#include<iostream>
using namespace std;
int main()
{
 int age,month;
 cout<<"Enter your age:";
 cin>>age;
 month=age*12;
 cout<<"Contain "<<month<<" months";
 return 0;
}
 

5.

#include<iostream>
using namespace std;
int change(int);

int main()
{
 int celsius,fahrenheit;
 cout<<"Please enter a Celsius value:";
 cin>>celsius;
 fahrenheit=change(celsius);
 cout<<celsius<<" degrees Celsius is "<<fahrenheit<<" degrees Fahrenheits.";
 return 0;
}

int change(int n)
{
 int m;
 m=1.8*n+32.0;
 return m;
}

 

6.

 

#include<iostream>
using namespace std;
double trans(double x);
int main()
{
 double lightyear,au;
 cout<<"Enter the number of light years:";
 cin>>lightyear;
 au=trans(lightyear);
 cout<<lightyear<<" light years="<<au<<"astronomical units.";
 return 0;
}

 

double trans(double x)
{
 double au=63240*x;
 return au;
}


7. 

#include<iostream>
using namespace std;
void trans(int a,int b);
int main()
{
 int hour,min;
 cout<<"Enter the number of hours:";
 cin>>hour;
 if(hour<0||hour>24)
 {
  cout<<"It's not a correct time!";
  return 0;
 }
 else
 {
 }
 cout<<"Enter the number of minutes:";
 cin>>min;
 if(min<0||min>60)
 {
  cout<<"It's not a correct time!";
  return 0;
 }
 else
 {
 }
 trans(hour,min);
 return 0;
}

void trans(int a,int b)
{
 cout<<"Time:"<<a<<":"<<b;
}

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值