Welcome to Dream.In.Code
Getting Help is Easy!

Join 132,439 Programmers for FREE! Get instant access to thousands of experts, tutorials, code snippets, and more! There are 1,460 people online right now. Registration is fast and FREE... Join Now!




Your 48 hours of code.

 
Reply to this topicStart new topic

Your 48 hours of code.

snoj
post 14 Aug, 2005 - 07:17 PM
Post #1


$Null

Group Icon
Joined: 31 Mar, 2003
Posts: 3,304



Thanked 5 times

Dream Kudos: 700
My Contributions


Alright, post your code from the contest if you'd like.


I only did problem 1 because #1, it was the only one I understood right away. And #2, I thought I could do it quickly. So without further ramblings by me....
CODE
<?php
$start = microtime(TRUE);
chdir(dirname(__FILE__));
$pal = file('./prob1.dat');

$alph = '('.implode('|',array_merge(range('a', 'z'), range('A', 'Z'))).')';

$fh = fopen('./hotsnoj_prob1.out', 'w');

for($i = 0, $a = 0; $i < count($pal); $i++, $a++) {
    $tmp_pal = $tmp2_pal = preg_grep($alph, str_split($pal[$i], 1));
    $len = count($tmp_pal);
    
    $op = ($len%2 == 0) ? ' - perfect' : ' - odd';
    if(strtolower(implode('', $tmp_pal)) == strtolower(implode('', array_reverse($tmp_pal)))) {
 fwrite($fh, str_replace(array("\r", "\n"), '', $pal[$i].$op)."\r\n");
    }
}

fclose($fh);
?>

(Obviously I left some old code in there.)
User is offlineProfile CardPM

Go to the top of the page

Acid Reign
post 14 Aug, 2005 - 08:09 PM
Post #2


Touched by his noodly appendage

*****
Joined: 11 Nov, 2002
Posts: 2,123



Dream Kudos: 2
My Contributions


CODE
<?
    $filename = "prob1.dat";
    $strings = file($filename);
    
    $output = fopen("acid reign_prob1.out",'a');
    
    foreach($strings as $index => $value){
 $value = rtrim($value);
 $forward = strtolower(ereg_replace("[^A-Za-z0-9]", "", $value));
 $reversed = strrev($forward);
 
 if($forward == $reversed){
     if(strlen($forward) % 2 == 1){
   fputs($output, $value . " - odd
");
     } else {
   fputs($output, $value . " - perfect
");
     }
 }
    }
    fclose($output);
 
?>


was mine.

nearly the same thing
User is offlineProfile CardPM

Go to the top of the page

lattyware
post 14 Aug, 2005 - 11:14 PM
Post #3


D.I.C Head

Group Icon
Joined: 8 Aug, 2005
Posts: 55



Dream Kudos: 75
My Contributions


#1:
CODE

Type Phrase
    Field Orig$
    Field NewV$
    Field Leng
    Field Tipe$
    Field NPli
End Type

ARead = ReadFile("Prob1.dat")

While Not Eof(ARead)

    Test.Phrase = New Phrase
 Test\Orig$ = ReadLine(ARead)
    
 Temp$=Lower$(Test\Orig$)

 For a = 32 To 47
     Temp$=Replace$(Temp$,Chr$(a),"")
 Next
 
 For a = 58 To 64
     Temp$=Replace$(Temp$,Chr$(a),"")
 Next
 
 For a = 91 To 96
     Temp$=Replace$(Temp$,Chr$(a),"")
 Next
 
 For a = 123 To 126
     Temp$=Replace$(Temp$,Chr$(a),"")
 Next
    
 Test\NewV$ = Temp$
 Test\Leng = Len(Test\NewV$)
 
    If Len(Test\NewV$) Mod 2 = 0 Then
 Test\Tipe$ = "perfect"
    Else
 Test\Tipe$ = "odd"
    EndIf
    
    If Test\Tipe$ = "odd" Then
 For a = 1 To Test\Leng/2
     If Not Mid(Test\NewV$,a,1) = Mid(Test\NewV$,Test\Leng-a+1,1) Then
   Test\NPli=True
     EndIf
 Next
    EndIf
    
    If Test\Tipe$ = "perfect" Then    
 For a = 1 To Floor(Test\Leng/2)
     If Not Mid(Test\NewV$,a,1) = Mid(Test\NewV$,Test\Leng-a+1,1) Then
   Test\NPli=True
     EndIf
 Next
    EndIf    
    
Wend

AWrite = WriteFile("Lattyware_prob1.out")

For Output.Phrase = Each Phrase
    If Output\Npli = False Then
 WriteLine(AWrite,Output\Orig$ + " - " + Output\Tipe$)
    EndIf
Next

End


#4:
CODE

ARead = ReadFile("Prob4.dat")
AWrite = WriteFile("Lattyware_prob4.out")

While Not Eof(ARead)

Size$=ReadLine(ARead)

While Not Temp$ = " "
    a = a + 1
    Num$=Num$+Temp$
    Temp$=Mid$(Size$,a,1)
Wend
W = Num$

Num$=""
Temp$=""

While Not a > Len(Size$)
    a = a + 1
    Num$=Num$+Temp$
    Temp$=Mid$(Size$,a,1)
Wend
H = Num$

Dim Block$(H,W)

Repeat
    TempNum = TempNum + 1
    In$=ReadLine(ARead)
    For a = 1 To H
 Block$(a,TempNum) = Lower(Mid$(In$,a,1))
    Next
Until TempNum = W

For a = 1 To H
    If Not Block$(a,1) = "x" Then Block$(a,1) = "y"
Next
For a = 1 To H
    If Not Block$(a,W) = "x" Then Block$(a,W) = "y"
Next
For a = 1 To W
    If Not Block$(1,a) = "x" Then Block$(1,a) = "y"
Next
For a = 1 To W
    If Not Block$(H,a) = "x" Then Block$(H,a) = "y"
Next

Round = True

While Round = True

    Round = False
    
    For a = 1 To H
 For b = 1 To W
     If Block$(a,b) = "y" Then
   If Not a = H Then
       If Block$(a+1,b) = "." Then
     Block$(a+1,b) = "y"
     Round = True
       EndIf
   EndIf
   If Not a = 1 Then
       If Block$(a-1,b) = "." Then
     Block$(a-1,b) = "y"
     Round = True
       EndIf
   EndIf
   If Not b = W Then
       If Block$(a,b+1) = "." Then
     Block$(a,b+1) = "y"
     Round = True
       EndIf
   EndIf
   If Not b = 1 Then
       If Block$(a,b-1) = "." Then
     Block$(a,b-1) = "y"
     Round = True
       EndIf
   EndIf
     EndIf
 Next
    Next

Wend

For a = 1 To H
    For b = 1 To W
 If Block$(a,b) = "." Then Area = Area + 1
    Next
Next

WriteLine(AWrite,Area)

ReadLine(ARead)

a = 0
W = 0
TempNum=0

Temp$=""
Num$=""

Area = 0

Wend

End


And here is what I started on 2, which basically read the file.

CODE

Type Net
    Field Got$
End Type

Type Stamp
    Field Value
    Field Set
    Field TCase
End Type

Type Set
    Field Total
    Field TCase
    Field SetNo
End Type

Type TCase
    Field MaxStamps
    Field NumOfSets
    Field TCaseNumb
End Type

ARead = ReadFile("Prob2.dat")
AWrite = WriteFile("Lattyware_prob2.out")

While Not Eof(ARead)

    Test = ReadLine(ARead)

    If Test = 0 Then Goto Done
    
    Total = Total + 1
    
    A1.TCase = New TCase
    A1\TCaseNumb = Total
    A1\MaxStamps = Test
    A1\NumOfSets = ReadLine(ARead)
    
    For Temp1 = 1 To A1\NumOfSets
 A2.Set = New Set
 A2\TCase = A1\TCaseNumb
 A2\SetNo = Temp1
 In$ = ReadLine(ARead)
 Split(In$," ")
 Get.Net = First Net
 A2\Total = Get\Got$
     For Temp2 = 1 To A2\Total
   A3.Stamp = New Stamp
   Get = After Get
   A3\Value = Get\Got$
   A3\Set = A2\SetNo
   A3\TCase = A1\TCaseNumb
     Next
    Next

Wend

.Done

Print GetStamps$(3,1)

WaitKey()
End

Function GetStamps$(TCase,Set)
    For A3.Stamp = Each Stamp
 If A3\Set = Set Then
     If A3\TCase = TCase Then
   Out$ = Out$ + A3\Value+","
     EndIf      
 EndIf
    Next
    Return Out$
End Function

Function Split(In$,C$)
    CleanNets()
    ANew = True
    For a = 1 To Len(In$)
 If ANew = True Then
     Split.Net = New Net
     Split\Got$ = Split\Got$ + Mid$(In$,a,1)
     ANew = False
 Else
     Split.Net = Last Net
     If Mid$(In$,a,1) = C$ Then
   ANew = True
     Else
   Split\Got$ = Split\Got$ + Mid$(In$,a,1)
     EndIf
 EndIf
    Next
End Function

Function CleanNets()
    For Clean.Net = Each Net
 Delete Clean
    Next
End Function


1 was pretty easy, just stripping it and reading in the letters from each end to see if it's a palindrome.

4 took me a while, but I eventually realised all non-enclosed space must connect to the edges, and the edges (unless they are walls) must be open space. Therefore you can just flood from the edges inwards, untill you can't anymore, and the areas left untouched, are enclosed space.

CODE

yyyyyyyyyyyyyyyyyy
yyyyyyyyyyyyyyyyyy
yxxxxxxxxxxxxxxxyy
yxyyyyyyyyyyyyyxyy
yxyyyyyyyyyyyyyxyy
yxyyyxxxxxxxxxyyyy
yxyyyx.......xyyyy
yxyyyx.......xyyyy
yxyyyx.......xyyyy
yxxxxxxxxxxxxxyyyy
yyyyyx...xyyyyyyyy
yyyyyxxxxxyyyyyyyy
yyyyyxyyyyyyyyyyyy


This post has been edited by lattyware: 15 Aug, 2005 - 03:16 AM
User is offlineProfile CardPM

Go to the top of the page

Acid Reign
post 15 Aug, 2005 - 09:25 PM
Post #4


Touched by his noodly appendage

*****
Joined: 11 Nov, 2002
Posts: 2,123



Dream Kudos: 2
My Contributions


QUOTE
Therefore you can just flood from the edges inwards, untill you can't anymore, and the areas left untouched, are enclosed space.


gah!
User is offlineProfile CardPM

Go to the top of the page

pioSko
post 16 Aug, 2005 - 08:36 AM
Post #5


still.dreaming

Group Icon
Joined: 6 Jun, 2003
Posts: 1,888



Thanked 3 times

Dream Kudos: 225
My Contributions


CODE
<?PHP

   /* ******************************
   Dream.in.code 48 Hour competition
   Member Name: Red|5
   Compiler: PHP Version 5.0.4
   Language Used: PHP
   Problem Name: #1 PALINDROMES
   ****************************** */
    
   include("gfx/top.htm");

   if(isset($_POST['file'])){
       $input = $_POST['file'];
   } else {
       $input = 'prob1.dat';
   }
   $output = 'red5_prob1.out';
    
   function testLine($str){
       $sample = strtoupper($str);
       $sample = str_split($sample);
 
       for($i = 0; $i < count($sample); $i++){
           if(ord($sample[$i]) < 65 || ord($sample[$i]) > 91){
               array_splice($sample, $i, 1);
                   $i--;
           }
       }
 
       $test = array_reverse($sample);
 
       $sample = implode("", $sample);
       $test = implode("", $test);
 
       if($test == $sample && strlen($sample) > 2){
           strlen($sample) % 2 == 0 ? $type = "a perfect" : $type = "an odd";
           return $str . " is " . $type . " palindrome\r\n";
       }
   }
    
   if($handle = @fopen($input, 'r')){
       $line = array();
       while (!feof($handle)) {
           $line[] = rtrim(ltrim(fgets($handle, 1024)));
       }    
       fclose($handle);
 
       if($handle = @fopen($output, 'w+')){
           for($i = 0; $i < count($line); $i++){
               fputs($handle, testLine($line[$i]));
           }      
           fclose($handle);
       } else {
           if ($handle = @fopen($output, 'a+')) {
               for($i = 0; $i < count($line); $i++){
                   fputs($handle, testLine($line[$i]));
               }      
               fclose($handle);
           } else {
               echo ("<p>File <b>$output</b> was not found, nor was it possible to create it.<br><br>");
               echo ("Please make sure your PHP settings are set to allow output of text files, then try again.</p>");
           }
       }
 
       function getFile($file){
           if ($grab = @fopen($file, 'r')) {
               echo ("<p><br>");
               while (!feof($grab)) {
                   echo (fgets($grab, 1024) . "<br>");
               }    
               fclose($grab);
               echo ("</p>");
           }
       }
 
       echo ('<p>Thank you.<br><br>');
       echo ('The <b>' . $input . '</b> file has been successfully proccessed.<br><br>');
       echo ('Results file <b>' . $output . '</b>, can be downloaded <a href="' . $output . '">here</a>.</p>');
       getFile($output);
   } else {
       echo ("<p>File <b>$input</b> was not found. Please try again.</p>");
       echo ('<form action="solution1.php" method="post"><p><input type="text" name="file" value="' . $input . '" style="width: 150px; border: solid 1px orange; padding: 2px"> <input type="submit" value=" Open " style="width: 80px"></p></form>');
   }

   include("gfx/bottom.htm");
?>


Attached File(s)
Attached File  red5_prob1.zip ( 3.25k ) Number of downloads: 146
User is offlineProfile CardPM

Go to the top of the page

pioSko
post 16 Aug, 2005 - 08:50 AM
Post #6


still.dreaming

Group Icon
Joined: 6 Jun, 2003
Posts: 1,888



Thanked 3 times

Dream Kudos: 225
My Contributions


unfinished problem #2

CODE
<?PHP

   /* **************************************
   Dream.in.code 48 Hour competition
   Member Name: Red|5
   Compiler: PHP Version 5.0.4
   Language Used: PHP
   Problem Name: #2 AUTOMATED LETTER HANDLER
   ************************************** */
    
   include("gfx/top.htm");

   if(isset($_POST['file'])){
       $input = $_POST['file'];
   } else {
       $input = 'prob2.dat';
   }
   $output = 'red5_prob2.out';
    
   if($handle = @fopen($input, 'r')){
       $data = array();
       while (!feof($handle)) {
           $sample = rtrim(ltrim(fgets($handle, 1024)));
           if($sample != "" && $sample != "0"){
               $data[] = $sample;
           } else if($sample == "0"){
               break;
           }
       }    
       fclose($handle);
 
       $S = array();
       $N = array();
       $RESULTS = array();
       $DENOMs = array();
 
       for ($i = 0; $i < count($data); $i++){
           array_push($S, $data[$i]);
           array_push($N, $data[$i + 1]);
     
           $temp = array();
     
           for($j = 0; $j < $data[$i + 1]; $j++){
               $raw = $data[$i + 2 + $j];
               $temp[$j] = explode(" ", $raw);
           }
     
           array_push($DENOMs, $temp);
     
           $i += $data[$i + 1] + 1;      
       }
 
       for ($i = 0; $i < count($S); $i++){
           for ($j = 0; $j < $N[$i]; $j++){
               $coverage = 1;
               $test = 0;
               while($k < $S[$i]){
                   for($k = 1; $k <= $DENOMs[$i][$j][0]; $k++){
                       if($test < $coverage){
                           $test += $DENOMs[$i][$j][$k];
                           $k--;
                       } else if ($test == $coverage){
                           break;
                       }
                   }
                   echo ($test."<BR>");
                   //echo $DENOMs[$i][$j][$k];
               }
               $coverage++;
           }
       }
   } else {
       echo ("<p>File <b>$input</b> was not found. Please try again.</p>");
       echo ('<form action="solution1.php" method="post"><p><input type="text" name="file" value="' . $input . '" style="width: 150px; border: solid 1px orange; padding: 2px"> <input type="submit" value=" Open " style="width: 80px"></p></form>');
   }    

   include("gfx/bottom.htm");
?>
User is offlineProfile CardPM

Go to the top of the page

mad
post 16 Aug, 2005 - 11:43 PM
Post #7


New D.I.C Head

Group Icon
Joined: 9 Feb, 2005
Posts: 41



Dream Kudos: 110
My Contributions


you php'ers are too simple. Here is the HARDCORE C++! Smash you down php!

CODE
#include <iostream>
#include <fstream>
using namespace std;

#define ODD 0
#define PERFECT 1

#define SPEC_Line 0
#define SPEC_Body 1

FILE *fp;

typedef struct
{
    char word[255];
    char wInverse[255];
    int numChar;
} __word;

typedef struct
{
    int numWord;
    __word word[150];
} __line;

int getNumWord(int spec, FILE *fp)
{
    int numWord = 0;
    char buffer[255];
    char c = 0;
    while((fscanf(fp, "%s", buffer) != EOF))
    {
 if(sscanf("%s", "%s", buffer))
 {
     if(spec == SPEC_Line)
     {
   if((c = getc(fp)) != '\n')
   {
       numWord++;
   }
   else if(c == '\n')
       return numWord + 1;
     }
     else if(spec == SPEC_Body)
     {
   numWord++;
     }
 }
    }
    return numWord;
}

int getNumChar(FILE *fp)
{
    int numChar = 0;
    char c = 0;

    while((c = getc(fp)) != EOF)
    {
 if(    c != '\n' && c != 33 && c != 34 && c != 39 && c != 40 && c != 41
   && c != 44 && c != 45 && c != 46 && c != 47 && c != 58
   && c != 59 && c != 63 && c!= 91 && c != 92 && c != 93
   && c != 95 && c != 123 && c != 125 && c != 130 && c != 132
   && c != 139 && c != 145    && c != 146    && c != 147
   && c != 148 && c != 150 && c != 151    && c != 155    && c != 161
   && c != 171    && c != 173    && c != 183 && c != 187
   && c != ' ')
 {
     numChar++;
 }
 else if(c == '\n' || (c != 33 && c != 34 && c != 39 && c != 40 && c != 41
   && c != 44 && c != 45 && c != 46 && c != 47 && c != 58
   && c != 59 && c != 63 && c!= 91 && c != 92 && c != 93
   && c != 95 && c != 123 && c != 125 && c != 130 && c != 132
   && c != 139 && c != 145    && c != 146    && c != 147
   && c != 148 && c != 150 && c != 151    && c != 155
   && c != 161 && c != 171    && c != 173    && c != 183 && c != 187
   ) || c == ' ')
 {
     return numChar;
 }
    }
    
    return 0; // should never return this!
}

int getNumLines(FILE *fp)
{
    int numLines = 0;
    char c = 0;

    while((c = getc(fp)) != EOF)
    {
 if(c == '\n')
 {
     numLines++;
 }
    }
    return numLines + 1;
}
    

void readWord(FILE *fp, char *buffer)
{
    char c = 0;
    int i = 0;
    while((c = getc(fp)) != EOF)
    {
 if(    c != '\n' && c != 33 && c != 34 && c != 39 && c != 40 && c != 41
   && c != 44 && c != 45 && c != 46 && c != 47 && c != 58
   && c != 59 && c != 63 && c!= 91 && c != 92 && c != 93
   && c != 95 && c != 123 && c != 125 && c != 130 && c != 132
   && c != 139 && c != 145    && c != 146    && c != 147
   && c != 148 && c != 150 && c != 151    && c != 155
   && c != 161 && c != 171    && c != 173    && c != 183 && c != 187
   && c != ' ')
 {
     buffer[i++] = c;
 }
 else if    (c == '\n' || (c != 33 && c != 34 && c != 39 && c != 40 && c != 41
   && c != 44 && c != 45 && c != 46 && c != 47 && c != 58
   && c != 59 && c != 63 && c!= 91 && c != 92 && c != 93
   && c != 95 && c != 123 && c != 125 && c != 130 && c != 132
   && c != 139 && c != 145    && c != 146    && c != 147
   && c != 148 && c != 150 && c != 151    && c != 155
   && c != 161 && c != 171    && c != 173    && c != 183 && c != 187
   ) || c == ' ')
 {
     return;
 }
    }
}

void Inverse(char *output, char *input, int numChar)
{
    int j = 0;
    cout << endl << "Inverse calculation started!!" << endl;
    for(int i = numChar-1; i >= 0; i--, j++)
    {
 output[j] =  input[i];
 cout << "Inverted " << output[j] << endl;
    }
    cout << "Inverted done successfully!" << endl;
}

int OddOrEven(int num)
{
    if((num&1) == 0)
 return 1;
    else
 return 0;
    
    return 0;
}

char CheckAscii(char c)
{
    if(c >= 65 && c <= 90)
 c += 32;
    return c;
}

int AlgorithmAnna(__word word)
{
    int chr = 0;
    bool isEven = false;

    cout << "Cutting the word into half" << endl;
    if(OddOrEven(word.numChar))
    {
 isEven = true;
 chr = word.numChar / 2;
    }
    else
    {
 isEven = false;
 int evenNumChar = word.numChar - 1;
 chr = evenNumChar /2;
    }
    __word partOne[1];
    __word partTwo[1];

    cout << "Part One: ";
    for(int i = 0; i < chr; i++)
    {
 partOne[0].word[i] = word.word[i];
 cout << partOne[0].word[i];
    }
    cout << endl;
    
    cout << "Part Two: ";
    for(int i = word.numChar-chr; i < word.numChar; i++)
    {
 partTwo[0].word[i] = word.word[i];
 cout << partTwo[0].word[i];
    }
    cout << endl;

    if(!isEven)
 cout << "Letter in middle: " << word.word[chr] << endl;

    Inverse(partOne[0].wInverse, partOne[0].word, chr);
    
    int matches = 0;
    int numCh;
    // chr is the middle letter for odds!
    if(isEven != true)
 numCh = chr+1;
    else
 numCh = chr;

    cout << endl << "Comparing part one's inverse to part two" << endl;
    for(int i = 0, j = numCh; i <= chr, j < word.numChar; i++, j++)
    {
 cout << endl << "Performing Algorithm <Anna> to " << partOne[0].wInverse[i] << " and " << partTwo[0].word[j] << endl;
 if(partOne[0].wInverse[i] == partTwo[0].word[j])
 {
     matches++;
     cout << "Match Found -> " << partOne[0].wInverse[i] << endl;
     cout << "Checking for full-text match." << endl;
     if(matches == chr)
     {
   cout << "Full-text match found!" << endl;
   cout << "VALUE -> PERFECT!" << endl;
   cout << "Test successfully done!" << endl << endl;
   return 1;
     }
 }
 else
 {
     if(i+1 == chr && matches == 0)
     {
   cout << "No match!" << endl;
   cout << "No total matches found." << endl;
   cout << "This line is not consist of any palindromic letters!" << endl;
   cout << "Test successfully done!" << endl << endl;
   return 0;
     }
     else if(i+1 != chr)
     {
   cout << "No match!" << endl;
   cout << "Testing another character" << endl;
   cout << "Character To Test: " << partOne[0].wInverse[i+1] << endl;
     }
     else if(i+1 == chr && matches > 0)
     {
   cout << matches << " matches found but no full-text match found!" << endl;
   cout << "VALUE -> ODD" << endl;
   cout << "Test successfully done!" << endl << endl;
   return 0;
     }
 }
    }
    return 0;
}

int AlgorithmMirror(__line line)
{
    __word fullText[1];    // it is gonna look like 1 word.
    int totalChar = 0;
    int chr = 0;
    bool isEven = false;
    
    strcpy(fullText[0].word, "");

    for(int i = 0; i <= line.numWord; i++)
    {
 totalChar += line.word[i].numChar;
 strcat(fullText[0].word, line.word[i].word);
    }
    cout << "Merged the numChar of each word in the line: " << totalChar << endl;
    cout << "Assembeled the line as follow: " << fullText[0].word << endl;
    
    if(OddOrEven(totalChar))
    {
 isEven = true;
 chr = totalChar / 2;
    }
    else
    {
 isEven = false;
 int evenNumChar = totalChar - 1;
 chr = evenNumChar /2;
    }
    
    __word partOne[1];
    __word partTwo[1];

    
    cout << "Cutting the word into half" << endl;
    cout << "Part One: ";
    for(int i = 0; i < chr; i++)
    {
 partOne[0].word[i] = fullText[0].word[i];
 cout << partOne[0].word[i];
    }
    cout << endl;
    
    cout << "Part Two: ";
    for(int i = totalChar-chr; i < totalChar; i++)
    {
 partTwo[0].word[i] = fullText[0].word[i];
 cout << partTwo[0].word[i];
    }
    cout << endl;

    if(!isEven)
 cout << "Letter in middle: " << fullText[0].word[chr] << endl;



    //Inverting part one;
    Inverse(partOne[0].wInverse, partOne[0].word, chr);

    cout << endl << "Comparing part one's inverse to part two" << endl;

    int matches = 0;
    int numCh;
 
    // chr is the middle letter for odds!
    if(isEven != true)
 numCh = chr+1;
    else
 numCh = chr;

    for(int i = 0, j = numCh; i <= chr, j < totalChar; i++, j++)
    {
 cout << endl << "Performing Algorithm <Mirror> to " << partOne[0].wInverse[i] << " and " << partTwo[0].word[j] << endl;
 if(partOne[0].wInverse[i] == partTwo[0].word[j])
 {
     matches++;
     cout << "Match Found -> " << partOne[0].wInverse[i] << endl;
     cout << "Checking for full-text match." << endl;
     if(matches == chr)
     {
   cout << "Full-text match found!" << endl;
   cout << "VALUE -> PERFECT!" << endl;
   cout << "Test successfully done!" << endl << endl;
   return 1;
     }
     else if(matches != chr)
     {
   cout << "Not enough matches for a full-text match" << endl;
   if(i+1 != chr)
   {
       cout << "Testing another character" << endl;
       cout << "Characters To Test: " << partOne[0].wInverse[i+1];
       cout << " and " << partTwo[0].word[j+1] << endl;
       
   }
   else
   {
       cout << matches << " matches found tottally." << endl;
       cout << "But not enough for a full-text match." << endl;
       cout << "VALUE -> ODD" << endl;
       cout << "Test successfully done!" << endl << endl;
       return 0;
   }
     }
 }
 else
 {
     if(i+1 == chr && matches == 0)
     {
   cout << "No match" << endl;
   cout << "No total matches found." << endl;
   cout << "This line is not consist of any palindromic letters!" << endl;
   cout << "VALUE -> ODD" << endl;
   cout << "Test successfully done!" << endl << endl;
   return 0;
     }
     else if(i+1 < chr)
     {
   cout << "No match!" << endl;
   cout << "Testing another character" << endl;
   cout << "Characters To Test: " << partOne[0].wInverse[i+1];
   cout << " and " << partTwo[0].word[j+1] << endl;
     }
     else if(i+1 == chr && matches > 0)
     {
   cout << "No match!" << endl;
   cout << matches << " matches found but no full-text match found!" << endl;
   cout << "VALUE -> ODD" << endl;
   cout << "Test successfully done!" << endl << endl;
   return 0;
     }
 }
    }
    return 0;
}

void CheckPalindromes(FILE *fp, __line line)
{
    if(line.numWord == 1 && line.numWord != 0)
    {
 for(int i = 0; i < line.numWord; i++)
 {
     if(AlgorithmAnna(line.word[i]) == PERFECT)
     {
   fprintf(fp, "%s - Perfect!\n", line.word[i].word);
     }
     else
     {
   fprintf(fp, "%s - Odd!\n", line.word[i].word);
     }
 }
    }
    else if(line.numWord > 1 && line.numWord != 0)
    {
 if(AlgorithmMirror(line) == PERFECT)
 {
     for(int i = 0; i < line.numWord; i++)
     {
   fprintf(fp, "%s ", line.word[i].word);
     }
     fprintf(fp, "- Perfect!\n");
 }
 else
 {
     for(int i = 0; i < line.numWord; i++)
     {
   fprintf(fp, "%s ", line.word[i].word);
     }
     fprintf(fp, "- Odd!\n");
 }
    }
}


__line line[150];
void main()
{
    FILE *fpW = fopen("prob1.dat", "a");
 fprintf(fpW, " ");
    fclose(fpW);
    fp = fopen("prob1.dat", "r");
   

    int a = getNumLines(fp);      // Get the number of lines
    rewind(fp);

    for(int i = 0; i <  a; i++)
    {
 line[i].numWord = getNumWord(SPEC_Line, fp); // Get the number of words by using SPEC_Body
 cout << "Number of Words: " << line[i].numWord << " in line " << i << endl;
    }
    rewind(fp);
    cout << "Number of Lines " << a << " // Note SPEC_Body sees whole text as a line" << endl;
    
    for(int l = 0; l < a; l++)
    {
 for(int m = 0; m < line[l].numWord; m++)
 {
     line[l].word[m].numChar = getNumChar(fp);
 }
    }
    rewind(fp);
    cout << endl;

    for(int k = 0; k < a; k++)
    {
 for(int j = 0; j < line[k].numWord; j++)
 {
     readWord(fp, line[k].word[j].word);
     cout << line[k].word[j].word << " ";
     for(int l = 0; l < line[k].word[j].numChar; l++)
     {
               line[k].word[j].word[l] = CheckAscii(line[k].word[j].word[l]);
     }
 }
 cout << endl;
    }

    fclose(fp);
    fp = fopen("mad_prob1.dat", "w");
    
    for(int i = 0; i < a; i++)
    {
 CheckPalindromes(fp, line[i]);
    }
    
    fclose(fp);
    cin.get();
}
if you can't understand i will explain you tongue.gif
User is offlineProfile CardPM

Go to the top of the page

Amadeus
post 17 Aug, 2005 - 05:16 AM
Post #8


g++ -o drink whiskey.cpp

Group Icon
Joined: 12 Jul, 2002
Posts: 12,172



Thanked 33 times

Dream Kudos: 25
My Contributions


Looking good, guys...although I will note that C++ does have the tools to accomplish the program in far fewer lines than shown....but this works, which is all that matters! smile.gif
User is offlineProfile CardPM

Go to the top of the page

zyruz
post 19 Aug, 2005 - 01:22 PM
Post #9


New D.I.C Head

*
Joined: 13 Aug, 2005
Posts: 31


My Contributions


ok, this is my c++ solution to the prob3

CODE


#include <iostream>
#include <fstream>
#include <vector>

using namespace std;

ifstream infile("prob3.dat");
ofstream outfile("zyruz_prob3.out");
struct station
{
   int nr;
   int cells;
   vector<int> target;
   int target_num;
   bool hit_t;
   bool hit_l;
};

station ms(string s1)
{
   station temp;
   bool readn = false;
   int count=0, tempn;
   temp.nr = atoi((s1.substr(0,s1.find(' '))).c_str());
   s1.erase(0, s1.find(' ')+1);
   temp.cells = atoi((s1.substr(0,s1.find(':'))).c_str());
   s1.erase(0, s1.find(':')+1);
   while (s1.size()>0)
   {
       string stemp;
       while (s1[0] =='1' || s1[0] =='2' || s1[0] == '3' || s1[0]=='4' ||s1[0] == '5' || s1[0] == '6' || s1[0] == '7' || s1[0] =='8' || s1[0]=='9' || s1[0]=='0')
       {
           stemp += s1[0];
           s1.erase(0,1);
       }
       tempn = atoi(stemp.c_str());    
       s1.erase(0, 1);
       temp.target.push_back(tempn);
       count++;
   }
   temp.target_num = count;
   temp.hit_t = false;
   temp.hit_l = false;
   return temp;
   
}

bool end(string s1)
{
   if (s1 == "POWER")
   {return true;}
   
   return false;
}
int simulate(vector<station>, int);
int main ()
{

   vector<station> st1;
   string s1,s2;
   char ch = '\0';
   bool finish = false;
   int numS = 0;
   
   while (finish == false)
   {
       infile.get(ch);
       if(infile.eof())
       {
           finish = true;
       }
       if (ch != '\n')
       {
           s1 += ch;
       }

       else if(ch == '\n' || finish == true)
       {
           if (!end(s1))
           {
              if(s1.size() > 2)
              {
               st1.push_back(ms(s1));
               numS++;
              }
           }
           else
           {
              outfile <<simulate(st1,numS)<<'\n';
              numS = 0;
              st1.clear();
           }
           s1.clear();
       }
               
   }
   system("pause");
   return 0;
}

int simulate(vector<station> stat, int numS)
{
   int total = 0;
   bool done = true;
   if(stat[0].cells > 0)
   {
       stat[0].hit_l = true;

       done = false;
   
       while (done != true)
       {
           done = true;
           for (int i = 0; i < numS; i++)
           {
               if (stat[i].hit_l == true)
               {
                   if(stat[i].cells > 0)
                   {
                       stat[i].cells--;
                       total++;
                       done = false;
                       for (int j = 0; j < stat[i].target_num; j++)
                       {
                           for(int k = 0; k < numS; k++)
                           {
                               if(stat[k].nr == stat[i].target[j] )
                               {
                                   stat[k].hit_t = true;
                               }
                           }
                       }
                       cout <<