本帖最后由 老刘1号 于 2020-4-29 11:28 编辑
- @Echo Off
- Setlocal Enabledelayedexpansion
- Title 【老刘编写】一元多项式相加算法
- Path "%~dp0"
- If Not Exist "Queue_LSS.Bat" (
- Echo "Queue_LSS"缺失!
- Pause&Exit
- )
- Set "Queue=Call Queue_LSS"
-
- Echo 多项式格式:系数1_指数1 系数2_指数2 ...
- Echo 要求:指数为整数且递减,但不必相邻。
- Echo 多项式例:3_5 4_4 -1_3 2_1 -1_0
- Echo 多项式例:2_4 1_3 -7_2 1_1
- Set /P Polynomial_1_String=输入多项式1:
- Set /P Polynomial_2_String=输入多项式2:
-
- Rem 生成队列。
- !Queue! :Init Polynomial_1
- For %%a In (!Polynomial_1_String!) Do (
- Set _TMP_Data1_=%%a
- !Queue! :Enqueue Polynomial_1 _TMP_Data1_
- )
- !Queue! :Init Polynomial_2
- For %%a In (!Polynomial_2_String!) Do (
- Set _TMP_Data2_=%%a
- !Queue! :Enqueue Polynomial_2 _TMP_Data2_
- )
-
- Rem 相加。
- !Queue! :Dequeue Polynomial_1 _TMP_Data1_
- !Queue! :Dequeue Polynomial_2 _TMP_Data2_
- :Loop
- Rem echo !_TMP_Data1_! !_TMP_Data2_!
- For /f "Tokens=1-4 Delims=_ " %%a In ("!_TMP_Data1_! !_TMP_Data2_!") Do (
- Set /A _TMP_Coef1_=%%a,_TMP_Expon1_=%%b
- Set /A _TMP_Coef2_=%%c,_TMP_Expon2_=%%d
- )
- rem Set _TMP_Expon
- If !_TMP_Expon1_! Gtr !_TMP_Expon2_! (
- Echo !_TMP_Expon1_!次项系数:!_TMP_Coef1_!
- !Queue! :Dequeue Polynomial_1 _TMP_Data1_
- Rem echo !ErrorLevel!←
- If !Errorlevel! Neq 0 (
- Rem 队1已清空,输出队2。
- For /f "Tokens=1-2 Delims=_ " %%a In ("!_TMP_Data2_!") Do (
- Echo %%b次项系数:%%a
- )
- rem echo 2
- Call :PrintLast Polynomial_2
- Pause&Exit
- )
- rem echo 3
- ) Else If !_TMP_Expon1_! Lss !_TMP_Expon2_! (
- Echo !_TMP_Expon2_!次项系数:!_TMP_Coef2_!
- !Queue! :Dequeue Polynomial_2 _TMP_Data2_
- If !Errorlevel! Neq 0 (
- Rem 队2已清空,输出队1。
- For /f "Tokens=1-2 Delims=_ " %%a In ("!_TMP_Data1_!") Do (
- Echo %%b次项系数:%%a
- )
- Call :PrintLast Polynomial_1
- Pause&Exit
- )
- ) Else (
- Set /A _TMP_CoefLast=_TMP_Coef1_+_TMP_Coef2_
- Echo !_TMP_Expon1_!次项系数:^(!_TMP_Coef1_!^)+^(!_TMP_Coef2_!^)=!_TMP_CoefLast!
- !Queue! :Dequeue Polynomial_1 _TMP_Data1_
- If !Errorlevel! Neq 0 (
- Rem 队1已清空,输出队2。
- Call :PrintLast Polynomial_2
- Pause&Exit
- )
- !Queue! :Dequeue Polynomial_2 _TMP_Data2_
- If !Errorlevel! Neq 0 (
- Rem 队2已清空,输出队1。
- Rem 由于刚才队1的一个元素出队,所以在这里先输出。
- For /f "Tokens=1-2 Delims=_ " %%a In ("!_TMP_Data1_!") Do (
- Echo %%b次项系数:%%a
- )
- Call :PrintLast Polynomial_1
- Pause&Exit
- )
- )
- Goto :Loop
-
- :PrintLast
- rem echo 4
- !Queue! :Dequeue "%~1" _TMP_Data_
- If !ErrorLevel! Equ 0 (
- For /f "Tokens=1-2 Delims=_ " %%a In ("!_TMP_Data_!") Do (
- Echo %%b次项系数:%%a
- )
- Goto :PrintLast
- )
- Goto :Eof
复制代码 Queue_LSS.BAT:批处理数据结构库 |