2021年5月31日 星期一

week15

1.1主題:存檔、讀檔

程式碼:

#include<stdio.h>//外掛printf() fprintf() fscanf() fopen()


int main(int argc,char**argv)


{


    FILE * fout=NULL;//檔案指標變數


    fout=fopen("a.txt","w+");//開啟檔案


    printf("Hello World\n");


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








1.2註解掉原先的程式碼

#include <stdio.h> ///外掛 printf(), fprintf(), fscanf(), fopen()


int main( int argc, char** argv )

{

///    FILE * fout=NULL;///step01 檔案指標 file output 變數

///    fout = fopen("檔名.txt", "w+");/// 開啟檔案

///

///    printf("Hello World\n");

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


    FILE * fin = NULL;///改成讀檔

    fin = fopen("檔名.txt", "r"); ///讀檔


  ///scanf(  "%d", &n ); //int

  ///scanf(  "%c", &c ); //char

  ///scanf(  "%f", &f ); //float

  ///scanf(  "%s", line ); //

    char line[100]; ///宣告字串 line

    fscanf(fin, "%s", line); ///讀入字串

     printf("現在讀到的是 %s \n", line );///step03 順便印出字串


    fscanf(fin, "%s", line); /// 讀入字串

     printf("現在讀到的是 %s \n", line );///印出字串

}








1.3跑出茶壺

程式碼:


#include <stdio.h>

#include <GLUT/glut.h>

void display()

{

    glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);

        glutSolidTeapot(0.3);

    glutSwapBuffers();

}

int main(int argc, char** argv)

{

    glutInit( &argc, argv );

    glutInitDisplayMode( GLUT_DOUBLE | GLUT_DEPTH );

    glutCreateWindow("week15 many angles");


    glutDisplayFunc(display);

    glutMainLoop();

}









2.1複製茶壺

程式碼:


#include <stdio.h>

#include <GLUTglut.h>

float angle=0, diff=2;


void timer(int t)

{

    glutTimerFunc(30, timer, t+1);///新的timer

    angle += diff;

    if(angle>90) diff = -2;

    if(angle<0) diff = +2;

    glutPostRedisplay();

}

void display()

{

    glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);

        glutSolidTeapot(0.3);

    glutSwapBuffers();

}

int main(int argc, char** argv)

{

    glutInit( &argc, argv );

    glutInitDisplayMode( GLUT_DOUBLE | GLUT_DEPTH );

    glutCreateWindow("week15 many angles");


    glutDisplayFunc(display);

    glutMainLoop();

}








2.2 有一隻手臂的茶壺

程式碼:


#include <stdio.h>

#include <GLUT/glut.h>

float angle=0, diff=2;


void timer(int t)

{

    glutTimerFunc( 30, timer, t+1 );

    angle += diff;

    if(angle>90) diff = -2;

    if(angle<0) diff = +2;

    glutPostRedisplay();

}

void display()

{

    glClear( GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT );

    glPushMatrix();

        glutSolidTeapot( 0.3 );

        glPushMatrix();

            glTranslatef(-0.3,0,0);

            glRotatef(angle, 0,0,1);

            glTranslatef(-0.3,0,0);

            glutSolidTeapot( 0.3 );

            glPushMatrix();

                glTranslatef(-0.3, 0,0);

                glRotatef(angle, 0,0,1);

                glTranslatef(-0.3, 0,0);

                glutSolidTeapot( 0.3 );

            glPopMatrix();

        glPopMatrix();

        glPushMatrix();

            glutSolidTeapot( 0.3 );

            glutSolidTeapot( 0.3 );

        glPopMatrix();

    glPopMatrix();

    glutSwapBuffers();

}

int main( int argc, char** argv )

{

    glutInit( &argc, argv );

    glutInitDisplayMode( GLUT_DOUBLE | GLUT_DEPTH );

    glutCreateWindow( "week15 many angles" );


    glutDisplayFunc( display );

    glutTimerFunc( 0, timer, 0);

    glutMainLoop();

}









沒有留言:

張貼留言

Week18期末作業(橘貓的跳舞熊熊)

 期末作業(橘貓的跳舞熊熊) 影片: https://youtu.be/R89tptMaQZw 程式碼: #include <opencv/highgui.h> #include <opencv/cv.h> #include <GL/glut.h...