跳轉到內容

高中數學擴充套件/數學程式設計/輸出例程

來自華夏公益教科書,開放書籍,為開放世界

添加了兩個新的全域性變數:double x_val 和 double delta。

添加了兩個新函式:void output() 和 double process()。

更改了一個函式:void execute_command(char command)。

全域性變數

double x_val:傳遞給 f(x) 的 x 值。

double delta:執行 + 或 - 命令時更改 x 的值。

函式新增

void output()

  • 此函式呼叫 process 以獲取 f(x) 的值,然後列印訊息 f(x)=value。

double process()

  • 執行 f(x) 的 C 實現。

函式更改

void execute_command(char command)

  • 呼叫新的函式 output 以顯示 f(x)=value。
  • 實現變數 x_val 和 delta 的 '-' 和 '+' 命令。

要更改的程式碼

[編輯 | 編輯原始碼]
新增並替換以下程式碼
 //function prototypes
 //...
 float process();
 void output();

 //global variables
 //...
 float x_val;
 float delta;

 //function definitions
 //...
 void execute_command(char command)
{
    switch (command)
    {
        case '=':   output();
                    break;
        case '+':   output();
                    break;
        case '-':   output();
                    break;
        case 'd':
        case 'D':   break;
        case 'x':
        case 'X':   done++;
                    break;
    }
}
float process()
{
    return x_val * x_val;
}
void output()
{
    float f_val;
    f_val=process();
    cprintf("f(%f)=%6f.\n",x_val,f_val);

}


下一步

[編輯 | 編輯原始碼]
華夏公益教科書