運算思維與程式設計課程程式範例:C語言

This Post Has 3 Comments

  1. hhliu

    三角形列印練習(1)

    #include <stdio.h>
    
    int main(int argc, const char * argv[]) {
        int i,j;
        int n;
        
        printf("請輸入N(高,4~9):");
        scanf("%d", &n);
        
        for(i=0;i<n;++i){
            for(j=0;j<=i;++j){
                printf("*");
            }
            printf("\n");
        }
        return 0;
    }
  2. hhliu

    三角形列印練習(2)

    #include <stdio.h>
    
    int main(int argc, const char * argv[]) {
        int i,j;
        int n;
        
        printf("請輸入N(高,4~9):");
        scanf("%d", &n);
        
        for(i=0;i<n;++i){
            for(j=0;j<n;++j){
                if (j<n-i-1)
                    printf(" ");
                else
                    printf("*");
            }
            printf("\n");
        }
        return 0;
    }
    
  3. hhliu

    輸入檢查與進制轉換前導程式範例:

    #include <stdio.h>
    #include <conio.h>   
    
    int main(int argc, const char * argv[]) {
        char nSys;
        int nSource = 0, nDest = 0;
        printf("選擇來源進制(1~16):");
        nSys = getch();
        char check = 1;
        while (nSys != 10) {
            if (check==1 && nSys >='1' && nSys<='9') {
                nSource = nSys - 48;
                printf("%d", nSource);
                check++;
                if (nSource > 1) {
                    printf("\n");
                    break;
                }
            } else if (check ==2 && nSys>='1' && nSys<='6') {
                nSource = nSource*10 + nSys - 48;
                printf("%d\n", nSys - 48);
                break;
            }
            nSys = getch();
        }
        
        printf("選擇目的進制(1~16):");
        nSys = getch();
        check = 1;
        while (nSys != 10) {
            if (check==1 && nSys >='1' && nSys<='9') {
                nDest = nSys - 48;
                printf("%d", nDest);
                check++;
                if (nSource > 1) {
                    printf("\n");
                    break;
                }
            } else if (check ==2 && nSys>='1' && nSys<='6') {
                nDest = nDest*10 + nSys - 48;
                printf("%d\n", nSys - 48);
                break;
            }
            nSys = getch();
        }
        
        printf("nSource = %d\n", nSource);
        printf("nDest = %d\n", nDest);
        
        return 0;
    }
    

發佈留言