2014-01-06 110 views
0

我有這個C++代碼,並將其轉換爲c#(.net框架4)代碼。 有沒有人爲我的malloc,免費和sprintf方法提示?C++代碼(malloc方法)以c#代碼

int monate = ee; 
double *forward; 
double *backward; 
size_t bytes = 6*sizeof(double); 
forward = (double *)malloc(monate * bytes); 
backward = (double *)malloc(monate * bytes); 

for (int i = 0; i <= monate; i++) { 
    forward[i] = 0.; 
    backward[i] = rate; 
} 

forward[0]  = auszahlungsbetrag; 
backward[0]  = 0.; 
backward[monate] = rate + rest; 

double ez = esolver(12 , monate, forward, backward); 
printf("Effektivzins: %.2f\n", ez); 

char tmp0[256]; 
sprintf (tmp0, "Rate: %.2f (%f)" , rate, rate); 
writeToLogFile(tmp0); 

char tmp[256]; 
sprintf (tmp, "Effektivzins: %.2f (%f)" , ez, ez); 
writeToLogFile(tmp); 

free(forward); 
free(backward); 

這是我轉換後的c#代碼。對於malloc方法,free和sprintf我有一些麻煩將它轉換爲c#。

using System;

namespace Zinsrechner 
{ 
public static class Basic 
{ 

    public static void basic(double aa, double auszahlungsbetrag, double cc, int ee, double rest) 
    { 

     Writelog.writeToLogFile("Start der Berechnung, Type BASIC"); 
     Writelog.writeToLogFileDouble("Kreditvolumen: ", aa); 
     Writelog.writeToLogFileDouble("Auszahlungsbetrag: ", auszahlungsbetrag); 
     Writelog.writeToLogFileDouble("Nominalzins: ", cc); 
     Writelog.writeToLogFileInt("Laufzeit: ", ee); 
     Writelog.writeToLogFileDouble("Restschuld: ", rest); 

     ///////// 

     // Ratenberechnung 

     double nomzins = (cc/12.0/100.0) + 1.0; 
     double unten = 0.0; 

     for (int x = 0 ; x <= ee-1 ; x++) 
     { 
      unten = unten + Math.Pow(nomzins,x); 
     } 

     double rate = ((aa * Math.Pow(nomzins, ee)) - rest)/unten; 
     Console.Write("Rate: {0:f2}\n", rate); 

     int monate = ee; 
     double[] forward; 
     double[] backward; 
     uint bytes = 6 * sizeof(double); 
     //C++ TO C# CONVERTER TODO TASK: The memory management function 'malloc' has no equivalent in C#: 
     //forward = (double)malloc(monate * bytes); 
     forward = (double)(monate * bytes); 
     //C++ TO C# CONVERTER TODO TASK: The memory management function 'malloc' has no equivalent in C#: 
     backward = (double)malloc(monate * bytes); 

     for (int i = 0; i <= monate; i++) 
     { 
      forward[i] = 0.0; 
      backward[i] = rate; 
     } 

     forward[0] = auszahlungsbetrag; 
     backward[0] = 0.0; 
     backward[monate] = rate + rest; 

     double ez = Effektivzinssolver.esolver(12, monate, forward, backward); 
     Console.Write("Effektivzins: {0:f2}\n", ez); 

     string tmp0 = new string(new char[256]); 
     tmp0 = string.Format("Rate: {0:f2} ({1:f})", rate, rate); 
     Writelog.writeToLogFile(tmp0); 

     string tmp = new string(new char[256]); 
     tmp = string.Format("Effektivzins: {0:f2} ({1:f})", ez, ez); 
     Writelog.writeToLogFile(tmp); 

     //C++ TO C# CONVERTER TODO TASK: The memory management function 'free' has no equivalent in C#: 
     free(forward); 
     //C++ TO C# CONVERTER TODO TASK: The memory management function 'free' has no equivalent in C#: 
     free(backward); 


    } 
} 
} 

+0

這不是C++代碼,那是C代碼。 – Angew

+2

@Angew這並不是說這裏的代碼不是有效的C++。爲什麼,我一直認爲「C++」是指整個語言,而不僅僅是那些不在經典C中的部分。 – user3109672

+0

@ user3109672 OK,是的。但是,將慣用的C++代碼轉換爲C#將遵循一個完全不同的過程,即將此「C與新奇文件擴展名」代碼轉換爲C#。 – Angew

回答

1
double[] forward = new double[monate * 6]; 

你不需要free()。 C#被垃圾收集。

1

在C#中,你不需要關心內存 - 垃圾收集器對付它很棒。你的代碼必須是這樣的:

int monate = ee; 
double[] forward = new double[monate * 6]; 
double[] backward = new double[monate * 6]; 

for (int i = 0; i <= monate; i++) { 
    forward[i] = 0.; 
    backward[i] = rate; 
} 

forward[0]  = auszahlungsbetrag; 
backward[0]  = 0; 
backward[monate] = rate + rest; 

double ez = esolver(12 , monate, forward, backward); 
Console.WriteLine("Effektivzins: {0}\n", ez); 

然後,不命名用德語和英語的變量一起;)

1

的malloc當量和自由是:

IntPtr memory = Marshal.AllocHGlobal(100); //malloc 
Marshal.FreeHGlobal(memory); //free 
+0

雖然這是事實,但我認爲這並不是很有用,因爲在這個例子中,你不需要在C#中進行任何手動內存管理。 – Tobi

0

你可以忘記MallocFree,因爲C#有一個垃圾收集器。至於sprintf看看String.Format()Console.WriteLine()

而且,因爲它看起來像你的應用似乎應付錢數,我會強烈建議改用decimal類型的double。 (decimal