1. 输入一个华氏温度,要求输出摄氏温度。
题目:输入一个华氏温度,要求输出摄氏温度。公式为 c=5(F-32)/9 输出要求有文字说明,取2位小数。
方法1:
#include<iostream>
#include<iomanip>
using namespace std;
int main()
{
float x,c;
scanf("%F",&x);
{
c=5*(x-32)/9;
}
printf("c=%.2F\n",c);
return 0;
}
方法2:
#include<iostream>
#include<iomanip>
using namespace std;
int main()
{
double c,F;
cin>>F;
c=5*(F-32)/9;
cout<<"c="<<fixed<<setprecision(2)<<c<<endl;
return 0;
}
2. 编程计算分段函数
题目:编程计算分段函数
输入x,打印出y值。
方法1:
#include <stdio.h>
#include <math.h>
int main()
{
int x;
double y;
scanf("%d", &x);
if (x > 0 )
{
y = exp(-x);
}
if (x=0)
{
y = 1;
}
else if (x=0)
{
y = - exp(x);
}
printf("y=%0.8f", y);
return 0;
}
方法2:
#include <iostream>
#include <cmath>
using namespace std;
int main()
{
int x;
double y;
cin>>x;
if(x>0)
y=exp(-x);
else
if(x==0)
y=1;
else
y=-exp(x);
cout<<"y="<<y<<endl;
return 0;
}
3. 计算将水从初始温度加热到最终温度所需的能量
题目:
编写程序,计算将水从初始温度加热到最终温度所需的能量。程序应该提示用户输入水的重量(以千克为单位),以及水的初始温度和最终温度。计算能量的公式是:
Q = M ×(最终温度 – 初始温度)× 4184
这里的M是以千克为单位的水的重量,温度以摄氏度为单位,而能量Q以焦耳为单位。
相关知识点:注意int型和double型数据之间的运算规则
方法1:
#include <stdio.h>
#include<math.h>
int main()
{
int M,a,b;
double Q;
scanf("%d%d%d",&M,&a,&b);
Q=M*(b-a)*4184;
printf("%g",Q);
return 0;
}
方法2:
#include <iostream>
using namespace std;
int main()
{
int m,t1,t2;
double q;
cin>>m>>t1>>t2;
q=4184.0*m*(t2-t1);
cout<<q<<endl;
return 0;
}
4. 计算并输出sin(x)、cos(x)、|x|、ex、xy的值
题目:编写程序,接收键盘输入的两个数据保存到变量x、y中,计算并输出sin(x)、cos(x)、|x|、ex、xy的值。提示:需要包含cmath库,直接实验cmath库里面的函数。其中ex表示e的x次方,xy表示x的y次方,pai的值取3.14159。 sin(x)、cos(x)计算的时候需要首先将x转换为弧度值。
相关知识:了解math.h自带的一些简单的函数
方法1:
#include <stdio.h>
#include <math.h>
int main() {
double x, y;
scanf("%lf %lf", &x, &y);
double sin_x = sin(x * M_PI / 180); // 将x转换为弧度值后计算sin(x)
double cos_x = cos(x * M_PI / 180); // 将x转换为弧度值后计算cos(x)
double abs_x = fabs(x); // 计算|x|
double exp_x = exp(x); // 计算ex
double pow_xy = pow(x, y); // 计算xy
printf("sin(x):%g\n", sin_x);
printf("cos(x):%g\n", cos_x);
printf("|x|:%g\n", abs_x);
printf("ex:%g\n", exp_x);
printf("xy:%g\n", pow_xy);
return 0;
}
方法2:
#include <iostream>
#include <cmath>
using namespace std;
int main()
{
double x,y;
cin>>x>>y;
cout<<"sin(x):"<<sin(3.14159*x/180)<<endl;
cout<<"cos(x):"<<cos(3.14159*x/180)<<endl;
cout<<"|x|:"<<fabs(x)<<endl;
cout<<"ex:"<<exp(x)<<endl;
cout<<"xy:"<<pow(x,y)<<endl;
return 0;
}
5. 长方形面积计算
题目:
定义一个长方形类Rectangle,私有数据成员为double型width、height(表示长方形的宽和高),成员函数包括构造函数Rectangle(用于实现对数据成员width、height的初始化,默认值为0)、成员函数GetArea(计算并返回长方形的面积)。在主函数中输入长方形的宽和高,定义长方形对象r1,无初值,输出r1的面积,再定义对象r2,使用之前输入的宽和高初始化,输出r2的面积。
输入示例:
10.2 25.5
输出示例:
0
260.1
方法1:
#include <stdio.h>
// 定义长方形类Rectangle
typedef struct Rectangle {
double width; // 宽度
double height; // 高度
} Rectangle;
// 构造函数,用于初始化长方形对象
Rectangle createRectangle(double width, double height) {
Rectangle r;
r.width = width;
r.height = height;
return r;
}
// 计算长方形的面积
double getArea(Rectangle r) {
return r.width * r.height;
}
int main() {
double width, height;
scanf("%lf %lf", &width, &height);
// 定义长方形对象r1,无初值
Rectangle r1 = createRectangle(0, 0);
double area1 = getArea(r1);
printf("%.0lf\n", area1);
// 定义长方形对象r2,使用输入的宽和高初始化
Rectangle r2 = createRectangle(width, height);
double area2 = getArea(r2);
printf("%0.7lg", area2);
return 0;
}
方法2:
#include<iostream>
using namespace std;
class Rectangle{
public:
Rectangle(double width = 0, double height = 0);
double GetArea();
private:
double m_width;
double m_height;
};
Rectangle::Rectangle(double width, double height)
{
m_width = width;
m_height = height;
}
double Rectangle::GetArea()
{
return m_width * m_height;
}
int main()
{
double width, height;
cin >> width >> height;
Rectangle r1;
cout << r1.GetArea() << endl;
Rectangle r2(width, height);
cout << r2.GetArea() << endl;
return 0;
}