Linux程序之可變參數選項那些事!

一、linux應用程序如何接收參數?

1. argc、argv

Linux應用程序執行時,我們往往通過命令行帶入參數給程序,比如

ls /dev/ -l  

其中參數 /dev/-l都是作為參數傳遞給命令 ls

應用程序又是如何接收這些參數的?

通常應用程序都是從main函數開始執行,傳統的main函數風格如下:

int main(int argc, char* argv[]) 
argc:
程序的命令行參數的數量,用于統計參數數量。 
argv:
是一個指向一個字符串數組的指針,數組包含了參數,每個字符串就是一個參數,最后一個元素為0。
過一般習慣使用多級指針來操作字符串。

*char argv[]有時候我們也寫成char argv

**argv[]**是一個存放字符類型元素地址的數組。

因為 C 中是有字符串的概念的:將每個字符存放在 char 數組,最后一個元素為**\0**表示字符串的結束。

**printf(%s)**就是輸出字符串。

并且一般使用argv指針來訪問、處理argv[]數組的內容。

C語言中,數組就是一個指針加偏移量。

所以argv則是指向一個指針數組argv[]的指針,不用定義,直接可以用。

在argv[]數組中存放的的指針指向輸入命令的各部分**(調用程序、選項、參數)**。

2. 舉例

下面我們用一個實例來理解argc和argv

/*
* argc: 命令行參數的個數
* argv: 字符指針數組(指向各個命令行參數的字符指針所構成的數組)
*/
int main(int argc, char* argv[]) // 接收命令行參數
{printf("argc=%d\n",argc);for (int i = 0; i < argc; i++) {printf("argv[%d]: %s\n", i, argv[i]); // 遍歷字符指針數組argv}return 0;
}

執行結果

peng@ubuntu:~/work$ ./peng arg1 arg2 arg3 
argc=4
argv[0]: ./peng
argv[1]: arg1
argv[2]: arg2
argv[3]: arg3

參數與argc,argv關系如下:

二、選項

1. 選項含義

linux程序除了上述情況以外,我們還經常會遇到一個使用方法就是選項應用,

比如:ping命令

peng@ubuntu:~/work$ ping -h
Usage: ping [-aAbBdDfhLnOqrRUvV] [-c count] [-i interval] [-I interface][-m mark] [-M pmtudisc_option] [-l preload] [-p pattern] [-Q tos][-s packetsize] [-S sndbuf] [-t ttl] [-T timestamp_option][-w deadline] [-W timeout] [hop1 ...] destination

參數含義:

-a:嘗試將IP地址解析為主機名。
-A:使用響應數據包中的附加數據。
-b:允許ping廣播地址。
-B:不允許ping廣播地址。
-c count:設置要發送的數據包數量。
-d:使用SO_DEBUG選項。
-D:不將socket設為分離模式。
-f:向目標發送一個“強制”數據包。
-h:顯示幫助信息。
-i interval:設置發送數據包之間的時間間隔。
-I interface:設置要使用的網絡接口。
-l preload:設置發送的數據包數量。
-m mark:設置ping數據包的標記。
-M pmtudisc_option:設置MTU發現選項。
-n:不要將IP地址解析為主機名。
-O:啟用原始輸出。
-p pattern:設置數據包的模式。
-Q tos:設置服務類型。
-r:不使用路由表,直接發送數據包到目標主機。
-R:啟用記錄路由。
-s packetsize:設置數據包的大小。
-S sndbuf:設置套接字的發送緩沖區大小。
-t ttl:設置數據包的TTL值。
-T timestamp_option:設置時間戳選項。
-U:使用UDP數據包。
-v:顯示詳細的ping命令輸出。
-V:顯示ping命令的版本信息。
-w deadline:設置等待響應的時間。
-W timeout:設置等待響應的超時時間。destination:指定要ping的目標主機或IP地址。

這些 - 開頭的都是選項,
[]表示可選的意思

[-aAbBdDfhLnOqrRUvV]    是無參的選項
[-c count] [-i interval] [-I interface]
[-m mark] [-M pmtudisc_option] [-l preload] [-p pattern] [-Q tos]
[-s packetsize] [-S sndbuf] [-t ttl] [-T timestamp_option]
[-w deadline] [-W timeout] [hop1 ...]  這些都是有參數的選項
destination   必須填寫的參數

前輩們利用這點發明了“UNIX 風格”的命令,選項前面加一個橫杠-,用于區分選項和參數。

2. 程序如何區分參數和選項?

在程序的代碼實現中,按照 UNIX 的代碼慣例,上來直接跳過第一個,然后判斷指針指向的字符串第一個字符是不是-,如果是的,那么進入一個switch判斷,用case列出多種支持的情況下,應該執行什么代碼。

例如下面這樣就可以判斷選項和處理參數:

int c;
while (--argc > 0 && (*++argv)[0] == '-' {while (c = *++argv[0] {switch(c){case 'x':...break;case 'n':...break;default:printf("xxx: illegal opyion %c\n", c);...break;}}
}

3. getopt、getopt_long

事實這么處理選項參數是比較麻煩的,

linux提供了選項解析的函數:

// 頭文件
#include<unistd.h>
#include<getopt.h>          /*所在頭文件 */
int getopt(intargc, char * const argv[], const char *optstring);
int getopt_long(int argc, char * const argv[], const char *optstring,const struct option *longopts, int*longindex);
int getopt_long_only(int argc, char * const argv[],const char *optstring,const struct option *longopts, int*longindex);
extern char *optarg;         /*系統聲明的全局變量 */
extern int optind, opterr, optopt;

三、getopt

1. 定義:

int getopt(int argc, char * const argv[], const char *optstring);
功能:getopt是用來解析命令行選項參數的,但是只能解析短選項: **-d 100**,不能解析長選項:**--prefix**
參數argc:main()函數傳遞過來的參數的個數argv:main()函數傳遞過來的參數的字符串指針數組optstring:選項字符串,告知 getopt()可以處理哪個選項以及哪個選項需要參數
返回:如果選項成功找到,返回選項字母;如果所有命令行選項都解析完畢,返回 -1;如果遇到選項字符不在 optstring 中,返回字符 ‘?’;如果遇到丟失參數,那么返回值依賴于 optstring 中第一個字符,如果第一個字符是 ‘:’ 則返回’:‘,否則返回’?'并提示出錯誤信息。

2. optstring 含義 【重要】

下邊重點舉例說明optstring的格式意義:

char*optstring = “ab:c::;
單個字符a         表示選項a沒有參數            格式:-a即可,不加參數
單字符加冒號b:     表示選項b有且必須加參數      格式:-b 100-b100,-b=100錯
單字符加2冒號c::   表示選項c可以有,也可以無     格式:-c200,其它格式錯誤

上面這個 optstring 在傳入之后,getopt 函數將依次檢查命令行是否指定了 -a, -b, -c(這需要多次調用 getopt 函數,直到其返回-1),當檢查到上面某一個參數被指定時,函數會返回被指定的參數名稱(即該字母)

系統聲明的4個全局變量含義如下:

optarg —— 指向當前選項參數(如果有)的指針。
optind —— 再次調用 getopt() 時的下一個 argv指針的索引。
optopt —— 最后一個未知選項。
opterr -—— 如果不希望getopt()打印出錯信息,則只要將全域變量opterr設為0即可。

3. 實例

說千道萬,不如來一個實例:

#include<stdio.h>
#include<unistd.h>
#include<getopt.h>
int main(intargc, char *argv[])
{int opt;char *string = "a::b:c:d";while ((opt = getopt(argc, argv, string))!= -1){  printf("opt = %c\t\t", opt);printf("optarg = %s\t\t",optarg);printf("optind = %d\t\t",optind);printf("argv[optind] = %s\n",argv[optind]);}  
}
  • 正確輸入參數,執行結果如下:
peng@ubuntu:~/work/test$ ./peng -a100 -b 200 -c 300 -d
opt = a		optarg = 100		optind = 2		argv[optind] = -b
opt = b		optarg = 200		optind = 4		argv[optind] = -c
opt = c		optarg = 300		optind = 6		argv[optind] = -d
opt = d		optarg = (null)		optind = 7		argv[optind] = (null)

或者

ork/test$ ./peng -a100 -b200 -c300 -d 
opt = a		optarg = 100		optind = 2		argv[optind] = -b200
opt = b		optarg = 200		optind = 3		argv[optind] = -c300
opt = c		optarg = 300		optind = 4		argv[optind] = -d
opt = d		optarg = (null)		optind = 5		argv[optind] = (null)
  • 輸入選項參數錯誤的情況
peng@ubuntu:~/work/test$ ./peng -a 100 -b 200 -c 300 -d
opt = a		optarg = (null)		optind = 2		argv[optind] = 100
opt = b		optarg = 200		optind = 5		argv[optind] = -c
opt = c		optarg = 300		optind = 7		argv[optind] = -d
opt = d		optarg = (null)		optind = 8		argv[optind] = (null)

導致解析錯誤,第一個 optarg = null,實際輸入參數 100,由于格式不正確造成的(可選參數格式固定)

  • 參數丟失,也會導致錯誤
peng@ubuntu:~/work/test$ ./peng -a -b 200 -c 
opt = a		optarg = (null)		optind = 2		argv[optind] = -b
opt = b		optarg = 200		optind = 4		argv[optind] = -c
./peng: option requires an argument -- 'c'
opt = ?		optarg = (null)		optind = 5		argv[optind] = (null)

c選項是必須有參數的

  • 命令行選項未定義,-e選項未在optstring中定義,會報錯:
peng@ubuntu:~/work/test$ ./peng -t
./peng: invalid option -- 't'
opt = ?		optarg = (null)		optind = 2		argv[optind] = (null)

四、getopt_long

1. 定義:

int getopt_long(int argc, char * const argv[], const char *optstring,
const struct option *longopts,int *longindex);
功能:包含 getopt 功能,增加了解析長選項的功能如:--prefix --help
參數:longopts 指明了長參數的名稱和屬性longindex 如果longindex非空,它指向的變量將記錄當前找到參數符合longopts里的第幾個元素的描述,即是 longopts 的下標值
返回:對于短選項,返回值同 getopt 函數;對于長選項,如果 flag 是 NULL ,返回 val ,否則返回 0 ;對于錯誤情況返回值同 getopt 函數

2. struct option

struct option {const char  *name;       /* 參數名稱 */int          has_arg;    /* 指明是否帶有參數 */int          *flag;      /* flag=NULL時,返回value;不為空時,*flag=val,返回0 */int          val;        /* 用于指定函數找到選項的返回值或flag非空時指定*flag的值 */
}; 

參數has_arg 說明:
has_arg 指明是否帶參數值,其數值可選:

	no_argument 表明長選項不帶參數,如:–name, --helprequired_argument 表明長選項必須帶參數,如:–prefix /root或 --prefix=/rootoptional_argument 表明長選項的參數是可選的,如:–help或 –prefix=/root,其它都是錯誤

3. 實例

#include<stdio.h>
#include<unistd.h>
#include<getopt.h>
int main(intargc, char *argv[])
{int opt;int digit_optind = 0;int option_index = 0;char *string = "a::b:c:d";static struct option long_options[] ={  {"reqarg", required_argument,NULL, 'r'},{"optarg", optional_argument,NULL, 'o'},{"noarg",  no_argument,         NULL,'n'},{NULL,     0,                      NULL, 0},}; while((opt =getopt_long_only(argc,argv,string,long_options,&option_index))!= -1){  printf("opt = %c\t\t", opt);printf("optarg = %s\t\t",optarg);printf("optind = %d\t\t",optind);printf("argv[optind] =%s\t\t", argv[optind]);printf("option_index = %d\n",option_index);}  
}
  • 正確執行命令
peng@ubuntu:~/work/test$ ./long --reqarg 100 --optarg=200 --noarg
opt = r		optarg = 100		optind = 3		argv[optind] =--optarg=200		option_index = 0
opt = o		optarg = 200		optind = 4		argv[optind] =--noarg		option_index = 1
opt = n		optarg = (null)		optind = 5		argv[optind] =(null)		option_index = 2

或者

peng@ubuntu:~/work/test$ ./long –reqarg=100 --optarg=200 --noarg
opt = o		optarg = 200		optind = 3		argv[optind] =--noarg		option_index = 1
opt = n		optarg = (null)		optind = 4		argv[optind] =(null)		option_index = 2
  • 可選選項可以不給參數
peng@ubuntu:~/work/test$ ./long --reqarg 100 --optarg --noarg
opt = r		optarg = 100		optind = 3		argv[optind] =--optarg		option_index = 0
opt = o		optarg = (null)		optind = 4		argv[optind] =--noarg		option_index = 1
opt = n		optarg = (null)		optind = 5		argv[optind] =(null)		option_index = 2
  • 輸入長選項錯誤的情況
peng@ubuntu:~/work/test$ ./long --reqarg 100 --optarg 200 --noarg
opt = r		optarg = 100		optind = 3		argv[optind] =--optarg		option_index = 0
opt = o		optarg = (null)		optind = 4		argv[optind] =200		option_index = 1
opt = n		optarg = (null)		optind = 6		argv[optind] =(null)		option_index = 2

五、getopt_long_only

getopt_long_only 函數與 getopt_long 函數使用相同的參數表,在功能上基本一致

只是 getopt_long 只將 --name 當作長參數,但 getopt_long_only 會將 --name 和 -name 兩種選項都當作長參數來匹配

getopt_long_only 如果選項 -name 不能在 longopts 中匹配,但能匹配一個短選項,它就會解析為短選項。

六、綜合實例

下面這個例子,是一口君從開源項目ifplug提取出來的命令提取小例子,

大家可以根據自己需要,基于這個框架,定制自己的程序。

#define _GNU_SOURCE
#include <stdlib.h>
#include <stdio.h>
#include <string.h>
#include <unistd.h>
#include <errno.h>
#include <getopt.h>
#include <sys/param.h>#define ETHCHECKD_VERSION "1.1"int delay_up = 0;
char *interface = "eth0";void usage(char *p) {if (strrchr(p, '/'))p = strchr(p, '/')+1;printf("%s [options]\n""   -i --iface=IFACE          Specify ethernet interface (%s)\n" "   -d --delay-up=SECS        Specify delay time (%i)\n""   -h --help                 Show this help\n",p,interface,delay_up);
}void parse_args(int argc, char *argv[]) {static struct option long_options[] = {{"iface",                required_argument, 0, 'i'},{"delay-up",             required_argument, 0, 'd'},{"help",                 no_argument, 0, 'h'},{"version",              no_argument, 0, 'v'},{0, 0, 0, 0}};int option_index = 0;int help = 0, _kill = 0, _check = 0, _version = 0, _suspend = 0, _resume = 0, _info = 0;for (;;) {int c;if ((c = getopt_long(argc, argv, "i:d:hv", long_options, &option_index)) < 0)break;switch (c) {case 'i' :interface = strdup(optarg);printf("interface %s\n",interface);break;case 'd':delay_up = atoi(optarg);printf("delay_up %d\n",delay_up);break;case 'h':usage(argv[0]);break;case 'v':printf("peng "ETHCHECKD_VERSION"\n");break;default:fprintf(stderr, "Unknown parameter.\n");exit(1);}}}static volatile int alarmed = 0;int main(int argc, char* argv[]) {parse_args(argc, argv);return 0;
}

下面是測試結果

  • 短選項
peng@ubuntu:~/work/test$ ./param -h
param [options]-i --iface=IFACE          Specify ethernet interface (eth0)-d --delay-up=SECS        Specify delay time (0)-h --help                 Show this help
peng@ubuntu:~/work/test$ ./param -v
peng 1.1peng@ubuntu:~/work/test$ ./param -vh
peng 1.1
param [options]-i --iface=IFACE          Specify ethernet interface (eth0)-d --delay-up=SECS        Specify delay time (0)-h --help                 Show this help  peng@ubuntu:~/work/test$ ./param -i eth3 -d 15
interface eth3
delay_up 15 peng@ubuntu:~/work/test$ ./param -i eth3 -d 15 -h
interface eth3
delay_up 15
param [options]-i --iface=IFACE          Specify ethernet interface (eth3)-d --delay-up=SECS        Specify delay time (15)-h --help                 Show this help
  • 長選項
peng@ubuntu:~/work/test$ ./param --help
param [options]-i --iface=IFACE          Specify ethernet interface (eth0)-d --delay-up=SECS        Specify delay time (0)-h --help                 Show this helppeng@ubuntu:~/work/test$ ./param --version
peng 1.1peng@ubuntu:~/work/test$ ./param --iface eth3 --delay-up 15
interface eth3
delay_up 15
talk is cheap!
test this code!

快操練起來吧!!!

更多嵌入式linux資料,后臺留言:資料

也可以加一口君好友

本文來自互聯網用戶投稿,該文觀點僅代表作者本人,不代表本站立場。本站僅提供信息存儲空間服務,不擁有所有權,不承擔相關法律責任。
如若轉載,請注明出處:http://www.pswp.cn/news/160096.shtml
繁體地址,請注明出處:http://hk.pswp.cn/news/160096.shtml
英文地址,請注明出處:http://en.pswp.cn/news/160096.shtml

如若內容造成侵權/違法違規/事實不符,請聯系多彩編程網進行投訴反饋email:809451989@qq.com,一經查實,立即刪除!

相關文章

Raspberry Pi 5 新一代單板計算機:樹莓派5代 (介紹、入門、解疑)

樹莓派5代正式發布后&#xff0c;硬件和性能的全面升級讓眾多開發者們都想入手感受一波&#xff0c;外觀上Raspberry Pi 5 與前代產品非常相似&#xff0c;不過&#xff0c;在保留信用卡大小的整體尺寸的同時&#xff0c;也更新了一些設計元素&#xff0c;以適應新芯片組的功能…

python實現調和反距離空間插值法AIDW

1 簡介 AIDW 主要是針對 IDW 的缺點進行了改進&#xff0c;考慮了樣本點與預測點的位置&#xff0c;即方向和距離&#xff0c;具體見下圖&#xff1a; 2 改進 IDW 公式&#xff1a; 從IDW算法可看出&#xff0c;插值點的估算值僅與插值樣本距插值點的遠近相關&#xff0c;并未…

貝葉斯AB測試

AB測試是用來評估變更效果的有效方法&#xff0c;但很多時候會運行大量AB測試&#xff0c;如果能夠在測試中復用之前測試的結果&#xff0c;將有效提升AB測試的效率和有效性。原文: Bayesian AB Testing[1] 隨機實驗&#xff0c;又稱AB測試&#xff0c;是行業中評估因果效應的既…

自定義類型:結構體

1.結構體類型的聲明 1.1結構體的概念 結構是?些值的集合&#xff0c;這些值稱為成員變量。結構的每個成員可以是不同類型的變量。 1.2 結構的聲明 struct tag {member-list; }variable-list; 例如描述?個學?&#xff1a; struct Stu {char name[20];//名字int age;//年…

【Mysql】[Err] 1293 - Incorrect table definition;

基本情況 SQL文件描述 /* Navicat MySQL Data TransferSource Server : cm4生產-200 Source Server Version : 50725 Source Host : 192.168.1.200:3306 Source Database : db_wmsTarget Server Type : MYSQL Target Server Version : 50725 File…

vxe編輯保存表格

業務需求&#xff1a; 1、需要點擊編輯時&#xff0c;全部表格顯示編輯框&#xff0c;點擊保存&#xff0c;全部保存。 2、因為位置問題&#xff0c;產品經理把24小時分成了兩行&#xff0c;開發就得分兩個表格。列標題是寫死的&#xff0c;文字偏移也是寫死的&#xff0c;其他…

服務器主機安全的重要性及防護策略

在數字化時代&#xff0c;服務器主機安全是任何組織都必須高度重視的問題。無論是大型企業還是小型企業&#xff0c;無論是政府機構還是個人用戶&#xff0c;都需要確保其服務器主機的安全&#xff0c;以防止數據泄露、網絡攻擊和系統癱瘓等嚴重后果。 一、服務器主機安全的重…

__int128類型movaps指令crash

結論 在使用__int128時&#xff0c;如果__int128類型的內存起始地址不是按16字節對齊的話&#xff0c;有些匯編指令會拋出SIGSEGV使程序crash。 malloc在64位系統中申請的內存地址&#xff0c;是按16字節對齊的&#xff0c;但一般使用時經常會申請一塊內存自己切割使用&#…

Qt的一個無邊界窗口公共類

頭文件&#xff1a; #pragma once #include <QWidget>class CFrameLessWidgetBase :public QWidget { public:CFrameLessWidgetBase(QWidget* p nullptr);~CFrameLessWidgetBase() {}protected:bool nativeEvent(const QByteArray& eventType, void* message, long…

static和extern

1.extern extern 是?來聲明外部符號的&#xff0c;如果?個全局的符號在A?件中定義的&#xff0c;在B?件中想使?&#xff0c;就可以使? extern 進?聲明&#xff0c;然后使?。 即在一個源文件中想要使用另一個源文件&#xff0c;即可通過這個extern來聲明使用。 2.st…

未來制造業的新引擎:工業機器人控制解決方案

制造業正經歷著一場革命性的變革 在這個變革的浪潮中&#xff0c;工業機器人成為推動制造業高效生產的關鍵力量。然而&#xff0c;要發揮機器人的最大潛力&#xff0c;一個強大而智能的控制系統是必不可少的。在這個領域&#xff0c;新一代的工業機器人控制解決方案正嶄露頭角&…

學習MySQL先有全局觀,細說其發展歷程及特點

學習MySQL先有全局觀&#xff0c;細說其發展歷程及特點 一、枝繁葉茂的MySQL家族1. 發展歷程2. 分支版本 二、特點分析1. 常用數據庫2. 選型角度及場景 三、三大組成部分四、總結 相信很多同學在接觸編程之初&#xff0c;就接觸過數據庫&#xff0c;而對于其中關系型數據庫中的…

這樣寫postman實現參數化,阿里p8都直呼牛逼

什么時候會用到參數化 比如&#xff1a;一個模塊要用多組不同數據進行測試 驗證業務的正確性 Login模塊&#xff1a;正確的用戶名&#xff0c;密碼 成功&#xff1b;錯誤的用戶名&#xff0c;正確的密碼 失敗 postman實現參數化 在實際的接口測試中&#xff0c;部分參數…

你的關聯申請已發起,請等待企業微信的管理員確認你的申請

微信支付對接時&#xff0c;需要申請AppID,具體在下面的位置&#xff1a; 關聯AppID&#xff0c;發起申請時&#xff0c;會提示這么一句話&#xff1a; 此時需要登錄企業微信網頁版&#xff0c;使用注冊人的企業微信掃碼登錄進去&#xff0c;然后按照下面的步驟操作即可。 點擊…

iEnglish全國ETP大賽:教育游戲助力英語習得

“seesaw,abacus,sword,feather,frog,lion,mouse……”11月18日,經過3局的激烈較量,“以過客之名隊”的胡玲、黃長翔、林家慷率先晉級“玩轉英語,用iEnglish”第三屆全國ETP大賽的16強,在過去的周末中,還有TIK徘徊者隊、不負昭華隊、溫柔殺戮者隊先后晉級。據悉,根據活動規則,在…

電腦內存升級

ddr代兼容 自從DDR內存時代開啟之后&#xff0c;只要滿足內存的插槽規格相同(DDR3或DDR4或DDR5即為內存規格)這一條件&#xff0c;不同品牌、不同頻率以及不同容量的茶品都可以一起使用&#xff0c;除了品牌和容量的影響之外&#xff0c;不同頻率的搭配可能會造成性能方面的影…

面試官:什么是三色標記

程序員的公眾號&#xff1a;源1024&#xff0c;獲取更多資料&#xff0c;無加密無套路&#xff01; 最近整理了一波電子書籍資料&#xff0c;包含《Effective Java中文版 第2版》《深入JAVA虛擬機》&#xff0c;《重構改善既有代碼設計》&#xff0c;《MySQL高性能-第3版》&…

git提交時會將target也提交

有時候大家在提交git時發現會將編譯文件target也提交上去&#xff0c;這種情況有以下幾種情況 情況1&#xff1a;項目沒有設置.gitignore 情況2&#xff1a;設置了.gitignore但是依然會提交。 第一種&#xff1a;添加.gitignore&#xff0c;并在文件中添加需要忽略的東西。 …

redis分布式鎖的學習記錄

核心性質 獨占性&#xff1a;對于同一把鎖&#xff0c;同一時刻只能被一個加鎖方獨占 健壯性&#xff1a;不能產生死鎖。如果有一個因為宕機無法主動解鎖&#xff0c;鎖也應該被正常加載 對稱性&#xff1a;加成和解鎖的使用方必須為同一個身份&#xff0c;不允許被非方釋放 高…

HCIA-實驗命令基礎學習:

視頻學習&#xff1a; 第一部分&#xff1a;基礎學習。 19——子網掩碼。 27——防火墻配置&#xff1a; 32——企業級路由器配置&#xff1a; 基礎實驗完成&#xff1a;&#xff08;完成以下目錄對應的實驗&#xff0c;第一部分基礎實驗就完成。&#xff09; 方法&#xff…