首页 > 试题广场 >

重载运算符

[编程题]重载运算符
  • 热度指数:4185 时间限制:C/C++ 1秒,其他语言2秒 空间限制:C/C++ 32M,其他语言64M
  • 算法知识视频讲解
建立一个角类,在这个类中重载减号运算符角度相减,并实现求出角度的正弦值的函数

输入描述:
输入第一行为样例数m,接下来有m行每行两个整数表示角度。


输出描述:
输出m行,表示两角相减的正弦值,保留小数点后两位。
示例1

输入

1
60 30

输出

0.50

Once AC
#define pi 3.14159
(2207)#include <bits/stdc++.h>
using namespace std;
class angle{
	public:
		int x;
		angle(int xx):x(xx){
		}
		void operator-(const angle& a){
			cout<<fixed<<setprecision(2)<<sin((x-a.x)*pi/180)<<endl;
		}
};
int main(){
	int m,x1,x2;
	cin>>m;
	while(m--){
		cin>>x1>>x2;
		angle a(x1);
		angle b(x2);
		a-b;
	}	
	return 0;
}


发表于 2020-03-11 21:17:55 回复(0)
注意这边要转化为弧度,才能用sin进行正确的计算
#include<iostream>
#include<algorithm>
#include<iomanip>
using namespace std;
class angle {
public:
    int n;
    angle operator -(angle a) {
        this->n -= a.n;
        return *this;
    }
    void cal() {
        cout << fixed << setprecision(2) << sin((double)n / 180 * 3.1415926) << endl;
    }
};
int main() {
    int m;
    cin >> m;
    for (int i = 0; i<m; i++) {
        angle a, b, temp;
        cin >> a.n >> b.n;
        temp = a - b;
        temp.cal();
    }
}

发表于 2019-02-26 14:30:58 回复(2)
#include <iostream>
#include <string>
#include <algorithm>
#include <cmath>
#include <iomanip>
using namespace std;
class angle{
    public:
    double angles;
    angle operator - (const angle &b){
         this->angles=this->angles-b.angles;
        return *this;
    }
};
int main()
{   int n;
    cin>>n;
    while(n>0){
    angle a,b;
    cin>>a.angles>>b.angles;
    cout<< fixed << setprecision(2)<<sin((a.angles-b.angles)*3.141592653/180)<<endl;
    n=n-1;
        
    }
    return 0;
}

发表于 2019-06-26 12:42:44 回复(0)
#include <iostream>
#include <cmath>
#include <iomanip>
using namespace std;
int main(){
    int m;
    cin >> m;
    while (m--){
        double a, b;
        cin >> a >> b;
        cout << fixed << setprecision(2) << sin((a-b)/180 * acos(-1)) << endl;
    }
}

编辑于 2024-03-15 16:12:22 回复(0)
import java.util.*;



// 注意类名必须为 Main, 不要有任何 package xxx 信息
public class Main {
    public static void main(String[] args) {
        Scanner in = new Scanner(System.in);
        while (in.hasNextInt()) {
            int m = in.nextInt();
            for (int i = 0; i < m; i++) {
                int angle1 = in.nextInt();
                int angle2 = in.nextInt();
                double result = Math.sin(angle1 * Math.PI / 180 - angle2 * Math.PI / 180);
                System.out.println(String.format("%.2f", result));
            }
        }
    }
}

发表于 2023-03-13 09:19:33 回复(0)
#include<iostream>
#include<cmath>
using namespace std;
class angle
{
    public:
    double n;
    angle operator -(angle a)
    {
        this->n -= a.n;
        return *this;
    }
    void caculate()
    {
        printf("%.2lf",sin(n / 180 * M_PI));//将角度转化为弧度再运用sin求解
        cout << endl;
    }
};

int main(void)
{
    int m;
    while(cin >> m)
    {
        while(m--)
        {
            angle x,y,z;
            cin >> x.n >> y.n;
            z = x - y;
            z.caculate();
        }
    }
    return 0;
}

发表于 2022-02-12 19:36:55 回复(0)
#include<stdio.h>
(737)#include<math.h>
#define pi acos(-1)
int main()
{
    int m,a,b;float key;
    scanf("%d",&m);
    while(m--)
    {
        scanf("%d%d",&a,&b);
        key=sin((a-b)*pi/180);
        printf("%.2f\n",key);
    }
}

发表于 2020-05-09 16:13:05 回复(0)
#include <stdio.h>
#include <stdlib.h>
#include <math.h>
#define PI acos(-1)

int main()
{
    int m;
    while(~scanf("%d", &m))
    {
        for(int i = 0; i < m; i++)
        {
            int alpha, beta;
            scanf("%d%d", &alpha, &beta);
            double sinx = sin((alpha - beta) * PI / 180.0);
            printf("%.2lf\n", sinx);
        }
    }

    return 0;
}
C语言里没有重载一说,也没有类。。
编辑于 2020-04-03 11:26:39 回复(0)
#include<iostream>
#include<stdio.h>
#include<math.h>
using namespace std;
class angle {
public:
	double degree;
	angle() { degree = 0; }
	angle(double d)
	{
		this->degree = d;
	}
	angle operator -(const angle other)
	{
		angle result;
		result.degree = degree - other.degree;
		return result;
	}
	void show()
	{
		double t = sin(this->degree*3.1415926 / 180);
		printf("%.2lf\n", t);
	}
};

int main()
{
	int m;
	angle a, b, c;
	cin >> m;
	while (m) {
		cin >> a.degree >> b.degree;
		c = a - b;
		c.show();
		m--;
	}
}


发表于 2020-01-21 02:26:34 回复(0)
好像不用类,直接算也能通过。。。
#include<stdio.h>
#include<math.h>
#define PI 3.1415926
int main()
{
	int m;
	while (!scanf("%d",&m))
	{
		while(getchar()!='\n')
			continue;
	}
	int i;
	double angle1=0,angle2;
	for (i=0;i<m;i++)
	{
		scanf("%lf %lf",&angle1,&angle2);//double类型格式化输入要用%lf
		printf("%.2f\n",sin((angle1-angle2)/180.0*PI));
	}
	return 0;
}


发表于 2020-01-10 15:21:39 回复(0)
#include <iostream>
#include <iomanip>
#include <cmath>
const double PI = acos(-1); 
using namespace std;
class Angle
{
    public:
    double degree;
    Angle(double de){degree=de;}
    Angle(){}
    Angle operator-(const Angle& a){
        Angle a1;
        a1.degree=degree-a.degree;
        return a1;
    }
};
int main(){
    int m;
    double d1,d2,result;
    cin>>m;
    for(int i=0;i<m;i++){
        cin>>d1>>d2;
        Angle angle1(d1),angle2(d2),angle0=angle1-angle2;
        result=sin(angle0.degree*PI/180);
        cout<<fixed<<setprecision(2)<<result<<endl;
    }
    return 0;
}

发表于 2019-12-24 16:50:33 回复(0)
最后别忘了把浮点数转化为与π相关的数哦
#include<iostream>
#include<cmath>
#include<iomanip>
const double PI = 3.1415925;
using namespace std;

class Angle{
public:
    Angle() : angle_(0) {}
    Angle(double a0) : angle_(a0) {}
    friend Angle operator -(const Angle&, const Angle&);
    double angle_;
};

Angle operator -(const Angle& a1, const Angle& a2)
{
    return Angle(a1.angle_ - a2.angle_);
}

int main()
{
    int m;
    cin>>m;
    while(m-- > 0)
    {
        double a1, a2;
        cin>>a1>>a2;
        Angle ang1(a1), ang2(a2), ang3;
        ang3 = ang1 - ang2;
        cout<<setiosflags(ios::fixed)<<setprecision(2)<<sin(ang3.angle_ / 180.0 * PI)<<endl;
    }
    return 0;
}

发表于 2019-12-18 08:19:42 回复(0)
#include<stdio.h>
#include<math.h>

const double PI = acos(-1);

class Angle {
public:
    int a;

    Angle(int num) {
        this->a = num;
    }

    Angle operator-(Angle b) {
        return Angle(a - b.a);
    }

    void printSin() {
        printf("%.2f\n", sin((this->a) / 180.0 * PI));
    }
};

int main() {
    int m, a, b;
    scanf("%d", &m);
    for (int i = 0; i < m; i++) {
        scanf("%d%d", &a, &b);
        Angle a1(a), a2(b), a3(0);
        a3 = a1 - a2;
        a3.printSin();
    }
    return 0;
}

发表于 2019-09-18 19:57:47 回复(0)
/*
https://zhidao.baidu.com/question/223161864
LWQ
*/
#include <stdio.h>
#include <math.h>
using namespace std;

#define PI 3.1415926

int main() {
	int m;
	while (scanf("%d", &m) != EOF) {
		float a, b;
		for (int i = 0; i < m; i++) {
			scanf("%f%f", &a, &b);
			printf("%.2f\n", sin((a - b)*PI / 180));
		}
	}
	return 0;
}

发表于 2019-09-09 14:26:10 回复(0)
import math

m = int(input())
for i in range(m):
    temp = list(map(int, input().split()))
    print('%.2f'%(math.sin(math.pi*(temp[0] - temp[1])/180)))

发表于 2019-03-28 19:55:25 回复(1)
#include <iostream>
#include <cmath>
#include <cstdio>
#define PI 3.1415926535

using namespace std;

class Angle
{
    public:
        float n;
        Angle operator -(Angle b)
        {
            Angle a;
            a.n = this->n - b.n;
            return a;
        }
        void cal(void)
        {
            printf("%.2f\n", sin(n*PI/180));
        }
};

int main()
{
    int m,i;
    cin >> m;
    for(i=0;i<m;i++)
    {
        Angle a,b,tmp;
        cin >> a.n >> b.n;
        tmp = a-b;
        tmp.cal();
    }
    return 0;
}
发表于 2019-03-26 20:51:00 回复(0)
 import math
class Tg:
    def __init__(self,a):
        self.a=a

    def __sub__(self, other):
        return self.a-other.a

if __name__ == '__main__':
    m=int(input())
    for i in range(m):
        jd=list(map(int,input().split()))
        temp=Tg(jd[0]).__sub__(Tg(jd[1]))*math.pi/180
        print('{:.2f}'.format(math.sin(temp)))

发表于 2019-03-22 23:23:32 回复(0)
import java.util.Scanner;
public class Main{
    public static void main(String[] args){
        Scanner s = new Scanner(System.in);
        int n = s.nextInt();
        while(n-- > 0){
            int a = s.nextInt();
            int b = s.nextInt();
            System.out.println(String.format("%.2f", Math.sin((a-b)*Math.PI/180)));
        }
    }
}

发表于 2019-03-11 21:43:07 回复(0)
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <math.h>
int main()
{
    int n;
    while(scanf("%d",&n)!=EOF)
    {
        int i;
        for(i=0;i<n;i++)
        {
            double a,b;
            scanf("%lf %lf",&a,&b);
            printf("%.2lf\n",sin((a-b)*3.1415926/180));
        }
    }
    return 0;
}

发表于 2019-03-09 11:10:07 回复(0)
#include <iostream>
#include<cmath>
#include<iomanip>
using namespace std;

int main()
{
    int n;
    cin>>n;
    for(int i=0;i<n;i++){
     int ang1,ang2;
     cin>>ang1>>ang2;
     cout<<fixed<<setprecision(2)<<sin(M_PI/180*(ang1-ang2))<<endl;
    }
    return 0;
}

发表于 2019-03-09 09:33:21 回复(0)

问题信息

上传者:小小
难度:
24条回答 2833浏览

热门推荐

通过挑战的用户

查看代码