数据类型转换
在 C++ 中,可以进行数据类型之间的转换,分为隐式转换和显式转换。
(一)隐式转换
当进行运算时,系统会自动进行一些数据类型的转换,例如将 int
类型转换为 double
类型进行运算。
int a = 5;
double b = a; // 隐式将 int 类型的 a 转换为 double 类型并赋值给 b
(二)显式转换(强制类型转换)
可以使用强制类型转换运算符将一种数据类型转换为另一种数据类型。
double a = 3.9;
int b = int(a); // 将 double 类型的 a 转换为 int 类型,结果为 3
int c = 3;
double d = double(c); // 将 int 类型的 c 转换为 double 类型,结果为 3.0
也可以使用传统的 C 风格强制类型转换 int(a)
和 double(a)
,但不推荐,因为其类型安全性不如 static_cast
等现代类型转换运算符。
<cmath>
库简介
<cmath>
是 C++ 标准库中的一个头文件,它提供了大量用于执行数学运算的函数。使用时,需要在代码中包含该头文件,即 #include <cmath>
。
1. 绝对值函数
#include <iostream>
#include <cmath>
using namespace std;
int main(){
int num = -10;
int result = abs(num);
cout << "整数 " << num << " 的绝对值是: " << result << endl;
return 0;
}
#include <iostream>
#include <cmath>
using namespace std;
int main(){
double num = -3.14;
double result = fabs(num);
cout << "浮点数 " << num << " 的绝对值是: " << result << endl;
return 0;
}
2. 幂函数 pow(double x, double y)
功能:返回 x
的 y
次幂,即xy
示例代码:
#include <iostream>
#include <cmath>
using namespace std;
int main(){
double base = 2;
double exponent = 3;
double result = pow(base, exponent);
cout << base << " 的 " << exponent << " 次幂是: " << result << endl;
return 0;
}
3. 平方根函数 sqrt(double x)
功能:返回非负实数 x
的平方根。如果 x
为负数,会导致未定义行为。
示例代码:
#include <iostream>
#include <cmath>
using namespace std;
int main(){
double num = 25;
double result = sqrt(num);
cout << num << " 的平方根是: " << result << endl;
return 0;
}
4. 向上取整函数 ceil(double x)
功能:返回不小于 x
的最小整数,结果为 double
类型。
示例代码:
#include <iostream>
#include <cmath>
using namespace std;
int main(){
double num = 3.2;
double result = ceil(num);
cout << num << " 向上取整的结果是: " << result << endl;
return 0;}
5. 向下取整函数 floor(double x)
功能:返回不大于 x
的最大整数,结果为 double
类型。
示例代码:
#include <iostream>
#include <cmath>
using namespace std;
int main(){
double num = 3.8;
double result = floor(num);
cout << num << " 向下取整的结果是: " << result << endl;
return 0;
}
swap
函数
功能:用于交换两个变量的值,该函数定义在 <algorithm>
头文件中。
#include <iostream>
#include <algorithm>
using namespace std;
int main(){
int a = 5;
int b = 10;
cout << "交换前: a = " << a << ", b = " << b << endl;
swap(a, b);
cout << "交换后: a = " << a << ", b = " << b << endl;
return 0;
}
max
函数
功能:返回两个值中的较大值,定义在 <algorithm>
头文件中。
#include <iostream>
#include <algorithm>
using namespace std;
int main(){
int x = 20;
int y = 30;
int result = max(x, y);
cout << x << " 和 " << y << " 中的较大值是: " << result << endl;
return 0;
}
min
函数
功能:返回两个值中的较小值,定义在 <algorithm>
头文件中。
#include <iostream>
#include <algorithm>
using namespace std;
int main(){
int m = 15;
int n = 25;
int result = std::min(m, n);
cout << m << " 和 " << n << " 中的较小值是: " << result << endl;
return 0;
}
作业代码
// A. 总分和平均分
#include <iostream>
#include <cstdio>
using namespace std;
int x, y, z, s;
int main(){
cin >> x >> y >> z;
s = x + y + z;
printf("%d\n%.1lf", s, s / 3.0);
return 0;
}
// B. 五位数之和
#include <iostream>
#include <cstdio>
using namespace std;
int c, g, s, b, q, t;
int main(){
cin >> c, t = c; // 1263
g = c % 10, c /= 10;
s = c % 10, c /= 10;
b = c % 10, c /= 10;
q = c % 10, c /= 10;
cout << t + g + s + b + q;
return 0;
}
// C. 文具店的折扣
#include <iostream>
#include <cstdio>
using namespace std;
int x, y, n;
int main(){
cin >> x >> y >> n;
printf("%.1lf", n - (x + y) * 0.9);
return 0;
}
// D. 对称的六位数
#include <iostream>
#include <cstdio>
using namespace std;
int n, g, s, b;
int main(){
cin >> n;
g = n % 10;
s = n / 10 % 10;
b = n / 100;
printf("%d%d%d%d", g, s, b, n);
return 0;
}
有话要说...