1. 程式人生 > 實用技巧 >FPGA的並行性

FPGA的並行性

以下實驗都在Visual Studio 2019 Preview平臺完成

———————————————————————————————————————————————————————

實驗3-1

#include <math.h>
#include<stdlib.h>
#include <stdio.h>
int main() {
    float a, b, c, x1, x2;
    float delta, real, imag;
    printf("Enter a, b, c: ");
    while (scanf_s("%f%f%f"
, &a, &b, &c ) != EOF) { if (a == 0) printf("not quadratic equation.\n\n"); else { delta = b * b - 4 * a * c; if (delta >= 0) { x1 = (-b + sqrt(delta)) / (2 * a); x2 = (-b - sqrt(delta)) / (2 * a); printf(
"x1 = %.2f, x2 = %.2f\n\n", x1, x2); } else { real = -b / (2 * a); imag = sqrt(-delta) / (2 * a); printf("x1 = %.2f + %.2fi, x2 = %.2f - %.2fi\n\n", real,imag, real, imag); } } printf("Enter a, b, c: "); }
return 0; }

執行過程: 程式在讀取了輸入的3個數後對第一個數進行判斷,若不是0再進行下一步運算→判斷b^2-4ac的值來確定結果是否有虛根→通過計算得到兩根的值。

ps:在對迴圈輸入結束時,要換到新的一行輸入^Z並再重複兩次,否則會無法中斷導致程式無限迴圈。(應該是沒有清除快取區內的資料,導致輸入的^Z被識別成了26(ASCII),參與了運算,因此無法結束)

—————————————————————————————————————————————————

實驗3-2

#include <stdio.h>
#include <stdlib.h>
#include <time.h>
#define N 5
int main() {
    int x, n;
    srand(time(0));
    n = 0;
    do {
        n++;
        x = rand() % 10; 
        printf("%3d", x);
    } while (n < N);
    printf("\n");
    return 0;
}

執行過程:定義N的值作為輸出的個數→設定當前系統時間為rand函式的種子→多次迴圈運算得到N個隨機數→取餘數以後輸出得到結果。

因為系統時間在不斷變化,因此每次輸出的結果都有所不同,如果將srand()中的值設為固定的數,那麼無論如何執行輸出的結果也都是固定的幾個數。

————————————————————————————————————————————————

實驗3-3

#include <stdio.h>
#include <stdlib.h>
int main()
{
    int a=100, b, c=0;
    while (a<=200)
    {
        a++;
        for ( b = 2; b < a; b++)
        {
            if (a%b==0)
            {
                break;
            }
        }
        if (b==a)
        {
            printf("%d ", b); c++;
            if (c%5==0)
            {
                printf("\n");
            }
        }
    }
    printf("\n一共有%d個素數",c);
    return 0;
}

——————————————————————————————————————————————

實驗3-4

#include <stdio.h>
#include <stdlib.h>
int main()
{
    long int s;
    int i = 1, a, b = 0;
    while (printf("Enter a number:"), scanf_s("%ld", &s) != EOF)
    {
        do
        {
            i *= 10;
        } while (i <= s);
        a = i;
        printf("new number is:");
        while (a > 1)
        {
            b = (s % a) / (a / 10); a /= 10;
            if (b % 2 == 1)
            {
                printf("%d", b);
            }
        }
        printf("\n\n");
    }
    return 0;
}

實驗思路:通過do while迴圈來判斷輸入的數的位數→通過取餘數來保證數字的高低位不產生變化→除以同位數(例有x位)的10......00(同樣有x位)得到該位上的數字→對該數取二的餘數來判斷奇偶→按位輸出得到結果

前方也是王道征途啊(讚賞)

——————————————————————————————————————————————

實驗3-5

#include <stdio.h>
#include <stdlib.h>
#include<math.h>
int main()
{
    double a, b = 1, c = 0, d, e = 1, f = 1;
    while (printf("Enter n(1~10):"), scanf_s("%lf", &a) != 0)
    {
        printf("\n");
        while (b <= a)
        {
            d = pow(-1, b - 1);
            while (f <= b)
            {
                e *= f; f++;
            }
            c += d * (1 / e);
            b++;
        }
        printf("n = %.0lf,s = %lf\n\n", a, c);
    }
    return 0;
}

———————————————————————————————————————————————————

實驗3-6

#include <stdio.h>
#include <stdlib.h>
#include<time.h>
int main()
{
    int x,a=0,b=0,c=0;
    srand(time(0));
    x = rand() % 31+1;
    printf("猜猜2020年12月哪一天會是你的luck day\n\n");
    printf("開始嘍,你有三次機會,猜吧(1~31):");
    scanf_s("%d", &a);
    if (a<x)
    {
        printf("你猜的日期早了,luck day還沒到呢\n再猜(1~31):");
        scanf_s("%d",&b);
        if (b<x)
        {
            printf("你猜的日期早了,luck day還沒到呢\n再猜(1~31):");
            scanf_s("%d",&c);
            if (c<x)
            {
                printf("你猜的日期早了,luck day還沒到呢\n\n次數用完啦,偷偷告訴你:12月,你的luck day是%d號",x);
            }
            else if (c > x)
            {
                printf("你猜的日期晚了,luck day悄悄溜到前面啦\n\n次數用完啦,偷偷告訴你:12月,你的luck day是%d號", x);
            }
            else
            {
                printf("猜對啦!");
            }
        }
        else if (b > x)
        {
            printf("你猜的日期晚了,luck day悄悄溜到前面啦\n再猜(1~31):");
            scanf_s("%d",&c);
            if (c < x)
            {
                printf("你猜的日期早了,luck day還沒到呢\n\n次數用完啦,偷偷告訴你:12月,你的luck day是%d號", x);
            }
            else if (c > x)
            {
                printf("你猜的日期晚了,luck day悄悄溜到前面啦\n\n次數用完啦,偷偷告訴你:12月,你的luck day是%d號", x);
            }
            else
            {
                printf("猜對啦!");
            }
        }
        else
        {
            printf("猜對啦!");
        }
    }
    else if (a > x)
    {
        printf("你猜的日期晚了,luck day悄悄溜到前面啦\n再猜(1~31):");
        scanf_s("%d",&b);
        if (b < x)
        {
            printf("你猜的日期早了,luck day還沒到呢\n再猜(1~31):");
            scanf_s("%d",&c);
            if (c < x)
            {
                printf("你猜的日期早了,luck day還沒到呢\n\n次數用完啦,偷偷告訴你:12月,你的luck day是%d號", x);
            }
            else if (c > x)
            {
                printf("你猜的日期晚了,luck day悄悄溜到前面啦\n\n次數用完啦,偷偷告訴你:12月,你的luck day是%d號", x);
            }
            else
            {
                printf("猜對啦!");
            }
        }
        else if (b > x)
        {
            printf("你猜的日期晚了,luck day悄悄溜到前面啦\n再猜(1~31):");
            scanf_s("%d",&c);
            if (c < x)
            {
                printf("你猜的日期早了,luck day還沒到呢\n\n次數用完啦,偷偷告訴你:12月,你的luck day是%d號", x);
            }
            else if (c > x)
            {
                printf("你猜的日期晚了,luck day悄悄溜到前面啦\n\n次數用完啦,偷偷告訴你:12月,你的luck day是%d號", x);
            }
            else
            {
                printf("猜對啦!");
            }
        }
        else
        {
            printf("猜對啦!");
        }
    }
    else
    {
        printf("猜對啦!");
    }
    return 0;
}

或者

#include <stdio.h>
#include <stdlib.h>
#include<time.h>
int main()
{
    int x, a = 0,b=0;
    srand(time(0));
    x = rand() % 31 + 1;
    printf("猜猜2020年12月哪一天會是你的luck day\n\n");
    printf("開始嘍,你有三次機會,猜吧(1~31):");
    scanf_s("%d", &a);
    for ( b = 1; b <=2; b++)
    {
        if (a < x)
        {
            printf("你猜的日期早了,luck day還沒到呢\n再猜:");
            scanf_s("%d", &a);
        }
        else if (a > x)
        {
            printf("你猜的日期晚了,luck day悄悄溜到前面啦\n再猜:");
            scanf_s("%d", &a);
        }
    }
    if (a!=x)
    {
        if (a < x)
        {
            printf("你猜的日期早了,luck day還沒到呢\n");
        }
        else if (a > x)
        {
            printf("你猜的日期晚了,luck day悄悄溜到前面啦\n");
        }
        printf("\n\n次數用完啦,偷偷告訴你:12月,你的luck day是%d號", x);
    }
    else
    {
        printf("猜對啦!");
    }
    return 0;
}