Subject: | how can i change this program to c++? |
hi
excuse me i want to write the powerset program in c++ sure a ((recursive)) program
and i have a program to produse powerset but i don't know it is in which language!!
but i want to have a c++ program
\thanks
bye
proc subsets { list } {
# Check list size
if { [llength $list] > 32 } {
error "You don't really expect to list all 2**[llength $list] subsets, do you?"
}
# Calculate total number of subsets
set n [expr { 1 << [llength $list] }]
# Enumerate the subsets. If the list is { a b c }, then element
# 0 is the empty set, 1 is the set { a }, 2 is the set { b },
# 3 is { a b }, 4 is { c }, 5 is { a c }, 6 is { b c }, and 7
# is { a b c }.
for { set i 0 } { $i < $n } { incr i } {
set subset {}
for { set j 0 } { $j < [llength $list] } { incr j } {
if { $i & ( 1 << $j ) } {
lappend subset [lindex $list $j]
}
}
# Markus asks for subsets to be sorted out by size. Collate
# them into arrays.
lappend subsets([llength $subset]) $subset
}
set retval {}
for { set k 0 } { $k <= [llength $list] } { incr k } {
lappend retval $subsets($k)
}
return $retval
}
proc subsets {l} {
set subsets [list [list]]
foreach e $l {
foreach subset $subsets {
lappend subsets [lappend subset $e]
}
}
return $subsets
}
puts [join [subsets { a b c d }] \n]
proc subsets2 { list size } {
if { $size == 0 } {
return [list [list]]
}
set retval {}
for { set i 0 } { ($i + $size) <= [llength $list] } { incr i } {
set firstElement [lindex $list $i]
set remainingElements [lrange $list [expr { $i + 1 }] end]
foreach subset [subsets2 $remainingElements [expr { $size - 1 }]] {
lappend retval [linsert $subset 0 $firstElement]
}
}
return $retval
}
for { set i 0 } { $i <= 4 } { incr i } {
puts [subsets2 {a b c d} $i]
}
proc subsets2b {l n} {
set subsets [list [list]]
set result [list]
foreach e $l {
foreach subset $subsets {
lappend subset $e
if {[llength $subset] == $n} {
lappend result $subset
} else {
lappend subsets $subset
}
}
}
return $result
}
proc subsets2c {myList size {prefix {}}} {
;# End recursion when size is 0 or equals our list size
if {$size == 0} {return [list $prefix]}
if {$size == [llength $myList]} {return [list [concat $prefix $myList]]}
set first [lindex $myList 0]
set rest [lrange $myList 1 end]
;# Combine solutions w/ first element and solutions w/o first element
set ans1 [subsets2c $rest [expr {$size-1}] [concat $prefix $first]]
set ans2 [subsets2c $rest $size $prefix]
return [concat $ans1 $ans2]
}
Minimize x subject to constraints; maximize x subject to constraints;
minimize y subject to constraints; maximize y subject to constraints;
minimize z subject to constraints; maximize z subject to constraints.
proc pairs L {
set res {}
foreach i $L {
foreach j $L {
if {$i != $j} {lappend res $i-$j}
}
}
set res
}