能帮我用C语言编写一个时钟的程序么 Visual c/c++ 新手 没分 求帮助
展开全部
#include <windows.h>
#include <gl/gl.h>
#include <gl/glu.h>
#include <gl/glut.h>
#include <math.h>
// Rotation amounts
static GLfloat xRot = 0.0f;
static GLfloat yRot = 0.0f;
// Current Time
static GLfloat h = 0; //小时
static GLfloat m = 0; //分钟
static GLfloat s= 0; //秒
// Light values and coordinates
GLfloat ambientLight[] = { 0.3f, 0.3f, 0.3f, 1.0f };
GLfloat diffuseLight[] = { 0.7f, 0.7f, 0.7f, 1.0f };
GLfloat specular[] = { 1.0f, 1.0f, 1.0f, 1.0f};
GLfloat specref[] = { 1.0f, 1.0f, 1.0f, 1.0f };
//
#define PI 3.141526
#define N 1000
#define R 1
///////////////////////////////////////////////////////////////////////////////
// Reset flags as appropriate in response to menu selections
void DrawBase()
{
int i;
float ang = PI * 2 / N;
glColor3f(1.0,0.0,1.0);
glBegin(GL_LINE_LOOP);
for(i=0; i<N;i++)
glVertex2f( R * cos(i* ang), R * sin(i * ang));
glEnd();
// 几个时间
glBegin(GL_LINES);
glVertex2f( 0,R * 0.9);glVertex2f( 0,R );
glVertex2f( R * 0.9,0);glVertex2f(R ,0 );
glVertex2f( 0,-R * 0.9);glVertex2f( 0,-R );
glVertex2f( -R * 0.9,0);glVertex2f(-R ,0 );
glEnd();
}
void DrawHour()
{
float ang = 360 - 360 * (h + m/60 + s/3600) / 12;
glPushMatrix();
glRotatef(ang, 0.0f, 0.0f, 1.0f);
glColor3f(0.0,0.0,1.0);
glBegin(GL_POLYGON);
glLineWidth(3.0f);
glVertex3f(0,0,0.2);
glVertex3f(-0.1,R*0.2,0.2);
glVertex3f(0,R*0.5,0.2);
// glVertex3f(0.1,R*0.2,0.2);
glEnd();
glPopMatrix();
}
void DrawMinute()
{
float ang = 360 - 360 * (m + s/60) / 60;
glPushMatrix();
glRotatef(ang, 0.0f, 0.0f, 1.0f);
glColor3f(1.0,0,0);
glBegin(GL_LINE_LOOP);
glLineWidth(3.0f);
glVertex3f(0,0,0.3);
glVertex3f(0,R*0.7,0.3);
glEnd();
glPopMatrix();
}
void DrawSecond()
{
float ang = 360 - 360 * s/60;
glPushMatrix();
glRotatef(ang, 0.0f, 0.0f, 1.0f);
glColor3f(0.0,1.0,0);
glBegin(GL_LINE_LOOP);
glLineWidth(3.0f);
glVertex3f(0,0,0.4);
glVertex3f(0,R*0.85,0.4);
glEnd();
glPopMatrix();
}
// Called to draw scene
void RenderScene(void)
{
// Clear the window
glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);
// Save matrix state and do the rotation
glPushMatrix();
glRotatef(xRot, 1.0f, 0.0f, 0.0f);
DrawHour();
// 表盘
DrawBase();
// 画秒针
DrawSecond();
// 分针
DrawMinute();
// 时针
glPopMatrix();
// Flush drawing commands
glutSwapBuffers();
}
// This function does any needed initialization on the rendering
// context.
void SetupRC()
{
// Black background
glClearColor(1.0f, 1.0f, 1.0f, 1.0f );
// Enable Depth Testing
glEnable(GL_DEPTH_TEST);
// Enable lighting
glEnable(GL_LIGHTING);
// Setup and enable light 0
glLightfv(GL_LIGHT0,GL_AMBIENT,ambientLight);
glLightfv(GL_LIGHT0,GL_DIFFUSE,diffuseLight);
glLightfv(GL_LIGHT0,GL_SPECULAR,specular);
glEnable(GL_LIGHT0);
// Enable color tracking
glEnable(GL_COLOR_MATERIAL);
// Set Material properties to follow glColor values
glColorMaterial(GL_FRONT, GL_AMBIENT_AND_DIFFUSE);
// All materials hereafter have full specular reflectivity
// with a high shine
glMaterialfv(GL_FRONT, GL_SPECULAR,specref);
glMateriali(GL_FRONT,GL_SHININESS,128);
// Set drawing color to blue
glColor3ub(0, 0, 255);
}
void SpecialKeys(int key, int x, int y)
{
if(key == GLUT_KEY_UP)
xRot-= 5.0f;
if(key == GLUT_KEY_DOWN)
//xRot += 5.0f;
h = h+1;
if(key == GLUT_KEY_LEFT)
// yRot -= 5.0f;
m = m+3;
if(key == GLUT_KEY_RIGHT)
//yRot += 5.0f;
s = s+5;
if(key > 356.0f)
xRot = 0.0f;
if(key < -1.0f)
xRot = 355.0f;
if(key > 356.0f)
yRot = 0.0f;
if(key < -1.0f)
yRot = 355.0f;
// Refresh the Window
glutPostRedisplay();
}
void ChangeSize(int w, int h)
{
GLfloat lightPos[] = { -50.f, 50.0f, 100.0f, 1.0f };
GLfloat nRange = 1.9f;
// Prevent a divide by zero
if(h == 0)
h = 1;
// Set Viewport to window dimensions
glViewport(0, 0, w, h);
// Reset projection matrix stack
glMatrixMode(GL_PROJECTION);
glLoadIdentity();
// Establish clipping volume (left, right, bottom, top, near, far)
if (w <= h)
glOrtho (-nRange, nRange, -nRange*h/w, nRange*h/w, -nRange, nRange);
else
glOrtho (-nRange*w/h, nRange*w/h, -nRange, nRange, -nRange, nRange);
// Reset Model view matrix stack
glMatrixMode(GL_MODELVIEW);
glLoadIdentity();
glLightfv(GL_LIGHT0,GL_POSITION,lightPos);
}
int main(int argc, char* argv[])
{
int nSolidMenu;
int nWireMenu;
int nMainMenu;
glutInit(&argc, argv);
glutInitDisplayMode(GLUT_DOUBLE | GLUT_RGB | GLUT_DEPTH);
glutCreateWindow("GLUT CLOCK");
glutReshapeFunc(ChangeSize);
glutSpecialFunc(SpecialKeys);
glutDisplayFunc(RenderScene);
SetupRC();
glutMainLoop();
return 0;
}
#include <gl/gl.h>
#include <gl/glu.h>
#include <gl/glut.h>
#include <math.h>
// Rotation amounts
static GLfloat xRot = 0.0f;
static GLfloat yRot = 0.0f;
// Current Time
static GLfloat h = 0; //小时
static GLfloat m = 0; //分钟
static GLfloat s= 0; //秒
// Light values and coordinates
GLfloat ambientLight[] = { 0.3f, 0.3f, 0.3f, 1.0f };
GLfloat diffuseLight[] = { 0.7f, 0.7f, 0.7f, 1.0f };
GLfloat specular[] = { 1.0f, 1.0f, 1.0f, 1.0f};
GLfloat specref[] = { 1.0f, 1.0f, 1.0f, 1.0f };
//
#define PI 3.141526
#define N 1000
#define R 1
///////////////////////////////////////////////////////////////////////////////
// Reset flags as appropriate in response to menu selections
void DrawBase()
{
int i;
float ang = PI * 2 / N;
glColor3f(1.0,0.0,1.0);
glBegin(GL_LINE_LOOP);
for(i=0; i<N;i++)
glVertex2f( R * cos(i* ang), R * sin(i * ang));
glEnd();
// 几个时间
glBegin(GL_LINES);
glVertex2f( 0,R * 0.9);glVertex2f( 0,R );
glVertex2f( R * 0.9,0);glVertex2f(R ,0 );
glVertex2f( 0,-R * 0.9);glVertex2f( 0,-R );
glVertex2f( -R * 0.9,0);glVertex2f(-R ,0 );
glEnd();
}
void DrawHour()
{
float ang = 360 - 360 * (h + m/60 + s/3600) / 12;
glPushMatrix();
glRotatef(ang, 0.0f, 0.0f, 1.0f);
glColor3f(0.0,0.0,1.0);
glBegin(GL_POLYGON);
glLineWidth(3.0f);
glVertex3f(0,0,0.2);
glVertex3f(-0.1,R*0.2,0.2);
glVertex3f(0,R*0.5,0.2);
// glVertex3f(0.1,R*0.2,0.2);
glEnd();
glPopMatrix();
}
void DrawMinute()
{
float ang = 360 - 360 * (m + s/60) / 60;
glPushMatrix();
glRotatef(ang, 0.0f, 0.0f, 1.0f);
glColor3f(1.0,0,0);
glBegin(GL_LINE_LOOP);
glLineWidth(3.0f);
glVertex3f(0,0,0.3);
glVertex3f(0,R*0.7,0.3);
glEnd();
glPopMatrix();
}
void DrawSecond()
{
float ang = 360 - 360 * s/60;
glPushMatrix();
glRotatef(ang, 0.0f, 0.0f, 1.0f);
glColor3f(0.0,1.0,0);
glBegin(GL_LINE_LOOP);
glLineWidth(3.0f);
glVertex3f(0,0,0.4);
glVertex3f(0,R*0.85,0.4);
glEnd();
glPopMatrix();
}
// Called to draw scene
void RenderScene(void)
{
// Clear the window
glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);
// Save matrix state and do the rotation
glPushMatrix();
glRotatef(xRot, 1.0f, 0.0f, 0.0f);
DrawHour();
// 表盘
DrawBase();
// 画秒针
DrawSecond();
// 分针
DrawMinute();
// 时针
glPopMatrix();
// Flush drawing commands
glutSwapBuffers();
}
// This function does any needed initialization on the rendering
// context.
void SetupRC()
{
// Black background
glClearColor(1.0f, 1.0f, 1.0f, 1.0f );
// Enable Depth Testing
glEnable(GL_DEPTH_TEST);
// Enable lighting
glEnable(GL_LIGHTING);
// Setup and enable light 0
glLightfv(GL_LIGHT0,GL_AMBIENT,ambientLight);
glLightfv(GL_LIGHT0,GL_DIFFUSE,diffuseLight);
glLightfv(GL_LIGHT0,GL_SPECULAR,specular);
glEnable(GL_LIGHT0);
// Enable color tracking
glEnable(GL_COLOR_MATERIAL);
// Set Material properties to follow glColor values
glColorMaterial(GL_FRONT, GL_AMBIENT_AND_DIFFUSE);
// All materials hereafter have full specular reflectivity
// with a high shine
glMaterialfv(GL_FRONT, GL_SPECULAR,specref);
glMateriali(GL_FRONT,GL_SHININESS,128);
// Set drawing color to blue
glColor3ub(0, 0, 255);
}
void SpecialKeys(int key, int x, int y)
{
if(key == GLUT_KEY_UP)
xRot-= 5.0f;
if(key == GLUT_KEY_DOWN)
//xRot += 5.0f;
h = h+1;
if(key == GLUT_KEY_LEFT)
// yRot -= 5.0f;
m = m+3;
if(key == GLUT_KEY_RIGHT)
//yRot += 5.0f;
s = s+5;
if(key > 356.0f)
xRot = 0.0f;
if(key < -1.0f)
xRot = 355.0f;
if(key > 356.0f)
yRot = 0.0f;
if(key < -1.0f)
yRot = 355.0f;
// Refresh the Window
glutPostRedisplay();
}
void ChangeSize(int w, int h)
{
GLfloat lightPos[] = { -50.f, 50.0f, 100.0f, 1.0f };
GLfloat nRange = 1.9f;
// Prevent a divide by zero
if(h == 0)
h = 1;
// Set Viewport to window dimensions
glViewport(0, 0, w, h);
// Reset projection matrix stack
glMatrixMode(GL_PROJECTION);
glLoadIdentity();
// Establish clipping volume (left, right, bottom, top, near, far)
if (w <= h)
glOrtho (-nRange, nRange, -nRange*h/w, nRange*h/w, -nRange, nRange);
else
glOrtho (-nRange*w/h, nRange*w/h, -nRange, nRange, -nRange, nRange);
// Reset Model view matrix stack
glMatrixMode(GL_MODELVIEW);
glLoadIdentity();
glLightfv(GL_LIGHT0,GL_POSITION,lightPos);
}
int main(int argc, char* argv[])
{
int nSolidMenu;
int nWireMenu;
int nMainMenu;
glutInit(&argc, argv);
glutInitDisplayMode(GLUT_DOUBLE | GLUT_RGB | GLUT_DEPTH);
glutCreateWindow("GLUT CLOCK");
glutReshapeFunc(ChangeSize);
glutSpecialFunc(SpecialKeys);
glutDisplayFunc(RenderScene);
SetupRC();
glutMainLoop();
return 0;
}
追问
感觉好深奥啊 不过还是要谢谢你啊 谢谢
展开全部
呵呵,我2分钟写出来一个……你参考这玩吧……
#include <stdio.h>
#include <time.h>
#define DAY (60*60*24)
int main(){
time_t t=0;
int h=0,m=0,s=0;//小时,分钟,秒
while(1){
t=time(0);
h=(t%DAY/3600+8)%24;
m=t%3600/60;
s=t%60;
printf("%02d:%02d:%02d\r",h,m,s);
fflush(stdout);
return 0;
}
#include <stdio.h>
#include <time.h>
#define DAY (60*60*24)
int main(){
time_t t=0;
int h=0,m=0,s=0;//小时,分钟,秒
while(1){
t=time(0);
h=(t%DAY/3600+8)%24;
m=t%3600/60;
s=t%60;
printf("%02d:%02d:%02d\r",h,m,s);
fflush(stdout);
return 0;
}
更多追问追答
追问
能给我个详细点的么 还有能出图像的啊 还有 我忘了告诉你 是机械时钟 就是有针的那种 嘿嘿 这个我是个小白 选这个课题是没办法了 喜欢的没有,这个看来最简单了
追答
晕,那就太麻烦了,如果用C的话还得画表盘……
已赞过
已踩过<
评论
收起
你对这个回答的评价是?
展开全部
怎么样一个功能呢?
追问
就是和普通的时钟一样,也要有秒针、分针和时针。C语言,毕业设计,很重要的 最好新颖一点 估计日后还要麻烦你 283930393 这是我QQ 方便的话加我一下 这样我找你就方便点 嘿嘿
追答
您这太麻烦了,不是不想帮您,是我的时间不允许啊!对不起啊
已赞过
已踩过<
评论
收起
你对这个回答的评价是?
推荐律师服务:
若未解决您的问题,请您详细描述您的问题,通过百度律临进行免费专业咨询