当前位置: 移动技术网 > IT编程>开发语言>C/C++ > 开始学习C++

开始学习C++

2018年01月10日  | 移动技术网IT编程  | 我要评论

三亚市卫星地图,网店咨询,周子礴

  今天是开通博客记录学习历程的第一篇。最近一段时间重新抓起C++进行学习。学习书目“C++ primer PLUS",编辑器是在线编辑Leetcode(该编辑器对cin不支持的样子)

2.4.3 用户定义的函数

#include <iostream>
#include <cmath>
void simon(int);//声明语句是语句要加分号!

int main() {
using namespace std;
simon(3);
int count;
cin >> count;
simon(count);
cout << "Done!" << endl;
return 0;
}

void simon(int n) //函数定义不是语句,不需要加分号;函数头,接受一个参数,返回一个值(void类型)
{
using namespace std;
cout << "Simon says touch you toes " << n << "times"<< endl;
}//函数体,结合函数头为一个原型。

2.4.5 Using namespace std;

  将 using namespace std放在最前面,之后的函数都能使用名称空间std中所有的元素。

练习:

2 单位转换

#include <iostream>
#include <cmath>
double LenMa(double LenLong);

int main() {
using namespace std;
double LenLong = 5;//变量必须在使用前声明,和matlab/python不同。
double Lenma = LenMa(LenLong);
cout << LenLong << " long " << "is " << Lenma << " Ma" << endl;
return 0;
}

double LenMa(double LenLong)
{
return 200*LenLong;
}

3 语句输出

#include <iostream>

#include <cmath>
using namespace std;
void Out1(int n);
void Out2(int n);

int main() {
using namespace std;
int n = 2;
int m = 2;
Out1(n);
Out2(m);
return 0;
}

void Out1(int n)
{
for(int i = 1;i<=n;i++)
cout << "Three blind mice" << endl;
}
void Out2(int n)
{
for(int i = 1;i<=n;i++)
cout << "See how they run" << endl;
}

4 单位转化

#include <iostream>
#include <cmath>
using namespace std;
double Temptrans(double n);

int main() {
cout << "Please enter a Celsius value:";
double n;
cin >> n;
cout << n << endl;
cout << n << " degrees Celsius is " << Temptrans(n) << " degrees Fahrenheit" << endl;
}

double Temptrans(double n)
{
return(1.8*n+32.0);
}

7 数值输出

#include <iostream>
#include <cmath>
using namespace std;
void Timeout(int n ,int m);
int main() {
int hour;
int minute;
cout << "Enter the number of hours:";
cin >> hour;
cout << hour << endl;
cout << "Enter the number of minutes: ";
cin >> minute;
cout << minute << endl;
Timeout(hour , minute);
return 0;
}

void Timeout(int hour ,int minute)
{
cout << "Time: " << hour << ":" << minute <<endl;
}

如对本文有疑问,请在下面进行留言讨论,广大热心网友会与你互动!! 点击进行留言回复

相关文章:

验证码:
移动技术网