题干

【根据宁波效实中学2012学年第一学期高一信息技术期末试卷改编】将一个十进制正整数x转换成十六进制数的方法和转换成二进制类似,反复除16取余法,它的算法如下

    第一步:x被除16取余数r1,r1是所求的十六进制数的一个数(第一次就是个位数);

    第二步:r1转换成十六进制数的一个基数并转换成字符,追加到十六进制数S的左边;

    第三步:求出x被除16的商x1;

    第四步:对x1重复(1)、(2)、(3)过程,直至商为0;

    第五步:最后的S即为所求的十六进制数;

下列Visual Basic程序的功能是将一个十进制正整数x转换成十六进制数。界面如图所示,在文本框Text1中输入一个十进制数制,单击“十进制转十六进制按钮”(Command1)后,在文本框Text2中输出转换的结果。 相应的Visual Basic程序如下:

Private Sub Command1_Click()

  Dim s As String, r As Integer

  dec = val(Text1.Text)                  ’dec得到一个十进制整数,例如:255;

  base = 16                               ’base表示16

     s = ""                                ’s变量存放要转换到的十六进制整数;

     Do While dec <> 0

          r =        ①   

          If r >= 10 Then                     ’如果base>10进制,r可能>10;

             s = Chr(65 + (r - 10)) + s   ’Chr (65)="A", 16进制数A对应10;

          Else                            ’Chr(66)="B", 16进制数B对应11;
             s = Str(r) + s      ‘余数r<10时,十进制数和十六进制基数相同;
          End If                   ‘Chr (65 +( r - 10))或str(r)在s左边,实现了反次序输出;
          dec =     ②              
    Loop
   Text2.Text = s
End Sub
上一题 下一题 0.0难度 选择题 更新时间:2018-12-11 11:03:09

答案(点此获取答案解析)

同类题4

阅读理解

    Five years ago, when I taught art at a school in Seattle, I used Tinkertoys as a test at the beginning of a term to find out something about my students. I put a small set of Tinkertoys in front of each student, and said: “Make something out of the Tinkertoys. You have 45 minutes today - and 45minutes each day for the rest of the week.”

    A few students hesitated to start. They waited to see the rest of the class would do. Several others checked the instructions and made something according to one of the model plans provided. Another group built something out of their own imaginations.

    Once I had a boy who worked experimentally with Tinkertoys in his free time. His constructions filled a shelf in the art classroom and a good part of his bedroom at home. I was delighted at the presence of such a student. Here was an exceptionally creative mind at work. His presence meant that I had an unexpected teaching assistant in class whose creativity would infect(感染)other students.

    Encouraging this kind of thinking has a downside. I ran the risk of losing those students who had a different style of thinking. Without fail one would declare, “But I'm just not creative.”

    “Do you dream at night when you're asleep?”

    “Oh, sure.”

    “So tell me one of your most interesting dreams.” The student would tell something wildly imaginative. Flying in the sky or in a time machine or growing three heads. “That's pretty creative. Who does that for you? “

    “Nobody. I do it.”

    “Really-at night, when you're asleep?”

    “Sure.”

    “Try doing it in the daytime, in class, okay?”