C
Curious
A business associate told me that I should be able to run C++ code in
C#.NET environment because C++ and C# belong to the same family.
He sent me the code below and asked me to run in C#.NET environment. I
got numerous compiling errors because #include stuff don't exist in
C#! Could anyone tell me if the code below should run in C#.NET?
-------------------------------------------------------------------------------------------------------------------------------
//#define NO_NEWIO
#include <iostream>
/// Include for CONNECT/C++
#include "stdafx.h"
#include "stdafx.cpp"
//#include "sconnect.h"
#include "c:\\data\\Splus61 Includes\\EasyEngineConnect.h"
#include "c:\\data\\Splus61 Includes\\EasyEngineConnect.cpp"
#include "c:\\data\\Splus61 Includes\\EasyCharacter.h"
//#include "c:\\data\\Splus61 Includes\\EasyCharacter.cpp"
#include "c:\\data\\Splus61 Includes\\EasyMatrix.h"
//#include "c:\\data\\Splus61 Includes\\EasyMatrix.cpp"
#include "c:\\data\\Splus61 Includes\\EasyNumeric.h"
//#include "c:\\data\\Splus61 Includes\\EasyNumeric.cpp"
using namespace std;
int main(int argc, char* argv[])
{
// Connect to S+
EasyEngineConnect easyconnect_(argc, argv);
/// Create matrix 9x9 elem.
//EasyMatrix x;
//x.Create("matrix(1:9, nrow=9, ncol=9)", "x");
// Create matrix 9x9 elem.
//EasyMatrix y;
//y.Create("matrix(1:9, nrow=9, ncol=9)", "y");
CSPobject Y;
if (!Y.Create("vector(\"double\",10.6)"))
{
cout << "Can not create vector." << endl;
return 1;
}
CSPobject X1;
if (!X1.Create("vector(\"double\",8.6)"))
{
cout << "Can not create vector." << endl;
return 1;
}
CSPobject X2;
if (!X2.Create("vector(\"double\",3.4)"))
{
cout << "Can not create vector." << endl;
return 1;
}
/// Connect to S+
// CSPengineConnect engineConnect(argc, argv); //EasyEngineConnect
easyconnect_(argc, argv);
/// Create matrix 9x9 elem.
CSPmatrix x;
x.Create("matrix(1:9, nrow=9, ncol=9)", "x");
CSPnumeric sx;
sx.Create("1:10","x");
CSPnumeric sy = sx * sx;
sy.Assign("y");
// engineConnect.SyncParseEval("z<-lm(y~x)");
/*
CSPengineConnect engineConnect(argc, argv);
CSPobject x;
/// Create matrix 24x400 elem.
x.Create("matrix(1:223, nrow=24, ncol=400)");
//x.Create("matrix(1.2, nrow=, ncol=10)");
CSPobject y;
/// Create matrix 24x400 elem.
y.Create("matrix(231:1, nrow=24, ncol=400)");
/// Assign.
x.Assign("x");
y.Assign("y");
/// Call S+ regression "z<-x+y".
int iSuccess = engineConnect.SyncParseEval("z<-x+y");
/// Error.
if (iSuccess == 0)
{
cout << "Error, Can not call \"z<-(x+y)\"" << endl;
return 1;
}
CSPmatrix z(engineConnect.get("z"));
if (!z.IsValid())
{
cout << "Error, Can not search object z" << endl;
}
/// Print.
for (int i = 1; i <= 5;i++); //z.GetNRow(); ++i)
{
for (int j = 1; j <= z.GetNCol(); ++j)
{
cout << (double)z(i, j) << " ";
}
cout << endl;
}
/*
//Create the connection to S-PLUS
CSPengineConnect engineConnect(argc, argv);
/// Create vector 24.
CSPobject A_vector;
if (!A_vector.Create("vector(\"double\",10)"))
{
cout << "Can not create vector." << endl;
return 1;
}
CSPobject B_matrix;
if (!B_matrix.Create("matrix(1.2, nrow=10, ncol=10)"))
{
cout << "Can not create matrix." << endl;
return 1;
}
/// C_matrix = B_matrix + A_vector.
CSPmatrix C_matrix(B_matrix + A_vector) ;
if (C_matrix == NULL)
{
cout << "Can not create matrix." << endl;
}
*/
/*
CSPnumeric sx;
sx.Create("1:10","x");
CSPnumeric sy = sx * sx;
sy.Assign("y");
engineConnect.SyncParseEval("z<-lm(y~x)");
*/
/*
/// Print C_matrix.
cout << "Print C_matrix = B_matrix + A_vector." << endl << endl;
for (int i = 1; i <= C_matrix.nrow(); ++i)
{
for (int j = 1; j <= C_matrix.ncol(); ++j)
{
cout << (double)C_matrix(i,j) << " "; //< Out to console.
}
cout << endl;
}
cout << endl << endl;
getchar();
/// Call procedure S+.
CSPcharacter returnObj;
/// Create a integer vector with one element
/// representing one argument to pass to function 'objects'
CSPinteger args("1");
/// Create a CSPcall object
CSPcall sCall;
/// Call function S+ (objects()).
cout << "Call function S+ (objects(1))...." << endl << endl;
sCall.Create("objects", args);
/// Call.
returnObj = sCall.Eval(); // objects(1)
if (returnObj == NULL)
{
cout << "Function objects S+ failed" << endl;
return 1;
}
/// Get quantity.
int quantity = returnObj.length();
cout << "Quantity objects : " << quantity << endl;
/// Print objects.
for (i = 0; i < quantity; ++i)
{
cout << " [ " << i+1 << " ] " << (char *)returnObj << endl;
}
*/
/*
//Create the connection to S-PLUS
g_engineConnect.Create( argc, argv);
//Create S object with name "x" in the current database.
//Same as x<-1:10 at the command line.
CSPnumeric sx;
sx.Create("1:10","x");
//Squaring sx, which is the same as S expression sy <- x*x in a local
frame,
//but here we set it to local C++ variable sy.
CSPnumeric sy = sx * sx;
// Assign the result as S object with name "y" in the current
database.
sy.Assign("y");
//Evaluate z<-lm(y~x)
g_engineConnect.SyncParseEval("z<-lm(y~x)");
maybe ------------------- CSPobject Eval ("z<-lm(y~x)");
//second example
CSPevaluator s;
CSPcharacter message("'hello'");
message.Print();
//CSPmatrix M("matrix(1:4,rnow=2)");
//M.Print();
*/
printf("Normal Termination\n");
return 0;
}
C#.NET environment because C++ and C# belong to the same family.
He sent me the code below and asked me to run in C#.NET environment. I
got numerous compiling errors because #include stuff don't exist in
C#! Could anyone tell me if the code below should run in C#.NET?
-------------------------------------------------------------------------------------------------------------------------------
//#define NO_NEWIO
#include <iostream>
/// Include for CONNECT/C++
#include "stdafx.h"
#include "stdafx.cpp"
//#include "sconnect.h"
#include "c:\\data\\Splus61 Includes\\EasyEngineConnect.h"
#include "c:\\data\\Splus61 Includes\\EasyEngineConnect.cpp"
#include "c:\\data\\Splus61 Includes\\EasyCharacter.h"
//#include "c:\\data\\Splus61 Includes\\EasyCharacter.cpp"
#include "c:\\data\\Splus61 Includes\\EasyMatrix.h"
//#include "c:\\data\\Splus61 Includes\\EasyMatrix.cpp"
#include "c:\\data\\Splus61 Includes\\EasyNumeric.h"
//#include "c:\\data\\Splus61 Includes\\EasyNumeric.cpp"
using namespace std;
int main(int argc, char* argv[])
{
// Connect to S+
EasyEngineConnect easyconnect_(argc, argv);
/// Create matrix 9x9 elem.
//EasyMatrix x;
//x.Create("matrix(1:9, nrow=9, ncol=9)", "x");
// Create matrix 9x9 elem.
//EasyMatrix y;
//y.Create("matrix(1:9, nrow=9, ncol=9)", "y");
CSPobject Y;
if (!Y.Create("vector(\"double\",10.6)"))
{
cout << "Can not create vector." << endl;
return 1;
}
CSPobject X1;
if (!X1.Create("vector(\"double\",8.6)"))
{
cout << "Can not create vector." << endl;
return 1;
}
CSPobject X2;
if (!X2.Create("vector(\"double\",3.4)"))
{
cout << "Can not create vector." << endl;
return 1;
}
/// Connect to S+
// CSPengineConnect engineConnect(argc, argv); //EasyEngineConnect
easyconnect_(argc, argv);
/// Create matrix 9x9 elem.
CSPmatrix x;
x.Create("matrix(1:9, nrow=9, ncol=9)", "x");
CSPnumeric sx;
sx.Create("1:10","x");
CSPnumeric sy = sx * sx;
sy.Assign("y");
// engineConnect.SyncParseEval("z<-lm(y~x)");
/*
CSPengineConnect engineConnect(argc, argv);
CSPobject x;
/// Create matrix 24x400 elem.
x.Create("matrix(1:223, nrow=24, ncol=400)");
//x.Create("matrix(1.2, nrow=, ncol=10)");
CSPobject y;
/// Create matrix 24x400 elem.
y.Create("matrix(231:1, nrow=24, ncol=400)");
/// Assign.
x.Assign("x");
y.Assign("y");
/// Call S+ regression "z<-x+y".
int iSuccess = engineConnect.SyncParseEval("z<-x+y");
/// Error.
if (iSuccess == 0)
{
cout << "Error, Can not call \"z<-(x+y)\"" << endl;
return 1;
}
CSPmatrix z(engineConnect.get("z"));
if (!z.IsValid())
{
cout << "Error, Can not search object z" << endl;
}
/// Print.
for (int i = 1; i <= 5;i++); //z.GetNRow(); ++i)
{
for (int j = 1; j <= z.GetNCol(); ++j)
{
cout << (double)z(i, j) << " ";
}
cout << endl;
}
/*
//Create the connection to S-PLUS
CSPengineConnect engineConnect(argc, argv);
/// Create vector 24.
CSPobject A_vector;
if (!A_vector.Create("vector(\"double\",10)"))
{
cout << "Can not create vector." << endl;
return 1;
}
CSPobject B_matrix;
if (!B_matrix.Create("matrix(1.2, nrow=10, ncol=10)"))
{
cout << "Can not create matrix." << endl;
return 1;
}
/// C_matrix = B_matrix + A_vector.
CSPmatrix C_matrix(B_matrix + A_vector) ;
if (C_matrix == NULL)
{
cout << "Can not create matrix." << endl;
}
*/
/*
CSPnumeric sx;
sx.Create("1:10","x");
CSPnumeric sy = sx * sx;
sy.Assign("y");
engineConnect.SyncParseEval("z<-lm(y~x)");
*/
/*
/// Print C_matrix.
cout << "Print C_matrix = B_matrix + A_vector." << endl << endl;
for (int i = 1; i <= C_matrix.nrow(); ++i)
{
for (int j = 1; j <= C_matrix.ncol(); ++j)
{
cout << (double)C_matrix(i,j) << " "; //< Out to console.
}
cout << endl;
}
cout << endl << endl;
getchar();
/// Call procedure S+.
CSPcharacter returnObj;
/// Create a integer vector with one element
/// representing one argument to pass to function 'objects'
CSPinteger args("1");
/// Create a CSPcall object
CSPcall sCall;
/// Call function S+ (objects()).
cout << "Call function S+ (objects(1))...." << endl << endl;
sCall.Create("objects", args);
/// Call.
returnObj = sCall.Eval(); // objects(1)
if (returnObj == NULL)
{
cout << "Function objects S+ failed" << endl;
return 1;
}
/// Get quantity.
int quantity = returnObj.length();
cout << "Quantity objects : " << quantity << endl;
/// Print objects.
for (i = 0; i < quantity; ++i)
{
cout << " [ " << i+1 << " ] " << (char *)returnObj << endl;
}
*/
/*
//Create the connection to S-PLUS
g_engineConnect.Create( argc, argv);
//Create S object with name "x" in the current database.
//Same as x<-1:10 at the command line.
CSPnumeric sx;
sx.Create("1:10","x");
//Squaring sx, which is the same as S expression sy <- x*x in a local
frame,
//but here we set it to local C++ variable sy.
CSPnumeric sy = sx * sx;
// Assign the result as S object with name "y" in the current
database.
sy.Assign("y");
//Evaluate z<-lm(y~x)
g_engineConnect.SyncParseEval("z<-lm(y~x)");
maybe ------------------- CSPobject Eval ("z<-lm(y~x)");
//second example
CSPevaluator s;
CSPcharacter message("'hello'");
message.Print();
//CSPmatrix M("matrix(1:4,rnow=2)");
//M.Print();
*/
printf("Normal Termination\n");
return 0;
}