#include
#include
#include
using namespace std;
//m*n 阶矩阵
class M
{
public:
M(int m, int n);
M(M& m);
virtual ~M();
int m;
int n;
private:
int *a;
friend istream& operator>>( istream& in, M& m );
friend ostream& operator<<( ostream& out, M& m );
friend M& operator+(M& a, M& b);
};
M::M(int m=1, int n=1) : m(m), n(n)
{
a = new int[n*m];
}
//拷贝构造
M::M(M& m) : m(m.m), n(m.n)
{
a = new int[m.n*m.m];
memcpy(a, m.a, m.n*m.m*sizeof(int));
}
M::~M()
{
delete a;
}
//输入矩阵
istream& operator>>( istream& in, M& m )
{
cout<<"input "<
cout<<"input line "< for( int j=0; j
in >> (*(m.a+i*m.n+j));
}
}
return in;
}
//输出矩阵
ostream& operator<<( ostream& out, M& m )
{
for( int i=0; i
for( int j=0; j
out<
out<
return out;
}
//矩阵加法
M& operator+(M& a, M& b)
{
if( !( a.m==b.m && a.n==b.n ) )
{
cout<<"Error: can not +"<
M m(a);
for( int i=0; i
for( int j=0; j
*(m.a+i*m.n+j) += *(b.a+i*m.n+j);
}
}
M* x = new M(m);
return *x;
}
int main(int argc, char *argv[])
{
M a(2, 3), b(2, 3);
cin>>a>>b;
M c = a + b;
cout<
return 0;
}
就会第一题,第二题的GUI没有研究,以下是C++程序代码:
#include
HDC
hdc;
PAINTSTRUCT
ps;
RECT
rect;
LRESULT
CALLBACK
WndProc
(HWND,
UINT,
WPARAM,
LPARAM)
;
int
WINAPI
WinMain
(HINSTANCE
hInstance,
HINSTANCE
hPrevInstance,
PSTR
szCmdLine,
int
nCmdShow)
{
static
TCHAR
szAppName
[]="applicaiton";
HWND
hwnd;
WNDCLASS
wndcls;
MSG
msg;
wndcls.cbClsExtra=0;
wndcls.cbWndExtra=0;
wndcls.hbrBackground=(HBRUSH)GetStockObject(WHITE_BRUSH);
wndcls.hCursor=LoadCursor(NULL,IDC_ARROW);
wndcls.hIcon=LoadIcon(NULL,IDI_APPLICATION);
wndcls.hInstance=hInstance;
wndcls.lpfnWndProc=WndProc;
wndcls.lpszMenuName=NULL;
wndcls.lpszClassName=szAppName;
wndcls.style=CS_HREDRAW|CS_VREDRAW;
if(!RegisterClass(&wndcls))
{
MessageBox(NULL,"Error","error",MB_ICONERROR);
return
0;
}
hwnd=CreateWindowEx(WS_EX_TOPMOST,
szAppName,
"windows窗口程序Demo",
WS_OVERLAPPEDWINDOW,
0,0,
400,300,
NULL,NULL,
hInstance,
NULL);
ShowWindow(hwnd,nCmdShow);
UpdateWindow(hwnd);
DWORD
start_time;
while(true)
{
hdc=GetDC(hwnd);
start_time=GetTickCount();
if(PeekMessage(&msg,NULL,0,0,PM_REMOVE))
{
if(msg.message==WM_QUIT)
break;
TranslateMessage(&msg);
DispatchMessage(&msg);
}
GetClientRect
(hwnd,
&rect)
;
FillRect(hdc,&rect,CreateSolidBrush(RGB(rand()%255,rand()%255,rand()%255)));
while(GetTickCount()-start_time<250)
{}
ReleaseDC(hwnd,hdc);
}
return
msg.wParam;
}
LRESULT
CALLBACK
WndProc
(HWND
hwnd,
UINT
msg,
WPARAM
wapram,
LPARAM
lapram)
{
switch(msg)
{
case
WM_PAINT:
{
hdc
=
BeginPaint
(hwnd,
&ps)
;
GetClientRect
(hwnd,
&rect)
;
FillRect(hdc,&rect,CreateSolidBrush(RGB(rand()%255,rand()%255,rand()%255)));
EndPaint
(hwnd,
&ps)
;
return
0;
}
break;
case
WM_DESTROY:
{
PostQuitMessage
(0)
;
return
0
;
}
break;
}
return
DefWindowProc
(hwnd,
msg,
wapram,
lapram)
;
}
注意:最后在菜单栏选择工程->设置->链接,或者快捷键
alt+F7,调出对话框,将工程选项的subsystem:console的console改为windows,嘿嘿!!!
搞定,运行即可。哈哈
由于这个程序使用的是GDI函数直接调用,没有优化,比较占资源,刚开始反映可能会有些迟钝。
就会第一题,第二题的GUI没有研究,以下是C++程序代码:
#include
HDC hdc;
PAINTSTRUCT ps;
RECT rect;
LRESULT CALLBACK WndProc (HWND,
UINT,
WPARAM,
LPARAM) ;
int WINAPI WinMain (HINSTANCE hInstance,
HINSTANCE hPrevInstance,
PSTR szCmdLine,
int nCmdShow)
{
static TCHAR szAppName []="applicaiton";
HWND hwnd;
WNDCLASS wndcls;
MSG msg;
wndcls.cbClsExtra=0;
wndcls.cbWndExtra=0;
wndcls.hbrBackground=(HBRUSH)GetStockObject(WHITE_BRUSH);
wndcls.hCursor=LoadCursor(NULL,IDC_ARROW);
wndcls.hIcon=LoadIcon(NULL,IDI_APPLICATION);
wndcls.hInstance=hInstance;
wndcls.lpfnWndProc=WndProc;
wndcls.lpszMenuName=NULL;
wndcls.lpszClassName=szAppName;
wndcls.style=CS_HREDRAW|CS_VREDRAW;
if(!RegisterClass(&wndcls))
{
MessageBox(NULL,"Error","error",MB_ICONERROR);
return 0;
}
hwnd=CreateWindowEx(WS_EX_TOPMOST,
szAppName,
"windows窗口程序Demo",
WS_OVERLAPPEDWINDOW,
0,0,
400,300,
NULL,NULL,
hInstance,
NULL);
ShowWindow(hwnd,nCmdShow);
UpdateWindow(hwnd);
DWORD start_time;
while(true)
{
hdc=GetDC(hwnd);
start_time=GetTickCount();
if(PeekMessage(&msg,NULL,0,0,PM_REMOVE))
{ if(msg.message==WM_QUIT)
break;
TranslateMessage(&msg);
DispatchMessage(&msg);
}
GetClientRect (hwnd, &rect) ;
FillRect(hdc,&rect,CreateSolidBrush(RGB(rand()%255,rand()%255,rand()%255)));
while(GetTickCount()-start_time<250)
{}
ReleaseDC(hwnd,hdc);
}
return msg.wParam;
}
LRESULT CALLBACK WndProc (HWND hwnd, UINT msg, WPARAM wapram, LPARAM lapram)
{
switch(msg)
{
case WM_PAINT:
{
hdc = BeginPaint (hwnd, &ps) ;
GetClientRect (hwnd, &rect) ;
FillRect(hdc,&rect,CreateSolidBrush(RGB(rand()%255,rand()%255,rand()%255)));
EndPaint (hwnd, &ps) ;
return 0;
}
break;
case WM_DESTROY:
{
PostQuitMessage (0) ;
return 0 ;
}
break;
}
return DefWindowProc (hwnd, msg, wapram, lapram) ;
}
注意:最后在菜单栏选择工程->设置->链接,或者快捷键 alt+F7,调出对话框,将工程选项的subsystem:console的console改为windows,嘿嘿!!!
搞定,运行即可。哈哈
由于这个程序使用的是GDI函数直接调用,没有优化,比较占资源,刚开始反映可能会有些迟钝。