2021年5月31日 星期一

你要出多少_week15

檔案輸出

先建立專案檔

下面是程式碼


///主題: 存檔、讀檔

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

int main(int argc,char** argv)

{

    FILE * fout=NULL;

    fout = fopen("檔名.txt","w+");


    printf("Hello World\n");

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

}

紅色程式碼印出的Hello World在黑色小視窗中


///主題: 存檔、讀檔

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

int main(int argc,char** argv)

{

    FILE * fout=NULL;

    fout = fopen("檔名.txt","w+");


    printf("Hello World\n");

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

}

而這個紅色程式碼的Hello World要去C槽找freeglut再選bin會看到檔名.txt,打開後就會看到Hello World


接著去用notepad++編輯自己的cbp檔,把working_dir後面""裡面的名稱改為.,接著存檔回到專案檔重建,檔名.txt檔案位子就會更換,這樣就是成功了

如圖下


接著我們來改程式碼,首先我們要把舊程式全部註解掉
再來照著下面的程式碼打

///主題: 存檔、讀檔

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

int main(int argc,char** argv)

{

    //FILE * fout=NULL;

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


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

   // fprintf(fout,"Hello World\n");  (註解掉的程式碼)


   FILE * fin = NULL;

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


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

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

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

   ///scanf( "%s",line );//char line[100];

   ///以上是複習的程式碼


   char line[100];

   fscanf(fin,"%s",line);

   printf("現在讀到的是 %s \n",line);


   fscanf(fin,"%s",line);

   printf("現在讀到的是 %s \n",line);

}

下面是執行結果


首先我們將程式碼備份,然後全部刪除,來打新的程式碼,以下是程式碼

#include <stdio.h>

#include <GL/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("week_15");


    glutDisplayFunc(display);

    glutMainLoop();


}

如果執行過程無法運行,就去把freeglut裡的bin的freeglut.dll複製貼到專案檔中的資料夾,再去執行就可以了


#include <stdio.h>
#include <GL/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);
        glutSolidTeapot(0.3);///身體
        glutSolidTeapot(0.3);///左手臂
        glutSolidTeapot(0.3);///左手肘
        glutSolidTeapot(0.3);///右手臂
        glutSolidTeapot(0.3);///右手肘
    glutSwapBuffers();
}
int main(int argc, char **argv)
{
    glutInit(&argc, argv);
    glutInitDisplayMode(GLUT_DOUBLE | GLUT_DEPTH);
    glutCreateWindow("week_15");


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


}

到這邊五個茶壺會固定不動,所以我們只會看到一個茶壺,因為他們疊在一起了


接著我們來做會動的手臂

#include <stdio.h>
#include <GL/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();///Step05-2
        glutSolidTeapot( 0.3 );///身體
        glPushMatrix();///Step05-2
            glTranslatef(-0.3,0,0);
            glRotatef(angle, 0,0,1);
            glTranslatef(-0.3,0,0);
            glutSolidTeapot( 0.3 );///左手臂, 但是,要用 T-R-T移位置
            glPushMatrix();
                glTranslatef(-0.3, 0,0);
                glRotatef(angle, 0,0,1);
                glTranslatef(-0.3, 0,0);
                glutSolidTeapot( 0.3 );///左手肘
            glPopMatrix();
        glPopMatrix();///Step05-2
        glPushMatrix();///Step05-2
            glutSolidTeapot( 0.3 );///右手臂
            glutSolidTeapot( 0.3 );///右手肘
        glPopMatrix();///Step05-2
    glPopMatrix();///Step05-2
    glutSwapBuffers();
}
int main(int argc, char **argv)
{
    glutInit(&argc, argv);
    glutInitDisplayMode(GLUT_DOUBLE | GLUT_DEPTH);
    glutCreateWindow("week_15");


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


}

下圖是執行結果


做出了左手臂,我們來做一下右手臂吧。
#include <stdio.h>
#include <GL/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();///Step05-2
        glutSolidTeapot( 0.3 );///身體
        glPushMatrix();
            glTranslatef(-0.3,0,0);
            glRotatef(angle, 0,0,1);
            glTranslatef(-0.3,0,0);
            glutSolidTeapot( 0.3 );///左手臂, 但是,要用 T-R-T移位置
            glPushMatrix();
                glTranslatef(-0.3, 0,0);
                glRotatef(angle, 0,0,1);
                glTranslatef(-0.3, 0,0);
                glutSolidTeapot( 0.3 );///左手肘
            glPopMatrix();
        glPopMatrix();
        glPushMatrix();
            glTranslatef(+0.3,0,0);
            glRotatef(-angle, 0,0,1);
            glTranslatef(+0.3,0,0);
            glutSolidTeapot( 0.3 );///右手臂
            glPushMatrix();///step05-3
                glTranslatef(+0.3,0,0);
                glRotatef(-angle, 0,0,1);
                glTranslatef(+0.3,0,0);
                glutSolidTeapot( 0.3 );///右手肘
            glPopMatrix();///Step05-3
        glPopMatrix();///Step05-2
    glPopMatrix();///Step05-2
    glutSwapBuffers();
}
int main(int argc, char **argv)
{
    glutInit(&argc, argv);
    glutInitDisplayMode(GLUT_DOUBLE | GLUT_DEPTH);
    glutCreateWindow("week_15");


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


}

執行結果如下






沒有留言:

張貼留言

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

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