|
马上注册,结交更多好友,享用更多功能^_^
您需要 登录 才可以下载或查看,没有账号?立即注册
x
要判断输入字符是数字还是字母并汇总输出,卡在了循环上,最后一次输入之后总是显示输入错误 求大神解答
#include "stdafx.h"
#include "stdlib.h"
int _tmain(int argc, _TCHAR* argv[])
{
char msgLoopTimes[] = "Input the number of times for looping in the range of 0-12: ";
char msgEntEle[] = "Enter an Alphabet(A-Z,a-z) or Numeric Digit(0-9): ";
char msgNumOfUpperLetter[] = "\nNumber of upper-case alphabets made by the user: ";
char msgNumOfLowerLetter[] = "\nNumber of lower-case alphabets made by the user: ";
char msgAverageOfNumber[] = "\nAverage of numeric inputs made by the user: ";
char msgRmd[] = "\nwith a remainder: ";
char msgNewLine[] = "\n";
char errCounter[] = "Error! Please input an integer in the range of 0-12: ";
char errDigit[] = "Input error! Please input an alphabet(A-Z, a-z) or numeric(0-9) digit : ";
char fmtInt[] = "%d";
char fmtStr[] = "%s";
int counter;
int inputDigit;
int numOfUpperLetter, numOfLowerLetter, numOfNumber;
int sum, average, remainder;
_asm {
// read a number, counter, in the range of 0-12
lea eax, msgLoopTimes ;
push eax ;
call printf ;
add esp, 4 ;
l_input_counter:
lea eax, counter ;
push eax ;
lea eax, fmtInt ;
push eax ;
call scanf_s ;
add esp, 8 ;
// If 0 is entered, the prog ram exits without looping.
mov eax, counter ;
cmp eax, 0 ;
je end ;
// if the number is not in the range, invalid
jb err_input_counter ;
cmp eax, 12 ;
ja err_input_counter ;
// if correct
jmp ctn ;
// if error
err_input_counter:
lea eax, errCounter ;
push eax ;
call printf ;
add esp, 4 ;
loop l_input_counter ;
// Otherwise it goes around a loop for counter times wherein it asks the user to enter an alphabet or numeric digit (i.e. 0-9) repeatedly.
ctn:
mov ecx, eax ;
mov numOfLowerLetter, 0 ;initialise the number of lower_case letters to 0
mov numOfUpperLetter, 0 ;initialise the number of upper_case letters to 0
mov numOfNumber, 0 ;initialise the number of numbers to 0
mov sum, 0 ;initialise the sum of numbers to 0
mov average, 0 ;initialise the average of numbers to 0
mov remainder, 0 ;initialise the remainder of numbers to 0
// ask to input
l_input_digit:
push ecx ;
lea eax, msgEntEle ;
push eax ;
call printf ;
add esp, 4 ;
// read
start_input:
call getchar ;
lea eax, inputDigit ;
push eax ;
lea eax, fmtStr ;
push eax ;
call scanf_s ;
add esp, 8 ;
mov al, byte ptr inputDigit ;test if the digit is a lower_case letter
cmp al, 'a' ;if its in the range of a - z in ASCII, then
jb is_upper_case_letter ;it is a lower_case letter
cmp al, 'z' ;the same way is used in testing upper_case
ja is_upper_case_letter ;letter and number
add numOfLowerLetter, 1 ;count this lower_case letter
jmp end_of_input ;
is_upper_case_letter:
cmp al, 'A' ;same way as testing lower_case letter
jb is_number ;
cmp al, 'Z' ;
ja is_number ;
add numOfUpperLetter, 1 ;count this upper_case letter
jmp end_of_input ;
is_number:
cmp al, '0' ;same way as testing lower_case letter
jb input_err ;
cmp al, '9' ;
ja input_err ;
mov ah, al ;get the ASCII code of the numeric digit
jmp end_of_input ;
input_err:
lea eax, errDigit ;
push eax ;
call printf ;
add esp, 4 ;
jmp start_input ;
// loop
end_of_input:
pop ecx ;
loop l_input_digit ;
number_calculate:
cbw ;extend ah to ax
cwde ;extend ax to eax
sub eax, 48 ;convert the ASCII code to its value, which
;must be a number of 0 - 9(48 - 57 in ASCII)
add numOfNumber, 1 ;count this number
add sum, eax ;add the number to sum
mov eax, sum ;this part of code is to calculate the averate of
;all the numbers when the input is already over,
;if the sum is 0, which means there is no number,
;this part will not be excuted
jz output ;
cdq ;expend the register
mov ecx, numOfNumber ;
idiv ecx ;divide eax by ecx(signed numbers)
mov average, eax ;get answer from eax
mov remainder, edx ;get reminder from edx
output:
lea eax, msgNumOfUpperLetter ;
push eax ;
call printf ;
add esp, 4 ;
mov eax, numOfUpperLetter ;
push eax ;
lea eax, fmtInt ;
call printf ;show the number of upper_case letters
add esp, 8 ;
lea eax, msgNumOfLowerLetter ;
push eax ;
call printf ;
add esp, 4 ;
mov eax, numOfLowerLetter ;
push eax ;
lea eax, fmtInt ;
call printf ;show the number of lower_case letters
add esp, 8 ;
lea eax, msgAverageOfNumber ;
push eax ;
call printf ;
add esp, 4 ;
mov eax, average ;
push eax ;
lea eax, fmtInt ;
call printf ;show the average of numbers
add esp, 8 ;
lea eax, msgNewLine ;
push eax ;
call printf ;go to a new line
add esp, 4 ;
lea eax, msgRmd ;
push eax ;
call printf ;
add esp, 4;
mov eax, remainder;
push eax;
lea eax, fmtInt;
push eax;
call printf;show remainder of average
add esp, 8;
lea eax, msgNewLine;
push eax;
call printf;go to a new line
add esp, 4
end:
}
system("pause");
return 0;
} |
|