csprintf源码的简单介绍
本文目录一览:
- 1、求用C语言模拟简单台球运动的源代码,不需要图形化界面
- 2、跪求C语言中,能实现2,8,10,16进制之间转换的源代码啊
- 3、谁能给个触摸屏手写输入中文的c言语源代码啊?
- 4、C语言的sprintf函数问题,到底怎么用啊???
- 5、求LINUX下,C语言编写的日志输出源码~
求用C语言模拟简单台球运动的源代码,不需要图形化界面
这源代码应该有个桌面类(Table),球类(Sphere),游戏类等等。我用C++
#pragma once (Table.h)
#endif // _MSC_VER 1000
#include "Base.h"
#define MESH_D3DFVF_CUSTOMVERTEX (D3DFVF_XYZ|D3DFVF_NORMAL|D3DFVF_TEX1)
class CTable:public CBase
{
public:
DWORD Render();
CTable(LPDIRECT3DDEVICE8 pD3DDevice,LPSTR pFilename);
virtual ~CTable();
LPD3DXMESH GetMeshTablePointer();
private:
void TransformTable();
LPDIRECT3DDEVICE8 m_pD3DDevice;
DWORD m_dwNumMaterials;
LPD3DXMESH m_pMeshTable;
D3DMATERIAL8 *m_pMeshTableMaterials;
LPDIRECT3DTEXTURE8 *m_pMeshTableTextures;
};#endif
#include "Table.h" (Table.cpp)
CTable::CTable(LPDIRECT3DDEVICE8 pD3DDevice,LPSTR pFilename)
{
LPD3DXBUFFER pMaterialsBuffer=NULL;
LPD3DXMESH pMeshTable=NULL;
m_pD3DDevice=pD3DDevice;
if(FAILED(D3DXLoadMeshFromX(pFilename,D3DXMESH_MANAGED,m_pD3DDevice,NULL,
pMaterialsBuffer,m_dwNumMaterials,pMeshTable)))
{
m_pMeshTable=NULL;
m_pMeshTableMaterials=NULL;
m_pMeshTableTextures=NULL;
LogError("liTable Mesh '%s' failed to load",pFilename);
return;
}
D3DXMATERIAL *matMaterials=(D3DXMATERIAL*)pMaterialsBuffer-GetBufferPointer();
//Create two arrays. One to hold the materials and one to hold the textures
m_pMeshTableMaterials=new D3DMATERIAL8[m_dwNumMaterials];
m_pMeshTableTextures=new LPDIRECT3DTEXTURE8[m_dwNumMaterials];
for(DWORD i=0;im_dwNumMaterials;i++)
{
//Copy the material
m_pMeshTableMaterials[i]=matMaterials[i].MatD3D;
//Set the ambient color for the material(D3DX does not do this)
m_pMeshTableMaterials[i].Ambient=m_pMeshTableMaterials[i].Diffuse;
D3DCOLORVALUE rgbaSpecular={0.0f,0.0f,0.0f,0.0f};
m_pMeshTableMaterials[i].Specular=rgbaSpecular;
m_pMeshTableMaterials[i].Power=50.0f;
//Create the texture
char buffer[255];
sprintf(buffer,"textures/%s",matMaterials[i].pTextureFilename);
if(FAILED(D3DXCreateTextureFromFile(m_pD3DDevice,
buffer, m_pMeshTableTextures[i])))
{
m_pMeshTableTextures[i]=NULL;
}
}
//finished with the material buffer,so release it
SafeRelease(pMaterialsBuffer);
//Make sure that the normals are setup for mesh
pMeshTable-CloneMeshFVF(D3DXMESH_MANAGED,MESH_D3DFVF_CUSTOMVERTEX,m_pD3DDevice,m_pMeshTable);
SafeRelease(pMeshTable);
// D3DXComputeNormals(m_pMesh);
LogInfo("liMesh '%s' loaded OK",pFilename);
}
CTable::~CTable()
{
SafeDelete(m_pMeshTableMaterials);
if(m_pMeshTableTextures != NULL)
{
for(DWORD i=0;im_dwNumMaterials;i++)
{
if(m_pMeshTableTextures[i])
SafeRelease(m_pMeshTableTextures[i]);
}
}
SafeDelete(m_pMeshTableTextures);
SafeRelease(m_pMeshTable);
LogInfo("liTable Mesh destroyed OK");
}
DWORD CTable::Render()
{
TransformTable();
if(m_pMeshTable!=NULL)
{
for(DWORD i=0;im_dwNumMaterials;i++)
{
m_pD3DDevice-SetMaterial(m_pMeshTableMaterials[i]);
m_pD3DDevice-SetTexture(0,m_pMeshTableTextures[i]);
m_pMeshTable-DrawSubset(i);
}
return m_pMeshTable-GetNumFaces();
}
else
return 0;
}
LPD3DXMESH CTable::GetMeshTablePointer()
{
return m_pMeshTable;
}
void CTable::TransformTable()
{
D3DXMATRIX matWorld;
D3DXMatrixTranslation(matWorld,0,0,0);
m_pD3DDevice-SetTransform(D3DTS_WORLD,matWorld);
}
(Sphere.h)
#if !defined (AFX_SPHERE_H__FC705F3B_568E_4973_B608_B8F7700D9ECE__INCLUDED_)
#define AFX_SPHERE_H__FC705F3B_568E_4973_B608_B8F7700D9ECE__INCLUDED_
#if _MSC_VER 1000
#pragma once
#endif // _MSC_VER 1000
#include "Base.h"
#define SPHERE_D3DFVF_CUSTOMVERTEX (D3DFVF_XYZ|D3DFVF_NORMAL|D3DFVF_TEX1)
class CSphere:public CBase
{
private:
struct SPHERE_CUSTOMVERTEX
{
float x,y,z; //Position of vertex in 3D space
float nx,ny,nz; //Lighting Normal
float tu,tv; //Texture coordinates
};
struct SPHERE_STATE
{
D3DXVECTOR3 sVector; //Position of Centigram in 3D space
D3DXVECTOR3 vVector; //Direction of Velocity in 3D space
float v; //Speed of Sphere
};
SPHERE_STATE *m_pSphereState;
D3DXVECTOR3 m_vecSavePosition; //Save sphere position for collision bar
D3DXVECTOR3 m_vecSavePosition2; //Save sphere position for collision sphere
public:
BOOL SetMaterial(D3DCOLORVALUE rgbaDiffuse,D3DCOLORVALUE rgbaAmbient,
D3DCOLORVALUE rgbaSpecular,D3DCOLORVALUE rgbaEmissive,float rPower);
BOOL SetTexture(const char* szTextureFilePath);
DWORD Render();
CSphere(LPDIRECT3DDEVICE8 pD3DDevice,int iRings=20,int iSegments=20);
void MoveSphere();
void MoveSphereForUser(float x,float z);
virtual ~CSphere();
inline void SetSpherePosition(float x,float y,float z)
{
m_pSphereState-sVector.x=x;
m_pSphereState-sVector.y=y;
m_pSphereState-sVector.z=z;
};
inline void GetSpherePosition(D3DXVECTOR3 vecSpherePos)
{
vecSpherePos=m_pSphereState-sVector;
};
inline void GetSavedSpherePosition(D3DXVECTOR3 vecSavedSpherePos)
{
vecSavedSpherePos=m_vecSavePosition;
};
inline void GetSavedSpherePosition2(D3DXVECTOR3 vecSavedSpherePos)
{
vecSavedSpherePos=m_vecSavePosition2;
};
inline void SaveSpherePosition()
{
m_vecSavePosition=m_pSphereState-sVector;
};
inline void SaveSpherePosition2()
{
m_vecSavePosition2=m_pSphereState-sVector;
};
inline void ContradictoryZv()
{
m_pSphereState-vVector.z=-m_pSphereState-vVector.z;
};
inline void ContradictoryXv()
{
m_pSphereState-vVector.x=-m_pSphereState-vVector.x;
};
void MirrorVAoubtAxis(D3DXVECTOR3 n);
inline void ReduceSphereVelocity(float percent)
{
m_pSphereState-v=m_pSphereState-v*percent;
};
inline float CheckSphereEnergy()
{
return m_pSphereState-v;
};
inline void SetSphereVelocityDir(const D3DXVECTOR3 vDir)
{
m_pSphereState-vVector=vDir;
};
inline void SetSphereVelocity(const float velocity)
{
m_pSphereState-v=velocity;
};
inline void GetSphereVelocityDir(D3DXVECTOR3 vDir)
{
vDir=m_pSphereState-vVector;
};
inline float GetSphereVelocity()
{
return m_pSphereState-v;
};
inline void SetSphereStateToFalse()
{
m_bSphereInUse=FALSE;
};
inline void SetSphereStateToTrue()
{
m_bSphereInUse=TRUE;
};
inline BOOL GetSphereState()
{
return m_bSphereInUse;
};
void SetSphereVelocityAt_Y_NegativeAxis();
inline float GetSpherePosAt_Y_Axis()
{
return m_pSphereState-sVector.y;
};
private:
BOOL CreateIndexBuffer();
BOOL UpdateVertices();
BOOL CreateVertexBuffer();
void TransformSphere();
void TransformSphereForUser();
void UpdateSpherePosition();
void FrictionReduseVelocity();
LPDIRECT3DDEVICE8 m_pD3DDevice;
LPDIRECT3DVERTEXBUFFER8 m_pVertexBuffer;
LPDIRECT3DTEXTURE8 m_pTexture;
D3DMATERIAL8 m_matMaterial;
LPDIRECT3DINDEXBUFFER8 m_pIndexBuffer;
int m_iRings;
int m_iSegments;
float m_fTotalDis;
D3DXVECTOR3 m_vecSphereRotationAxis;
BOOL m_bSphereInUse;
DWORD m_dwNumOfVertices;
DWORD m_dwNumOfIndices;
DWORD m_dwNumOfPolygons;
};#endif
跪求C语言中,能实现2,8,10,16进制之间转换的源代码啊
全能csprintf源码的csprintf源码!
#includestdio.h
two_eight()
{
char *p,s[20];
long n;
int i;
p=s;
printf(" 请输入一个二进制数: ");
scanf("%s",p); n=0;
while(*(p)!='\0')
{
n=n*2+*p-'0';
p++;
}
i=0;
while(n)
{
s[i]=n%8;
n=n/8;
i++;
}
printf(" 2 进制转化为 8 进制:");
for(i--;i=0;i--)
printf("%d",s[i]);
printf("\n");
}
two_ten()
{
char *p,s[20];
long n;
p=s;
printf(" 请输入一个二进制数: ");
scanf("%s",p); n=0;
while(*(p)!='\0')
{
n=n*2+*p-'0';
p++;
}
printf(" 2 进制转化为 10 进制:%d\n",n);
}
two_sixten()
{
char *p,s[20],str[20];
long n;
int i,mark;
p=s;
printf(" 请输入一个二进制数: ");
scanf("%s",p); n=0;
while(*(p)!='\0')
{
n=n*2+*p-'0';
p++;
}
i=0;
while(n)
{
s[i]=n;
n=n/16;
mark=s[i];
switch(mark)
{
case 0: str[i]='0'; break;
case 1: str[i]='1'; break;
case 2: str[i]='2'; break;
case 3: str[i]='3'; break;
case 4: str[i]='4'; break;
case 5: str[i]='5'; break;
case 6: str[i]='6'; break;
case 7: str[i]='7'; break;
case 8: str[i]='8'; break;
case 9: str[i]='9'; break;
case 10: str[i]='A'; break;
case 11: str[i]='B'; break;
case 12: str[i]='C'; break;
case 13: str[i]='D'; break;
case 14: str[i]='E'; break;
case 15: str[i]='F'; break;
}
i++;
}
printf(" 2 进制转化为 16 进制:");
for(i--;i=0;i--)
printf("%c",str[i]);
printf("\n");
}
eight_two()
{
char *p,s[20];
long n;
int i;
p=s;
printf(" 请输入一个八进制数: ");
scanf("%s",p); n=0;
while(*(p)!='\0')
{
n=n*8+*p-'0';
p++;
}
i=0;
while(n)
{
s[i]=n%2;
n=n/2;
i++;
}
printf(" 8 进制转化为 2 进制:");
for(i--;i=0;i--)
printf("%d",s[i]);
printf("\n");
}
eight_ten()
{
char *p,s[20];
long n;
p=s;
printf(" 请输入一个八进制数: ");
scanf("%s",p); n=0;
while(*(p)!='\0')
{
n=n*8+*p-'0';
p++;
}
printf(" 8 进制转化为 10 进制:%d\n",n);
}
eight_sixten()
{
char *p,s[20],str[20];
long n;
int i,mark;
p=s;
printf(" 请输入一个八进制数: ");
scanf("%s",p); n=0;
while(*(p)!='\0')
{
n=n*8+*p-'0';
p++;
}
i=0;
while(n)
{
s[i]=n;
n=n/16;
mark=s[i];
switch(mark)
{
case 0: str[i]='0'; break;
case 1: str[i]='1'; break;
case 2: str[i]='2'; break;
case 3: str[i]='3'; break;
case 4: str[i]='4'; break;
case 5: str[i]='5'; break;
case 6: str[i]='6'; break;
case 7: str[i]='7'; break;
case 8: str[i]='8'; break;
case 9: str[i]='9'; break;
case 10: str[i]='A'; break;
case 11: str[i]='B'; break;
case 12: str[i]='C'; break;
case 13: str[i]='D'; break;
case 14: str[i]='E'; break;
case 15: str[i]='F'; break;
}
i++;
}
printf(" 8 进制转化为 16 进制:");
for(i--;i=0;i--)
printf("%c",str[i]);
printf("\n");
}
ten_two()
{
int s[20],n,i;
printf(" 请输入一个10进制数: ");
scanf("%d",n);
i=0;
while(n)
{
s[i]=n%2;
n=n/2;
i++;
}
printf(" 10 进制转化为 2 进制::");
for(i--;i=0;i--)
printf("%d",s[i]);
printf("\n");
}
ten_eight()
{
int s[20],n,i;
printf(" 请输入一个10进制数: ");
scanf("%d",n);
i=0;
while(n)
{
s[i]=n%8;
n=n/8;
i++;
}
printf(" 10 进制转化为 8 进制:");
for(i--;i=0;i--)
printf("%d",s[i]);
printf("\n");
}
ten_sixten()
{
int s[20],n,i,mark;
char str[20];
printf(" 请输入一个10进制数: ");
scanf("%d",n);
i=0;
while(n)
{
s[i]=n;
n=n/16;
mark=s[i];
switch(mark)
{
case 0: str[i]='0'; break;
case 1: str[i]='1'; break;
case 2: str[i]='2'; break;
case 3: str[i]='3'; break;
case 4: str[i]='4'; break;
case 5: str[i]='5'; break;
case 6: str[i]='6'; break;
case 7: str[i]='7'; break;
case 8: str[i]='8'; break;
case 9: str[i]='9'; break;
case 10: str[i]='A'; break;
case 11: str[i]='B'; break;
case 12: str[i]='C'; break;
case 13: str[i]='D'; break;
case 14: str[i]='E'; break;
case 15: str[i]='F'; break;
}
i++;
}
printf(" 10 进制转化为 16 进制:");
for(i--;i=0;i--)
printf("%c",str[i]);
printf("\n");
}
sixten_two()
{
int s[20],n=0,i,j;
char str[20];
printf(" 请输入一个16进制数: ");
scanf("%s",str);
i=0;
while(str[i]!='\0')
{
switch(str[i])
{
case '0': s[i]=0; break;
case '1': s[i]=1; break;
case '2': s[i]=2; break;
case '3': s[i]=3; break;
case '4': s[i]=4; break;
case '5': s[i]=5; break;
case '6': s[i]=6; break;
case '7': s[i]=7; break;
case '8': s[i]=8; break;
case '9': s[i]=9; break;
case 'A': s[i]=10; break;
case 'B': s[i]=11; break;
case 'C': s[i]=12; break;
case 'D': s[i]=13; break;
case 'E': s[i]=14; break;
case 'F': s[i]=15; break;
case 'a': s[i]=10; break;
case 'b': s[i]=11; break;
case 'c': s[i]=12; break;
case 'd': s[i]=13; break;
case 'e': s[i]=14; break;
case 'f': s[i]=15; break;
}
i++;
}
for(j=0;ji;j++)
{
n=n*16+s[j];
}
i=0;
while(n)
{
s[i]=n%2;
n=n/2;
i++;
}
printf(" 16 进制转化为 2 进制::");
for(i--;i=0;i--)
printf("%d",s[i]);
printf("\n");
}
sixten_eight()
{
int s[20],n=0,i,j;
char str[20];
printf(" 请输入一个16进制数: ");
scanf("%s",str);
i=0;
while(str[i]!='\0')
{
switch(str[i])
{
case '0': s[i]=0; break;
case '1': s[i]=1; break;
case '2': s[i]=2; break;
case '3': s[i]=3; break;
case '4': s[i]=4; break;
case '5': s[i]=5; break;
case '6': s[i]=6; break;
case '7': s[i]=7; break;
case '8': s[i]=8; break;
case '9': s[i]=9; break;
case 'A': s[i]=10; break;
case 'B': s[i]=11; break;
case 'C': s[i]=12; break;
case 'D': s[i]=13; break;
case 'E': s[i]=14; break;
case 'F': s[i]=15; break;
case 'a': s[i]=10; break;
case 'b': s[i]=11; break;
case 'c': s[i]=12; break;
case 'd': s[i]=13; break;
case 'e': s[i]=14; break;
case 'f': s[i]=15; break;
}
i++;
}
for(j=0;ji;j++)
{
n=n*16+s[j];
}
i=0;
while(n)
{
s[i]=n%8;
n=n/8;
i++;
}
printf(" 16 进制转化为 8 进制:");
for(i--;i=0;i--)
printf("%d",s[i]);
printf("\n");
}
sixten_ten()
{
int s[20],n=0,i,j;
char str[20];
printf(" 请输入一个16进制数: ");
scanf("%s",str);
i=0;
while(str[i]!='\0')
{
switch(str[i])
{
case '0': s[i]=0; break;
case '1': s[i]=1; break;
case '2': s[i]=2; break;
case '3': s[i]=3; break;
case '4': s[i]=4; break;
case '5': s[i]=5; break;
case '6': s[i]=6; break;
case '7': s[i]=7; break;
case '8': s[i]=8; break;
case '9': s[i]=9; break;
case 'A': s[i]=10; break;
case 'B': s[i]=11; break;
case 'C': s[i]=12; break;
case 'D': s[i]=13; break;
case 'E': s[i]=14; break;
case 'F': s[i]=15; break;
case 'a': s[i]=10; break;
case 'b': s[i]=11; break;
case 'c': s[i]=12; break;
case 'd': s[i]=13; break;
case 'e': s[i]=14; break;
case 'f': s[i]=15; break;
}
i++;
}
for(j=0;ji;j++)
{
n=n*16+s[j];
}
printf(" 16 进制转化为 10 进制:%d\n",n);
}
void main()
{
int select,i; char str[20], n ;
printf("\n");
printf("\t**************************欢迎使用******************************\n");
printf("\t*----------------万能进制转换器v1.0 版-------------------------*\n");
printf("\t*——使用方法: *\n");
printf("\t* 请选择输入下列功能前的数字序号后按enter键进入。 *\n");
printf("\t* ~~~~仅用于学习交流。 *\n");
printf("\t*\t\t\t ------祝你成功 *\n");
printf("\t****************************************************************\n");
printf("\n");
printf("\n");
printf("\t 1 : 2 进制转化为 8 进制。");
printf(" 2 : 2 进制转化为 10 进制。\n");
printf("\t 3 : 2 进制转化为 16 进制。");
printf(" 4 : 8 进制转化为 2 进制。\n");
printf("\t 5 : 8 进制转化为 10 进制。");
printf(" 6 : 8 进制转化为 16 进制。\n");
printf("\t 7 :10 进制转化为 2 进制。");
printf(" 8 :10 进制转化为 8 进制。\n");
printf("\t 9 :10 进制转化为 16 进制。");
printf(" 10 :16 进制转化为 2 进制。\n");
printf("\t 11 :16 进制转化为 8 进制。");
printf(" 12 :16 进制转化为 10 进制。\n");
printf("\n");
printf("按任意键 回车 后继续,按 n 键 回车 后退出程序。");
n=getchar();
printf("\n");
while(n!='n')
{
printf("请选择您要转换的类型!!!");
scanf("%d",select);
for(;select1||select12;)
{
printf("输入错误,请重新输入!!\n");
scanf("%d",select);
}
switch(select)
{
case 1: two_eight(); break;
case 2: two_ten(); break;
case 3: two_sixten(); break;
case 4: eight_two(); break;
case 5: eight_ten(); break;
case 6: eight_sixten(); break;
case 7: ten_two(); break;
case 8: ten_eight(); break;
case 9: ten_sixten(); break;
case 10: sixten_two(); break;
case 11: sixten_eight(); break;
case 12: sixten_ten(); break;
}
printf("按任意键回车继续,按 n 键退出程序。");
getchar();
n=getchar();
printf("\n");
}
}
谁能给个触摸屏手写输入中文的c言语源代码啊?
#include dos.h /*DOS接口函数*/
#include math.h /*数学函数的定义*/
#include conio.h /*屏幕操作函数*/
#include stdio.h /*I/O函数*/
#include stdlib.h /*库函数*/
#include stdarg.h /*变量长度参数表*/
#include graphics.h /*图形函数*/
#include string.h /*字符串函数*/
#include ctype.h /*字符操作函数*/
#define UP 0x48 /*光标上移键*/
#define DOWN 0x50 /*光标下移键*/
#define LEFT 0x4b /*光标左移键*/
#define RIGHT 0x4d /*光标右移键*/
#define ENTER 0x0d /*回车键*/
void *rar; /*全局变量,保存光标图象*/
struct palettetype palette; /*使用调色板信息*/
int GraphDriver; /* 图形设备驱动*/
int GraphMode; /* 图形模式值*/
int ErrorCode; /* 错误代码*/
int MaxColors; /* 可用颜色的最大数值*/
int MaxX, MaxY; /* 屏幕的最大分辨率*/
double AspectRatio; /* 屏幕的像素比*/
void drawboder(void); /*画边框函数*/
void initialize(void); /*初始化函数*/
void computer(void); /*计算器计算函数*/
void changetextstyle(int font, int direction, int charsize); /*改变文本样式函数*/
void mwindow(char *header); /*窗口函数*/
int specialkey(void) ; /*获取特殊键函数*/
int arrow(); /*设置箭头光标函数*/
/*主函数*/
int main()
{
initialize();/* 设置系统进入图形模式 */
computer(); /*运行计算器 */
closegraph();/*系统关闭图形模式返回文本模式*/
return(0); /*结束程序*/
}
/* 设置系统进入图形模式 */
void initialize(void)
{
int xasp, yasp; /* 用于读x和y方向纵横比*/
GraphDriver = DETECT; /* 自动检测显示器*/
initgraph( GraphDriver, GraphMode, "" );
/*初始化图形系统*/
ErrorCode = graphresult(); /*读初始化结果*/
if( ErrorCode != grOk ) /*如果初始化时出现错误*/
{
printf("Graphics System Error: %s\n",
grapherrormsg( ErrorCode ) ); /*显示错误代码*/
exit( 1 ); /*退出*/
}
getpalette( palette ); /* 读面板信息*/
MaxColors = getmaxcolor() + 1; /* 读取颜色的最大值*/
MaxX = getmaxx(); /* 读屏幕尺寸 */
MaxY = getmaxy(); /* 读屏幕尺寸 */
getaspectratio( xasp, yasp ); /* 拷贝纵横比到变量中*/
AspectRatio = (double)xasp/(double)yasp;/* 计算纵横比值*/
}
/*计算器函数*/
void computer(void)
{
struct viewporttype vp; /*定义视口类型变量*/
int color, height, width;
int x, y,x0,y0, i, j,v,m,n,act,flag=1;
float num1=0,num2=0,result; /*操作数和计算结果变量*/
char cnum[5],str2[20]={""},c,temp[20]={""};
char str1[]="1230.456+-789*/Qc=^%";/* 定义字符串在按钮图形上显示的符号 */
mwindow( "Calculator" ); /* 显示主窗口 */
color = 7; /*设置灰颜色值*/
getviewsettings( vp ); /* 读取当前窗口的大小*/
width=(vp.right+1)/10; /* 设置按钮宽度 */
height=(vp.bottom-10)/10 ; /*设置按钮高度 */
x = width /2; /*设置x的坐标值*/
y = height/2; /*设置y的坐标值*/
setfillstyle(SOLID_FILL, color+3);
bar( x+width*2, y, x+7*width, y+height );
/*画一个二维矩形条显示运算数和结果*/
setcolor( color+3 ); /*设置淡绿颜色边框线*/
rectangle( x+width*2, y, x+7*width, y+height );
/*画一个矩形边框线*/
setcolor(RED); /*设置颜色为红色*/
outtextxy(x+3*width,y+height/2,"0."); /*输出字符串"0."*/
x =2*width-width/2; /*设置x的坐标值*/
y =2*height+height/2; /*设置y的坐标值*/
for( j=0 ; j4 ; ++j ) /*画按钮*/
{
for( i=0 ; i5 ; ++i )
{
setfillstyle(SOLID_FILL, color);
setcolor(RED);
bar( x, y, x+width, y+height ); /*画一个矩形条*/
rectangle( x, y, x+width, y+height );
sprintf(str2,"%c",str1[j*5+i]);
/*将字符保存到str2中*/
outtextxy( x+(width/2), y+height/2, str2);
x =x+width+ (width / 2) ; /*移动列坐标*/
}
y +=(height/2)*3; /* 移动行坐标*/
x =2*width-width/2; /*复位列坐标*/
}
x0=2*width;
y0=3*height;
x=x0;
y=y0;
gotoxy(x,y); /*移动光标到x,y位置*/
arrow(); /*显示光标*/
putimage(x,y,rar,XOR_PUT);
m=0;
n=0;
strcpy(str2,""); /*设置str2为空串*/
while((v=specialkey())!=45) /*当压下Alt+x键结束程序,否则执行下面的循环*/
{
while((v=specialkey())!=ENTER) /*当压下键不是回车时*/
{
putimage(x,y,rar,XOR_PUT); /*显示光标图象*/
if(v==RIGHT) /*右移箭头时新位置计算*/
if(x=x0+6*width)
/*如果右移,移到尾,则移动到最左边字符位置*/
{
x=x0;
m=0;
}
else
{
x=x+width+width/2;
m++;
} /*否则,右移到下一个字符位置*/
if(v==LEFT) /*左移箭头时新位置计算*/
if(x=x0)
{
x=x0+6*width;
m=4;
} /*如果移到头,再左移,则移动到最右边字符位置*/
else
{
x=x-width-width/2;
m--;
} /*否则,左移到前一个字符位置*/
if(v==UP) /*上移箭头时新位置计算*/
if(y=y0)
{
y=y0+4*height+height/2;
n=3;
} /*如果移到头,再上移,则移动到最下边字符位置*/
else
{
y=y-height-height/2;
n--;
} /*否则,移到上边一个字符位置*/
if(v==DOWN) /*下移箭头时新位置计算*/
if(y=7*height)
{
y=y0;
n=0;
} /*如果移到尾,再下移,则移动到最上边字符位置*/
else
{
y=y+height+height/2;
n++;
} /*否则,移到下边一个字符位置*/
putimage(x,y,rar,XOR_PUT); /*在新的位置显示光标箭头*/
}
c=str1[n*5+m]; /*将字符保存到变量c中*/
if(isdigit(c)||c=='.') /*判断是否是数字或小数点*/
{
if(flag==-1) /*如果标志为-1,表明为负数*/
{
strcpy(str2,"-"); /*将负号连接到字符串中*/
flag=1;
} /*将标志值恢复为1*/
sprintf(temp,"%c",c); /*将字符保存到字符串变量temp中*/
strcat(str2,temp); /*将temp中的字符串连接到str2中*/
setfillstyle(SOLID_FILL,color+3);
bar(2*width+width/2,height/2,15*width/2,3*height/2);
outtextxy(5*width,height,str2); /*显示字符串*/
}
if(c=='+')
{
num1=atof(str2); /*将第一个操作数转换为浮点数*/
strcpy(str2,""); /*将str2清空*/
act=1; /*做计算加法标志值*/
setfillstyle(SOLID_FILL,color+3);
bar(2*width+width/2,height/2,15*width/2,3*height/2);
outtextxy(5*width,height,"0."); /*显示字符串*/
}
if(c=='-')
{
if(strcmp(str2,"")==0) /*如果str2为空,说明是负号,而不是减号*/
flag=-1; /*设置负数标志*/
else
{
num1=atof(str2); /*将第二个操作数转换为浮点数*/
strcpy(str2,""); /*将str2清空*/
act=2; /*做计算减法标志值*/
setfillstyle(SOLID_FILL,color+3);
bar(2*width+width/2,height/2,15*width/2,3*height/2); /*画矩形*/
outtextxy(5*width,height,"0."); /*显示字符串*/
}
}
if(c=='*')
{
num1=atof(str2); /*将第二个操作数转换为浮点数*/
strcpy(str2,""); /*将str2清空*/
act=3; /*做计算乘法标志值*/
setfillstyle(SOLID_FILL,color+3); bar(2*width+width/2,height/2,15*width/2,3*height/2);
outtextxy(5*width,height,"0."); /*显示字符串*/
}
if(c=='/')
{
num1=atof(str2); /*将第二个操作数转换为浮点数*/
strcpy(str2,""); /*将str2清空*/
act=4; /*做计算除法标志值*/
setfillstyle(SOLID_FILL,color+3);
bar(2*width+width/2,height/2,15*width/2,3*height/2);
outtextxy(5*width,height,"0."); /*显示字符串*/
}
if(c=='^')
{
num1=atof(str2); /*将第二个操作数转换为浮点数*/
strcpy(str2,""); /*将str2清空*/
act=5; /*做计算乘方标志值*/
setfillstyle(SOLID_FILL,color+3); /*设置用淡绿色实体填充*/
bar(2*width+width/2,height/2,15*width/2,3*height/2); /*画矩形*/
outtextxy(5*width,height,"0."); /*显示字符串*/
}
if(c=='%')
{
num1=atof(str2); /*将第二个操作数转换为浮点数*/
strcpy(str2,""); /*将str2清空*/
act=6; /*做计算模运算乘方标志值*/
setfillstyle(SOLID_FILL,color+3); /*设置用淡绿色实体填充*/
bar(2*width+width/2,height/2,15*width/2,3*height/2); /*画矩形*/
outtextxy(5*width,height,"0."); /*显示字符串*/
}
if(c=='=')
{
num2=atof(str2); /*将第二个操作数转换为浮点数*/
switch(act) /*根据运算符号计算*/
{
case 1:result=num1+num2;break; /*做加法*/
case 2:result=num1-num2;break; /*做减法*/
case 3:result=num1*num2;break; /*做乘法*/
case 4:result=num1/num2;break; /*做除法*/
case 5:result=pow(num1,num2);break; /*做x的y次方*/
case 6:result=fmod(num1,num2);break; /*做模运算*/
}
setfillstyle(SOLID_FILL,color+3); /*设置用淡绿色实体填充*/
bar(2*width+width/2,height/2,15*width/2,3*height/2); /*覆盖结果区*/
sprintf(temp,"%f",result); /*将结果保存到temp中*/
outtextxy(5*width,height,temp); /*显示结果*/
}
if(c=='c')
{
num1=0; /*将两个操作数复位0,符号标志为1*/
num2=0;
flag=1;
strcpy(str2,""); /*将str2清空*/
setfillstyle(SOLID_FILL,color+3); /*设置用淡绿色实体填充*/
bar(2*width+width/2,height/2,15*width/2,3*height/2); /*覆盖结果区*/
outtextxy(5*width,height,"0."); /*显示字符串*/
}
if(c=='Q')exit(0); /*如果选择了q回车,结束计算程序*/
}
putimage(x,y,rar,XOR_PUT); /*在退出之前消去光标箭头*/
return; /*返回*/
}
/*窗口函数*/
void mwindow( char *header )
{
int height;
cleardevice(); /* 清除图形屏幕 */
setcolor( MaxColors - 1 ); /* 设置当前颜色为白色*/
setviewport( 20, 20, MaxX/2, MaxY/2, 1 ); /* 设置视口大小 */
height = textheight( "H" ); /* 读取基本文本大小 */
settextstyle( DEFAULT_FONT, HORIZ_DIR, 1 );/*设置文本样式*/
settextjustify( CENTER_TEXT, TOP_TEXT );/*设置字符排列方式*/
outtextxy( MaxX/4, 2, header ); /*输出标题*/
setviewport( 20,20+height+4, MaxX/2+4, MaxY/2+20, 1 ); /*设置视口大小*/
drawboder(); /*画边框*/
}
void drawboder(void) /*画边框*/
{
struct viewporttype vp; /*定义视口类型变量*/
setcolor( MaxColors - 1 ); /*设置当前颜色为白色 */
setlinestyle( SOLID_LINE, 0, NORM_WIDTH );/*设置画线方式*/
getviewsettings( vp );/*将当前视口信息装入vp所指的结构中*/
rectangle( 0, 0, vp.right-vp.left, vp.bottom-vp.top ); /*画矩形边框*/
}
/*设计鼠标图形函数*/
int arrow()
{
int size;
int raw[]={4,4,4,8,6,8,14,16,16,16,8,6,8,4,4,4}; /*定义多边形坐标*/
setfillstyle(SOLID_FILL,2); /*设置填充模式*/
fillpoly(8,raw); /*画出一光标箭头*/
size=imagesize(4,4,16,16); /*测试图象大小*/
rar=malloc(size); /*分配内存区域*/
getimage(4,4,16,16,rar); /*存放光标箭头图象*/
putimage(4,4,rar,XOR_PUT); /*消去光标箭头图象*/
return 0;
}
/*按键函数*/
int specialkey(void)
{
int key;
while(bioskey(1)==0); /*等待键盘输入*/
key=bioskey(0); /*键盘输入*/
key=key0xff? key0xff:key8; /*只取特殊键的扫描值,其余为0*/
return(key); /*返回键值*/
}
C语言的sprintf函数问题,到底怎么用啊???
那个缓存区只能自己定义csprintf源码了csprintf源码,不想自己定义csprintf源码的话那么用stringstream
stringstream stream;
stream"frist"12333333;
string temp=stream.str();
求LINUX下,C语言编写的日志输出源码~
#include stdlib.h
#include string.h
#include stdio.h
#include dirent.h
#include time.h
#define LOGFILE "./dir_log_0"
int g_Count;
//#define MAXLEN 1024
void WriteDebugLog(char *str);
int main(int argc, char **argv)
{
char str[1024]={0};
strcpy(str,"file no find");
int i=0,j=0;
for (i=0; i10; i++)
{
for (j=0; j50; j++)
{
WriteDebugLog(str);
}
}
return 0;
}
void WriteDebugLog(char *str)
{
char buf[2048]={0};
char logFileName[50]={0};
//long MAXLEN = 50*1024*1024;//50MB
int iMax = 1024;//1K
time_t timep;
FILE *fp = NULL;
struct tm *p;
time(timep);
p = localtime(timep);
memset(buf,0,sizeof(buf));
sprintf(buf,"[%d-%d-%d %d:%d:%d][DEBUG]",(1900+p-tm_year),(1+p-tm_mon), p-tm_mday,p-tm_hour, p-tm_min, p-tm_sec); //星期p-tm_wday
strcat(buf,str);
strcat(buf,"\r\n");
strcpy(logFileName,LOGFILE);
int len = strlen(logFileName);
logFileName[len-1] = '0'+g_Count;
fp = fopen(logFileName,"r");
if(fp==NULL)
{
fp = fopen(logFileName,"w+");
}
else
{
fseek(fp,0,2);//SEEK_END值为2
if( ftell(fp) = iMax)
{
fclose(fp);
if (g_Count = 9)
{
logFileName[len-1] = '0';
g_Count=0;
}
else
{
g_Count++;
logFileName[len-1] = '0'+g_Count;
// printf("\n%c",'0'+g_Count);
}
fp = fopen(logFileName,"w+");
}
else
{
fclose(fp);
fp = fopen(logFileName,"a");
}
}
fwrite(buf,1,strlen(buf),fp);
fclose(fp);
}