문제

I'm trying to create a Kakuro with Pascal, the program should get the Kakuro empty (something like this) and return it completed (something like this). I already have loaded the data (from a file) and pass to one 2d array.

The problem I've found is related with the diagonally divided squares, I don't know how I can print this division and a number in each of the sides with Pascal console.

I've tried to use Pascal graphic libraries, but the alghoritm should run on several computers with diferent compilers and Pascal has not unified libraries, only crt and don't help with this (or I can't find it).

Also I try something like ASCII, creatings grid with -- and ’|` but when I print the values with two digits deform all the output, the code is this:

for c := 1 to maxc do
begin
  for f := 1 to maxf do
  begin
    WriteLn('+---+');
    WriteLn('|\',tablero[v,f,c],'|');
    WriteLn('| \ |');
    WriteLn('|', tablero[h,f,c], '  \|');
    WriteLn('+---+');
  end;
  WriteLn();
end;

And the problem in the output you can see this:

+---+
|\-1|
| \ |
|23 \|
+---+
+---+
|\0|
| \ |
|0 \|
+---+

I thought of creating another 2d array inside my 2d array but if I do that I get something like:

+---+
|   |
|---|
|   | 
+---+

Divided by half, and it need to be done diagonally, so doesn't work very well either.

도움이 되었습니까?

해결책

Use the MinWidth specifier for your integer values.

WriteLn(intValue:2);   // displays the number with a width of 2

For floating point values, you can also add a value for the number of decimals:

WriteLn(floatVal:5:2); 

You can find documentation for the specifier in System.Write - it's Delphi documentation, but Delphi had its roots long ago in Turbo Pascal, and Write/WriteLn are legacy Pascal functions and haven't changed.

A write parameter has the form:

OutExpr [: MinWidth [: DecPlaces ] ]

where OutExpr is an output expression.

MinWidth and DecPlaces are type integer expressions:

MinWidth specifies the minimum field width, which must be greater than 0. Exactly MinWidth characters are written (using leading blanks if necessary) except when OutExpr has a value that must be represented in more than MinWidth characters. In that case, enough characters are written to represent the value of OutExpr. Likewise, if MinWidth is omitted, then the necessary number of characters is written to represent the value of OutExpr.

DecPlaces specifies the number of decimal places in a fixed-point representation of one of the Real types. It can be specified only if OutExpr is one of the Real types, and if MinWidth is also specified. When MinWidth is specified, it must be greater than or equal to 0.

Here's a quick sample:

NT = 12;
NB = -1;

WriteLn('+---+');         
WriteLn('|\', NT:2, '|'); 
WriteLn('| \ |');         
WriteLn('|', NB:2, '\|'); 
WriteLn('+---+');         
ReadLn;

This produces

+---+
|\12|
| \ |
|-1\|
+---+

If you need more output than two wide, you'll need to enlarge your box:

+-----+
|\ 120|
| \   |
|  \  |
|   \ |
|123 \|
+-----+
라이센스 : CC-BY-SA ~와 함께 속성
제휴하지 않습니다 StackOverflow
scroll top