2023年5月4日 星期四

91 week12

 week12-1:

開一個空白檔案取名.cpp

要開檔案指標 fopen()

程式碼:

#include <stdio.h>

int main()

{

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

    printf("Hello World\n");

    fprintf(fout,"Hello World在檔案裡\n");

}


week12-2:
開一個新檔名week12-2.cpp
程式碼:
#include <stdio.h>
int main()
{
    char line[20];
    FILE*fin=fopen("file.txt","r");///r:read
    fscanf(fin,"%s",line);
    printf("讀到了:%s\n",line);///馬上印
    fscanf(fin,"%s",line);
    printf("讀到了:%s\n",line);///馬上印
}
week12-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]);///
        printf("%d ",b[i]);
    }
    fclose(fin);
}

week12-4:
開一個glut專案
程式碼:
#include <stdio.h>
#include <GL/glut.h>
float teapotX=0,teapotY=0;
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;
    display();
}
int main(int argc,char**argv)
{
    glutInit(&argc,argv);
    glutInitDisplayMode(GLUT_DOUBLE|GLUT_DEPTH);
    glutCreateWindow("week12");
    glutDisplayFunc(display);
    glutMouseFunc(mouse);
    glutMainLoop();
}

修改程式碼:
#include <stdio.h>
#include <GL/glut.h>
float teapotX=0,teapotY=0;
FILE*fout=NULL;
FILE*fin=NULL;
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){
        if(fout==NULL) fout=fopen("file4.txt","w");
        fprintf(fout,"%f %f\n",teapotX,teapotY);
    }
    display();
}
void keyboard(unsigned char key,int x,int y)
{
    if(fin==NULL){
        fclose(fout);
        fin=fopen("file4.txt","r");
    }
    fscanf(fin,"%f%f",&teapotX,&teapotY);
    display();
}
int main(int argc,char**argv)
{
    glutInit(&argc,argv);
    glutInitDisplayMode(GLUT_DOUBLE|GLUT_DEPTH);
    glutCreateWindow("week12");
    glutDisplayFunc(display);
    glutKeyboardFunc(keyboard);
    glutMouseFunc(mouse);
    glutMainLoop();
}

codeblocks week12-4選擇save everything,查看week12-4的資料夾,找到week12-4.cbp,
notepad++開啟,查看working-->properties-->-->build target-->-->execution working dir的空格
改成小數點,最後工作目錄會變成在week12-4的專案裡。

期末作品:
codeblocks關掉,copy整個week12-4目錄,名稱改成Final_Project,專案從week12-4.cbp改成Final_Project.cbp。codeblocks打開project,查看working-->properties-->-->build target-->-->execution working dir的空格
改成小數點。
project-->working-->properties-->title改成Final_Project。

git指令上傳。










沒有留言:

張貼留言