在用户输入中查找第一个和最后一个大写字母 [英] Finding first and last capital letter in user input

查看:234
本文介绍了在用户输入中查找第一个和最后一个大写字母的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

输入将取自a-z或A-Z,输入以星号*结尾.

Input is to be taken from a-z or A-Z and the input is ended by an asterisk *.

我们需要输入字符的第一个和最后一个大写字母作为输出.另外,我们应该显示每次使用的输入. N.B.我们按字符而不是字符串来接受输入.

We need to have the first and last Capital letters of the input characters as the output. Also, we should show the input we have taken each time. N.B. We take the inputs character by character, not as a string.

测试案例1:输入:aAbCcP*输出:AP

Test case 1: input: aAbCcP* output: AP

测试案例2:输入:ZabCBc*输出:ZB

Test case 2: input: ZabCBc* output: ZB

我在下面编写了这段代码,它满足测试用例1,但不满足2:

I have written this code below, which satisfies Test Case 1, but not 2:

.MODEL
.STACK 100H
.DATA
   STR DB 'Enter letters:$'
.CODE

MAIN PROC

MOV AX, @DATA
MOV DS, AX

LEA DX, STR
MOV AH, 9
INT 21H 

cycle: 

    MOV AH, 1
    INT 21H

    CMP AL, '*'
    JZ output 
    CMP AL, 'Z' 
    JA save


head: 
    CMP BL, 1
    JZ save

    MOV BL, 1
    MOV BH, AL 

clear:
    XOR AL, AL  

save:
    MOV CH, AL

LOOP cycle 

output:
    MOV AH, 2
    MOV DL, BH
    INT 21H 

    MOV AH, 2
    MOV DL, CH
    INT 21H 


MAIN ENDP 
END MAIN 

推荐答案

首先问自己以下问题:

  • 什么是大写字母?
    如果不考虑带重音符号的字符,则大写字母是指ASCII码在65到90之间的字符.

    First ask yourself these questions:

    • What are capitals?
      If we don't consider accented characters, then capitals are characters with ASCII codes ranging from 65 to 90.

      我可以信任用户仅输入a-z或A-Z中的字符吗?
      不,你不能.您无法控制用户在键盘上的操作,这就是为什么您的程序应采取防御性方法并以比单个cmp al, 'Z'更好的方式测试大写字母.

      Can I trust the user to only input characters from a-z or A-Z?
      No you can't. You don't have control over what the user does at the keyboard, and that's why your program should take a defensive approach and test for capitals with something better than a single cmp al, 'Z'.

      如果输入不包含单个大写字母,结果将是什么?
      您可以选择打印两个空格或描述性消息,或者就像我什么都没显示一样.

      What will be the result if the input didn't contain a single capital?
      You could choose to print two spaces, or a descriptive message, or like I did display nothing at all.

      如果输入仅包含一个大写字母,结果将是什么?
      您可以选择打印一个大写字母,或者像我将其显示两次一样,因为考虑到它,那个单个大写字母同时是首个出现的资本,也是最后一个出现的资本.

      What will be the result if the input contains only one capital?
      You could choose to print that one capital, or like I did display it twice because if you think of it, that single capital is at the same time the first occurence of a capital and also the last occurence of a capital.

      我将使用哪些输入/输出功能?
      对于单字符输入,您可以在DOS函数01h,06h,07h,08h,0Ch和3Fh之间进行选择.
      对于单字符输出,可以在DOS函数02h,06h和40h之间进行选择.
      如果您不熟悉汇编程序,则坚持使用简单的汇编程序,并使用功能01h和02h.使用任何DOS功能之前,请务必先查阅 API参考.当然,请使用emu8086检查其是否完全支持该功能!

      What input/output functions will I use?
      For single character input you have a choice between DOS functions 01h, 06h, 07h, 08h, 0Ch, and 3Fh.
      For single character output you have a choice between DOS functions 02h, 06h, and 40h.
      If you're new to assembly then stick with the simpler ones and use functions 01h and 02h. Do consult the API reference before using any DOS function. And of course check with emu8086 whether it supports the function altogether!

      您需要对以上所有内容进行决策才能解决此任务.重要的是,对于您做出的每一个选择,您都可以捍卫自己的选择.

      You need to decide about all of the above in order to tackle the task. What is important, is that for every choice you make, you can defend your choice.

      以下是此任务的我版本.为简单起见,我使用的是微型程序模型.参见顶部的ORG 256指令?此程序模型的主要优点是,所有段寄存器均均指向您的程序(CS = DS = ES = SS).

      Below is my version of this task. For simplicity I'm using the tiny program model. See the ORG 256 directive on top? This program model has the major benefit of having all the segment registers pointing equally to your program (CS = DS = ES = SS).

      程序运行2个循环. 第一个循环一直进行到收到大写为止. (不言而喻,如果输入中包含星号,它将更早停止.)因为该资本同时是资本的首次出现和资本的最后出现,所以我在DLDL中将其保存了两次. DH.

      The program runs 2 loops. The first loop runs until a capital is received. (Goes without saying that it stops earlier if the input contains an asterisk.) Because that capital is at the same time the first occurence of a capital and also the last occurence of a capital, I save it twice, both in DL and DH.

      第二个循环运行,直到收到星号.每次出现新的大写字母时,它都会替换DH中写的内容.当此循环最终结束时,DLDH都将按照顺序显示在屏幕上.

      The second loop runs until an asterisk is received. Each time that a new capital comes along, it replaces what is written in DH. When this loop finally ends, both DL and DH are displayed on screen and in this order of course.

      程序以首选的DOS功能4Ch退出,以终止程序.

      The program exits with the preferred DOS function 4Ch to terminate a progam.

      我写了一些基本的注释,避免添加多余的注释,并为程序中的标签使用了描述性名称.请注意漂亮的表格布局.出于可读性考虑,这很关键.

      I've written some essential comments, refrained from adding redundant ones, and used descriptive names for the labels in the program. Do note that nice tabular layout. For readability it's crux.

              ORG     256
      
      Loop1:  mov     ah, 01h     ; DOS.GetKeyboardCharacter
              int     21h         ; -> AL
              cmp     al, "*"     ; Found end of input marker ?
              je      Done
              cmp     al, "A"
              jb      Loop1
              cmp     al, "Z"
              ja      Loop1
              mov     dl, al      ; For now it's the first
              mov     dh, al      ; AND the last capital
      
      Loop2:  mov     ah, 01h     ; DOS.GetKeyboardCharacter
              int     21h         ; -> AL
              cmp     al, "*"     ; Found end of input marker ?
              je      Show
              cmp     al, "A"
              jb      Loop2
              cmp     al, "Z"
              ja      Loop2
              mov     dh, al      ; This is the latest capital
              jmp     Loop2
      
      Show:   mov     ah, 02h     ; DOS.DisplayCharacter
              int     21h         ; -> (AL)
              mov     dl, dh
              mov     ah, 02h     ; DOS.DisplayCharacter
              int     21h         ; -> (AL)
      
      Done:   mov     ax, 4C00h   ; DOS.TerminateWithReturnCode
              int     21h
      

      示例:

      a Z e R T y *

      aZeRTy * ZT

      aZeRTy*ZT


      如果您采用简单的方法并仅复制/粘贴我的代码,那将非常令人失望.我试图详细解释它,希望您能从中学到很多.


      It would be very disappointing if you took it the easy way and just copy/pasted my code. I've tried to explain it in great detail and hope that you learn a lot from it.

      对于这个任务,我的解决方案当然不是唯一好的解决方案.您可以例如首先输入所有字符并将它们存储在内存中的某个位置,然后按照与我的操作类似的方式从内存中处理这些字符.
      请尝试编写可通过这种替代方式完成工作的版本.您只会变得更聪明!编程愉快.

      My solution is certainly not the only good solution for this task. You could e.g. first input all of the characters and store them in memory somewhere, after which you process these characters from memory similar to how I did it.
      Please try to write a working version that does it in this alternative way.You can only get smarter! Happy programming.

      这篇关于在用户输入中查找第一个和最后一个大写字母的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
相关文章
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆