Dev - C++ - Alguien me puede ayudar a traducir un codigo de c# a c++?

 
Vista:
sin imagen de perfil

Alguien me puede ayudar a traducir un codigo de c# a c++?

Publicado por Daniel (1 intervención) el 03/06/2020 01:20:50
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
using System;
using System.Text;
 
namespace MulitplyPolinomy
{
    class Program
    {
        static void Main(string[] args)
        {
            // (x^2 + 2x -3)
            var a = new double[] { -3, 2, 1 };
            // (x+2)
            var b = new double[] { 2, 1 };
 
            var m = Multiply(a, b);
            Console.WriteLine(PolinomyToString(m));
 
        }
 
 
        static double[] Multiply(double[] a, double[] b)
        {
            var result = new double[a.Length + b.Length - 1];
            for (int i = 0; i < a.Length; i++)
            {
                for (int j = 0; j < b.Length; j++)
                {
                    result[i + j] += a[i] * b[j];
                }
            }
            return result;
        }
 
        static string PolinomyToString(double[] p)
        {
            var sb = new StringBuilder();
            for (int i = 0; i < p.Length; i++)
            {
                if (i > 0) sb.Append(" + ");
                sb.Append(p[i].ToString());
                if (i > 0) sb.Append("x^").Append(i.ToString());
            }
            return sb.ToString();
 
        }
    }
}
 
//Necesito traducir este codigo de C# a C++
Valora esta pregunta
Me gusta: Está pregunta es útil y esta claraNo me gusta: Está pregunta no esta clara o no es útil
0
Responder

Alguien me puede ayudar a traducir un codigo de c# a c++?

Publicado por Alejandro Caro (4 intervenciones) el 20/12/2023 00:42:42
Aquí lo tienes

#include <iostream>
#include <string>
#include <sstream>
#include <vector>

class MultiplyPolynomial {
public:
static void Main() {
// (x^2 + 2x -3)
std::vector<double> a = { -3, 2, 1 };
// (x+2)
std::vector<double> b = { 2, 1 };

std::vector<double> m = Multiply(a, b);
std::cout << PolinomyToString(m) << std::endl;
}

static std::vector<double> Multiply(const std::vector<double>& a, const std::vector<double>& b) {
std::vector<double> result(a.size() + b.size() - 1, 0);
for (size_t i = 0; i < a.size(); i++) {
for (size_t j = 0; j < b.size(); j++) {
result[i + j] += a[i] * b[j];
}
}
return result;
}

static std::string PolinomyToString(const std::vector<double>& p) {
std::stringstream ss;
for (size_t i = 0; i < p.size(); i++) {
if (i > 0) ss << " + ";
ss << p[i];
if (i > 0) ss << "x^" << i;
}
return ss.str();
}
};

int main() {
MultiplyPolynomial::Main();
return 0;
}
Valora esta respuesta
Me gusta: Está respuesta es útil y esta claraNo me gusta: Está respuesta no esta clara o no es útil
0
Comentar
Imágen de perfil de kevin

RE: Alguien me puede ayudar a traducir un codigo de c# a c++?

Publicado por kevin (59 intervenciones) el 24/12/2023 07:41:08
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
#include <iostream>
#include <vector>
 
std::vector<double> multiply(const std::vector<double> & a, const std::vector<double> & b)
{
    auto result = std::vector<double>((a.size() + b.size()) -1);
 
    for (int i = 0; i < a.size(); i++)
    {
        for (int j = 0; j < b.size(); j++)
        {
                result[i + j] += a[i] * b[j];
        }
    }
 
    return result;
}
 
std::string PolinomyToString(const std::vector<double> & p) {
    std::string sb;
    for (int i = 0; i < p.size(); i++)
    {
        if (i > 0) sb.append(" + ");
        sb.append(std::to_string(p.at(i)));
        if (i > 0) sb.append("x^").append(std::to_string(i));
    }
 
    return sb;
}
 
int main(int argc, char const *argv[])
{
 
    // (x^2 + 2x -3)
    std::vector<double> a {-3,2,1};
 
    // (x+2)
    std::vector<double> b {2,1};
 
    auto m = multiply(a,b);
 
    std::cout << PolinomyToString(m) << std::endl;
 
    return 0;
}
Valora esta respuesta
Me gusta: Está respuesta es útil y esta claraNo me gusta: Está respuesta no esta clara o no es útil
0
Comentar