首页 文章 精选 留言 我的

精选列表

搜索[c 17],共10000篇文章
优秀的个人博客,低调大师

C#函数重载

using System; using System.Collections.Generic; using System.Linq; using System.Text; using System.Threading.Tasks; namespace Demo { class Program { static int MaxValue(int[] intArray) { int maxVal = intArray[0]; for (int i = 1; i < intArray.Length; i++) { if (intArray[i] > maxVal) { maxVal = intArray[i]; } } return maxVal; } /// <summary> /// 函数重载了 /// </summary> /// <param name="doubleArray"></param> /// <returns></returns> static double MaxValue(double[] doubleArray) { double maxVal = doubleArray[0]; for (int i = 1; i < doubleArray.Length; i++) { if (doubleArray[i] > maxVal) { maxVal = doubleArray[i]; } } return maxVal; } static void Main(string[] args) { int[] myArray = { 1, 3, 5, 8, 2, 10, 9, 100, 40 }; int maxVal = MaxValue(myArray); Console.WriteLine("最大值为:{0}", maxVal); double[] doubleArray = { 1, 3, 5, 8, 2, 10, 9.2, 100.11, 40 }; double maxDoubleVal = MaxValue(doubleArray); Console.WriteLine("最大值为:{0}", maxDoubleVal); Console.ReadKey(); } } } 本文转自TBHacker博客园博客,原文链接:http://www.cnblogs.com/jiqing9006/p/6748280.html,如需转载请自行联系原作者

优秀的个人博客,低调大师

C++ Exercises(十)

1.1 找出第K大的数 方法1: #include <iostream> #include <vector> #include <algorithm> #include <iterator> using namespace std; int main() { int data[] = {3,54,254,52,13,667,234,67,256,78,467,32,65,324,889,34,5}; int len = sizeof(data)/sizeof(int); vector<int> v1(data,data+len); ostream_iterator<int> out(cout," "); copy(v1.begin(),v1.end(),out); cout<<endl; sort(v1.begin(),v1.end(),greater<int>()); copy(v1.begin(),v1.end(),out); cout<<endl; int k; cin>>k; cout<<v1[k-1]<<endl; system("pause"); return 0; } 方法2: #include <iostream> #include <list> #include <algorithm> #include <iterator> using namespace std; class LessThan { public: LessThan(int val):value(val){} ~LessThan(){} bool operator()(int rhs) { return rhs<=value; } private: int value; }; int main() { int data[] = {3,54,254,52,475,667,234,67,256,78,467,32,65,324,889,34,5}; int len = sizeof(data)/sizeof(int); int k; cin>>k; list<int> list1(data,data+k); ostream_iterator<int> out(cout," "); list1.sort(greater<int>());//对前K个数进行排序 list<int>::iterator pos; for(int i=k;i<len;++i) { if(data[i]>list1.back()) {//比最后一个元素大 pos = find_if(list1.begin(),list1.end(),LessThan(data[i]));//找到第一个比要插入值小的元素位置 list1.insert(pos,data[i]);//插入新值 list1.pop_back();//删除最后一个多余的元素 } } copy(list1.begin(),list1.end(),out); cout<<endl; cout<<"第"<<k<<"大的数是: "<<endl; int count = 1; for(pos = list1.begin();pos!=list1.end();++pos,++count) { if(count==k) cout<<(*pos)<<endl; } system("pause"); return 0; } 方法3:用最大堆排序 #include <iostream> #include <vector> #include <algorithm> #include <iterator> using namespace std; template<typename T> void HeapAdjust(vector<T> &v,size_t start,size_t end) { T tmp = v[start]; size_t s = start; for(size_t j=(2*s+1);j<=end;j=(2*j+1)) { if(j<end&&v[j+1]>v[j]) j++; if(tmp<v[j]) { v[s] = v[j]; s = j; } } v[s] = tmp; } template<typename T> void Swap(T &a,T &b) { T tmp = a; a = b; b = tmp; } template<typename T> void HeapSort(vector<T> &v) { int i = 0,j=0; //建最大堆 for(i=(v.size()-1)/2;i>=0;--i) { HeapAdjust(v,i,v.size()-1); } cout<<"输入K:"<<endl; int k; cin>>k; for(i=v.size(),j=k;i>1&&j>0;--i,--j) { Swap(v[0],v[i-1]); if(j==1) { cout<<v[i-1]; } else { HeapAdjust(v,0,i-2); } } } int main() { int data[]={93,5,233,55,3,67,2,67,32,6,89,355}; int len = sizeof(data)/sizeof(int); vector<int> v1(data,data+len); HeapSort(v1); return 0; } 本文转自Phinecos(洞庭散人)博客园博客,原文链接:http://www.cnblogs.com/phinecos/archive/2008/03/31/1131862.html,如需转载请自行联系原作者

优秀的个人博客,低调大师

C++ Exercises(五)

问题:在窗体上画一个圆,并且可以用鼠标拖动它在窗体上运动到任何位置 分析: 一个圆是由它的外接矩阵决定的,而外接矩阵可以由矩阵的左上角点和矩阵的高度和宽度表示,这里显然要求外接矩阵的大小是不变的,变化的仅仅是外接矩阵的左上角点,所以要处理的就是矩阵左上角点的变化。 为此设置类的成员变量如下: CPoint m_pointTopLeft;//外接矩形左上角点 const CSize m_sizeEcllipse;//外接矩形大小 CSize m_sizeOffSet;//光标距离左上角点距离 bool m_bCaptured;//鼠标是否处于被捕获状态 其中矩阵大小是常量,在初始化的时候指明,这里我指定左上角点为坐标原点(0,0),高度100,宽度100,初始的鼠标光标和原点重合(即两者距离都是0),并且鼠标未被捕获。 CTestView::CTestView():m_sizeEcllipse(100,100),m_pointTopLeft(0,0),m_sizeOffSet(0,0) { this->m_bCaptured = FALSE;//初始状态为未捕获 } 接下来就是在OnDraw里面画圆了,因为m_pointTopLeft和m_sizeEcllipse已经唯一决定了圆的位置与大小,因此直接利用这两个变量进行绘图就可以了。 void CTestView::OnDraw(CDC* pDC) { CE05Doc* pDoc = GetDocument(); ASSERT_VALID(pDoc); CBrush brushHatch(HS_DIAGCROSS, RGB(255, 0, 0)); pDC->SelectObject(&brushHatch);//选择刷子 pDC->Ellipse(CRect(m_pointTopLeft, this->m_sizeEcllipse)); } 最后就是如何响应鼠标的三个事件:down,move,up了,再这里主要就是处理左上角点的变化,在down的时候,由于第一次点击圆内的点时就确定了鼠标光标与初始左上角点的距离,在以后的move中只需要用当前所在的point减去确定的m_sizeOffSet就可以确定此时的新左上角点,并且在move中利用前后两次的外接矩阵进行重画就可以了,最后在up中把鼠标的捕获释放掉。 void CTestView::OnLButtonDown(UINT nFlags, CPoint point) { CRect rt(this->m_pointTopLeft,this->m_sizeEcllipse); CRgn rg; CClientDC dc(this); OnPrepareDC(&dc); dc.LPtoDP(&rt);//转换到设备坐标 rg.CreateEllipticRgnIndirect(&rt); if(rg.PtInRegion(point)) {//在圆内单击左键了 this->SetCapture();//捕获光标 CPoint pointTopLeft(this->m_pointTopLeft);//前一次的左上角 this->m_sizeOffSet = point-pointTopLeft;//第一次点左键时光标距离左上角的距离 this->m_bCaptured = TRUE;//捕获标志设为true ::SetCursor(::LoadCursor(NULL,IDC_CROSS));//更改光标形状 } } void CTestView::OnLButtonUp(UINT nFlags, CPoint point) { if(this->m_bCaptured) {//光标处于捕获状态中 this->m_bCaptured = FALSE; //捕获标志设为false ::ReleaseCapture();//释放鼠标捕获 } } void CTestView::OnMouseMove(UINT nFlags, CPoint point) { if(this->m_bCaptured) {//光标处于捕获状态中 CClientDC dc(this); OnPrepareDC(&dc); CRect oldRect(this->m_pointTopLeft,this->m_sizeEcllipse);//老的外接矩形 dc.LPtoDP(&oldRect); this->InvalidateRect(&oldRect,TRUE);//强制重画 this->m_pointTopLeft = point-this->m_sizeOffSet;//新的左上角点 CRect newRect(this->m_pointTopLeft,this->m_sizeEcllipse);//新外接矩形 dc.LPtoDP(&newRect); this->InvalidateRect(&newRect,TRUE); //强制重画 } 本文转自Phinecos(洞庭散人)博客园博客,原文链接:http://www.cnblogs.com/phinecos/archive/2007/06/11/778954.html,如需转载请自行联系原作者

优秀的个人博客,低调大师

C++ Exercises(十一)

1, #include <d3d9.h> #pragma warning( disable : 4996 ) // disable deprecated warning #include <strsafe.h> #pragma warning( default : 4996 ) //----------------------------------------------------------------------------- // Global variables //----------------------------------------------------------------------------- LPDIRECT3D9 g_pD3D = NULL; // Used to create the D3DDevice LPDIRECT3DDEVICE9 g_pd3dDevice = NULL; // Our rendering device //----------------------------------------------------------------------------- // Name: InitD3D() // Desc: Initializes Direct3D //----------------------------------------------------------------------------- HRESULT InitD3D( HWND hWnd ) { // Create the D3D object, which is needed to create the D3DDevice. if( NULL == ( g_pD3D = Direct3DCreate9( D3D_SDK_VERSION ) ) ) return E_FAIL; // Set up the structure used to create the D3DDevice. Most parameters are // zeroed out. We set Windowed to TRUE, since we want to do D3D in a // window, and then set the SwapEffect to "discard", which is the most // efficient method of presenting the back buffer to the display. And // we request a back buffer format that matches the current desktop display // format. D3DPRESENT_PARAMETERS d3dpp; ZeroMemory( &d3dpp, sizeof(d3dpp) ); d3dpp.Windowed = TRUE; d3dpp.SwapEffect = D3DSWAPEFFECT_DISCARD; d3dpp.BackBufferFormat = D3DFMT_UNKNOWN; // Create the Direct3D device. Here we are using the default adapter (most // systems only have one, unless they have multiple graphics hardware cards // installed) and requesting the HAL (which is saying we want the hardware // device rather than a software one). Software vertex processing is // specified since we know it will work on all cards. On cards that support // hardware vertex processing, though, we would see a big performance gain // by specifying hardware vertex processing. if( FAILED( g_pD3D->CreateDevice( D3DADAPTER_DEFAULT, D3DDEVTYPE_HAL, hWnd, D3DCREATE_SOFTWARE_VERTEXPROCESSING, &d3dpp, &g_pd3dDevice ) ) ) { return E_FAIL; } // Device state would normally be set here return S_OK; } //----------------------------------------------------------------------------- // Name: Cleanup() // Desc: Releases all previously initialized objects //----------------------------------------------------------------------------- VOID Cleanup() { //释放D3D设备对象 if( g_pd3dDevice != NULL) g_pd3dDevice->Release(); //释放D3D对象 if( g_pD3D != NULL) g_pD3D->Release(); } //----------------------------------------------------------------------------- // Name: Render() // Desc: Draws the scene //----------------------------------------------------------------------------- VOID Render() { if( NULL == g_pd3dDevice ) return; // Clear the backbuffer to a blue color g_pd3dDevice->Clear( 0, NULL, D3DCLEAR_TARGET, D3DCOLOR_XRGB(0,0,255), 1.0f, 0 ); // Begin the scene if( SUCCEEDED( g_pd3dDevice->BeginScene() ) ) { // Rendering of scene objects can happen here // End the scene g_pd3dDevice->EndScene(); } // Present the backbuffer contents to the display g_pd3dDevice->Present( NULL, NULL, NULL, NULL ); } //----------------------------------------------------------------------------- // Name: MsgProc() // Desc: The window's message handler //----------------------------------------------------------------------------- LRESULT WINAPI MsgProc( HWND hWnd, UINT msg, WPARAM wParam, LPARAM lParam ) { switch( msg ) { case WM_DESTROY: Cleanup(); PostQuitMessage( 0 ); return 0; case WM_PAINT: Render(); ValidateRect( hWnd, NULL ); return 0; } return DefWindowProc( hWnd, msg, wParam, lParam ); } //----------------------------------------------------------------------------- // Name: WinMain() // Desc: The application's entry point //----------------------------------------------------------------------------- INT WINAPI WinMain( HINSTANCE hInst, HINSTANCE, LPSTR, INT ) { // Register the window class WNDCLASSEX wc = { sizeof(WNDCLASSEX), CS_CLASSDC, MsgProc, 0L, 0L, GetModuleHandle(NULL), NULL, NULL, NULL, NULL, "D3D Tutorial", NULL }; RegisterClassEx( &wc ); // Create the application's window HWND hWnd = CreateWindow( "D3D Tutorial", "D3D Tutorial 01: CreateDevice", WS_OVERLAPPEDWINDOW, 100, 100, 300, 300, NULL, NULL, wc.hInstance, NULL ); // Initialize Direct3D if( SUCCEEDED( InitD3D( hWnd ) ) ) { //初始化成功 // Show the window ShowWindow( hWnd, SW_SHOWDEFAULT ); UpdateWindow( hWnd ); // Enter the message loop MSG msg; while(true) { if(PeekMessage(&msg,NULL,0,0,PM_REMOVE)) {//消息处理 if(msg.message==WM_QUIT) break; TranslateMessage(&msg); DispatchMessage(&msg); } else {//空闲时绘制 Render(); } } } UnregisterClass( "D3D Tutorial", wc.hInstance ); return 0; } 2, #include <d3d9.h> #pragma warning( disable : 4996 ) // disable deprecated warning #include <strsafe.h> #pragma warning( default : 4996 ) LPDIRECT3D9 g_pD3D = NULL; // Used to create the D3DDevice LPDIRECT3DDEVICE9 g_pd3dDevice = NULL; // Our rendering device LPDIRECT3DVERTEXBUFFER9 g_pVB = NULL; // Buffer to hold vertices 顶点缓冲区 // A structure for our custom vertex type struct CUSTOMVERTEX { FLOAT x, y, z, rhw; // The transformed position for the vertex DWORD color; // The vertex color }; // Our custom FVF, which describes our custom vertex structure #define D3DFVF_CUSTOMVERTEX (D3DFVF_XYZRHW|D3DFVF_DIFFUSE) HRESULT InitD3D( HWND hWnd ) { // Create the D3D object. if( NULL == ( g_pD3D = Direct3DCreate9( D3D_SDK_VERSION ) ) ) return E_FAIL; // Set up the structure used to create the D3DDevice D3DPRESENT_PARAMETERS d3dpp; ZeroMemory( &d3dpp, sizeof(d3dpp) ); d3dpp.Windowed = TRUE; d3dpp.SwapEffect = D3DSWAPEFFECT_DISCARD; d3dpp.BackBufferFormat = D3DFMT_UNKNOWN; // Create the D3DDevice if( FAILED( g_pD3D->CreateDevice( D3DADAPTER_DEFAULT, D3DDEVTYPE_HAL, hWnd, D3DCREATE_SOFTWARE_VERTEXPROCESSING, &d3dpp, &g_pd3dDevice ) ) ) { return E_FAIL; } // Device state would normally be set here return S_OK; } HRESULT InitVB() { // Initialize three vertices for rendering a triangle CUSTOMVERTEX vertices[] = { { 150.0f, 50.0f, 0.5f, 1.0f, 0xffff0000, }, // x, y, z, rhw, color { 250.0f, 250.0f, 0.5f, 1.0f, 0xff00ff00, }, { 50.0f, 250.0f, 0.5f, 1.0f, 0xff00ffff, }, }; // Create the vertex buffer. Here we are allocating enough memory // (from the default pool) to hold all our 3 custom vertices. We also // specify the FVF, so the vertex buffer knows what data it contains. if( FAILED( g_pd3dDevice->CreateVertexBuffer( 3*sizeof(CUSTOMVERTEX), 0, D3DFVF_CUSTOMVERTEX, D3DPOOL_DEFAULT, &g_pVB, NULL ) ) ) { return E_FAIL; } // Now we fill the vertex buffer. To do this, we need to Lock() the VB to // gain access to the vertices. This mechanism is required becuase vertex // buffers may be in device memory. VOID* pVertices; if( FAILED( g_pVB->Lock( 0, sizeof(vertices), (void**)&pVertices, 0 ) ) ) return E_FAIL; memcpy( pVertices, vertices, sizeof(vertices) ); g_pVB->Unlock(); return S_OK; } VOID Cleanup() { if( g_pVB != NULL ) g_pVB->Release(); if( g_pd3dDevice != NULL ) g_pd3dDevice->Release(); if( g_pD3D != NULL ) g_pD3D->Release(); } VOID Render() { // Clear the backbuffer to a blue color g_pd3dDevice->Clear( 0, NULL, D3DCLEAR_TARGET, D3DCOLOR_XRGB(0,0,255), 1.0f, 0 ); // Begin the scene if( SUCCEEDED( g_pd3dDevice->BeginScene() ) ) { // Draw the triangles in the vertex buffer. g_pd3dDevice->SetStreamSource( 0, g_pVB, 0, sizeof(CUSTOMVERTEX) ); g_pd3dDevice->SetFVF( D3DFVF_CUSTOMVERTEX ); g_pd3dDevice->DrawPrimitive( D3DPT_TRIANGLELIST, 0, 1 ); // End the scene g_pd3dDevice->EndScene(); } // Present the backbuffer contents to the display g_pd3dDevice->Present( NULL, NULL, NULL, NULL ); } LRESULT WINAPI MsgProc( HWND hWnd, UINT msg, WPARAM wParam, LPARAM lParam ) { switch( msg ) { case WM_DESTROY: Cleanup(); PostQuitMessage( 0 ); return 0; } return DefWindowProc( hWnd, msg, wParam, lParam ); } INT WINAPI WinMain( HINSTANCE hInst, HINSTANCE, LPSTR, INT ) { // Register the window class WNDCLASSEX wc = { sizeof(WNDCLASSEX), CS_CLASSDC, MsgProc, 0L, 0L, GetModuleHandle(NULL), NULL, NULL, NULL, NULL, "D3D Tutorial", NULL }; RegisterClassEx( &wc ); // Create the application's window HWND hWnd = CreateWindow( "D3D Tutorial", "D3D Tutorial 01: CreateDevice", WS_OVERLAPPEDWINDOW, 100, 100, 300, 300, NULL, NULL, wc.hInstance, NULL ); // Initialize Direct3D if( SUCCEEDED( InitD3D( hWnd ) ) ) { //初始化成功 // Create the vertex buffer if( SUCCEEDED( InitVB() ) ) { // Show the window ShowWindow( hWnd, SW_SHOWDEFAULT ); UpdateWindow( hWnd ); // Enter the message loop MSG msg; while(true) { if(PeekMessage(&msg,NULL,0,0,PM_REMOVE)) {//消息处理 if(msg.message==WM_QUIT) break; TranslateMessage(&msg); DispatchMessage(&msg); } else {//空闲时绘制 Render(); } } } } UnregisterClass( "D3D Tutorial", wc.hInstance ); return 0; } 本文转自Phinecos(洞庭散人)博客园博客,原文链接:http://www.cnblogs.com/phinecos/archive/2008/04/26/1172475.html,如需转载请自行联系原作者

优秀的个人博客,低调大师

C++ Exercises(七)

Dll的示例代码 namespace MathFuncs { class MyMathFuncs { public: // Returns a + b static __declspec(dllexport) double Add(double a, double b); // Returns a - b static __declspec(dllexport) double Subtract(double a, double b); // Returns a * b static __declspec(dllexport) double Multiply(double a, double b); // Returns a / b // Throws DivideByZeroException if b is 0 static __declspec(dllexport) double Divide(double a, double b); }; } #include "MathFuncsDll.h" #include <stdexcept> using namespace std; namespace MathFuncs { double MyMathFuncs::Add(double a, double b) { return a + b; } double MyMathFuncs::Subtract(double a, double b) { return a - b; } double MyMathFuncs::Multiply(double a, double b) { return a * b; } double MyMathFuncs::Divide(double a, double b) { if (b == 0) { throw new invalid_argument("b cannot be zero!"); } return a / b; } } // MyExecRefsDll.cpp : 定义控制台应用程序的入口点。 // #include "stdafx.h" #include "MathFuncsDll.h" #include <iostream> using namespace std; int _tmain(int argc, _TCHAR* argv[]) { double a = 7.4; int b = 99; cout << "a + b = " << MathFuncs::MyMathFuncs::Add(a, b) << endl; cout << "a - b = " << MathFuncs::MyMathFuncs::Subtract(a, b) << endl; cout << "a * b = " << MathFuncs::MyMathFuncs::Multiply(a, b) << endl; cout << "a / b = " << MathFuncs::MyMathFuncs::Divide(a, b) << endl; cin>>a; return 0; } 本文转自Phinecos(洞庭散人)博客园博客,原文链接:http://www.cnblogs.com/phinecos/archive/2007/11/16/961857.html,如需转载请自行联系原作者

优秀的个人博客,低调大师

C++ Exercises(八)

全排列问题: #include "stdafx.h" #include <math.h> #include <stdio.h> #include <iostream> using namespace std; void swap(int& a,int& b) { int tmp; tmp = a; a = b; b = tmp; } void Rerange(int a[],int m,int n) { if(m==n) { for(int j=0;j<=m;++j) cout<<a[j]<<'\t'; cout<<endl; } else { for(int i=m;i<=n;++i) { swap(a[m],a[i]); Rerange(a,m+1,n); swap(a[m],a[i]); } } } int main(void) { int a[] = {1,2,3},tmp; Rerange(a,0,2); cin>>tmp; return 0; } 本文转自Phinecos(洞庭散人)博客园博客,原文链接:http://www.cnblogs.com/phinecos/archive/2007/11/19/963829.html,如需转载请自行联系原作者

优秀的个人博客,低调大师

C++ Exercises(一)

一个3D向量类 // Vertex3D.h: interface for the Vertex3D class. // ////////////////////////////////////////////////////////////////////// class Vertex3D {//3维向量类 private: double x,y,z; public: Vertex3D(); Vertex3D(double tx,double ty,double tz); Vertex3D(const Vertex3D& vt);//拷贝构造函数 Vertex3D& operator = (const Vertex3D &vt);//重载赋值运算符 bool operator == (const Vertex3D &vt)const;//重载"=="运算符 bool operator != (const Vertex3D &vt)const;//重载"!="运算符 Vertex3D operator -()const;//负向量 Vertex3D operator - (const Vertex3D &vt)const;//向量减法 Vertex3D operator + (const Vertex3D &vt)const;//向量加法 Vertex3D operator * (double fac)const;//与标量相乘 Vertex3D operator / (double fac)const;//与标量相除 Vertex3D& operator += (const Vertex3D &vt); Vertex3D& operator -= (const Vertex3D &vt); Vertex3D& operator *= (double fac);//与标量相乘 Vertex3D& operator /= (double fac);//与标量相除 double operator * (const Vertex3D &vt)const;//向量点乘 void reset(); void normalize();//向量单位化 double VertexLength()const;//向量模 virtual ~Vertex3D(); friend Vertex3D crossProduct(const Vertex3D &a,const Vertex3D &b); friend double Distance(const Vertex3D &a,const Vertex3D &b); }; // Vertex3D.cpp: implementation of the Vertex3D class. // ////////////////////////////////////////////////////////////////////// #include "Vertex3D.h" #include <math.h> ////////////////////////////////////////////////////////////////////// // Construction/Destruction ////////////////////////////////////////////////////////////////////// Vertex3D::Vertex3D() { this->x = 0.0f; this->y = 0.0f; this->z = 0.0f; } Vertex3D::Vertex3D(double tx,double ty,double tz) { this->x = tx; this->y = ty; this->z = tz; } Vertex3D::Vertex3D(const Vertex3D &vt) { this->x = vt.x; this->y = vt.y; this->z = vt.z; } Vertex3D& Vertex3D::operator = (const Vertex3D& vt) { this->x = vt.x; this->y = vt.y; this->z = vt.z; return *this; } bool Vertex3D::operator == (const Vertex3D& vt)const {//判断向量是否相等 return this->x==vt.x&&this->y==vt.y&&this->z==vt.z; } bool Vertex3D::operator != (const Vertex3D& vt)const { return this->x!=vt.x&&this->y!=vt.y&&this->z!=vt.z; } Vertex3D Vertex3D::operator - ()const {//负向量 return Vertex3D(-x,-y,-z); } Vertex3D Vertex3D::operator - (const Vertex3D& vt)const {//向量减法 return Vertex3D(x-vt.x,y-vt.y,z-vt.z); } Vertex3D Vertex3D::operator + (const Vertex3D& vt)const {//向量加法 return Vertex3D(x+vt.x,y+vt.y,z+vt.z); } Vertex3D Vertex3D::operator * (double fac)const {//与标量乘法 return Vertex3D(x*fac,y*fac,z*fac); } Vertex3D Vertex3D::operator / (double fac)const {//与标量相除 return Vertex3D(x/fac,y/fac,z/fac); } Vertex3D& Vertex3D::operator += (const Vertex3D &vt) { this->x += vt.x; this->y += vt.y; this->z += vt.z; return *this; } Vertex3D& Vertex3D::operator -= (const Vertex3D &vt) { this->x -= vt.x; this->y -= vt.y; this->z -= vt.z; return *this; } Vertex3D& Vertex3D::operator *= (double fac) { this->x *= fac; this->y *= fac; this->z *= fac; return *this; } Vertex3D& Vertex3D::operator /= (double fac) { this->x /= fac; this->y /= fac; this->z /= fac; return *this; } void Vertex3D::reset() {//置为零向量 this->x = 0.0f; this->y = 0.0f; this->z = 0.0f; } double Vertex3D::VertexLength()const {//向量长度 double tmp = x*x+y*y+z*z; return sqrt(tmp); } void Vertex3D::normalize() {//向量单位化 double len = this->VertexLength();//获取向量长度 if(len>0.0f) { double tmp = 1.0f/len; this->x *= tmp; this->y *= tmp; this->z *= tmp; } } double Vertex3D::operator * (const Vertex3D &vt)const {//向量点乘 return x*vt.x+y*vt.y+z*vt.z; } Vertex3D::~Vertex3D() { } Vertex3D crossProduct(const Vertex3D &a,const Vertex3D &b) {//向量叉乘 return Vertex3D(a.y*b.z - a.z*b.y,a.z*b.x - a.x*b.z,a.x*b.y - a.y*b.x); } double Distance(const Vertex3D &a,const Vertex3D &b) {//向量距离 double dx = a.x - b.x,dy = a.y - b.y,dz = a.z - b.z; return sqrt(dx*dx+dy*dy+dz*dz); } 本文转自Phinecos(洞庭散人)博客园博客,原文链接:http://www.cnblogs.com/phinecos/archive/2007/04/29/732444.html,如需转载请自行联系原作者

优秀的个人博客,低调大师

C++ 操作 Oracle

#include <string> #include <occi.h> #include <iostream> using namespace std; using namespace oracle::occi; struct Student_struct { int no; int age; string name; }; std::string getSQL(void) { std::string str_sql = "SELECT * FROM emp WHERE mgr=(:1)"; return str_sql; } /* 连接 Oracle 操作步骤 */ void connectOracle() { const string userName = "scott"; const string password = "tiger"; const string connString = "localhost:1521/orcl"; try { Environment *env = Environment::createEnvironment(Environment::DEFAULT); Connection *con = env->createConnection(userName, password, connString); cout << "Success to connect!" << endl; Statement *stmt = con->createStatement(); //动态 SQL stmt->setSQL("SELECT * FROM emp WHERE mgr=(:1) AND sal=(:2)"); stmt->setInt(1, 7902); stmt->setInt(2, 800); ResultSet *rs = stmt->executeQuery(); while (rs->next()) { int no = rs->getInt(1); string name = rs->getString(2); cout << "no is:" << no << endl; cout << "name :" << name << endl; cout << "MGR : "<<rs->getInt(4)<<endl; cout << "SAL :"<<rs->getInt(6)<<endl; } con->terminateStatement(stmt); env->terminateConnection(con); Environment::terminateEnvironment(env); } catch (SQLException &ex) { cout << ex.what() << endl; } system("pause"); } /* 增加 数据 */ void insertData(Connection *p_conn) { Statement *p_stmt = NULL; int m_no; string m_name; //string m_name; int m_age; int Flag = 0; //执行sql,返回结果并显示 cout << "PLEASE INPUT THE no! " << endl; for(;;) { fflush(stdin); if((!scanf_s("%d", &m_no)) || (m_no) < 0) { cout << "The no that you just input is invalid,please input it again !" << endl; continue; } else break; } string Verify_strsql( "select * from student where no =(:1)" ); p_stmt = p_conn->createStatement( Verify_strsql ); p_stmt->setInt(1, m_no); ResultSet *rset = p_stmt-> executeQuery(); /*while( rset->next() ) { Flag = rset->getInt(1); }*/ rset->next(); Flag = rset->getInt(1); if((m_no != 0) && (Flag != m_no)) /*判断是否为0或已经存在*/ { try { cout << "PLEASE INPUT THE name! " << endl; cin >> m_name; cout << "PLEASE INPUT THE age! " << endl; for(;;) { fflush(stdin); if((!scanf_s("%d", &m_age)) || (m_age) < 0) { cout << "The age that you just input is invalid,please input it again !!" << endl; continue; } else break; } string strsql = "insert into student (no,name,age) values (:1,:2,:3)"; p_stmt = p_conn->createStatement( strsql ); //p_stmt->setSQL(strsql); p_stmt->setInt(1, m_no); p_stmt->setString(2, m_name); p_stmt->setInt(3, m_age); cout << "The Statement that you input is " << strsql << endl; p_stmt-> executeUpdate(); p_conn->commit(); cout << "Successfully inserted a new record ! " << endl; } catch(SQLException *e) { cout << "exception: " << e->what() << endl; } p_conn->terminateStatement(p_stmt); } else { cout << "The user_no is invalid or has been exist!,please input it again!" << endl; } } /* 删除 数据 */ void deleteData(Connection *p_conn) { int m_no; int Flag = 0; Statement *p_stmt = NULL; //执行sql,返回结果并显示 //delete_data: cout << "Please input the number that you want to delete :" << endl; for(;;) { fflush(stdin); if(!scanf_s("%d", &m_no)) { cout << "输入字符出错!请重新输入!" << endl; continue; } else break; } string Verify_strsql( "select * from student where no =(:1)" ); p_stmt = p_conn->createStatement( Verify_strsql ); p_stmt->setInt(1, m_no); ResultSet *rset = p_stmt-> executeQuery(); /*while( rset->next() ) { Flag = rset->getInt(1); }*/ rset->next(); Flag = rset->getInt(1); if(Flag) { string strsql( "delete from student where no = (:1)"); p_stmt = p_conn->createStatement( strsql ); p_stmt->setInt(1, m_no); try { p_stmt-> executeUpdate(); p_conn->commit(); cout << "Successfully deleted an old record !" << endl; } catch(SQLException *e) { cout << "exception: " << e->what() << endl; } } else { cout << "The number that you just input didn't exist! please input a new number!" << endl; cout << "_____________________________________________________________________" << endl; //goto delete_data; } p_conn->terminateStatement(p_stmt); } /* 修改 数据 */ void setData(Connection *p_conn) { Statement *p_stmt = NULL; try { int m_no; int Flag = 0; string m_name; int m_age; cout << "Please input the number that you want to updata :" << endl; //cin>>m_no; for(;;) { fflush(stdin); if(!scanf_s("%d", &m_no)) { cout << "输入字符出错!请重新输入!" << endl; continue; } else break; } string Verify_strsql( "select * from student where no =(:1)" ); p_stmt = p_conn->createStatement( Verify_strsql ); p_stmt->setInt(1, m_no); ResultSet *rset = p_stmt-> executeQuery(); /*while( rset->next() ) { Flag = rset->getInt(1); }*/ rset->next(); Flag = rset->getInt(1); if(Flag) { cout << "PLEASE INPUT THE NEW name! " << endl; cin >> m_name; cout << "PLEASE INPUT THE NEW age! " << endl; //cin>>m_age; for(;;) { fflush(stdin); if((!scanf_s("%d", &m_age)) || (m_age) < 0) { cout << "The age that you just input is invalid,please input it again !!" << endl; continue; } else break; } string strsql = "update student set name=(:1),age=(:2) where no=(:3)"; p_stmt = p_conn->createStatement( strsql ); //p_stmt -> setSQL(strsql); int number = m_no; string name = m_name; int age = m_age; p_stmt->setInt(2, age); p_stmt->setString(1, name); p_stmt->setInt(3, number); p_stmt-> executeUpdate(); //cout <<"The Statement that you input is "<<strsql<< endl; cout << p_stmt->getSQL() << endl; p_conn->commit(); cout << " SUCCESSFULLY UPDATA A NEW RECORD !" << endl; } else { cout << "The number that you just input didn't exist!please input a new number!" << endl; cout << "_____________________________________________________________________" << endl; } } catch(SQLException *e) { cout << "exception: " << e->what() << endl; } p_conn->terminateStatement(p_stmt); } /* 查找 数据 */ void displayData(Connection *p_conn) { Statement *p_stmt = NULL; int row = 1; string strsql( "select * from student order by no" ); p_stmt = p_conn->createStatement( strsql ); ResultSet *rset = p_stmt-> executeQuery(); //查询语句 try { cout << setiosflags(ios::left) << setw(5) << "行号" << setiosflags(ios::left) << setw(5) << " 编号" << setiosflags(ios::left) << setw(8) << " 姓名" << setiosflags(ios::left) << setw(8) << " 年龄" << endl; while( rset->next() ) { int no = rset->getInt(1); int age = rset->getInt(3); string name = rset->getString(2); cout << "row." << setiosflags(ios::left) << setw(5) << row << " no=" << setiosflags(ios::left) << setw(5) << no << " name=" << setiosflags(ios::left) << setw(8) << name << " age=" << setiosflags(ios::left) << setw(10) << age << endl; row++; } } catch (SQLException *e) { cout << "exception: " << e->what() << endl; } p_stmt->closeResultSet(rset); p_conn->terminateStatement(p_stmt); } void findData(Connection *p_conn) { Statement *p_stmt = NULL; int no; int Flag = 0; cout << "Please input the number that you want to search :" << endl; //cin>> no; for(;;) { fflush(stdin); if(!scanf_s("%d", &no)) { cout << "输入字符出错!请重新输入!" << endl; continue; } else break; } string Verify_strsql( "select * from student where no =(:1)" ); p_stmt = p_conn->createStatement( Verify_strsql ); p_stmt->setInt(1, no); ResultSet *rset = p_stmt-> executeQuery(); /*while( rset->next() ) { Flag = rset->getInt(1); }*/ rset->next(); Flag = rset->getInt(1); if(Flag) { string strsql( "select * from student where no =(:1)" ); p_stmt = p_conn->createStatement( strsql ); p_stmt->setInt(1, no); ResultSet *rset = p_stmt-> executeQuery(); //查询语句 try { while( rset->next() ) { int no = rset->getInt(1); int age = rset->getInt(3); string name = rset->getString(2); cout << "no==" << no << " name==" << name << " age==" << age << endl; } } catch (SQLException *e) { cout << "exception: " << e->what() << endl; } } else { cout << "The data that you just input didn't exist! please input a new number!" << endl; cout << "___________________________________________________________________" << endl; } p_stmt->closeResultSet(rset); p_conn->terminateStatement(p_stmt); } void Export_data(Connection *p_conn) { unsigned int i = 0; Statement *p_stmt = NULL; Student_struct m_struct_student; vector<Student_struct> stu_strc; stu_strc.reserve(100); string strsql( "select * from student order by no" ); p_stmt = p_conn->createStatement( strsql ); ResultSet *rset = p_stmt-> executeQuery(); //查询语句 try { while( rset->next() ) { m_struct_student.no = rset->getInt(1); m_struct_student.age = rset->getInt(3); m_struct_student.name = rset->getString(2); stu_strc.push_back(m_struct_student); } /*vector<Student_struct>::iterator iter; iter = stu_strc.begin(); while(iter != stu_strc.end()) { cout<<*(iter++)<<endl; }*/ ofstream Vector_to_file("d:\\Database_file.txt"); if(!Vector_to_file) { cout << "error" << endl; return; } else { for(i = 0; i < stu_strc.size(); i++) { Vector_to_file << setiosflags(ios::left) << setw(8) << stu_strc[i].no << setiosflags(ios::left) << setw(8) << stu_strc[i].name << " " << setiosflags(ios::left) << setw(8) << stu_strc[i].age << endl; } Vector_to_file.close(); cout << "您已成功将以下数据写入d:\\Database_file.txt文件中 !" << endl; cout << endl; cout << setiosflags(ios::left) << setw(8) << "编号" << " " << setiosflags(ios::left) << setw(8) << "姓名" << " " << setiosflags(ios::left) << setw(8) << "年龄" << endl; for(i = 0; i < stu_strc.size(); i++) { cout << setiosflags(ios::left) << setw(8) << stu_strc[i].no << " " << setiosflags(ios::left) << setw(8) << stu_strc[i].name << " " << setiosflags(ios::left) << setw(8) << stu_strc[i].age << endl; } } //return EXIT_SUCCESS; } catch (SQLException *e) { cout << "exception: " << e->what() << endl; } p_stmt->closeResultSet(rset); p_conn->terminateStatement(p_stmt); } void Vector_save(Connection *p_conn) { Statement *p_stmt = NULL; Student_struct m_struct_student;//结构体变量 vector<Student_struct> stu_strc;//声明一个向量 stu_strc.reserve(100); string strsql( "select * from student order by no" ); p_stmt = p_conn->createStatement( strsql ); ResultSet *rset = p_stmt-> executeQuery(); //查询语句 try { while( rset->next() ) { m_struct_student.no = rset->getInt(1); m_struct_student.age = rset->getInt(3); m_struct_student.name = rset->getString(2); stu_strc.push_back(m_struct_student); } /*vector<Student_struct>::iterator iter; iter = stu_strc.begin(); while(iter != stu_strc.end()) { cout<<*(iter++)<<endl; }*/ cout << "您已成功将以下数据装入vector容器中 !" << endl; cout << endl; cout << setiosflags(ios::left) << setw(5) << "编号" << " " << setiosflags(ios::left) << setw(5) << "姓名" << " " << setiosflags(ios::left) << setw(5) << "年龄" << endl; for(unsigned int i = 0; i < stu_strc.size(); i++) { cout << setiosflags(ios::left) << setw(5) << stu_strc[i].no << " " << setiosflags(ios::left) << setw(5) << stu_strc[i].name << " " << setiosflags(ios::left) << setw(5) << stu_strc[i].age << endl; } } catch (SQLException *e) { cout << "exception: " << e->what() << endl; } p_stmt->closeResultSet(rset); p_conn->terminateStatement(p_stmt); } int main() { connectOracle(); return 0; }

资源下载

更多资源
Mario

Mario

马里奥是站在游戏界顶峰的超人气多面角色。马里奥靠吃蘑菇成长,特征是大鼻子、头戴帽子、身穿背带裤,还留着胡子。与他的双胞胎兄弟路易基一起,长年担任任天堂的招牌角色。

Spring

Spring

Spring框架(Spring Framework)是由Rod Johnson于2002年提出的开源Java企业级应用框架,旨在通过使用JavaBean替代传统EJB实现方式降低企业级编程开发的复杂性。该框架基于简单性、可测试性和松耦合性设计理念,提供核心容器、应用上下文、数据访问集成等模块,支持整合Hibernate、Struts等第三方框架,其适用范围不仅限于服务器端开发,绝大多数Java应用均可从中受益。

Rocky Linux

Rocky Linux

Rocky Linux(中文名:洛基)是由Gregory Kurtzer于2020年12月发起的企业级Linux发行版,作为CentOS稳定版停止维护后与RHEL(Red Hat Enterprise Linux)完全兼容的开源替代方案,由社区拥有并管理,支持x86_64、aarch64等架构。其通过重新编译RHEL源代码提供长期稳定性,采用模块化包装和SELinux安全架构,默认包含GNOME桌面环境及XFS文件系统,支持十年生命周期更新。

Sublime Text

Sublime Text

Sublime Text具有漂亮的用户界面和强大的功能,例如代码缩略图,Python的插件,代码段等。还可自定义键绑定,菜单和工具栏。Sublime Text 的主要功能包括:拼写检查,书签,完整的 Python API , Goto 功能,即时项目切换,多选择,多窗口等等。Sublime Text 是一个跨平台的编辑器,同时支持Windows、Linux、Mac OS X等操作系统。

用户登录
用户注册