2016-11-20 46 views
1

我嘗試在Windows批處理文件中寫一些簡單的程序。該程序類似於來自「C編程語言」Kernighan和Ritchie的一些簡單應用程序。 這個程序的任務是計算字符數字和字數。 Belove有一個源代碼嵌套,如果在Windows批處理文件CMD

rem Char Number and words number 
@echo off 
cls 
echo =============================================================== 
echo input strin 
set /P a= 
::set variables 
set Temp_str=%a% 
set /A charcounter = 0 
set /A wordscounter = 0 
:loop 
if defined Temp_str (
    set /P actual_char=%Temp_str:~0,1% 
    if %actual_char%=="" (set /A wordscounter+=1) 
    set Temp_str=%Temp_str:~1% 
    set /A charcounter+=1; 
    goto loop 
) 
echo %a% %charcounter% %wordscounter% 

有一些錯誤,但我找不到它。我不知道什麼是錯的。我實際上從批處理窗口編程開始。

+0

後的錯誤消息。 –

回答

0

要調試批處理文件總是禁用@echo off否則你盲目飛行

從那裏,你將看到的問題是上線

if %actual_char%=="" (set /A wordscounter+=1) 

您需要添加引號。

if "%actual_char%"=="" (set /A wordscounter+=1) 

此外還有其他一些問題與代碼:

1)。

set /P actual_char=%Temp_str:~0,1% 

不應該有/ p選項,因爲您不想提示用戶輸入字符。

2)您的字詞計數器應該檢查空白字符串之外的空格,並且還應該檢查第一個單詞結尾處會發生什麼情況。


rem Char Number and words number 
rem @echo off 
cls 
echo =============================================================== 
set /P a=input string: 
::set variables 
set Temp_str=%a% 
set /A charcounter = 0 
set /A wordscounter = 0 
:loop 
if "%Temp_str%" NEQ "" (
    set actual_char=%Temp_str:~0,1% 
    if "%actual_char%"==" " (set /A wordscounter+=1) 
    set Temp_str=%Temp_str:~1% 
    set /A charcounter+=1; 
    goto loop 
) 
REM Increment word count for the final word 
if "%a%" NEQ "" (set /A wordscounter+=1) 
echo %a% %charcounter% %wordscounter% 
+0

我根據你的線索正確的代碼,但它仍然不起作用 – gonskabalbinka

+0

我糾正了代碼。您必須依照您對單詞和角色的定義來修復它。 – FloatingKiwi

+0

謝謝你的幫助。現在沒問題。 – gonskabalbinka