0
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 循环。

我写这个是为了接受一个整数,然后打印出最多十次,不同的整数将整数向后添加到自身,反复检查它是否是回文。

主程序以及 rev 函数已经用 MIPS 编写。我只是不明白我将如何进行回文程序。

任何提示将不胜感激!

我试过这个:

首先,我执行将持续 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?? 

我知道这一定是非常错误的,但这是我第一次这样做。

4

1 回答 1

0

我没有更改 rev 函数,但我必须更改回文程序才能将 C 编译为 MIPS。我无法编译cincout(不是那些 C++ 吗?)所以cincout注释掉了。

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
于 2013-02-16T07:59:17.457 回答