1/* BEGIN LICENSE BLOCK 2 * Version: CMPL 1.1 3 * 4 * The contents of this file are subject to the Cisco-style Mozilla Public 5 * License Version 1.1 (the "License"); you may not use this file except 6 * in compliance with the License. You may obtain a copy of the License 7 * at www.eclipse-clp.org/license. 8 * 9 * Software distributed under the License is distributed on an "AS IS" 10 * basis, WITHOUT WARRANTY OF ANY KIND, either express or implied. See 11 * the License for the specific language governing rights and limitations 12 * under the License. 13 * 14 * The Original Code is The ECLiPSe Constraint Logic Programming System. 15 * The Initial Developer of the Original Code is Cisco Systems, Inc. 16 * Portions created by the Initial Developer are 17 * Copyright (C) 1997-2006 Cisco Systems, Inc. All Rights Reserved. 18 * 19 * Contributor(s): 20 * 21 * END LICENSE BLOCK */ 22 23/* 24 * ECLiPSe Application Example 25 * 26 * $Id: eg_c_sendmore.c,v 1.1 2008/06/30 17:43:54 jschimpf Exp $ 27 * 28 * IDENTIFICATION: eg_sendmore.c 29 * 30 * AUTHOR: Joachim Schimpf 31 * AUTHOR: Stefano Novello 32 * 33 * DESCRIPTION: 34 * Example of solving a constraint problem from C 35 */ 36 37#include "eclipse.h" 38 39#define NVARS 8 40 41main() 42{ 43 dident plus,times; 44 ec_refs Vars; 45 pword varlist; 46 int i, res; 47 48 ec_init(); 49 plus = ec_did("+",2); 50 times = ec_did("*",2); 51 ec_exec_string("lib(fd)",0); 52 53 Vars = ec_refs_create_newvars(NVARS); 54 varlist = ec_listofrefs(Vars); 55 ec_post_goal( 56 ec_term(ec_did("::",2), 57 varlist, ec_term(ec_did("..",2), ec_long(0), ec_long(9))) 58 ); 59 ec_post_goal( 60 ec_term(ec_did("alldistinct",1), varlist) 61 ); 62 ec_post_goal( 63 ec_term(ec_did("##",2), ec_refs_get(Vars,0), ec_long(0)) 64 ); 65 ec_post_goal( 66 ec_term(ec_did("##",2), ec_refs_get(Vars,4), ec_long(0)) 67 ); 68 ec_post_goal( 69 ec_term(ec_did("#=",2), 70 ec_term(plus, 71 ec_term(times, ec_long(1000), ec_refs_get(Vars,0)), 72 ec_term(plus, 73 ec_term(times, ec_long(100), ec_refs_get(Vars,1)), 74 ec_term(plus, 75 ec_term(times, ec_long(10), ec_refs_get(Vars,2)), 76 ec_term(plus, 77 ec_refs_get(Vars,3), 78 ec_term(plus, 79 ec_term(times, ec_long(1000), ec_refs_get(Vars,4)), 80 ec_term(plus, 81 ec_term(times, ec_long(100), ec_refs_get(Vars,5)), 82 ec_term(plus, 83 ec_term(times, ec_long(10), ec_refs_get(Vars,6)), 84 ec_refs_get(Vars,1) 85 ))))))), 86 ec_term(plus, 87 ec_term(times, ec_long(10000), ec_refs_get(Vars,4)), 88 ec_term(plus, 89 ec_term(times, ec_long(1000), ec_refs_get(Vars,5)), 90 ec_term(plus, 91 ec_term(times, ec_long(100), ec_refs_get(Vars,2)), 92 ec_term(plus, 93 ec_term(times, ec_long(10), ec_refs_get(Vars,1)), 94 ec_refs_get(Vars,7) 95 )))) 96 ) 97 ); 98 ec_post_goal( 99 ec_term(ec_did("labeling",1), varlist) 100 ); 101 102 res = ec_resume1(0); /* solve */ 103 104 if (res == PSUCCEED) /* print solution */ 105 { 106 for (i=0; i<NVARS; i++) 107 { 108 long sol; 109 res = ec_get_long(ec_refs_get(Vars,i), &sol); 110 if (res == PSUCCEED) 111 printf("X%d = %d\n", i, sol); 112 else 113 printf("X%d = ?\n"); 114 } 115 } 116 else printf("No solution\n"); 117 118 ec_refs_destroy(Vars); 119 ec_cleanup(); 120} 121 122