WeixiangYan/CodeTransOcean huggingface dataset:
Task: Handle a signal


C++: #include <chrono>
#include <csignal>
#include <ctime>
#include <iostream>
#include <thread>

volatile sig_atomic_t gotint = 0;

void handler(int signum) {
	
	gotint = 1;
}

int main() {
	using namespace std;

	signal(SIGINT, handler);

	int i = 0;
	clock_t startTime = clock();
	while (true) {
		if (gotint) break;
		std::this_thread::sleep_for(std::chrono::milliseconds(500));
		if (gotint) break;
		cout << ++i << endl;
	}
	clock_t endTime = clock();

	double dt = (endTime - startTime) / (double)CLOCKS_PER_SEC;
	cout << "Program has run for " << dt << " seconds" << endl;

	return 0;
}








COBOL:        identification division.
       program-id. signals.
       data division.
       working-storage section.
       01 signal-flag  pic 9 external.
          88 signalled value 1.
       01 half-seconds usage binary-long.
       01 start-time   usage binary-c-long.
       01 end-time     usage binary-c-long.
       01 handler      usage program-pointer.
       01 SIGINT       constant as 2.

       procedure division.
       call "gettimeofday" using start-time null
       set handler to entry "handle-sigint"
       call "signal" using by value SIGINT by value handler

       perform until exit
           if signalled then exit perform end-if
           call "CBL_OC_NANOSLEEP" using 500000000
           if signalled then exit perform end-if
           add 1 to half-seconds
           display half-seconds
       end-perform

       call "gettimeofday" using end-time null
       subtract start-time from end-time
       display "Program ran for " end-time " seconds"
       goback.
       end program signals.

       identification division.
       program-id. handle-sigint.
       data division.
       working-storage section.
       01 signal-flag  pic 9 external.

       linkage section.
       01 the-signal   usage binary-long.

       procedure division using by value the-signal returning omitted.
       move 1 to signal-flag
       goback.
       end program handle-sigint.
Task: Handle a signal





Java: import sun.misc.Signal;
import sun.misc.SignalHandler;

public class ExampleSignalHandler {
    public static void main(String... args) throws InterruptedException {
        final long start = System.nanoTime();
        Signal.handle(new Signal("INT"), new SignalHandler() {
            public void handle(Signal sig) {
                System.out.format("\nProgram execution took %f seconds\n", (System.nanoTime() - start) / 1e9f);
                System.exit(0);
            }
        });
        int counter = 0;
        while(true) {
            System.out.println(counter++);
            Thread.sleep(500);
        }
    }
}





COBOL:        identification division.
       program-id. signals.
       data division.
       working-storage section.
       01 signal-flag  pic 9 external.
          88 signalled value 1.
       01 half-seconds usage binary-long.
       01 start-time   usage binary-c-long.
       01 end-time     usage binary-c-long.
       01 handler      usage program-pointer.
       01 SIGINT       constant as 2.

       procedure division.
       call "gettimeofday" using start-time null
       set handler to entry "handle-sigint"
       call "signal" using by value SIGINT by value handler

       perform until exit
           if signalled then exit perform end-if
           call "CBL_OC_NANOSLEEP" using 500000000
           if signalled then exit perform end-if
           add 1 to half-seconds
           display half-seconds
       end-perform

       call "gettimeofday" using end-time null
       subtract start-time from end-time
       display "Program ran for " end-time " seconds"
       goback.
       end program signals.

       identification division.
       program-id. handle-sigint.
       data division.
       working-storage section.
       01 signal-flag  pic 9 external.

       linkage section.
       01 the-signal   usage binary-long.

       procedure division using by value the-signal returning omitted.
       move 1 to signal-flag
       goback.
       end program handle-sigint.
Task: Handle a signal







Python: import time

def counter():
    n = 0
    t1 = time.time()
    while True:
        try:
            time.sleep(0.5)
            n += 1
            print n
        except KeyboardInterrupt, e:
            print 'Program has run for %5.3f seconds.' % (time.time() - t1)
            break

counter()



COBOL:        identification division.
       program-id. signals.
       data division.
       working-storage section.
       01 signal-flag  pic 9 external.
          88 signalled value 1.
       01 half-seconds usage binary-long.
       01 start-time   usage binary-c-long.
       01 end-time     usage binary-c-long.
       01 handler      usage program-pointer.
       01 SIGINT       constant as 2.

       procedure division.
       call "gettimeofday" using start-time null
       set handler to entry "handle-sigint"
       call "signal" using by value SIGINT by value handler

       perform until exit
           if signalled then exit perform end-if
           call "CBL_OC_NANOSLEEP" using 500000000
           if signalled then exit perform end-if
           add 1 to half-seconds
           display half-seconds
       end-perform

       call "gettimeofday" using end-time null
       subtract start-time from end-time
       display "Program ran for " end-time " seconds"
       goback.
       end program signals.

       identification division.
       program-id. handle-sigint.
       data division.
       working-storage section.
       01 signal-flag  pic 9 external.

       linkage section.
       01 the-signal   usage binary-long.

       procedure division using by value the-signal returning omitted.
       move 1 to signal-flag
       goback.
       end program handle-sigint.
Task: Handle a signal








VB: Module Module1
    Dim startTime As Date

    Sub Main()
        startTime = Date.Now
        
        AddHandler Console.CancelKeyPress, AddressOf Console_CancelKeyPress

        Dim counter = 0
        While True
            counter += 1
            Console.WriteLine(counter)
            Threading.Thread.Sleep(500)
        End While
    End Sub

    Sub Console_CancelKeyPress(sender As Object, e As ConsoleCancelEventArgs)
        Dim stopTime = Date.Now
        Console.WriteLine("This program ran for {0:000.000} seconds", (stopTime - startTime).TotalMilliseconds / 1000)
        Environment.Exit(0)
    End Sub

End Module


COBOL:        identification division.
       program-id. signals.
       data division.
       working-storage section.
       01 signal-flag  pic 9 external.
          88 signalled value 1.
       01 half-seconds usage binary-long.
       01 start-time   usage binary-c-long.
       01 end-time     usage binary-c-long.
       01 handler      usage program-pointer.
       01 SIGINT       constant as 2.

       procedure division.
       call "gettimeofday" using start-time null
       set handler to entry "handle-sigint"
       call "signal" using by value SIGINT by value handler

       perform until exit
           if signalled then exit perform end-if
           call "CBL_OC_NANOSLEEP" using 500000000
           if signalled then exit perform end-if
           add 1 to half-seconds
           display half-seconds
       end-perform

       call "gettimeofday" using end-time null
       subtract start-time from end-time
       display "Program ran for " end-time " seconds"
       goback.
       end program signals.

       identification division.
       program-id. handle-sigint.
       data division.
       working-storage section.
       01 signal-flag  pic 9 external.

       linkage section.
       01 the-signal   usage binary-long.

       procedure division using by value the-signal returning omitted.
       move 1 to signal-flag
       goback.
       end program handle-sigint.
Task: Handle a signal




Go: package main

import (
    "fmt"
    "os"
    "os/signal"
    "time"
)

func main() {
    start := time.Now()
    k := time.Tick(time.Second / 2)
    sc := make(chan os.Signal, 1)
    signal.Notify(sc, os.Interrupt)
    for n := 1; ; {
        
        
        select {
        case <-k:
            fmt.Println(n)
            n++
        case <-sc:
            fmt.Printf("Ran for %f seconds.\n",
                time.Now().Sub(start).Seconds())
            return
        }
    }
}






COBOL:        identification division.
       program-id. signals.
       data division.
       working-storage section.
       01 signal-flag  pic 9 external.
          88 signalled value 1.
       01 half-seconds usage binary-long.
       01 start-time   usage binary-c-long.
       01 end-time     usage binary-c-long.
       01 handler      usage program-pointer.
       01 SIGINT       constant as 2.

       procedure division.
       call "gettimeofday" using start-time null
       set handler to entry "handle-sigint"
       call "signal" using by value SIGINT by value handler

       perform until exit
           if signalled then exit perform end-if
           call "CBL_OC_NANOSLEEP" using 500000000
           if signalled then exit perform end-if
           add 1 to half-seconds
           display half-seconds
       end-perform

       call "gettimeofday" using end-time null
       subtract start-time from end-time
       display "Program ran for " end-time " seconds"
       goback.
       end program signals.

       identification division.
       program-id. handle-sigint.
       data division.
       working-storage section.
       01 signal-flag  pic 9 external.

       linkage section.
       01 the-signal   usage binary-long.

       procedure division using by value the-signal returning omitted.
       move 1 to signal-flag
       goback.
       end program handle-sigint.
Task: Handle a signal






PHP: <?php
declare(ticks = 1);

$start = microtime(true);

function mySigHandler() {
  global $start;
  $elapsed = microtime(true) - $start;
  echo "Ran for $elapsed seconds.\n";
  exit();
}

pcntl_signal(SIGINT, 'mySigHandler');

for ($n = 0; ; usleep(500000)) // 0.5 seconds
   echo ++$n, "\n";
?>
Task: Handle a signal






PHP: <?php
declare(ticks = 1);

$start = microtime(true);

function mySigHandler() {
  global $start;
  $elapsed = microtime(true) - $start;
  echo "Ran for $elapsed seconds.\n";
  exit();
}

pcntl_signal(SIGINT, 'mySigHandler');

for ($n = 0; ; usleep(500000)) // 0.5 seconds
   echo ++$n, "\n";
?>






Ada: with Ada.Interrupts; use Ada.Interrupts;
with Ada.Interrupts.Names; use Ada.Interrupts.Names;

package Sigint_Handler is
      protected Handler is
      entry Wait;
      procedure Handle;
      pragma Interrupt_Handler(Handle);
      pragma Attach_Handler(Handle, Sigint);
      private
      Call_Count : Natural := 0;
   end Handler;

end Sigint_Handler;
Task: Handle a signal






PHP: <?php
declare(ticks = 1);

$start = microtime(true);

function mySigHandler() {
  global $start;
  $elapsed = microtime(true) - $start;
  echo "Ran for $elapsed seconds.\n";
  exit();
}

pcntl_signal(SIGINT, 'mySigHandler');

for ($n = 0; ; usleep(500000)) // 0.5 seconds
   echo ++$n, "\n";
?>




COBOL:        identification division.
       program-id. signals.
       data division.
       working-storage section.
       01 signal-flag  pic 9 external.
          88 signalled value 1.
       01 half-seconds usage binary-long.
       01 start-time   usage binary-c-long.
       01 end-time     usage binary-c-long.
       01 handler      usage program-pointer.
       01 SIGINT       constant as 2.

       procedure division.
       call "gettimeofday" using start-time null
       set handler to entry "handle-sigint"
       call "signal" using by value SIGINT by value handler

       perform until exit
           if signalled then exit perform end-if
           call "CBL_OC_NANOSLEEP" using 500000000
           if signalled then exit perform end-if
           add 1 to half-seconds
           display half-seconds
       end-perform

       call "gettimeofday" using end-time null
       subtract start-time from end-time
       display "Program ran for " end-time " seconds"
       goback.
       end program signals.

       identification division.
       program-id. handle-sigint.
       data division.
       working-storage section.
       01 signal-flag  pic 9 external.

       linkage section.
       01 the-signal   usage binary-long.

       procedure division using by value the-signal returning omitted.
       move 1 to signal-flag
       goback.
       end program handle-sigint.
Task: Non-decimal radices_Output



C#: using System;

namespace NonDecimalRadicesOutput
{
    class Program
    {
        static void Main(string[] args)
        {
            for (int i = 0; i < 42; i++)
            {
                string binary = Convert.ToString(i, 2);
                string octal = Convert.ToString(i, 8);
                string hexadecimal = Convert.ToString(i, 16);
                Console.WriteLine(string.Format("Decimal: {0}, Binary: {1}, Octal: {2}, Hexadecimal: {3}", i, binary, octal, hexadecimal));
            }

            Console.ReadKey();
        }
    }
}









Ada: with Ada.Integer_Text_IO;  use Ada.Integer_Text_IO;
with Ada.Text_IO;          use Ada.Text_IO;

procedure Test_Integer_Text_IO is
begin
  for I in 1..33 loop
    Put (I, Width =>3, Base=> 10);
    Put (I, Width =>7, Base=> 16);
    Put (I, Width =>6, Base=>  8);
    New_Line;
  end loop;
end Test_Integer_Text_IO;
Task: Non-decimal radices_Output

C: #include <stdio.h>

int main()
{
  int i;

  for(i=1; i <= 33; i++)
    printf("%6d %6x %6o\n", i, i, i);

  return 0;
}











Ada: with Ada.Integer_Text_IO;  use Ada.Integer_Text_IO;
with Ada.Text_IO;          use Ada.Text_IO;

procedure Test_Integer_Text_IO is
begin
  for I in 1..33 loop
    Put (I, Width =>3, Base=> 10);
    Put (I, Width =>7, Base=> 16);
    Put (I, Width =>6, Base=>  8);
    New_Line;
  end loop;
end Test_Integer_Text_IO;
Task: Non-decimal radices_Output


C++: #include <iostream>
#include <iomanip>

int main()
{
  for (int i = 0; i <= 33; i++)
    std::cout << std::setw(6) << std::dec << i << " "
              << std::setw(6) << std::hex << i << " "
              << std::setw(6) << std::oct << i << std::endl;

  return 0;
}










Ada: with Ada.Integer_Text_IO;  use Ada.Integer_Text_IO;
with Ada.Text_IO;          use Ada.Text_IO;

procedure Test_Integer_Text_IO is
begin
  for I in 1..33 loop
    Put (I, Width =>3, Base=> 10);
    Put (I, Width =>7, Base=> 16);
    Put (I, Width =>6, Base=>  8);
    New_Line;
  end loop;
end Test_Integer_Text_IO;
Task: Non-decimal radices_Output




Go: package main

import (
    "fmt"
    "math/big"
    "strconv"
)

func main() {
    
    fmt.Printf("%b\n", 13)
    fmt.Printf("%o\n", 13)
    fmt.Printf("%d\n", 13)
    fmt.Printf("%x\n", 13)
    
    d := big.NewInt(13)
    fmt.Printf("%b\n", d)
    fmt.Printf("%o\n", d)
    fmt.Printf("%d\n", d)
    fmt.Printf("%x\n", d)
    
    
    
    fmt.Println(strconv.FormatInt(1313, 19))
}








Ada: with Ada.Integer_Text_IO;  use Ada.Integer_Text_IO;
with Ada.Text_IO;          use Ada.Text_IO;

procedure Test_Integer_Text_IO is
begin
  for I in 1..33 loop
    Put (I, Width =>3, Base=> 10);
    Put (I, Width =>7, Base=> 16);
    Put (I, Width =>6, Base=>  8);
    New_Line;
  end loop;
end Test_Integer_Text_IO;
Task: Non-decimal radices_Output





Java: public static void main(String args[]){
   for(int a= 0;a < 33;a++){
      System.out.println(Integer.toBinaryString(a));
      System.out.println(Integer.toOctalString(a));
      System.out.println(Integer.toHexString(a));
      
      

      System.out.printf("%3o %2d %2x\n",a ,a ,a); 
   }
}







Ada: with Ada.Integer_Text_IO;  use Ada.Integer_Text_IO;
with Ada.Text_IO;          use Ada.Text_IO;

procedure Test_Integer_Text_IO is
begin
  for I in 1..33 loop
    Put (I, Width =>3, Base=> 10);
    Put (I, Width =>7, Base=> 16);
    Put (I, Width =>6, Base=>  8);
    New_Line;
  end loop;
end Test_Integer_Text_IO;
Task: Non-decimal radices_Output







Python: >>> for n in range(34):
  print " {0:6b} {1:3o} {2:2d} {3:2X}".format(n, n, n, n)
  
  
  

  
      0   0  0  0
      1   1  1  1
     10   2  2  2
     11   3  3  3
    100   4  4  4
    101   5  5  5
    110   6  6  6
    111   7  7  7
   1000  10  8  8
   1001  11  9  9
   1010  12 10  A
   1011  13 11  B
   1100  14 12  C
   1101  15 13  D
   1110  16 14  E
   1111  17 15  F
  10000  20 16 10
  10001  21 17 11
  10010  22 18 12
  10011  23 19 13
  10100  24 20 14
  10101  25 21 15
  10110  26 22 16
  10111  27 23 17
  11000  30 24 18
  11001  31 25 19
  11010  32 26 1A
  11011  33 27 1B
  11100  34 28 1C
  11101  35 29 1D
  11110  36 30 1E
  11111  37 31 1F
 100000  40 32 20
 100001  41 33 21
>>>





Ada: with Ada.Integer_Text_IO;  use Ada.Integer_Text_IO;
with Ada.Text_IO;          use Ada.Text_IO;

procedure Test_Integer_Text_IO is
begin
  for I in 1..33 loop
    Put (I, Width =>3, Base=> 10);
    Put (I, Width =>7, Base=> 16);
    Put (I, Width =>6, Base=>  8);
    New_Line;
  end loop;
end Test_Integer_Text_IO;
Task: Non-decimal radices_Output

C: #include <stdio.h>

int main()
{
  int i;

  for(i=1; i <= 33; i++)
    printf("%6d %6x %6o\n", i, i, i);

  return 0;
}
Task: Non-decimal radices_Output



C#: using System;

namespace NonDecimalRadicesOutput
{
    class Program
    {
        static void Main(string[] args)
        {
            for (int i = 0; i < 42; i++)
            {
                string binary = Convert.ToString(i, 2);
                string octal = Convert.ToString(i, 8);
                string hexadecimal = Convert.ToString(i, 16);
                Console.WriteLine(string.Format("Decimal: {0}, Binary: {1}, Octal: {2}, Hexadecimal: {3}", i, binary, octal, hexadecimal));
            }

            Console.ReadKey();
        }
    }
}
Task: Non-decimal radices_Output


C++: #include <iostream>
#include <iomanip>

int main()
{
  for (int i = 0; i <= 33; i++)
    std::cout << std::setw(6) << std::dec << i << " "
              << std::setw(6) << std::hex << i << " "
              << std::setw(6) << std::oct << i << std::endl;

  return 0;
}
Task: Non-decimal radices_Output





Java: public static void main(String args[]){
   for(int a= 0;a < 33;a++){
      System.out.println(Integer.toBinaryString(a));
      System.out.println(Integer.toOctalString(a));
      System.out.println(Integer.toHexString(a));
      
      

      System.out.printf("%3o %2d %2x\n",a ,a ,a); 
   }
}
Task: Non-decimal radices_Output







Python: >>> for n in range(34):
  print " {0:6b} {1:3o} {2:2d} {3:2X}".format(n, n, n, n)
  
  
  

  
      0   0  0  0
      1   1  1  1
     10   2  2  2
     11   3  3  3
    100   4  4  4
    101   5  5  5
    110   6  6  6
    111   7  7  7
   1000  10  8  8
   1001  11  9  9
   1010  12 10  A
   1011  13 11  B
   1100  14 12  C
   1101  15 13  D
   1110  16 14  E
   1111  17 15  F
  10000  20 16 10
  10001  21 17 11
  10010  22 18 12
  10011  23 19 13
  10100  24 20 14
  10101  25 21 15
  10110  26 22 16
  10111  27 23 17
  11000  30 24 18
  11001  31 25 19
  11010  32 26 1A
  11011  33 27 1B
  11100  34 28 1C
  11101  35 29 1D
  11110  36 30 1E
  11111  37 31 1F
 100000  40 32 20
 100001  41 33 21
>>>
Task: Non-decimal radices_Output




Go: package main

import (
    "fmt"
    "math/big"
    "strconv"
)

func main() {
    
    fmt.Printf("%b\n", 13)
    fmt.Printf("%o\n", 13)
    fmt.Printf("%d\n", 13)
    fmt.Printf("%x\n", 13)
    
    d := big.NewInt(13)
    fmt.Printf("%b\n", d)
    fmt.Printf("%o\n", d)
    fmt.Printf("%d\n", d)
    fmt.Printf("%x\n", d)
    
    
    
    fmt.Println(strconv.FormatInt(1313, 19))
}
Task: Non-decimal radices_Output



C#: using System;

namespace NonDecimalRadicesOutput
{
    class Program
    {
        static void Main(string[] args)
        {
            for (int i = 0; i < 42; i++)
            {
                string binary = Convert.ToString(i, 2);
                string octal = Convert.ToString(i, 8);
                string hexadecimal = Convert.ToString(i, 16);
                Console.WriteLine(string.Format("Decimal: {0}, Binary: {1}, Octal: {2}, Hexadecimal: {3}", i, binary, octal, hexadecimal));
            }

            Console.ReadKey();
        }
    }
}








Fortran: do n = 1, 33
  write(*, "(b6, o4, i4, z4)") n, n, n, n
end do
Task: Non-decimal radices_Output


C++: #include <iostream>
#include <iomanip>

int main()
{
  for (int i = 0; i <= 33; i++)
    std::cout << std::setw(6) << std::dec << i << " "
              << std::setw(6) << std::hex << i << " "
              << std::setw(6) << std::oct << i << std::endl;

  return 0;
}









Fortran: do n = 1, 33
  write(*, "(b6, o4, i4, z4)") n, n, n, n
end do
Task: Non-decimal radices_Output

C: #include <stdio.h>

int main()
{
  int i;

  for(i=1; i <= 33; i++)
    printf("%6d %6x %6o\n", i, i, i);

  return 0;
}










Fortran: do n = 1, 33
  write(*, "(b6, o4, i4, z4)") n, n, n, n
end do
Task: Non-decimal radices_Output





Java: public static void main(String args[]){
   for(int a= 0;a < 33;a++){
      System.out.println(Integer.toBinaryString(a));
      System.out.println(Integer.toOctalString(a));
      System.out.println(Integer.toHexString(a));
      
      

      System.out.printf("%3o %2d %2x\n",a ,a ,a); 
   }
}






Fortran: do n = 1, 33
  write(*, "(b6, o4, i4, z4)") n, n, n, n
end do
Task: Non-decimal radices_Output







Python: >>> for n in range(34):
  print " {0:6b} {1:3o} {2:2d} {3:2X}".format(n, n, n, n)
  
  
  

  
      0   0  0  0
      1   1  1  1
     10   2  2  2
     11   3  3  3
    100   4  4  4
    101   5  5  5
    110   6  6  6
    111   7  7  7
   1000  10  8  8
   1001  11  9  9
   1010  12 10  A
   1011  13 11  B
   1100  14 12  C
   1101  15 13  D
   1110  16 14  E
   1111  17 15  F
  10000  20 16 10
  10001  21 17 11
  10010  22 18 12
  10011  23 19 13
  10100  24 20 14
  10101  25 21 15
  10110  26 22 16
  10111  27 23 17
  11000  30 24 18
  11001  31 25 19
  11010  32 26 1A
  11011  33 27 1B
  11100  34 28 1C
  11101  35 29 1D
  11110  36 30 1E
  11111  37 31 1F
 100000  40 32 20
 100001  41 33 21
>>>




Fortran: do n = 1, 33
  write(*, "(b6, o4, i4, z4)") n, n, n, n
end do
Task: Non-decimal radices_Output






PHP: <?php
foreach (range(0, 33) as $n) {
  echo decbin($n), "\t", decoct($n), "\t", $n, "\t", dechex($n), "\n";
}
?>





Fortran: do n = 1, 33
  write(*, "(b6, o4, i4, z4)") n, n, n, n
end do
Task: Non-decimal radices_Output






PHP: <?php
foreach (range(0, 33) as $n) {
  echo decbin($n), "\t", decoct($n), "\t", $n, "\t", dechex($n), "\n";
}
?>
Task: Non-decimal radices_Output






PHP: <?php
foreach (range(0, 33) as $n) {
  echo decbin($n), "\t", decoct($n), "\t", $n, "\t", dechex($n), "\n";
}
?>






Ada: with Ada.Integer_Text_IO;  use Ada.Integer_Text_IO;
with Ada.Text_IO;          use Ada.Text_IO;

procedure Test_Integer_Text_IO is
begin
  for I in 1..33 loop
    Put (I, Width =>3, Base=> 10);
    Put (I, Width =>7, Base=> 16);
    Put (I, Width =>6, Base=>  8);
    New_Line;
  end loop;
end Test_Integer_Text_IO;