gpt4 book ai didi

c++ - 如何添加返回 3 个数字的函数,而不仅仅是 1 个

转载 作者:行者123 更新时间:2023-11-28 06:31:39 25 4
gpt4 key购买 nike

<分区>

我的程序使用公式 S = 0.5 * a * b * sin(A)(A 是 a 和 b 之间的角度)计算用户输入的三角形的平方。该程序做对了,但我想知道如何添加新函数,然后在 for 循环中调用它。我要创建的函数应该包含这两行代码。问题是我不知道如何编写返回多个值的函数,而不仅仅是一个值。

cout<<"Enter triangle"<<endl; 
cin>>a>>b>>angle;

程序源码为:

#include <iostream>
#include <math.h>
using namespace std;

double calc(double A, double B, double Angle);
void printResults(double square, double max_square);

int main()
{
int i, n;
double a, b, angle, square, max_square = 0;

cout<<"How many triangles do you want to enter?"<<endl;
cin>> n;

for(i=1; i<=n; i++)
{
cout<<"Enter triangle"<<endl; // so I want to call my own function here
cin>>a>>b>>angle; // the body of function should contain these 2 lines

square = calc(a,b,angle);
if (square > max_square)
max_square = square;
printResults(square, max_square);
}
return 0;
}
double calc(double A, double B, double Angle)
{
double s, s_max = 0;
s = 0.5 * A * B * sin(Angle);
return s;
}
void printResults(double square, double max_square)
{
cout<<"------------------------\nCurrent square is"<<square<<endl;
cout<<"Maximum square is"<<max_square<<"\n------------------------\n";
return;
}

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com