1. 程式人生 > 實用技巧 >nginx實現檔案伺服器

nginx實現檔案伺服器

任務1

// ex1.cpp
#include<stdio.h>
int main(){
    int a=5,b=7,c=100,d,e,f;
    
    d=a/b*c;
    e=a*c/b;
    f=c/b*a;
    printf("d=%d,e=%d,f=%d\n",d,e,f);
    
    return 0;
}

r對應的數學計算式

d=[a/b]*c=[5/7]*100

e=[a*c/b]=[5*100/7]

F=[c/b]*a=[100/7]*5

結果不同的原因

除號取整以及運算的先後順序造成計算結果不同

任務2

// ex2.cpp
#include<stdio.h>
int
main(){ int x=1234; float f=123.456; double m=123.456; char ch='a'; char a[]="hello,world!"; //定義一個數組a,陣列中存放字串常量 hello,world! int y=3,z=4; printf("%d %d\n",y,z); printf("y=%d,z=%d\n",y,z); printf("%8d,%2d\n",x,x); printf("%f,%8f,%8.1f,%0.2f,%.2e\n",f,f,f,f,f); printf(
"%lf\n",m); printf("%3c\n",ch); printf("%s\n%15s\n%10.5s\n%2.5s\n%.3s\n",a,a,a,a,a); return 0; }

這個程式中用到的格式符的用法

%d %f %s 輸出對應的整型/浮點型數字/字串

%8d 以八位輸出對應的整型數字,不足的位在前面補空格,

%2d 以二位輸出對應的整型數字,超過的部分正常輸出

%8f 以八位輸出對應的浮點型數字,超過的部分正常輸出

%8.1f 以八位輸出對應的浮點型數字,保留小數點後一位,不足的位在前面補空格

%0.2f 輸出對應的浮點型數字,保留小數點後兩位

%.2e 以指數形式輸出對應的浮點型數字,保留小數點後兩位

%lf double輸入/輸出對應的浮點型數字

%3c 以三位輸出對應的字元,不足的位在前面補空格

%15s 以十五位輸出對應的字串,不足的位在前面補空格

%10.5s 以十位輸出對應的字串的前五個字元,不足的位在前面補空格

%2.5s 以兩位輸出對應的字串的前五個字元,超過的部分正常輸出

%.3s 輸出對應的字串的前三個字元

任務3

// ex3.cpp
#include<stdio.h>
int main(){
    double x,y;
    char c1,c2,c3;
    int a1,a2,a3; 
    
    scanf("%d%d%d",&a1,&a2,&a3);
    printf("%d,%d,%d\n",a1,a2,a3);
    scanf(" %c %c %c",&c1,&c2,&c3);
    printf("%c%c%c\n",c1,c2,c3);
    scanf("%lf,%lf",&x,&y);
    printf("%f,%lf\n",x,y);
    
    return 0;
}

任務4

// ex4.cpp
// 判斷字元型別 
#include<stdio.h>
int main(){
    char x;
    
    x=getchar();
    
    if(x>='0'&&x<='9') //待補足①,判斷x是數字字元表示式 
        printf("%c是數字字元\n",x);
    else if(x>='a'&&x<='z'||x>='A'&&x<='Z') //待補足②,判斷x是大寫或小寫英文字母的表示式
    printf("%c是英文字母\n",x);
    else
    printf("%c是其他字元\n",x);
    
    return 0;
}

任務5

// ex5.cpp
#include<stdio.h>
int main(){
    char ans1, ans2;
    
    printf("複習了沒?(輸入y或Y表示複習了,輸入n或N表示沒複習): ");
    ans1 = getchar(); //從鍵盤輸入一個字元,賦值給ans1
    
    getchar(); //思考這裡為什麼要加這一行,試著去掉這一行,看看對執行有沒有影響。
    
    printf("\n動手敲程式碼了沒?(輸入y或Y表示敲了,輸入n或N表示木有敲): "); 
    ans2 = getchar();
    
    if( (ans1=='y' || ans1=='Y') && (ans2=='y' || ans2=='Y') ) //待補足,判斷使用者回答ans1和ans2都是小寫y或大寫Y 
        printf("\n羅馬不是一天建成的:)\n");
    else
        printf("\n羅馬不是一天毀滅的。。。\n");
    
    return 0;
}

任務6

// ex6.cpp
#include<stdio.h>
int main(){
    int n,i,x,sum;
    
    scanf("%d",&n);
    i=1;
    sum=1;
    x=n-1;
    
    while(x>0){
        i=i*2;
        sum += i;
        x--;
    }
    
    printf("n = %d 時, sum = %d \n",n,sum);
    
    return 0;
}

任務7

// ex7.cpp
#include<stdio.h>
int main(){
    char s1[]=" O ",s2[]="<H>",s3[]="I I",s[]="   ";
    int i,x,n;
    i=8;
        
while(i>0){
    
    n=(8-i)/2;
    while(n>0){
        printf("%9s",s);
        n--;
    }
    printf("%s",s1);
    x=i;
    while(x>1){
        printf("%9s",s1);
        x--;
    }
    printf("%9s\n",s1);
    
    n=(8-i)/2;
    while(n>0){
        printf("%9s",s);
        n--;
    }
    printf("%s",s2);
    x=i;
       while(x>1){
        printf("%9s",s2);
        x--;
    }
    printf("%9s\n",s2);
    
    n=(8-i)/2;
    while(n>0){
        printf("%9s",s);
        n--;
    }
    printf("%s",s3);
    x=i;
       while(x>1){
        printf("%9s",s3);
        x--;
    }
    printf("%9s\n",s3);
    
    i--,i--;
}
    
    n=(8-i)/2;
    while(n>0){
        printf("%9s",s);
        n--;
    }
     printf("%s\n",s1);
     
    n=(8-i)/2;
    while(n>0){
        printf("%9s",s);
        n--;
    }
     printf("%s\n",s2);
    
    n=(8-i)/2;
    while(n>0){
        printf("%9s",s);
        n--;
    }
     printf("%s\n",s3);
    
    return 0;
}

實驗總結

本次實驗涉及的知識點主要是輸入輸出函式以及相應函式中格式符的使用,%d,%c,%f,%s等最基礎常用的格式符的使用方法。

存在的問題

在任務3當中,實驗要求是修改兩行程式碼,而再修改的過程中我修改了三行程式碼,可能是因為我對與語法格式還不夠熟練

在任務5當中,同樣的一個邏輯語句,同樣的一個編譯軟體,在不同的地點執行,結果不同,具體原因未知,猜測由於網路原因造成

在任務7當中,有一個在下一個第九位輸出的格式符需要運用,但由於我忘記了,只能用%9d代替,雖然實現了相同的效果,但還是希望下次可以牢記所用到的格式符