并行计算实现判断一个数是不是素数--Win32和.Net两种方式结合

  1. // Win32D.cpp : 定义控制台应用程序的入口点。  
  2. //  
  3.   
  4. #include "stdafx.h"  
  5. #include <windows.h>  
  6. #include <process.h>  
  7. #include <math.h>  
  8. #include <iostream>  
  9. #include <fstream>  
  10. using namespace std;  
  11. int m;  
  12. float c;  
  13. int flag=0;  
  14. int c2;  
  15. HANDLE Thread1,Thread2;  
  16. void StartThread1(LPVOID param)  
  17. {  
  18.     for(int i=1;i<=m;i+=2)  
  19.     {  
  20.         if(flag==0)  
  21.         {  
  22.             if(c2%i==0&&i!=1)  
  23.                 flag=1;  
  24.         }  
  25.         else  
  26.             break;  
  27.     }  
  28. }  
  29.   
  30. void StartThread2(LPVOID param)  
  31. {  
  32.     for(int j=2;j<=m;j+=2)  
  33.     {  
  34.         if(flag==0)  
  35.         {  
  36.             if(c2%j==0)  
  37.                 flag=1;  
  38.         }  
  39.         else  
  40.             break;  
  41.     }  
  42. }  
  43.   
  44.   
  45. int _tmain(int argc, _TCHAR* argv[])  
  46. {  
  47.     cin>>c;  
  48.     m=sqrt(c);  
  49.     c2=(int)c;  
  50.     Thread1=CreateEvent(NULL,FALSE,FALSE,NULL);  
  51.     //cout<<"1"<<endl;  
  52.     Thread2=CreateEvent(NULL,FALSE,FALSE,NULL);  
  53.     //cout<<"2"<<endl;  
  54.     _beginthread(StartThread1,0,NULL);  
  55.     //cout<<"3"<<endl;  
  56.     _beginthread(StartThread2,0,NULL);  
  57.     //cout<<"4"<<endl;  
  58.     //WaitForSingleObject(Thread2,INFINITE);  
  59.     cout<<"5"<<endl;  
  60.     if(flag==0)  
  61.         cout<<"是素数"<<endl;  
  62.     else   
  63.         cout<<"不是素数"<<endl;  
  64.     cin>>c;//保留输出框  
  65.     return 0;  
  66. }  
  67. /*  
  68.     .NET并行判断一个数是不是素数  
  69.  */  
  70.   
  71. using System;  
  72. using System.Collections.Generic;  
  73. using System.Linq;  
  74. using System.Text;  
  75. using System.Threading;  
  76. namespace DotNetWyYt  
  77. {  
  78.     class Program  
  79.     {  
  80.         public static int m;  
  81.         public static int c;  
  82.         public static int flag = 0;  
  83.         public static int c2;  
  84.         public  static void WithDraw1()  
  85.         {  
  86.             for (int i = 1; i <= m; i += 2)  
  87.             {  
  88.                 if (flag == 0)  
  89.                 {  
  90.                     if (c2 % i == 0 && i != 1)  
  91.                         flag = 1;  
  92.                 }  
  93.                 else  
  94.                     break;  
  95.             }  
  96.         }  
  97.         public static void WithDraw2()  
  98.         {  
  99.             for (int j = 2; j <= m; j += 2)  
  100.             {  
  101.                 if (flag == 0)  
  102.                 {  
  103.                     if (c2 % j == 0)  
  104.                         flag = 1;  
  105.                 }  
  106.                 else  
  107.                     break;  
  108.             }  
  109.         }  
  110.         static  void Main(string[] args)  
  111.         {  
  112.             c = 23;  
  113.             m = (Int32)Math.Sqrt((double)c);  
  114.             c2 = (int)c;  
  115.             ThreadStart thread1 = new ThreadStart(WithDraw1);  
  116.             Thread newThread1 = new Thread(thread1);  
  117.             ThreadStart thread2 = new ThreadStart(WithDraw2);  
  118.             Thread newThread2 = new Thread(thread2);  
  119.             newThread1.Start();  
  120.             newThread2.Start();  
  121.             if (flag == 0)  
  122.                 Console.WriteLine("是素数");  
  123.             else  
  124.                 Console.WriteLine("不是素数");  
  125.         }  
  126.     }  
  127. }  

你可能感兴趣的:(并行计算实现判断一个数是不是素数--Win32和.Net两种方式结合)