用C 语言或C++写离散数学有关程序

用C 语言或C++写离散数学有关程序,第1张

我这学期正在学习数据结构呵呵

如果你学过数据结构的话写的话应该不难.

数组法--对应 顺序表

链表法--可以用 单链表(其它链表也可)

位串法这个不太清楚 - -!!

我只说下思想已∩为例:已知A和B,求C=A∩B

1.建表(A和B)

2.你可以你可以每次取A中的一个元素与B中的元素逐个比较,如果相同则保存在C中,

知道A中元素比较完

写了一个类。。。。

==========调用方法

Private Sub Command1_Click()

Dim sElement As String

sElement = "{ a , b, c , d , e , f }"

Dim sRelate As String

sRelate = "{ <a ,b >, <a , c >, <b, c >, <d , c >, <e , c >, <e , d >, <e , e >} "

sElement = Replace(sElement, " ", "")

sRelate = Replace(sRelate, " ", "")

Dim vMatrix As Variant

Dim oRM As New RMAdapter

vMatrix = oRM.R2M(sElement, sRelate)

End Sub

RMAdapter.cls======================================

Option Explicit

Private mMatrix As Variant

Private mElement As Variant

Private mRelation As Variant

Public Function R2M(ByVal psElement As String, ByVal psRelation As String) As Variant

Dim bRetAs Boolean

mElement = Element2Array(psElement)

Call printElement

mRelation = Relation2Array(psRelation)

Call printRelation

Call InitMatrix

Call FillMatrix

Call PrintMatrix

ExitHere:

R2M = mMatrix

Exit Function

End Function

Private Sub InitMatrix()

Dim i As Integer

Dim j As Integer

ReDim mMatrix(LBound(mElement) To UBound(mElement), LBound(mElement) To UBound(mElement)) As Variant

For i = LBound(mElement) To UBound(mElement)

For j = LBound(mElement) To UBound(mElement)

mMatrix(i, j) = 0

Next j

Next i

End Sub

Private Function Element2Array(ByVal psElement As String) As Variant

Dim vElement As Variant

Dim sElement As String

sElement = Mid(psElement, 2, Len(psElement) - 2)

Debug.Print sElement

vElement = Split(sElement, ",")

Element2Array = vElement

End Function

Private Function Relation2Array(ByVal psRelation As String) As Variant

Dim vRelation As Variant

Dim sRelation As String

sRelation = Mid(psRelation, 3, Len(psRelation) - 4)

Debug.Print sRelation

vRelation = Split(sRelation, ">,<")

Relation2Array = vRelation

End Function

Private Sub printElement()

Dim i As Integer

Dim s As String

Debug.Print "totally element= " &(UBound(mElement) + 1)

For i = LBound(mElement) To UBound(mElement)

Debug.Print i &" " &mElement(i)

Next i

Debug.Print s

End Sub

Private Sub printRelation()

Dim i As Integer

Dim s As String

Debug.Print "totally relation= " &(UBound(mRelation) + 1)

For i = LBound(mRelation) To UBound(mRelation)

Debug.Print i &" " &mRelation(i)

Next i

End Sub

Private Sub PrintMatrix()

Dim i As Integer

Dim j As Integer

Dim s As String

Debug.Print "print matrix"

For i = LBound(mElement) To UBound(mElement)

s = ""

For j = LBound(mElement) To UBound(mElement)

s = s &mMatrix(i, j) &" "

Next

Debug.Print s

Next

End Sub

Private Sub FillMatrix()

Dim i As Long

Dim sElement1 As String

Dim sElement2 As String

Dim sRelation As String

Dim vSAs Variant

Dim pos1 As Integer

Dim pos2 As Integer

For i = LBound(mRelation) To UBound(mRelation)

sRelation = mRelation(i)

vS = Split(sRelation, ",")

sElement1 = vS(0)

sElement2 = vS(1)

Debug.Print "1=" &sElement1

Debug.Print "2=" &sElement2

pos1 = FindPosInElement(sElement1)

pos2 = FindPosInElement(sElement2)

If pos1 <>-1 And pos2 <>-1 Then

mMatrix(pos1, pos2) = 1

Else

MsgBox "error found =" &sRelation

End If

Next i

End Sub

Private Function FindPosInElement(ByVal psElement As String) As Integer

Dim i As Integer

Dim bfound As Boolean

For i = LBound(mElement) To UBound(mElement)

If psElement = mElement(i) Then

bfound = True

Exit For

End If

Next i

If bfound Then

FindPosInElement = i

Else

FindPosInElement = -1

End If

End Function

结果=============================

print matrix

0 1 1 0 0 0

0 0 1 0 0 0

0 0 0 0 0 0

0 0 1 0 0 0

0 0 1 1 1 0

0 0 0 0 0 0

F2803离散数学与程序设计是计算机专业

离散数学是为计算机专业量身打造的一门数学,极大地突出了计算机专业的逻辑性、条理性、抽象性,其实不在于知识本身,为编程提供了良好的理论基础和解决问题的一般条件。

由于数字电子计算机是一个离散结构,它只能处理离散的或离散化了的数量关系, 因此,无论计算机科学本身,还是与计算机科学及其应用密切相关的现代科学研究领域,都面临着如何对离散结构建立相应的数学模型。

学科内容

集合论部分:集合及其运算、二元关系与函数、自然数及自然数集、集合的基数。

图论部分:图的基本概念、欧拉图与哈密顿图、树、图的矩阵表示、平面图、图着色、支配集、覆盖集、独立集与匹配、带权图及其应用。

代数结构部分:代数系统的基本概念、半群与独异点、群、环与域、格与布尔代数。

组合数学部分:组合存在性定理、基本的计数公式、组合计数方法、组合计数定理。


欢迎分享,转载请注明来源:内存溢出

原文地址: http://outofmemory.cn/yw/11058761.html

(0)
打赏 微信扫一扫 微信扫一扫 支付宝扫一扫 支付宝扫一扫
上一篇 2023-05-13
下一篇 2023-05-13

发表评论

登录后才能评论

评论列表(0条)

保存