0

编辑:所以,我的编码中似乎确实有问题。每当我运行程序并输入变量时,它总是返回相同的答案..“位置 76 的内容为 0。

好的,伙计们,几天前我在这里发布了一个问题,但这只是一个编译错误,所以如果这看起来很熟悉,那就是原因。我要重申,我是编程新手,我不是最好的,所以我会为了简单起见。这也是一个 SML 程序。无论如何,这是一个家庭作业,我想要一个好成绩。所以我一直在寻找意见,并确保这个程序能做我希望他们正在寻找的东西。无论如何,这里有说明: 编写 SML(Simpletron 机器语言)程序来完成以下每一项任务:

A) 使用哨兵控制的循环读取正数 s 并计算和打印它们的总和。输入负数时终止输入。B)使用计数器控制循环读取七个数字,一些正数和一些负数,然后计算 + 打印平均值。C) 读取一系列数字,确定并打印最大的数字。读取的第一个数字表示应该处理多少个数字。

不用多说,这是我的程序。全部一起。

程序 A

#include <iostream>
using namespace std;

int main()
{
 int memory[100]; //Making it 100, since simpletron contains a 100 word mem.

 int operation; //taking the rest of these variables straight out of the book seeing as how they were italisized.

 int operand;

 int accum = 0; // the special register is starting at 0

 int j;

 for (j = 0; j < 100; j++ ) //Simply stating that for int j is = to 0, j must be less than 100 because that is the memory limit, and for every pass-through, increment j.
  memory[j] = 0;


 // This is for part a, it will take in positive variables in a sent-controlled loop and compute + print their sum. Variables from example in text.
 memory [00] = 1010;

 memory [01] = 2009;

 memory [02] = 3008;

 memory [03] = 2109;

 memory [04] = 1109;

 memory [05] = 4300;

 memory [06] = 1009;

 j = 0; //Makes the variable j start at 0.

 while ( true )
 {

  operand = memory[ j ]%100; // Finds the op codes from the limit on the memory (100)
  operation = memory[ j ]/100;

  //using a switch loop to set up the loops for the cases
  switch ( operation ){
   case 1: //reads a variable into a word from loc. Enter in -1 to exit
    cout <<"\n Input a positive variable:  ";
    cin >> memory[ operand ]; break;

   case 2: // takes a word from location
    cout << "\n\nThe content at location " << operand   << "is " << memory[operand]; break;

   case 3:// loads
    accum = memory[ operand ]; break;

   case 4: //stores
    memory[ operand ] = accum; break;

   case 5: //adds
    accum = accum + memory[ operand ]; break;


   case 6: // subtracts
    accum = accum - memory[ operand ]; break;

   case 7: //divides
    accum = accum / (memory[ operand ]); break;

   case 8: // multiplies
    accum = accum*memory [ operand ]; break;

   case 9: // Branches to location
    j = -1; break;

   case 10: //branches if acc. is < 0
    if (accum < 0)
    j = 5; 
    break;

   case 11: //branches if acc = 0
    if (accum == 0)
     j = 5; 
    break;

   case 12: // Program ends
    exit(0); break;
 }
 j++;
 }
return 0;
}

方案 B

//Part b finding the sum + avg.

int main()
{
 int mem[100];
 int operation;
 int operand;
 int accum = 0;
 int pos = 0;

 int j;

 for (j = 0; j < 100; j++ ) 
  memory[j] = 0;

 mem[22] = 7; // loop 7 times
 mem[25] = 1; // increment by 1

 mem[00] = 4306;

 mem[01] = 2303;

 mem[02] = 3402;

 mem[03] = 6410;

 mem[04] = 3412;

 mem[05] = 2111;

 mem[06] = 2002;

 mem[07] = 2312;

 mem[08] = 4210;

 mem[09] = 2109;

 mem[10] = 4001;

 mem[11] = 2015;

 mem[12] = 3212;

 mem[13] = 2116;

 mem[14] = 1101;

 mem[15] = 1116;

 mem[16] = 4300;

 j = 0;

 while ( true )
 {

  operand = memory[ j ]%100; // Finds the op codes from the limit on the memory (100)
  operation = memory[ j ]/100;

  //using a switch loop to set up the loops for the cases
  switch ( operation ){
   case 1: //reads a variable into a word from loc. Enter in -1 to exit
    cout <<"\n enter #:  ";
    cin >> memory[ operand ]; break;

   case 2: // takes a word from location
    cout << "\n\nThe content at location " << operand   << "is " << memory[operand]; break;

   case 3:// loads
    accum = memory[ operand ]; break;

   case 4: //stores
    memory[ operand ] = accum; break;

   case 5: //adds
    accum = accum + memory[ operand ]; break;


   case 6: // subtracts
    accum = accum - memory[ operand ]; break;

   case 7: //divides
    accum = accum / (memory[ operand ]); break;

   case 8: // multiplies
    accum = accum*memory [ operand ]; break;

   case 9: // Branches to location
    j = operand; break;

   case 10: //branches if acc. is < 0

    break;

   case 11: //branches if acc = 0
    if (accum == 0)
     j = operand; 
    break;

   case 12: // Program ends
    exit(0); break;
 }
 j++;
 }
return 0;
}

程序 C

///Part c
int main()
{
 int mem[100];
 int operation;
 int operand;
 int accum = 0;


 int j;

 for (j = 0; j < 100; j++ ) //Simply stating that for int j is = to 0, j must be less than 100 because that is the memory limit, and for every pass-through, increment j.
  memory[j] = 0;

 mem[23] = 1; //decrements 1 place in mem

 mem[0] = 1030; // Takes in # of values to be stored.

 mem[01] = 4123; // These 4 memory slots check for the largest variable then store
 mem[02] = 4134;
 mem[03] = 1011;
 mem[04] = 3204;

 mem[05] = 4005; // These 5 decrement the count+ store + branch.
 mem[06] = 4006;
 mem[07] = 4007;
 mem[08] = 4008;
 mem[09] = 4009;

 mem[10] = 4010;
 mem[11] = 4311; // exits

 j = 0; // this is the starting value..

 while ( true )
 {

  operand = memory[ j ]%100; // Finds the op codes from the limit on the memory (100)
  operation = memory[ j ]/100;

  //using a switch loop to set up the loops for the cases
  switch ( operation ){
   case 1: //reads a variable into a word from loc. Enter in -1 to exit
    cout <<"\n enter #:  ";
    cin >> memory[ operand ]; break;

   case 2: // takes a word from location
    cout << "\n\nThe content at location " << operand   << "is " << memory[operand]; break;

   case 3:// loads
    accum = memory[ operand ]; break;

   case 4: //stores
    memory[ operand ] = accum; break;

   case 5: //adds
    accum = accum + memory[ operand ]; break;


   case 6: // subtracts
    accum = accum - memory[ operand ]; break;

   case 7: //divides
    accum = accum / (memory[ operand ]); break;

   case 8: // multiplies
    accum = accum*memory [ operand ]; break;

   case 9: // Branches to location
    j = operand; break;

   case 10: //branches if acc. is < 0

    break;

   case 11: //branches if acc = 0
    if (accum == 0)
     j = operand; 
    break;

   case 12: // Program ends
    exit(0); break;
   case 13: // checks > than
    if (accum < mem[operand])
     accum = mem[operand];
    break;
  }
 j++;
 }
return 0;
}
4

5 回答 5

1
  • int memory[100] = {0};将内存定义为一个数组,int其所有元素都初始化为 0。那么你就不需要for循环了。
  • 您可以使用速记符号。例如accum += memory[operand];
    代替accum = accum + memory[operand];. 这种方式更具可读性。
于 2010-03-09T20:14:44.067 回答
1

你的代码中都有神奇的数字。您应该执行以下操作:

const int OP_LOAD = 3;
const int OP_STORE = 4;
const int OP_ADD = 5;
...

const int OP_LOCATION_MULTIPLIER = 100;

mem[0] = OP_LOAD * OP_LOCATION_MULTIPLIER + ...;
mem[1] = OP_ADD * OP_LOCATION_MULTIPLIER + ...;

operand = memory[ j ] % OP_LOCATION_MULTIPLIER;
operation = memory[ j ] / OP_LOCATION_MULTIPLIER;
于 2010-03-09T20:19:07.283 回答
1
 mem[09] = 4009;

09 到底是哪个八进制数?:)

编辑:对于程序 A(以及其他程序),您可能会发现在点击 switch 语句之前打印出“操作数”和“操作”变量会很有帮助。输出:

Operand: 10 Operation: 10
Operand: 9 Operation: 20
Operand: 8 Operation: 30
Operand: 9 Operation: 21
Operand: 9 Operation: 11
Operand: 9 Operation: 10
Operand: 0 Operation: 0
Operand: 0 Operation: 0
... *LOTS of lines omitted....
Operand: 0 Operation: 0
Operand: -97 Operation: -9498072
Operand: 0 Operation: 0
Operand: 88 Operation: 20898776
Operand: 12 Operation: 0
Operand: 8 Operation: 22856
Operand: 69 Operation: 20898775

switch()在最终要求用户输入之前,代码运行了近 1300 次。在我看来,当操作数 = 0 时,程序 A 会脱轨,当您点击这条线时会发生这种情况:memory [05] = 4300;

于 2010-03-09T23:33:54.580 回答
0

现在已经超过 2 年了,因此对你的任务没有更多用处,但其他人可能会在这里结束,所以这里......

这个问题的线索在你的第一段,它的结尾是:

“编写 SML(Simpletron 机器语言)程序来完成以下每一项任务:[原文如此]”

关键词是“编写 SML 程序...”——您尝试编写 C++ 程序,这不太可能为您赢得许多作业学分。

我相信这个作业起源于 Deitel & Deitel 的书“C:如何编程”。在他们的书中,他们展示了一个非常简单的机器模拟器 Simpletron,作为练习。SML 是这台假设计算机的机器语言。在介绍了机器及其语言之后,练习首先要求学生用 SML 编写一些简单的程序。后来学生被要求用 C 语言构建 Simpletron。

于 2013-08-14T07:29:31.180 回答
0

我建议尝试通过使用函数使程序更加模块化。我假设你已经被介绍给他们。

初始化(将内存清除为零值)和解释器是成为函数的主要候选者。对于您的所有示例,这些功能都是相同的(即通用的)。在这两种情况下,唯一的函数参数是memory.

我认为结合@R Samuel Klatchko 的建议将使您和标记更容易阅读程序。

最后,对于“额外信用”,我建议您cin在存储到内存之前验证用户的输入(来自)。比如确保它是适当范围内的正整数。这个输入和验证可以做成它自己的函数,以避免在你的程序中嵌套太深。

我不知道,但我怀疑程序 A 错误地有一个固定地址被分支到指令1011.

于 2010-03-09T20:48:19.193 回答