⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 mainold.cpp

📁 这个程序是一个Win32程序
💻 CPP
字号:
//
// Main.cpp - Simple Dialog Program Entrypoint
// Rev. 1
//
// by Razorfish (razorfish2k@bigfoot.com)
// ------------------------------------------------------------------
//

// How to Compile (Visual C++ 4.0)
// First, Build->Settings->Link and add "libcmt.lib"
// Second, in the same list of libs, add the text: 
//     /NODEFAULTLIB:LIBCD 
// even though it's not a lib, it works.
// Third, Compile.

#define STRICT
#define _MT					// For Multi-Threading
#pragma warning( disable : 4201 4514) 

#include <windows.h>		// Win32 API Functions
#include <process.h>		// _beginthread and _endthread
#include <stdlib.h>			// ftoa, atof
#include <stdio.h>			// sprintf
#include "GenClass.h"		// General Classes

// Prototypes
// ------------------------------------------------------------------

LRESULT CALLBACK SimpleWndProc (HWND hwnd, UINT message, 
								WPARAM wParam, LPARAM lParam);
BOOL CALLBACK SimpleDlgProc (HWND hDlg, UINT message, WPARAM wParam, 
							 LPARAM lParam);
void AppendText(HWND hwndEdit, char *text);
void ClearText(HWND hwndEdit);
void fibwrapper1(void *dummy);
void fibwrapper2(void *dummy);
void fibwrapper3(void *dummy);
double fib(double n);
double fib2(double n);
double fib3(double n);

// Some Global Variables (Yucky!)
// ------------------------------------------------------------------

HINSTANCE g_hInst;
char AppTitle[80];

char buffer[1000];
double the_fibonacci_index = 24.0;
double the_fibonacci_number;

char *EditText;
HWND hwndEdit;
HMENU hMenu;
DWORD start;
DWORD end;

// WinMain - Program Entry Point
// ------------------------------------------------------------------

int WINAPI WinMain (HINSTANCE hInstance, HINSTANCE hPrevInstance,
					PSTR szCmdLine, int iCmdShow)
{		
	HWND w1;
	MSG msg;
	g_hInst = hInstance;
	hPrevInstance; szCmdLine;

	strcpy(AppTitle, "Fibonacci Program");
		
	cWindow myWindow
              (hInstance, "MySimpleWindow", SimpleWndProc, iCmdShow);
	myWindow.setMenu(MAKEINTRESOURCE(IDR_FIBMENU));
	myWindow.reg();

	w1 = myWindow.create(AppTitle);

	myWindow.show(w1);

	while (GetMessage (&msg, NULL, 0, 0))
	{
		TranslateMessage (&msg);
		DispatchMessage (&msg);
	}	
		
	return msg.wParam;
}

// Window Procedure (Message Processor)
// ------------------------------------------------------------------

LRESULT CALLBACK SimpleWndProc 
              (HWND hwnd, UINT message, WPARAM wParam, LPARAM lParam)
{
	static cDialog NewDlg(hwnd, MAKEINTRESOURCE(IDD_FIBDLG), 
		(DLGPROC)SimpleDlgProc);

	switch (message)
	{
	case WM_CREATE: 
		{
			hwndEdit = CreateWindow("EDIT", "", WS_CHILD | WS_VISIBLE 
				| WS_HSCROLL | WS_VSCROLL | ES_MULTILINE | 
				ES_WANTRETURN, CW_USEDEFAULT, CW_USEDEFAULT, 
				CW_USEDEFAULT, CW_USEDEFAULT, hwnd, (HMENU)200, 
				g_hInst, NULL);      

			EditText = new char[80];
			strcpy(EditText, 
				"Welcome to SouperMan's Fibonacci Program.");
			SendMessage(hwndEdit, WM_SETTEXT, 0, (LPARAM)EditText); 
			AppendText(hwndEdit, "\r\n\r\nPlease choose a fibonacci ");
			AppendText(hwndEdit, "index number from the Option menu.");
			sprintf(buffer, "\r\nCurrent fibonacci index is: %0.0f", 
				the_fibonacci_index);
			AppendText(hwndEdit, buffer);

			NewDlg.doCreate(lParam);
			break;
		}
	case WM_SIZE:
		{
			if(wParam != SIZE_MINIMIZED)
			MoveWindow(GetDlgItem(hwnd, 200), 0, 0, LOWORD(lParam),
            HIWORD(lParam), TRUE);
			break;
		}
 	case WM_COMMAND:
		{
			hMenu = GetMenu(hwnd);
			switch (LOWORD (wParam))
			{
// ------------------------------------------------------------------
			case ID_OPTION_CHOOSE_N: 
				{      		
					NewDlg.show();   
					sprintf(buffer, 
						"\r\n\r\nCurrent fibonacci index is: %0.0f", 
						the_fibonacci_index);  
					AppendText(hwndEdit, buffer);
					break;
				}
			case ID_OPTION_FIB1:
				{
					sprintf(buffer, 
						"\r\n\r\nCalculating f(%0.0f) using a recursive function. Please wait ...", 
						the_fibonacci_index);  
					AppendText(hwndEdit, buffer);	
					EnableMenuItem(hMenu, ID_OPTION_FIB1, MF_GRAYED); 
					EnableMenuItem(hMenu, ID_OPTION_FIB2, MF_GRAYED); 
					EnableMenuItem(hMenu, ID_OPTION_FIB3, MF_GRAYED); 
					EnableMenuItem(hMenu, ID_OPTION_CHOOSE_N, MF_GRAYED); 
					
					start = GetTickCount();
					_beginthread( fibwrapper1, 0, NULL);
					break;		
				}
			case ID_OPTION_FIB2: 
				{
					sprintf(buffer, 
						"\r\n\r\nCalculating f(%0.0f) using a large array. Please wait ...", 
						the_fibonacci_index);  
					AppendText(hwndEdit, buffer);	
					EnableMenuItem(hMenu, ID_OPTION_FIB1, MF_GRAYED); 
					EnableMenuItem(hMenu, ID_OPTION_FIB2, MF_GRAYED); 
					EnableMenuItem(hMenu, ID_OPTION_FIB3, MF_GRAYED); 
					EnableMenuItem(hMenu, ID_OPTION_CHOOSE_N, MF_GRAYED); 

					start = GetTickCount();
					_beginthread( fibwrapper2, 0, NULL);
					break;
				}
			case ID_OPTION_FIB3:
				{
					sprintf(buffer, 
						"\r\n\r\nCalculating f(%0.0f) using a tiny array. Please wait ...", 
						the_fibonacci_index);  
					AppendText(hwndEdit, buffer);	
					EnableMenuItem(hMenu, ID_OPTION_FIB1, MF_GRAYED); 
					EnableMenuItem(hMenu, ID_OPTION_FIB2, MF_GRAYED); 
					EnableMenuItem(hMenu, ID_OPTION_FIB3, MF_GRAYED); 
					EnableMenuItem(hMenu, ID_OPTION_CHOOSE_N, MF_GRAYED); 

					start = GetTickCount();
					_beginthread( fibwrapper3, 0, NULL);
					break;
				}
			case ID_FILE_EXIT: 
				{
					SendMessage (hwnd, WM_CLOSE, 0, 0);
					return 0;
				}
			case ID_HELP_ABOUT: 
				{
					cMsgBox 
						AboutMsg(
						"This program has been brought to you by RazorFish!\n(c) 2000, Razorfish (http://takahiro.cjb.com/)", 
						"About Fibonacci Program...");
					AboutMsg.show(); 
				}
			} // end switch(LOWORD(WPARAM))
		}
		break; // end WM_COMMAND
	case WM_DESTROY:		
		{
			PostQuitMessage(0);
			return 0;		
		}
	} // end switch(message) 
	return DefWindowProc (hwnd, message, wParam, lParam);
}


// Dialog Procedure (Message Processor)
// ------------------------------------------------------------------
BOOL CALLBACK SimpleDlgProc(HWND hDlg, UINT message, WPARAM wParam, 
							LPARAM lParam)
{
	//lParam; wParam;

	switch (message)
	{
	case WM_INITDIALOG: 
		{
			SetDlgItemText(hDlg, IDC_NUM, 
				itoa((int)the_fibonacci_index, buffer, 10));  
			return TRUE;
		}		
	case WM_COMMAND:
		{			
			if (LOWORD(wParam) == IDOK) 
			{
				GetDlgItemText(hDlg, IDC_NUM, buffer, 20);
				if ((atof(buffer) > 1000000.0) ||  (the_fibonacci_index < 1.0)) 
				{
					cMsgBox temp1(
						"Please choose a number less than a million.", 
						"Sorry.");
					temp1.show();
				} else 
				{		  
					the_fibonacci_index = atof(buffer);
					EndDialog(hDlg, FALSE);
				}    
				return FALSE;        
			}
			if (LOWORD(wParam) == IDCANCEL) 
			{
				EndDialog (hDlg, FALSE);		
			}
			return TRUE;				
		} // end WM_COMMAND
	} // end switch(message)
	return FALSE;
}

// Add Text to Edit Control
// ------------------------------------------------------------------

void AppendText(HWND hwndEdit, char *text)
{
    int oldlen;
    char *oldtext;

    oldlen = strlen(EditText);
    oldtext = new char[oldlen + 1];
    sprintf(oldtext, "%s", EditText);      
    
    delete EditText;

    if (oldlen > 1) 
	{
		EditText = new char[strlen(text) + oldlen + 1];
		sprintf(EditText, "%s%s", oldtext, text);
    }
    else 
	{
		EditText = new char[strlen(text) + 11];
		sprintf(EditText, "%s!!!!", text);
    }

    delete oldtext;

    SendMessage(hwndEdit, WM_SETTEXT, 0, 
                (LPARAM)EditText); 
}

// Clear All Text in Edit Control
// ------------------------------------------------------------------

void ClearText(HWND hwndEdit)
{

    delete EditText;
    EditText = new char[1];

    sprintf(EditText, "\0");

    SendMessage(hwndEdit, WM_SETTEXT, 0, 
                (LPARAM)EditText); 

}

// Fibonacci Method 1 - Recursion
// ------------------------------------------------------------------

void fibwrapper1(void *dummy)
{

	the_fibonacci_number = fib(the_fibonacci_index);
	sprintf(buffer, "\r\nResult: f(%0.0f) = %0.0f.", the_fibonacci_index, the_fibonacci_number);  
	AppendText(hwndEdit, buffer);
  	EnableMenuItem(hMenu, ID_OPTION_FIB1, MF_ENABLED); 
	EnableMenuItem(hMenu, ID_OPTION_FIB2, MF_ENABLED); 
	EnableMenuItem(hMenu, ID_OPTION_FIB3, MF_ENABLED); 
	EnableMenuItem(hMenu, ID_OPTION_CHOOSE_N, MF_ENABLED); 

	end = GetTickCount();
	sprintf(buffer, "\r\nTime elapsed: %d milliseconds", (int)end-start);
	AppendText(hwndEdit, buffer);
	the_fibonacci_number = 0;

	_endthread();
}

double fib(double n)
{
	if((n==1)||(n==2))
	{
		return(1);
	}
	else
	{
		return(fib(n-1)+fib(n-2));
	}
} 

// Fibonacci Method 2 - Large Array
// ------------------------------------------------------------------

void fibwrapper2(void *dummy)
{

	the_fibonacci_number = fib2(the_fibonacci_index);
	sprintf(buffer, "\r\nResult: f(%0.0f) = %0.0f.", the_fibonacci_index, the_fibonacci_number);  
	AppendText(hwndEdit, buffer);
  	EnableMenuItem(hMenu, ID_OPTION_FIB1, MF_ENABLED); 
	EnableMenuItem(hMenu, ID_OPTION_FIB2, MF_ENABLED); 
	EnableMenuItem(hMenu, ID_OPTION_FIB3, MF_ENABLED); 
	EnableMenuItem(hMenu, ID_OPTION_CHOOSE_N, MF_ENABLED); 

	end = GetTickCount();
	sprintf(buffer, "\r\nTime elapsed: %d milliseconds", (int)end-start);
	AppendText(hwndEdit, buffer);
	the_fibonacci_number = 0;

	_endthread();
}

double fib2(double n)
{
	double *F, ans;
	F = new double[(int)n+1];
	F[1] = 1.0;
	F[2] = 1.0;
	for (int i=3; i<n+1; i++)
	{
		F[i] = F[i-1] + F[i-2];
	}
	ans = F[(int)n];
	delete F;
	return ans;
}

// Fibonacci Method 3 - Tiny Array
// ------------------------------------------------------------------

void fibwrapper3(void *dummy)
{

	the_fibonacci_number = fib3(the_fibonacci_index);
	sprintf(buffer, "\r\nResult: f(%0.0f) = %0.0f.", the_fibonacci_index, the_fibonacci_number);  
	AppendText(hwndEdit, buffer);
  	EnableMenuItem(hMenu, ID_OPTION_FIB1, MF_ENABLED); 
	EnableMenuItem(hMenu, ID_OPTION_FIB2, MF_ENABLED); 
	EnableMenuItem(hMenu, ID_OPTION_FIB3, MF_ENABLED); 
	EnableMenuItem(hMenu, ID_OPTION_CHOOSE_N, MF_ENABLED); 

	end = GetTickCount();
	sprintf(buffer, "\r\nTime elapsed: %d milliseconds", (int)end-start);
	AppendText(hwndEdit, buffer);
	the_fibonacci_number = 0;

	_endthread();
}


double fib3(double n)
{
	double F[2];
	int flip = 1;	
	F[0] = 1.0;
	F[1] = 1.0;
	for (int i=3; i<n+1; i++) 
	{
		if (flip == 1)
			F[0] = F[0] + F[1];
		else
			F[1] = F[0] + F[1];
		flip = -flip;
	}
	if (flip == 1)
	{
		return F[1];
	}
	else
	{
		return F[0];
	}	
}

⌨️ 快捷键说明

复制代码 Ctrl + C
搜索代码 Ctrl + F
全屏模式 F11
切换主题 Ctrl + Shift + D
显示快捷键 ?
增大字号 Ctrl + =
减小字号 Ctrl + -