怎樣做C語言課程設計?(三)

    (續二)


    好吧,之前說過,C語言課程設計是對課程知識的覆蓋式應用,所以不妨來翻翻書。控制流語句,有;數組,有;函數,有;指針,有;結構體,沒有;文件,沒有。理論上,應該還是可以繼續改進的。

    把結構體納入程序其實很容易,當我們看到加法的時候,很自然的就會聯想到四則運算,同時,考慮到除法的特殊性(整數/整數=整數),應該將數據類型從int修改爲double。
    寫到這的時候,我已經不太想寫了,因爲需要對現有的程序做傷筋動骨的大修改......算了,誰讓咱命苦呢!


    設計4:

#include "stdio.h"
#include "stdlib.h"
#include "time.h"
#define MAXSIZE 20

/* 結構體 */
struct compute
{
    double a;
    double b;
    char op;
    double r;
};  /* a op b = r */

/* 四則運算 */
double plus(double a, double b)
{
    return a+b;
}
double minus(double a, double b)
{
    return a-b;
}
double multi(double a, double b)
{
    return a*b;
}
double divide(double a, double b)
{
    return (b==0?0:a/b);
}

/* 輸入兩個整數,計算其和 */
void inputData(struct compute *px)
{
    printf("請輸入表達式:");
    scanf("%lf%c%lf", &px->a, &px->op, &px->b);
}
void process(struct compute *px)
{
    double (*pf)(double, double);
    switch(px->op)
    {
        case '+':   pf=plus;    break;
        case '-':   pf=minus;   break;
        case '*':   pf=multi;   break;
        case '/':   pf=divide;  break;
    }
    px->r = pf(px->a, px->b);
}
void outputData(struct compute x)
{
    printf("%.2lf %c %.2lf = %.2lf\n", x.a, x.op, x.b, x.r);
}

/* 隨機生成n個整數,計算其和 */
int randomNum(int low, int high)
{
    int t;
    t=rand();
    while(t<0)    t=rand();
    return low+t%(high-low+1);
}
void inputBatch(int *pn)
{
    do {
        printf("請輸入計算次數(1-%d):", MAXSIZE);
        scanf("%d", pn);
    }while(*pn<1 || *pn>MAXSIZE);
}
void processBatch(struct compute ax[], int n)
{
    int i, t;
    for(i=0; i<n; ++i)
    {
        ax[i].a=randomNum(0,100);
        ax[i].b=randomNum(0,100);
        t=randomNum(0,3);
        switch(t)
        {
            case 0:  ax[i].op='+';  break;
            case 1:  ax[i].op='-';  break;
            case 2:  ax[i].op='*';  break;
            case 3:  ax[i].op='/';  break;
        }
        process(&ax[i]);
    }
}
void outputBatch(struct compute ax[], int n)
{
    int i;
    for(i=0; i<n; ++i)
        outputData(ax[i]);
}

void showMenu()
{

    struct compute xVal, xArr[MAXSIZE];
    int n;
    int in, exitFlag=0;
    srand((unsigned)time(NULL));  /* 以當前時間做隨機數種子 */
    while(1)
    {
        system("cls");
        printf("***************************************\n");
        printf("*************** 歡迎使用 **************\n");
        printf("***************************************\n");
        printf("請選擇:\n");
        printf("    1----輸入一個表達式,計算結果\n");
        printf("    2----輸入多個表達式,批量計算結果\n");
        printf("    0----結束程序\n");
        printf("請輸入(1,2,0):");
        scanf("%d", &in);
        switch(in)
        {
        case 1:
             inputData(&xVal);
             process(&xVal);
             outputData(xVal);
             system("pause");  break;
        case 2:
             inputBatch(&n);
             processBatch(xArr,n);
             outputBatch(xArr,n);
             system("pause");  break;
        case 0:    exitFlag=1;  break;
        }
        if (exitFlag)
        {
        printf("***************************************\n");
        printf("********* 感謝您的使用,再見!*********\n");
        printf("***************************************\n");
        break;
        }
    }
    system("pause");
}

main()
{
    showMenu();
}


    這裏同樣思考一個問題:把數據類型從int修改爲double,是不是跑題了?答案是沒有,因爲程序中隨機數生成的仍然是整數,所以只能算是擴展了課題要求。


    (未完待續)


發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章