int Reverse(int num)
{
int remainder = 0;
int rev = 0;
while ( num != 0)
{
remainder = num % 10;
num = num /10;
rev = rev * 10 + remainder;
}
return rev;
}
void Palindrome()
{
int num;
cin >> num;
Reverse(num);
cout << num;
for (int i = 0; i < 10; i++)
{
if ( num != Reverse(num))
{
num = num + Reverse(num);
cout << " " << num;
}
if ( num == Reverse(num))
{
cout << " ";
}
}
}
我最大的问题是循环。实际的数学并不困难,但我无法弄清楚如何设置循环,尤其是for循环中的if循环。
我把它写成一个整数,然后打印出十次,不同的整数 将整数向后添加到自身,反复检查它是否是回文。
主程序已经用MIPS编写,以及rev函数。我只是没有 了解我将如何进行Palindrome程序。
任何提示都会非常感激!
我试过这个:
首先我做的循环将继续进行10次。
然后我在回文程序中调用rev函数
jal rev # call rev function
la $a0, spaces # display the number
bne $a0, $v0, then # if num isnt equal to rev
add $a0, $a0, $t1 # num = num + reverse(num)
la spaces, $a0 # display the added number each time
beq $a0, $v0, then # if num IS equal to rev
// display spaces instead??
我知道这一定是非常错误但这是我第一次这样做。
答案 0 :(得分:0)
我没有更改rev功能,但我不得不更改Palindrome程序以将C编译为MIPS。我无法编译cin
或cout
(不是那些C ++?)所以cin
和cout
被注释掉了。
int Reverse(int num)
{
int remainder = 0;
int rev = 0;
while ( num != 0)
{
remainder = num % 10;
num = num /10;
rev = rev * 10 + remainder;
}
return rev;
}
void Palindrome()
{
int num = 0;
int i;
//cin >> num;
Reverse(num);
//cout << num;
for (i = 0; i < 10; i++)
{
if ( num != Reverse(num))
{
num = num + Reverse(num);
//cout << " " << num;
}
if ( num == Reverse(num))
{
//cout << " ";
}
}
}
int main ( int argc , char * argv [] )
{
return 0;
}
# -G value = 8, Cpu = 3000, ISA = 1
# GNU C version cygnus-2.7.2-970404 (mips-mips-ecoff) compiled by GNU C version cygnus-2.7.2-970404.
# options passed: -msoft-float
# options enabled: -fpeephole -ffunction-cse -fkeep-static-consts
# -fpcc-struct-return -fcommon -fverbose-asm -fgnu-linker -msoft-float
# -meb -mcpu=3000
gcc2_compiled.:
__gnu_compiled_c:
.text
.align 2
.globl Reverse
.ent Reverse
Reverse:
.frame $fp,16,$31 # vars= 8, regs= 1/0, args= 0, extra= 0
.mask 0x40000000,-8
.fmask 0x00000000,0
subu $sp,$sp,16
sw $fp,8($sp)
move $fp,$sp
sw $4,16($fp)
sw $0,0($fp)
sw $0,4($fp)
$L2:
lw $2,16($fp)
bne $2,$0,$L4
j $L3
$L4:
lw $2,16($fp)
li $3,1717960704 # 0x66660000
ori $3,$3,0x6667
mult $2,$3
mfhi $6
sra $3,$6,2
sra $4,$2,31
subu $3,$3,$4
move $5,$3
sll $4,$5,2
addu $4,$4,$3
sll $3,$4,1
subu $2,$2,$3
sw $2,0($fp)
lw $2,16($fp)
li $3,1717960704 # 0x66660000
ori $3,$3,0x6667
mult $2,$3
mfhi $6
sra $3,$6,2
sra $4,$2,31
subu $2,$3,$4
sw $2,16($fp)
lw $2,4($fp)
move $4,$2
sll $3,$4,2
addu $3,$3,$2
sll $2,$3,1
lw $3,0($fp)
addu $2,$2,$3
sw $2,4($fp)
j $L2
$L3:
lw $3,4($fp)
move $2,$3
j $L1
$L1:
move $sp,$fp # sp not trusted here
lw $fp,8($sp)
addu $sp,$sp,16
j $31
.end Reverse
.align 2
.globl Palindrome
.ent Palindrome
Palindrome:
.frame $fp,32,$31 # vars= 8, regs= 2/0, args= 16, extra= 0
.mask 0xc0000000,-4
.fmask 0x00000000,0
subu $sp,$sp,32
sw $31,28($sp)
sw $fp,24($sp)
move $fp,$sp
sw $0,16($fp)
lw $4,16($fp)
jal Reverse
sw $0,20($fp)
$L6:
lw $2,20($fp)
slt $3,$2,10
bne $3,$0,$L9
j $L7
$L9:
lw $4,16($fp)
jal Reverse
lw $3,16($fp)
beq $3,$2,$L10
lw $4,16($fp)
jal Reverse
lw $3,16($fp)
addu $2,$3,$2
sw $2,16($fp)
$L10:
lw $4,16($fp)
jal Reverse
lw $3,16($fp)
bne $3,$2,$L8
$L11:
$L8:
lw $2,20($fp)
addu $3,$2,1
sw $3,20($fp)
j $L6
$L7:
$L5:
move $sp,$fp # sp not trusted here
lw $31,28($sp)
lw $fp,24($sp)
addu $sp,$sp,32
j $31
.end Palindrome
.align 2
.globl main
.ent main
main:
.frame $fp,24,$31 # vars= 0, regs= 2/0, args= 16, extra= 0
.mask 0xc0000000,-4
.fmask 0x00000000,0
subu $sp,$sp,24
sw $31,20($sp)
sw $fp,16($sp)
move $fp,$sp
sw $4,24($fp)
sw $5,28($fp)
jal __main
move $2,$0
j $L12
$L12:
move $sp,$fp # sp not trusted here
lw $31,20($sp)
lw $fp,16($sp)
addu $sp,$sp,24
j $31
.end main