AHelianthuss 2014-07-05 05:30 采纳率: 0%
浏览 3945

如何讲一段C++代码放入C#的windows窗体应用程序中做可视化界面

光纤铺设问题 我想用c#做张地图在上面画出最优铺设线路,C#画图我会,C++部分代码也完成了 我只是需要如何把C++代码放入C#中 。。。。不知道我的表达清不清晰
#include
#include
#include
#include
#include
#include
#define STATUS int
#define OK 1
#define ERROR 0
#define INFINITY INT_MAX
#define VERTEX_NUM 13
char a[VERTEX_NUM][20]={"信息楼","网络中心","图书馆","理学院","国际交流学院","体育馆","采矿馆","冶金馆","大成教学馆","综合楼","逸夫楼","何世礼教学馆","汉卿会堂"};
//建筑数组
typedef struct ArcCell
{
int distance;
bool pushe;//是否铺设的bool型变量
}ArcCell,DisMatrix[VERTEX_NUM][VERTEX_NUM];

typedef struct
{
char* name;
}VertexType;

typedef struct
{
VertexType vexs[VERTEX_NUM];
DisMatrix dist;
int vexnum;
}MGraph;//图结构

typedef struct
{
int adj;

}NODE;//堆排序数组

NODE Heap[25];
typedef struct UFSet
{
int x;
int y;
}UFSet;//并查集结构体

typedef struct
{
string name;
double x,y;
}Building;

UFSet set[25];
int rank[VERTEX_NUM];
int father[VERTEX_NUM];
STATUS ShowAllGraph(MGraph &G);//显示所有线路分布
STATUS CreateGraph(MGraph &G);//创建校园节点图
STATUS MiniSpanTree_KRUSCAL2(MGraph &G);//最小生成树KR2
void heapsort(int n);//排序
void Heapadjust(int i, int n);//调节大顶堆
void MakeSet(int x);//初始化并查集
int Find_set(int x);//查找父节点
void Uion_set(int x, int y);
void showMap(MGraph &G);
int menu();
STATUS ShowDist(int a,int b,MGraph &G)
{
printf("%s--%s(%d)\n",G.vexs[a],G.vexs[b],G.dist[a][b].distance);
return OK;
}
STATUS ShowAllGraph(MGraph &G)//显示所有节点及线路
{
int i,j;
for(i=0;i {
for (j=i+1;j {
if (G.dist[i][j].distance!=INFINITY) ShowDist(i,j,G);
}
}
return OK;
}
void heapsort(int n)//排序
{
int i,e;
for(i=(n-1)/2;i>=0;--i)
{
Heapadjust(i,n);
}

for(i=n-1;i>=1;--i)
{
e=Heap[0].adj;
Heap[0].adj=Heap[i].adj;
Heap[i].adj=e;
Heapadjust(0,i-1);
}
}
void Heapadjust(int s, int m)//调节大顶堆
{
int j;
int e=Heap[s].adj;
for(j=2*s;j<=m;j*=2)
{
if(j ++j;
if(e>=Heap[j].adj)
break;
Heap[s].adj=Heap[j].adj;
s=j;
}
Heap[s].adj=e;
}
void MakeSet(int x)//初始化并查集
{
father[x]=x;
rank[x]=0;
}
int Find_set(int x)//查找父节点
{
if (x != father[x])
{
father[x] = Find_set(father[x]);
}
return father[x];
}
void Uion_set(int x, int y)
{

if (x == y) return;
if (rank[x] > rank[y])
{
father[y] = x;
}
else
{
if (rank[x] == rank[y])
{
rank[y]++;
}
father[x] = y;
}
}

STATUS CreateGraph(MGraph &G)//创建一个图
{
int i,j,k;
G.vexnum =VERTEX_NUM;
for( i=0;i<VERTEX_NUM;i++)
{
G.vexs[i].name=a[i];
}

for(i=0;i<VERTEX_NUM;i++)
{
for (j=0;j<VERTEX_NUM;j++)
{
G.dist[i][j].distance=INFINITY;
G.dist[i][j].pushe=false;
}
}
G.dist[0][1].distance=103;
G.dist[0][2].distance=113;
G.dist[0][4].distance=170;
G.dist[1][2].distance=115;
G.dist[1][3].distance=128;
G.dist[2][3].distance=105;
G.dist[2][4].distance=154;
G.dist[2][6].distance=165;
G.dist[2][7].distance=288;
G.dist[2][9].distance=271;
G.dist[3][9].distance=240;
G.dist[3][11].distance=247;
G.dist[3][12].distance=229;
G.dist[4][5].distance=159;
G.dist[4][6].distance=97;
G.dist[5][6].distance=182;
G.dist[6][7].distance=151;
G.dist[7][8].distance=156;
G.dist[7][9].distance=173;
G.dist[8][9].distance=170;
G.dist[8][10].distance=354;
G.dist[9][10].distance=224;
G.dist[9][11].distance=153;
G.dist[10][11].distance=73;
G.dist[11][12].distance=267;
for(i=0;i<VERTEX_NUM;i++)
{
for (j=i+1;j<VERTEX_NUM;j++)
{
if (G.dist[i][j].distance!=INFINITY) G.dist[j][i].distance=G.dist[i][j].distance;
}
}
return OK;
}
STATUS MiniSpanTree_KRUSCAL2(MGraph &G)//最小生成树KR2
{
int i,j,k,m,n,x,y,z,a,b,c,d;
k=0;
m=0;
z=0;
for(i=0;i<G.vexnum;i++)
{
for(j=i;j<G.vexnum;j++)
{
if(G.dist[i][j].distance!=INFINITY)
{
Heap[k].adj=G.dist[i][j].distance;
k++;
}
}
}
for(i=0;i<G.vexnum;i++)
{
for(j=i;j<G.vexnum;j++)
{
if(G.dist[i][j].distance!=INFINITY)
{
set[z].x=i;
set[z].y=j;
z++;
}
}
}
for(a=0;a<G.vexnum;a++)
{
MakeSet(a);
}
heapsort(k);
while(m<25)
{
for(i=0;i<G.vexnum;i++)
{
for(j=i;j<G.vexnum;j++)
{
if(Heap[m].adj==G.dist[i][j].distance)
{
x=i;
y=j;
d=0;
while(d<25)

{
if(set[d].x==x&&set[d].y==y)
{
b=Find_set(set[d].x);
c=Find_set(set[d].y);
if(b==c)
{
Heap[m].adj=0;
}
else
{
Uion_set(b, c);
}
}
d++;
}
m++;
}
}
}
}
heapsort(k);
return 0;
}

void showMap(MGraph &G)
{

int i,j,k,x,y;
k=0;

while(Heap[k].adj==0)
{
k++;
}
while(k<25)
{
for(i=0;i<G.vexnum;i++)
{
for(j=i;j<G.vexnum;j++)
{
if(Heap[k].adj==G.dist[i][j].distance)
{
x=i;
y=j;
printf("%s--%s(%d)",G.vexs[x].name,G.vexs[y].name,Heap[k].adj);
printf("\n");
k++;
}
}
}
}
}

int menu()
{
char i;
printf(" ***************************************************************\n");
printf(" ***************************************************************\n");
printf(" 1.显示东大楼距 \n");
printf(" 2.克鲁斯卡尔二 \n");
printf(" 3.退出 \n");
printf(" ***************************************************************\n");
printf(" ***************************************************************\n");

return 0;

}
int main()
{
MGraph G;
int i;
CreateGraph(G);
while(1)
{
menu();
printf("Please press[1,2,3]to chose:");
scanf_s("%d",&i);
switch(i)
{
case 1:
ShowAllGraph(G);
break;
case 2:
MiniSpanTree_KRUSCAL2(G);
showMap(G);
break;
case 3:
return 0;
}
}
}

  • 写回答

1条回答

  • ChatGPTᴼᴾᴱᴺᴬᴵ 2023-03-16 12:16
    关注

    该回答引用ChatGPT

    C++代码和C#代码并不直接兼容,它们是不同的编程语言。如果你想在 C# 的 Windows 窗体应用程序中使用你的 C++ 代码,你需要使用一些方法将这两种语言组合在一起。


    一种方法是使用 C++/CLI。这是一种混合语言,它可以使用 C++ 编写代码,同时还支持使用 .NET 框架。你可以将你的 C++ 代码编译为一个 DLL 文件,然后在 C# 项目中使用它。下面是一个简单的示例:

    • 首先,创建一个 C++/CLI 项目,并将你的 C++ 代码添加到其中。

    • 将 C++ 代码编译为 DLL 文件。

    • 在 C# 项目中添加对该 DLL 的引用。

    • 使用 "using" 关键字引用 C++ 命名空间和类,并调用 C++ 函数。

    另一种方法是使用 P/Invoke。这是一种 C# 的功能,它允许你调用 C++ DLL 中的函数。下面是一个简单的示例:

    • 将 C++ 代码编译为 DLL 文件。

    • 在 C# 项目中使用 "DllImport" 属性声明 C++ DLL 中的函数。

    • 调用声明的函数。

    无论使用哪种方法,你都需要了解你的 C++ 代码的参数和返回值类型,以便在 C# 中正确使用它。此外,你还需要了解如何处理可能的异常或错误,以确保你的应用程序能够正确运行。


    以下是一个使用 P/Invoke 调用 C++ DLL 的示例:

    在 C++ 中,你可以使用以下代码定义一个简单的函数:

    extern "C" __declspec(dllexport) int Add(int a, int b)
    {
        return a + b;
    }
    

    在 C# 中,你可以使用以下代码来调用它:

    using System.Runtime.InteropServices;
    
    class Program
    {
        [DllImport("YourCppDll.dll")]
        public static extern int Add(int a, int b);
    
        static void Main(string[] args)
        {
            int result = Add(1, 2);
            Console.WriteLine(result); // 输出 3
        }
    }
    

    你需要将 YourCppDll.dll 替换为你的 DLL 文件的名称。在上面的代码中,我们使用 DllImport 属性声明了 Add 函数,并在 Main 方法中调用了它。

    评论

报告相同问题?

悬赏问题

  • ¥15 Matlab怎么求解含参的二重积分?
  • ¥15 苹果手机突然连不上wifi了?
  • ¥15 cgictest.cgi文件无法访问
  • ¥20 删除和修改功能无法调用
  • ¥15 kafka topic 所有分副本数修改
  • ¥15 小程序中fit格式等运动数据文件怎样实现可视化?(包含心率信息))
  • ¥15 如何利用mmdetection3d中的get_flops.py文件计算fcos3d方法的flops?
  • ¥40 串口调试助手打开串口后,keil5的代码就停止了
  • ¥15 电脑最近经常蓝屏,求大家看看哪的问题
  • ¥60 高价有偿求java辅导。工程量较大,价格你定,联系确定辅导后将采纳你的答案。希望能给出完整详细代码,并能解释回答我关于代码的疑问疑问,代码要求如下,联系我会发文档