2022年5月24日 星期二

Week 14

寫入名為 file.txt 的檔案


#include <stdio.h>

int main()

{

    FILE * fout = fopen("file.txt","w+");


    printf("Hello World\n");

    fprintf(fout,"Hello World\n");


    fclose(fout);

讀入檔案並寫出來並寫出來

#include <stdio.h>

int main()

{

    FILE * fout = fopen("file2.txt","w+");


    fprintf(fout,"angle1 %d\n",999);


    fclose(fout);


    char line[200];

    int a;

    FILE * fin = fopen("file2.txt","r");

    fscanf(fin ,"%s %d",line ,&a);

    printf("讀到字串:%s 和整數%d \n",line,a);

    fclose(fin);



}


將angle的數值寫入file.txt


#include <GL/glut.h>

#include <stdio.h>

float angle[20], oldX=0;

int angleID=0;

FILE * fout =NULL;

void myWrite(){

    if(fout==NULL) fout = fopen("file.txt","w+");

    for(int i=0;i<20;i++){

        printf("%.2f ",angle[i]);    

        fprintf(fout, "%.2f ",angle[i]);

    }

    printf("\n");

    fprintf(fout,"\n");

}

void keyboard(unsigned char key,int x,int y){

    if(key=='0') angleID=0;

    if(key=='1') angleID=1;

    if(key=='2') angleID=2;

    if(key=='3') angleID=3;

}

void mouse(int button,int state,int x,int y){

    oldX=x;

}

void motion(int x,int y){

    angle[angleID]+=(x-oldX);

    myWrite();

    oldX=x;

    glutPostRedisplay();

}


void display()

{

    glClear(GL_COLOR_BUFFER_BIT|GL_DEPTH_BUFFER_BIT);

    glColor3f(1,1,1);

    glRectf(0.3,0.5,-0.3,-0.2);///瘦身


        glPushMatrix();///左半邊

            glTranslatef(-0.3, 0.5, 0);///把下面旋轉中的東西 "掛在"本體

            glRotatef( angle[2], 0,0,1 );///旋轉

            glTranslatef(0.3,-0.4, 0);///

            glColor3f(1,0,0.5);///粉紅色的

            glRectf(-0.3,0.5,-0.8,0.3);///手臂

            glPushMatrix();

                glTranslatef(-0.8, 0.4, 0);///把下面旋轉中的東西 "掛在"手臂

                glRotatef( angle[3], 0,0,1 );///旋轉

                glTranslatef(0.8,-0.4, 0);///

                glColor3f(1,0.5,0);///橘色的

                glRectf(-0.8,0.5,-1.1,0.3);///手肘

            glPopMatrix();

        glPopMatrix();


        glPushMatrix();///右半邊

            glTranslatef(0.3, 0.5, 0);///把下面旋轉中的東西 "掛在"本體

            glRotatef( angle[0], 0,0,1 );///旋轉

            glTranslatef(-0.3,-0.4, 0);///

            glColor3f(1,0,0.5);///粉紅色的

            glRectf(0.3,0.5,0.8,0.3);///手臂

            glPushMatrix();

                glTranslatef(0.8, 0.4, 0);///把下面旋轉中的東西 "掛在"手臂

                glRotatef( angle[1], 0,0,1 );///旋轉

                glTranslatef(-0.8,-0.4, 0);///

                glColor3f(1,0.5,0);///橘色的

                glRectf(0.8,0.5,1.1,0.3);///手肘

            glPopMatrix();

        glPopMatrix();

    glutSwapBuffers();

}



int main( int argc,char**argv)

{


    glutInit(&argc,argv);

    glutInitDisplayMode(GLUT_DOUBLE|GLUT_DEPTH);


    glutCreateWindow("week14 T-R-T");


    glutKeyboardFunc(keyboard);

    glutMouseFunc(mouse);

    glutMotionFunc(motion);

    glutDisplayFunc(display);


    glutMainLoop();


}



float angle[20], oldX=0;

int angleID=0;

FILE * fout =NULL, *fin=NULL;

void myWrite(){

    if(fout==NULL) fout = fopen("file.txt","w+");

    for(int i=0;i<20;i++){

        printf("%.2f ",angle[i]);

        fprintf(fout, "%.2f ",angle[i]);

    }

    printf("\n");

    fprintf(fout,"\n");

}

void myRead(){

    if(fin=NULL) fin =fopen("file.txt","r");

    for(int i=0;i<20;i++){

        fscanf(fin,"%f",&angle[i]);

    }

    glutPostRedisplay();

}

void keyboard(unsigned char key,int x,int y){

    if(key=='0') angleID=0;

    if(key=='1') angleID=1;

    if(key=='2') angleID=2;

    if(key=='3') angleID=3;

    if(key=='r'){

        myRead();

    }


計時器(鬧鐘)

#include <GL/glut.h>
#include <stdio.h>
void timer(int t){
    printf("Wake Up %d\n",t);
    printf("Next\n");
    glutTimerFunc(1000,timer,t+1);
    printf("Go Back\n");
}
void display()
{

}
int main( int argc,char**argv)
{

    glutInit(&argc,argv);
    glutInitDisplayMode(GLUT_DOUBLE|GLUT_DEPTH);
    glutCreateWindow("week14 Timer");

    glutTimerFunc(3000,timer,0);
    glutDisplayFunc(display);
    glutMainLoop();
}

沒有留言:

張貼留言