Advertisement
Guest User

Untitled

a guest
Sep 29th, 2011
278
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C# 2.33 KB | None | 0 0
  1. using System;
  2. using System.Collections.Generic;
  3. using System.Linq;
  4. using System.Text;
  5. using System.Threading;
  6.  
  7. namespace B3CC_Pract1
  8. {
  9.  
  10.     class Program
  11.     {
  12.         public static int threadcounter;
  13.         public static int oplossingen;
  14.         public static int modulus;
  15.         public static int bovengrens;
  16.  
  17.         public static void Main()
  18.         {
  19.             // Gedeelde counter voor threads
  20.             threadcounter = 0;
  21.             oplossingen = 0;
  22.  
  23.             // Start inputer
  24.             int locktype = int.Parse(Console.ReadLine());       // 0 = TaS-lock, 1 = TTaS-lock, 2 = Array-lock
  25.             int ondergrens = int.Parse(Console.ReadLine());     // 0 - ondergrens
  26.             bovengrens = int.Parse(Console.ReadLine());         // ondergrens - 10^9
  27.             modulus = int.Parse(Console.ReadLine());            // 0 - 256
  28.             int threads = int.Parse(Console.ReadLine());        // 1 - 256
  29.             // Einde input
  30.  
  31.             threadcounter = ondergrens;
  32.  
  33.             // Start threading
  34.             for (int t = 0; t < threads; t++)
  35.             {
  36.                 Thread thr = new Thread(new ThreadStart(doStuff));
  37.                 thr.Start();
  38.                 Console.WriteLine("Thread {0} created.", t);
  39.             }
  40.             // Einde threading
  41.  
  42.  
  43.             // REMOVE
  44.             Console.WriteLine(oplossingen);
  45.             Console.ReadKey(true);
  46.         }
  47.  
  48.         public static int Calculate(int toCalc, int modulus)
  49.         {
  50.             int Total = 0;
  51.             for (int t = 1; t < toCalc.ToString().Length; t++)
  52.             {
  53.                 Total += (((int)toCalc / (int)Math.Pow(10, t - 1)) % 10) * t;
  54.             }
  55.             return (int)Total % modulus;
  56.         }
  57.  
  58.         public static void doStuff()
  59.         {
  60.             // De berekening
  61.             // interlocked.Increment zorgt voor een atomaire ophoging van een teller
  62.             int getal = (int)Interlocked.Increment(ref threadcounter);
  63.             if (getal <= (int)bovengrens)
  64.             {
  65.                 if (Calculate((int)getal, (int)modulus) == 0)
  66.                 {
  67.                     // interlocked.Increment zorgt voor een atomaire ophoging van een teller
  68.                     Interlocked.Increment(ref oplossingen);
  69.                 }
  70.                 doStuff();
  71.             }
  72.         }
  73.     }
  74. }
  75.  
  76.  
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement