Showing posts with label fun. Show all posts
Showing posts with label fun. Show all posts

02 October 2013

Orders Aggregator - a funny program

Today, I was responsible about aggregating the food orders from my colleges, and since I wanted it to be accurate I decided to write my first ruby script (Thanks to Google)

Here it is:
# Orders Aggregator

class BreadType
  attr_accessor :type
  def initialize(type)
    @type = type
  end
  
  def eql?(other)
    @type = other.type
  end
  
  def hash
    @type.hash
  end
  
  def to_s
    @type
  end
end

class Sandwich
  attr_accessor :name, :type

  def initialize (name, type)
    @name = name
    @type = type
  end
  
  def eql?(other)
    @name = other.name #&& @type = other.type # buggy and I cannot resolve, so I decided to comment
  end
  
  def hash
    @name.hash ^ @type.hash
  end
  
  def to_s
    @name.to_s << " : " << @type.to_s
  end
end

balady = BreadType.new "Balady"
shamy = BreadType.new "Shamy"

# Talaat
talaat = []
talaat << Sandwich.new(:Ta3mya, balady) << Sandwich.new(:Batates_omlete, balady)
# Hewedy
hewedy = []
2.times {
  hewedy << Sandwich.new(:Ta3mya, balady)
}
# Ahmad
ahmad = []
ahmad << Sandwich.new(:Swabe3_Salada, balady) << Sandwich.new(:Fol_Eskandrany, balady)
# Khalid
khalid = []
khalid << Sandwich.new(:Ta3mya_Eskandrany, balady)
# Alaa
alaa = []
alaa << Sandwich.new(:Ta3mya_Eskandrany, balady)
# Hatem
hatem = []
3.times {
  hatem << Sandwich.new(:Ta3mya, shamy)
}

total = talaat + hewedy + ahmad + khalid + alaa + hatem

counts = Hash.new(0)
total.each { |name| counts[name] += 1 }

puts "Aggregation: "
counts.each { |k, v|
  puts k.to_s << " ::" << v.to_s
}

Output:
Aggregation: 
Batates_omlete : Balady ::1
Ta3mya : Shamy ::3
Swabe3_Salada : Balady ::1
Ta3mya : Balady ::3
Ta3mya_Eskandrany : Balady ::2
Fol_Eskandrany : Balady ::1

13 August 2011

Read n bytes from start of a large file

SA,

How are you all doing? I hope that you are doing well...

I was just playing.... ya playing...

I was trying to install Mac OS X on my new HP computer, I had an old MAC OS X iso file and when I try to use it from inside VMWare, it keep saying .. "No Operating System found!" so I need to know why??

I've bough a new DVD Disk and tried to burn that iso image but WIN 7 Burn program told me that the iso file is not in a valid format ...

So, I need to see how this iso file looks like...
I've tried to open this huge (4+ GB) iso file in np++, but this cause the np++ to hang .. so I need to read the first n bytes from the file and see how these bytes looks like..

I am a Java programmer with little knowledge in C/C++. But I wanted to write this program in CPP...

It reads starting from a random byte to 10000 bytes from the iso file and write to some arbitrary file on the FS..

Here's the source code:


  1. #include <iostream>
  2. #include <fstream>
  3. #include <cstdlib>
  4. #include <ctime>
  5. using namespace std;
  6. 
  7. int main(void)
  8. {
  9.     ifstream f;
 10.     ofstream out;
 11.     f.open("D:\\Programs\\mac\\Mac OS X\\Mac OS X 10.4.8 [JaS AMD-Intel-SSE2-SSE3 with PPF1].iso", fstream::in);
 12.     out.open("D:\\mac.out.txt", fstream::out);
 13.     
 14.     srand(time(NULL));
 15.     int start = rand(); 
 16.     cout << "start reading fomr byte: " << start << endl;
 17.     
 18.     char c = 0;
 19.     for (int i=start; i < start + 10000; i++)
 20.     {
 21.         f >> c;
 22.         out <<c;
 23.     }
 24.        
 25.     f.close();
 26.     out.close();
 27.     
 28.     return 0;
 29. }

Hence I am not a CPP Guru, I've used cplusplus.com as API reference..

25 June 2011

convert fault written arabic letters to English

#include <iostream>
#include <cstring>
#include <fstream>

using namespace std;

void getWord(int, char**, char**);
void translateAndPrint(char*, ostream&);

int main(int argc, char** args)
{
   char* word = new char[1024];
   getWord(argc, args, &word);

    fstream fileout;
    fileout.open("out.txt", ios_base::out);
    
    translateAndPrint(word, fileout);

    //wchar_t x = 'ب';
    //fileout << (char)x << endl;
    fileout.flush();
    fileout.close();
    delete [] word;
    system("pause");
   return 0;
}


void translateAndPrint(char* word, ostream& out)
{
     wchar_t map(char en);
     
     while (*word != '\0')
     {
          out << (char)map(*word);
          
          word++;
     }
}

void getWord(int argc, char** args, char** word)
{
    if (argc > 1)
       strcpy(*word, args[1]);
    else{
       cout << "Enter an invalid word: " << endl;
       *word = new char[1024];
       cin.getline(*word, 1024);
    }
}

wchar_t map(char en)
{
     wchar_t ret;
     switch(en)
     {
        default:
        ret = en;
        break;
        
        case 'a':
        ret = 'ش';
        break;
        
        case 'b':
        ret = 'لا';
        break;        
        
        case 'c':
        ret = 'ؤ';
        break;
        
        case 'd':
        ret = 'ي';
        break;
        
        case 'e':
        ret = 'ث';
        break;
        
        
        case 'f':
        ret = 'ب';
        break;
        
        case 'g':
        ret = 'ل';
        break;
        
        case 'h':
        ret = 'ا';
        break;
        
        case 'i':
        ret = 'ه';
        break;
        
        
        case 'j':
        ret = 'ت';
        break;
        
        case 'k':
        ret = 'ن';
        break;
                
        case 'l':
        ret = 'م';
        break;
        
        case 'm':
        ret = 'ة';
        break;
        
        
        case 'n':
        ret = 'ى';
        break;
        
        
        case 'o':
        ret = 'خ';
        break;
        
        
        case 'p':
        ret = 'ح';
        break;
        
        
        case 'q':
        ret = 'ض';
        break;
        
        case 'r':
        ret = 'ق';
        break;
        
        case 's':
        ret = 'س';
        break;
        
        case 't':
        ret = 'ف';
        break;
        
        
        case 'u':
        ret = 'ع';
        break;
        
        
        case 'v':
        ret = 'ر';
        break;
        
        
        case 'w':
        ret = 'ص';
        break;
        
        case 'x':
        ret = 'ء';
        break;
        
        case 'y':
        ret = 'غ';
        break;
        
        
        case 'z':
        ret = 'ئ';
        break;
        
        case ']':
        ret = 'د';
        break;
        
        case '`':
        ret = 'ذ';
        break;
        
        case ',':
        ret = 'ظ';
        break;
        
        case '.':
        ret = 'ظ';
        break;
        
        case '/':
        ret = 'ظ';
        break;
     }
     return ret;
}

13 January 2011

Playing with JavaScript

Hello folks,

I am a programmer that dislike working with the client-side in web programming.
I dislike to do somethings like on button click, go and disable the textbox, and alike.

Nowadays I am working with GWT that do that some of that dirty work for me. but I really wanted to try doing some Javasript myself, and the following is a simple valueless example, just for fun :)


<html>
  <head>
    <script language="Javascript" type="text/javascript">
    function submitUserAction(rowId)
    {
        var flage = confirm("Are you sure?");
        if(flage)
        {
            deleteRow(rowId);
        }
    }
    
    function deleteRow(rowId)
    {
        
        alert("deleting Row " + document.getElementById("rowId").value );
    }
    
    function changeCascade()
    {
        document.getElementById("lbl").value = document.getElementById("bla").value;
        document.getElementById("rowId").value = document.getElementById("bla").value;
        
        document.getElementById("lbl").size = document.getElementById("bla").size++;
    }
    
    </script>

    <title></title>
  </head>
  <body>
    <input type="button" value="click me"  onclick="submitUserAction()" />
    <input type="text" id="bla" value onkeyup="changeCascade()"  />
    <input type="hidden" id="rowId" />
    
    <input type="text" id="lbl" value= "****" />
    
  </body>
</html>


And again, it is just for fun :)