C语言数组学习 - 使用窗口版程序演示
生活随笔
收集整理的這篇文章主要介紹了
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); }總結
以上是生活随笔為你收集整理的C语言数组学习 - 使用窗口版程序演示的全部內容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: netcore 学习
- 下一篇: Win32 API 窗口版本转换度分秒为