C语言数组学习 - 使用窗口版程序演示

C数组基本;

使用数组输出 4*4 矩阵;

二维数组;

/*-------------------------------------------------
bobo, 2020
-------------------------------------------------*/

#include <windows.h>
#include <windowsx.h>

LRESULT CALLBACK WndProc(HWND, UINT, WPARAM, LPARAM);

int WINAPI WinMain(HINSTANCE hInstance, HINSTANCE hPrevInstance,
	PSTR  szCmdLine, int iCmdShow)
{
	static TCHAR szAppName[] = TEXT("numsDemo");
	HWND         hwnd;
	MSG          msg;
	WNDCLASS     wndclass;

	wndclass.style = CS_HREDRAW | CS_VREDRAW;
	wndclass.lpfnWndProc = WndProc;
	wndclass.cbClsExtra = 0;
	wndclass.cbWndExtra = 0;
	wndclass.hInstance = hInstance;
	wndclass.hIcon = LoadIcon(NULL, IDI_APPLICATION);
	wndclass.hCursor = LoadCursor(NULL, IDC_ARROW);
	wndclass.hbrBackground = (HBRUSH)GetStockObject(WHITE_BRUSH);
	wndclass.lpszMenuName = NULL;
	wndclass.lpszClassName = szAppName;

	if (!RegisterClass(&wndclass))
	{
		MessageBox(NULL, TEXT("Program requires Windows NT!"),
			szAppName, MB_ICONERROR);
		return 0;
	}

	hwnd = CreateWindow(szAppName, TEXT("C语言数组Demo"),
		WS_OVERLAPPEDWINDOW,
		CW_USEDEFAULT, CW_USEDEFAULT,
		CW_USEDEFAULT, CW_USEDEFAULT,
		NULL, NULL, hInstance, NULL);

	ShowWindow(hwnd, iCmdShow);
	UpdateWindow(hwnd);

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

LRESULT CALLBACK WndProc(HWND hwnd, UINT message, WPARAM wParam, LPARAM lParam)
{
	HDC         hdc;
	PAINTSTRUCT ps ;
	char buffer[65];
	char buf2[30];
	int nums[10];
    int i,j;
	int sum = 0;  //当前科目的总成绩
    int average;  //总平均分
    int v[3];  //各科平均分

	int a[4] = {203, 345, 700, 22};
	int b[4] = {56725, 9999, 20098, 2};
	int c[4] = {233, 205, 11, 6656};
	int d[4] = {34, 99, 23, 230779};

	int cj[5][3] = {{80,75,92}, {61,65,71}, {59,63,70}, {85,87,90}, {76,77,85}};
	
	switch (message)
	{
	case WM_CREATE:
		return 0;

	case WM_SIZE:
		return 0;

	case WM_RBUTTONDOWN:
		return 0;

	case WM_LBUTTONDOWN:
		hdc = GetDC(hwnd);	
		for(i=0; i<10; i++){
			nums[i] = (i+1);
			wsprintf(buffer,"%d",nums[i]);
			TextOut(hdc, 100, 20+i*20, buffer, 2);
		}

		wsprintf(buffer, "%-9d %-9d %-9d %-9d\n", a[0], a[1], a[2], a[3]);
		TextOut(hdc, 300, 20, buffer, 36);
		wsprintf(buffer, "%-9d %-9d %-9d %-9d\n", b[0], b[1], b[2], b[3]);
		TextOut(hdc, 300, 40, buffer, 36);
		wsprintf(buffer, "%-9d %-9d %-9d %-9d\n", c[0], c[1], c[2], c[3]);
		TextOut(hdc, 300, 60, buffer, 36);
		wsprintf(buffer, "%-9d %-9d %-9d %-9d\n", d[0], d[1], d[2], d[3]);
		TextOut(hdc, 300, 80, buffer, 36);
		
		for(i=0; i<3; i++){
			for(j=0; j<5; j++){
				sum += cj[j][i];  //计算当前科目的总成绩
			}
			v[i] = sum / 5;  // 当前科目的平均分
			sum = 0;
		}
		average = (v[0] + v[1] + v[2]) / 3;
		wsprintf(buffer, "Math:  %d    C Languag:  %d    English:  %d", v[0], v[1], v[2]);
		TextOut(hdc, 600, 20, buffer, 43);
		wsprintf(buf2, "Total: %d\n", average);
		TextOut(hdc, 600, 120, buf2, 9);		
		
		return 0;

	case WM_PAINT:
		hdc = BeginPaint(hwnd, &ps);

		EndPaint(hwnd, &ps);
		return 0;

	case WM_DESTROY:
		PostQuitMessage(0);
		return 0;
	}
	return DefWindowProc(hwnd, message, wParam, lParam);
}

发布了434 篇原创文章 · 获赞 512 · 访问量 294万+

猜你喜欢

转载自blog.csdn.net/bcbobo21cn/article/details/103931160
今日推荐