在程式入口添加密碼檢查,正確才繼續執行:
#include <graphics.h>
#include <math.h>
#include <stdlib.h>
#include <string.h>
#define PI 3.1415926
int main(int argc, char *argv[]) {
int x, y, r1, rs, color;
double a;
int gdriver = DETECT, gmode;
// 檢查參數個數
if (argc < 5) {
printf("用法: %s password r1 rs color\n", argv[0]);
return 1;
}
// 密碼驗證
if (strcmp(argv[1], "pass") != 0) {
printf("password error!\n");
return 0;
}
initgraph(&gdriver, &gmode, "..\\bgi");
r1 = atoi(argv[2]); // 基礎圓半徑
rs = atoi(argv[3]); // 同心圓半徑
color = atoi(argv[4]); // 背景色
cleardevice();
setbkcolor(color);
setcolor(4);
for (a = 0; a <= 2*PI; a += PI/18) {
x = r1 * cos(a) + 320;
y = r1 * sin(a) + 240;
circle(x, y, rs);
}
getch();
closegraph();
return 0;
}
命令列: L6-30 pass 20 40 3
│ │ │ │ │
▼ ▼ ▼ ▼ ▼
argv[0] argv[1] argv[2] argv[3] argv[4]
"pass" "20" "40" "3"
argv[1] 為密碼字符串,用 strcmp 比較argv[2]~argv[4] 為數值參數,用 atoi 轉整數argv[0] 永遠是程式名graphics.h)為 Turbo C 專用,現代編譯器需替換