C++编程 -- 基础练习(8)

文章介绍了C++中的几种编程实例,包括华氏到摄氏温度的转换、分段函数的实现、计算水加热能量、使用math库进行数学运算以及创建一个长方形类来计算面积。展示了基础数据类型和相关函数的使用。
摘要由CSDN通过智能技术生成

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;
}

评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值