原始碼
#include "stdafx.h"
#include "Ethernet.h"
#include "CSMA030390.h"
#include "cmath"
#ifdef _DEBUG
#define new DEBUG_NEW
#undef THIS_FILE
#endif
static char THIS_FILE[] = __FILE__;
// The one and only application object
CWinThread *thread1,*thread2;//定義變數
DWORD ID1,ID2,Bus=0;//初始化共享記憶體
UINT aThread(LPVOID pParam);//執行緒A代表主機A
UINT bThread(LPVOID pParam);//執行緒B代表主機B
using namespace std;
CWinApp theApp;
using namespace std;
int _tmain(int argc, TCHAR* argv[], TCHAR* envp[])
{
int nRetCode = 0;
// initialize MFC and print and error on failure
if (!AfxWinInit(::GetModuleHandle(NULL), NULL, ::GetCommandLine(), 0))
{
// TODO: change error code to suit your needs
cerr << _T("Fatal Error: MFC initialization failed") << endl;
nRetCode = 1;
}
else
{
// TODO: code your application's behavior here.
//CString strHello;
//strHello.LoadString(IDS_HELLO);
//cout << (LPCTSTR)strHello << endl;
thread1=AfxBeginThread(aThread,NULL);//啟動執行緒A
ID1=thread1->m_nThreadID;//執行緒A的ID號
thread2=AfxBeginThread(bThread,NULL);
ID2=thread2->m_nThreadID;
getchar();
}
return nRetCode;
}
UINT aThread(LPVOID pParam)
{
int i=0;
int CollisionCounter=16;//沖突次數
double collisionWindow=0.005;//沖突視窗取0.005
int randNum=rand()%3;//亂數
Loop:if(Bus==0)
{
Bus=Bus|ID1;//或符號
Sleep(12);
if(Bus==ID1)
{
printf("%d Send Success/n/n",ID1);
Bus=0;
CollisionCounter=16;
Sleep(rand()%10);
i++;
printf("主機A發送成功次數=%d/n/n",i);
if(i<10)
goto Loop;
}
else
{
printf("%d Send Collision/n/n",ID1);
Bus=0;
CollisionCounter--;
if(CollisionCounter>0)
{
Sleep(randNum*(int)pow(2,(CollisionCounter>10)?10:CollisionCounter)*collisionWindow);
goto Loop;
}
else
printf("%ld send Failure/n/n",ID1);
}
}
goto Loop;
return 0;
}
UINT bThread(LPVOID pParam)
{
int j=0;
int CollisionCounter=16;
double collisionWindow=0.005;
int randNum=rand()%3;
Loop:if(Bus==0)
{
Sleep(2);
Bus=Bus|ID2;
Sleep(3);
if(Bus==ID2)
{
printf("%d Send Success/n/n",ID2);
Bus=0;
CollisionCounter=16;
Sleep(rand()%10);
j++;
printf("主機B發送成功次數=%d/n/n",j);
if(j<10)
goto Loop;
}
else
{
printf("%d Send Collision/n/n",ID2);
Bus=0;
CollisionCounter--;
if(CollisionCounter>0)
{
Sleep(randNum*(int)pow(2,(CollisionCounter>10)?10:CollisionCounter)*collisionWindow);
goto Loop;
}
else
printf("%ld send Failure/n/n",ID2);
}
}
goto Loop;
return 0;
}
錯誤:
Cannot open include file: 'stdafx.h': No such file or directory
Cannot open include file: 'Ethernet.h': No such file or directory
Cannot open include file: 'CSMA030390.h': No such file or directory
三個頭檔案的錯誤是這樣的
把他們注釋后,
錯誤是下面這樣
--------------------Configuration: SendFrame - Win32 Debug--------------------
Compiling...
SendFrame.cpp
D:\sendTCP\SendFrame.cpp(11) : error C2143: syntax error : missing ';' before '*'
D:\sendTCP\SendFrame.cpp(11) : error C2501: 'CWinThread' : missing storage-class or type specifiers
D:\sendTCP\SendFrame.cpp(11) : error C2501: 'thread1' : missing storage-class or type specifiers
D:\sendTCP\SendFrame.cpp(11) : error C2501: 'thread2' : missing storage-class or type specifiers
D:\sendTCP\SendFrame.cpp(12) : error C2146: syntax error : missing ';' before identifier 'ID1'
D:\sendTCP\SendFrame.cpp(12) : error C2501: 'DWORD' : missing storage-class or type specifiers
D:\sendTCP\SendFrame.cpp(12) : fatal error C1004: unexpected end of file found
Error executing cl.exe.
SendFrame.obj - 7 error(s), 0 warning(s)
uj5u.com熱心網友回復:
頭檔案不可以注釋掉就完事,找不到你找原因,解決掉它,應該是工程包含目錄不對找不到后面的錯誤都是因為沒有正確找到定義出現的。
轉載請註明出處,本文鏈接:https://www.uj5u.com/houduan/244059.html
標籤:C++ 語言
上一篇:大家看一下我哪里有問題,謝謝! 利用函式編程:從鍵盤輸入一個由大小寫字母組成的字串,統計與第一個字母相同的字母有幾個。
