C/C++: Syntactic Sugar for If Statement

A standard If Statement in C/C++ can become an one line statement by using the following structure

variable = (STATEMENT)?(RETURN VALUE IF TRUE):(RETURN VALUE IF FALSE)

which is the same as

if (STATEMENT){
  variable = (RETURN VALUE IF TRUE);
}else{
  variable = (RETURN VALUE IF FALSE);
}


C/C++: Capture stdout from a system() command

	FILE *results;
	int queen;
	if (!(results = popen("./precosat -f nqueens.cnf | tail -n +2 | head -n -1 | tr -d v | tr ' ' '\n' | grep -v '^$' | grep ^[0-9]", "r"))) {
		cout << "Unable to Execute Precosat" << endl << "./precosat -f nqueens.cnf | tail -n +2 | head -n -1 | tr -d v | tr ' ' '\n' | grep -v '^$' | grep ^[0-9]" << endl;
		exit(1);
	}
	for (i = 0; i < queens; i++) {
		fscanf(results,"%d",&queen);
		cout << "Queen #" << i+1 << " goes to [" << ((queen%queens)?(queen%queens):queens) << "]" << endl;
	}
	pclose(results);

Above, you will find an example that was used to extract data from the PrecoSAT SAT Solver in order to solve the N-Queens problem. It returns all positive variables, one per line.

You can see that after the call to popen, it creates a pointer to a file stream that can be used as a regular file to read the data from the called process.


Bash: grep: A couple of usefull applications

(How) to delete all empty lines from a file:

cat someFile | grep -v '^$'

(How) to get all numbers that have no sign in front of them (all positive numbers, from a file where on each line there is one number only):

cat someFile | grep ^[0-9]

(How) to extract all negative numbers (from a file where on each line there is one number only):
You can of course replace the minus ‘-‘ character with any other you want to use as a starting character for a line.

cat someFile | grep ^-


Bash: tr: A couple of usefull applications

(How) to delete all instances of a character from a file:
The following example deletes all instances of the character ‘v’

cat someFile | tr -d v

(How) to replace all instances of a character with another:
The following example replaces all space characters with the new line character

cat someFile | tr ' ' '\n'


Bash: head: Remove last N lines from file 3

head -n -$N inputFile > outputFile

Where $N is the number of the line from the end which you want to stop printing.
For example if you want to delete the last line you must issue the following:

head -n -1 inputFile > outputFile

Bash: tail: Remove first N lines from file

tail -n +$N inputFile > outputFile

Where $N is the number of the line which you want to start printing from.
For example if you want to delete only the first line / start printing from the second line you must issue the following:

tail -n +2 inputFile > outputFile


Ubuntu: How to compile PrecoSAT v.570 under ubuntu 11.10

By downloading PrecoSAT from http://fmv.jku.at/precosat/ and trying to install it on Ubuntu 11.10 32-bit (Oneiric Ocelot) by:

    • Decompressing the files
    • and calling ./configure && make

I got the following error at make:

g++ -O3 -m32 -static -Wall -Wextra -DNDEBUG -DNLOGPRECO -DNSTATSPRECO  -c precomain.cc
In file included from precomain.cc:23:0:
precosat.hh:164:31: error: ‘ptrdiff_t’ has not been declared
precosat.hh:270:13: error: ‘ptrdiff_t’ has not been declared
make: *** [precomain.o] Error 1

which can be resolved by adding

#include <cstddef>

at the includes in the file precosat.hh


Currency Signs

Euro sign “€”
Dollar sign “$”
Pound sign “£”


Java: error: Double.MIN_NORMAL not found

To fix the error of Double.MIN_NORMAL not defined, replace it with

Double.longBitsToDouble(0x0010000000000000L)

Double.MIN_NORMAL is a constant holding the smallest positive normal value of type double, 2-1022.