2023年5月4日 星期四

0504

 WEEK12

step01-1 

新的空白檔案 練習寫檔案


程式碼:


#include <stdio.h>
int main()
{
    ///檔案指標 fout = fopen("檔名","模式");要開檔
    FILE * fout = fopen("file.txt","w");
    printf("Hello World");
    fprintf(fout, "Hello World在檔案裡\n");
}

step01-2 

程式碼


#include<stdio.h>
int main()
{
    char line[20];
    ///scanf("%s",line);
    FILE * fin = fopen("file.txt","r");///r:read讀
    fscanf(fin,"%s",line);
    printf("讀到了:%s\n",line);///馬上印
    fscanf(fin,"%s",line);
    printf("讀到了:%s\n",line);///馬上印
}


step01-3

程式碼

#include <stdio.h>
int main()
{
    int a[10]={10,20,30,40,50,60,70,80,90,100};
    FILE * fout = fopen("file3.txt","w");
    for ( int i=0;i<10;i++ )
    {
        fprintf(fout, "%d ",a[i]);///寫到檔案去 同時
        printf("%d ",a[i]);///順便印出來 讓你看看
    }

    fclose(fout);

    int b[10];
    FILE * fin = fopen("file3.txt","r");
    for ( int i=0;i<10;i++ )
    {
        fscanf(fin, "%d", &b[i]);///讀到陣列b[i]裡
        printf("%d ",b[i]);

    }

    fclose(fin);

}


STEP02-1

開心的GLUT專案

茶壺跟著滑鼠案的地方移動

程式碼:



STEP02-2

程式碼:

#include <stdio.h>
#include <GL/glut.h>
float teapotX=0,teapotY=0;
FILE * fout = NULL;///step02-1
FILE * fin = NULL;///step02-2
void display()
{
    glClear(GL_COLOR_BUFFER_BIT|GL_DEPTH_BUFFER_BIT);
    glPushMatrix();
        glTranslatef(teapotX,teapotY,0);
        glutSolidTeapot(0.3);
    glPopMatrix();
    glutSwapBuffers();
}
void mouse( int button, int state, int x, int y)
{
    teapotX = (x-150)/150.0;
    teapotY = (150-y)/150.0;
    if ( state==GLUT_DOWN )///如果mouse按下去
    {
        if( fout==NULL )  fout=fopen("file4.txt","w");///step02-2

        fprintf(fout, "%f %f\n", teapotX, teapotY);
    }
    display();
}
void keyboard(unsigned char key, int x, int y )///step02-2
{
    if ( fin==NULL )
    {
        fclose(fout);///step02-2
        fin = fopen("file4.txt","r");///step02-2


    }
    display();///step02-2
}
int main(int argc , char** argv)
{
    glutInit(&argc,argv);
    glutInitDisplayMode(GLUT_DOUBLE | GLUT_DEPTH);
    glutCreateWindow("week12");

    glutDisplayFunc(display);
    glutKeyboardFunc(keyboard);///step02-2
    glutMouseFunc(mouse);///step02-1

    glutMainLoop();

}



茶壺會跟著滑鼠案的地方移動
再按空白鍵 茶壺會跟著



STEP03-1

CodeBlocks的專案設定 project  properties 裡第二個 build target 
Executing working dir 工作執行目錄
原本是:C:\User\Adiministrator\Desktop\freeglut\bin
改成小數點. 再file save everthing 便能將專案檔設好 存檔
之後再執行工作執行目錄 就在你的程式專案的那個目錄裡
但執行就會少一個 freeglut.dll檔 手動複製到自己的專案裡

















沒有留言:

張貼留言