我有一个练习要在MIPS程序集中解决(我有一些疑问,但其他事情很清楚),但是编写代码时遇到了一些问题。练习问我: 编写一个程序,从键盘获取一个字符串,计算具有较多出现次数的字符的出现次数并显示它。
我如何检查所有26个字符,找出谁有较高的出现次数?
实施例: 给我一个字符串:Hello world! 出现次数较高的字符是:l
非常感谢将来的回答。
P.S。 这是我的第一部分:
#First message
li $v0, 4
la $a0, mess
syscall
#Stack space allocated
addi $sp, $sp, -257
#Read the string
move $a0, $sp
li $a1, 257
li $v0, 8
syscall
答案 0 :(得分:1)
由于这是你的任务,我将把MIPS程序集实现留给你。我将以更高级别的语言向您展示代码的逻辑:
// You'd keep these variables in some MIPS registers of your choice
int c, i, count, max_count=0;
char max_char;
// Iterate over all ASCII character codes
for (c = 0; c < 128; c+=1) {
count = 0;
// Count the number of occurences of this character in the string
for (i = 0; string[i]!=0; i+=1) {
if (string[i] == c) count++;
}
// Was is greater than the current max?
if (count > max_count) {
max_count = count;
max_char = c;
}
}
// max_char now hold the ASCII code of the character with the highest number
// of occurences, and max_count hold the number of times that character was
// found in the string.
答案 1 :(得分:0)
@Marco:
您可以创建一个包含26个计数器的临时数组(初始化为0)。
每个计数器对应于每个字母(即每个字母出现的数字)。例如,counter[0]
对应于字母&#39;&#39;,counter[1]
出现的字母&#39; b&#39;等等...
然后迭代输入字符序列中的每个字符,并为每个字符执行:
a)获取counter
数组中字符的索引
b)将counter["obtained index"]
增加1.
要获取角色的索引,您可以执行以下操作:
a)首先要确保角色不是资本,即只有&#39; a&#39;到&#39; z&#39;允许而不是&#39; A&#39;到&#39; Z&#39;如果不是,请转换它
b)提取字母&#39; a&#39;从角色。通过这种方式&#39; a&#39; a&#39;得到0,&#39; b&#39; - &#39; a&#39;给出1,&#39; c&#39;&#39; a&#39;给2等等......
我将使用C语言进行演示,因为它是您在MIPS上的练习(我的意思是目标是学习MIPS汇编语言):
#include <stdio.h>
int main()
{
//Maximum length of string:
int stringMaxLength = 100;
//Create string in stack. Size of string is length+1 to
//allow the '\0' character to mark the end of the string.
char str[stringMaxLength + 1];
//Read a string of maximum stringMaxLength characters:
puts("Enter string:");
scanf("%*s", stringMaxLength, str);
fflush(stdin);
//Create array of counters in stack:
int counter[26];
//Initialize the counters to 0:
int i;
for (i=0; i<26; ++i)
counter[i] = 0;
//Main counting loop:
for (i=0; str[i] != '\0'; ++i)
{
char tmp = str[i]; //Storing of str[i] in tmp, to write tmp if needed,
//instead of writing str[i] itself. Optional operation in this particular case.
if (tmp >= 'A' && tmp <= 'Z') //If the current character is upper:
tmp = tmp + 32; //Convert the character to lower.
if (tmp >= 'a' && tmp <='z') //If the character is a lower letter:
{
//Obtain the index of the letter in the array:
int index = tmp - 'a';
//Increment its counter by 1:
counter[index] = counter[index] + 1;
}
//Else if the chacacter is not a lower letter by now, we ignore it,
//or we could inform the user, for example, or we could ignore the
//whole string itself as invalid..
}
//Now find the maximum occurences of a letter:
int indexOfMaxCount = 0;
int maxCount = counter[0];
for (i=1; i<26; ++i)
if (counter[i] > maxCount)
{
maxCount = counter[i];
indexOfMaxCount = i;
}
//Convert the indexOfMaxCount back to the character it corresponds to:
char maxChar = 'a' + indexOfMaxCount;
//Inform the user of the letter with maximum occurences:
printf("Maximum %d occurences for letter '%c'.\n", maxCount, maxChar);
return 0;
}
如果您不明白为什么我通过添加32将大写字母转换为较低的字母,请继续阅读:
每个字符对应于内存中的整数值,当您对字符进行算术运算时,就像在编码表中将它们设置为相应的数字一样。
编码只是一个将这些字母与数字相匹配的表格
例如&#39; a&#39;对应于ASCII encoding/decoding/table中的数字97
例如&#39; b&#39;对应ASCII encoding/decoding/table中的数字98。
所以&#39; +1给出97 + 1 = 98这是字符&#39; b&#39;。它们都是内存中的数字,区别在于你如何表示它们( decode )。当然,编码的同一表也用于解码。
示例:
printf("%c", 'a'); //Prints 'a'.
printf("%d", (int) 'a'); //Prints '97'.
printf("%c", (char) 97); //Prints 'a'.
printf("%d", 97); //Prints '97'.
printf("%d", (int) 'b'); //Prints '98'.
printf("%c", (char) (97 + 1)); //Prints 'b'.
printf("%c", (char) ( ((int) 'a') + 1 ) ); //Prints 'b'.
//Etc...
//All the casting in the above examples is just for demonstration,
//it would work without them also, in this case.