第二章:开始学习 C++
//ex2.1--display your name and address
#include<iostream>
int main(void)
{
using namespace std;
cout<<"My name is liao chunguang and I live in hunan chenzhou.\n ”;
}
//ex2.2--convert the furlong units to yard uints- 把浪单位换位码单位
#include<iostream>
double fur2yd(double);
int main()
{
using namespace std;
cout<<"enter the distance measured by furlong units:";
double fur;
cin>>fur;
cout<<"convert the furlong to yard"<<endl;
double yd;
yd=fur2yd(fur);
cout<<fur<<" furlong is "<<yd<<" yard"<<endl;
return 0;
}
double fur2yd(double t)
{
return 220*t;
}
//ex2.3- 每个函数都被调用两次
#include<iostream>
void mice();
void see();
using namespace std;
int main()
kilogram{
mice();
mice();
see();
see();
return 0;
}
void mice()
{
cout<<"three blind mice"<<endl;
}
void see()
{
cout<<"see how they run"<<endl;
}
#include<iostream>
int main()
{
using namespace std;
cout<<"Enter your age:";
int age;
cin>>age;
int month;
month=age*12;
cout<<age<<" years is "<<month<<" months"<<endl;
return 0;
}
//ex2.5---convert the Celsius valve to Fahrenheit value
#include<iostream>
double C2F(double);
int main()
{
using namespace std;
cout<<"please enter a Celsius value:";
double C;
cin>>C;
double F;
F=C2F(C);
cout<<C<<" degrees Celsius is "<<F<<" degrees Fahrenheit."<<endl; return 0;
}
double C2F(double t)
{
return 1.8*t+32;
}
//ex2.6---convert the light years valve to astronomical units-- 把光年变换为天文单位
#include<iostream>
double convert(double);// 函数原型
int main()
{
using namespace std;
cout<<"Enter the number of light years:";
double light_years;
cin>>light_years;
double astro_units;
astro_units=convert(light_years);
cout<<light_years<<" light_years = "<<astro_units<<" astronomical units."<<endl;
return 0;
}
double convert(double t)
{
return 63240*t;//1 光年 =63240 天文单位
}
//ex2.7-- 显示用户输入的小时数和分钟数
#include<iostream>
void show();
main()
{
using namespace std;
show();
return 0;
}
void show()
{
using namespace std;
int h,m;
cout<<"enter the number of hours:";
cin>>h;
cout<<"enter the number of minutes:";
cin>>m;
cout<<"Time:"<<h<<":"<<m<<endl;
}
第三章:办理数据
//ex3.1 —将身高用英尺 (feet)和英寸 (inch)表示
#include<iostream>
const int inch_per_feet=12;//
const
常量 --1feet=12inches--1
英尺 =12 英寸
版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系QQ:729038198,我们将在24小时内删除。
发表评论