我正在开发一个程序,将z3表达式转换为qdimcas格式。下面的代码将qdimacs格式的代码打印到一个文件中。
在这里,每次调用函数时都会修改变量clause_count。有没有一种方法可以只使用或只打印clause_count的最终值?
#include<iostream>
#include "z3++.h"
#include<string>
#include<fstream>
using namespace z3;
using namespace std;
int dimacs(int t, string oprt, int arg_num,int element[],int variables)
{ int static clause_count = 0;
int static check =0;
std::cout<<"Value of T is: "<<t<<" operator is: "<<oprt<<" Nuumber of arguments: "<<arg_num<<endl;
if(arg_num==2)
std::cout<<element[0]<<" "<<element[1]<<endl;
else
std::cout<<element[0]<<endl;
ofstream myfile;
myfile.open("contents.txt",ios::app);
myfile.clear();
if(check == 0)
{
myfile<<"p cnf \n";
check++;
}
if(arg_num == 2)
{ clause_count+=3;
if(oprt.compare("and")==0)
{
myfile<<"-"<<t<<" "<<element[0]<<" 0 \n";
myfile<<"-"<<t<<" "<<element[1]<<" 0 \n";
myfile<<"-"<<element[0]<<" -"<<element[1]<<" "<<t<<" 0 \n";
myfile.close();
std::cout<<"printing done for AND.\n";
}
else if(oprt.compare("or")==0)
{
myfile<<t<<" -"<<element[0]<<" 0 \n";
myfile<<t<<" -"<<element[1]<<" 0 \n";
myfile<<"-"<<t<<" "<<element[0]<<" "<<element[1]<<" 0 \n";
myfile.close();
std::cout<<"printing done for OR.\n";
}
else if(oprt.compare("=>")==0)
{
myfile<<"-"<<t<<" -"<<element[0]<<" 0 \n";
myfile<<"-"<<t<<" "<<element[1]<<" 0\n";
myfile<<"-"<<element[1]<<" -"<<t<<" "<<element[0]<<" 0 \n";
myfile.close();
std::cout<<"printing done for implies.\n";
}
}
else if(arg_num == 1)
{ clause_count+=2;
if(oprt.compare("not")==0)
{
myfile<<"-"<<t<<" -"<<element[0]<<" 0\n";
myfile<<t<<" "<<element[0]<<" 0 \n";
myfile.close();
std::cout<<"printing done for NOT.\n";
}
}
std::cout<<clause_count<<endl;
std::cout<<variables<<endl;
}
我基本上只需要存储clause_count的最终值并将其传递给另一个函数。
实际上有另一个文件调用此函数,该文件运行递归。通过递归调用,这个dimacs函数被调用,并通过它传递参数。最后,无论何时调用此函数,dimacs文件都会打印输出,输出应如下所示:
p cnf 3 4
1 2 0
2 1 0
2 4 0
4 5 0
这里有行"p cnf“有两个值,即。3和4,其中4必须存储在我的程序的变量clause_count中。但是由于递归的原因,我打印了clause_count的每个值。我只需要最终的值。
发布于 2019-06-21 09:47:13
由于您使用的是静态变量(基本上是全局变量,仅在dimacs中可见),我建议将静态clause_count声明移到函数外部的全局变量声明中。
https://stackoverflow.com/questions/56700370
复制相似问题