Week12
step01
開啟空白檔案 > 存至桌面 > 命名為week12-1_printf_fprintf.cpp > 程式碼執行 > 到桌面目錄找到檔名為file.txt
開啟空白檔案
↓
檔案存至桌面 > 命名為week12-1_printf_fprintf.cpp
↓
程式碼執行 > 到桌面目錄找到檔名為file.txt
#include <stdio.h>
int main(){
///檔案指標fout=開檔案("檔名","用什麼模式")
FILE*fout=fopen("file.txt","w");
printf("Hello World\n");
fprintf(fout,"Hello World在檔案裡\n");
}
-1
step02
檔案存至桌面 > 命名為week12-2_scanf_fscanf.cpp
程式碼執行 > 到桌面目錄找到檔名為file.txt
#include <stdio.h>
int main(){
///檔案指標fout=開檔案("檔名","用什麼模式")
FILE*fin=fopen("file.txt","r");
char line[100];
fscanf(fin,"%s",line);
printf("從file.txt讀到了:%s\n",line);
fscanf(fin,"%s",line);
printf("從file.txt讀到了:%s\n",line);
}
-2
要先把小黑視窗關掉file3.txt才會顯示文字
檔案存至桌面 > 命名為week12-3_scanf__printf_fscanf_fprintf.cpp
程式碼執行 > 到桌面目錄找到檔名為file.txt
#include <stdio.h>
int main(){
int a[10]={10,20,30,40,50,60,70,80,90,100};
int b[10]={};
FILE*fout=fopen("file3.txt","w");
for(int i=0;i<10;i++){
printf("%d",a[i]);
fprintf(fout,"%d ",a[i]);
}
printf("\n");
fprintf(fout,"\n");
fclose(fout);
FILE*fin=fopen("file3.txt","r");
for(int i=0;i<10;i++){
fscanf(fin,"%d",&b[i]);
printf("b%d:%d ",i,b[i]);
}
fclose(fin);
}
-3
使用keyboard mouse操控茶壺
#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){
if(state==GLUT_DOWN){
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();
}
-4
開GLUT檔
Project > Properties > Build targets > Execution working dir > Debug和Release兩個都要改 > 預設設定改成. > 要Save everthing
↓
↓
↓
#include <stdio.h>
#include <GL/glut.h>
FILE*fout=NULL;
FILE*fin=NULL;
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){
if(state==GLUT_DOWN){
teapotX=(x-150)/150.0;
teapotY=(150-y)/150.0;
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);
glutMouseFunc(mouse);
glutKeyboardFunc(keyboard);
glutMainLoop();
}
-5
安裝Git GitBash
-cd desktop
-git clone https://github.com/Owoyayou/2023graphicsa
-cd 2023graphicsa
-start .
(打開2023graphicsa資料夾,把今日程式碼檔案放進來)
-git add .
-git status
(開啟檔案總管)
-git config --global user.email ______________
-git config --global user.name Owoyayou
-git commit -m "week12"
-git push










沒有留言:
張貼留言