0

編集:それで、コーディングのどこかに問題があるようです。プログラムを実行して変数を入力すると、常に同じ答えが返されます。"場所76のコンテンツは0です。

さて、私は数日前に質問についてここに投稿しましたが、それは単なるコンパイルエラーだったので、これがおなじみのように見える場合は、それが理由です。繰り返しになりますが、私はプログラミングに不慣れで、私は最高ではないので、単純にするために行きます。また、これはSMLプログラムです。とにかく、これは宿題であり、私はこれで良い成績を望んでいます。だから私はインプットを探していて、このプログラムが彼らが探していることを私が望んでいることを実行することを確認しました。とにかく、ここに手順があります:次の各タスクを実行するためのSML(Simpletron Machine言語)プログラムを作成します。

A)センチネル制御ループを使用して、正の数sを読み取り、それらの合計を計算して出力します。負の数が入力されたら入力を終了します。B)カウンター制御ループを使用して、正と負の7つの数値を読み取り、平均を計算して出力します。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

コード全体に魔法数があります。次のようなことを行う必要があります。

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
  • int memory[100] = {0};メモリを配列として定義し、そのすべての要素を0に初期化します。その場合、ループはint必要ありません。for
  • 省略表記を使用できます。たとえばaccum += memory[operand];
    、の代わりにaccum = accum + memory[operand];。この方法でより読みやすくなります。
于 2010-03-09T20:14:44.067 に答える
1
 mem[09] = 4009;

正確に 09 である 8 進数は?? :)

編集: プログラム 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

関数を使用して、プログラムをよりモジュール化することをお勧めします。私はあなたが彼らに紹介されたと仮定しています。

初期化 (メモリーをゼロ値にクリアする) とインタープリターは、関数にする最有力候補です。これらの関数は、すべての例で同じ (つまり共通) です。どちらの場合も、唯一の関数パラメータはmemory.

@R Samuel Klatchko の提案と組み合わせると、自分自身とマーカーにとってプログラムが読みやすくなると思います。

最後に、「余分なクレジット」としてcin、メモリに保存する前にユーザーの入力 (から) を検証することをお勧めします。適切な範囲の正の整数であることを確認するなど。この入力と検証を独自の関数にすることで、プログラムのネストが深くなりすぎないようにすることができます。

わかりませんが、プログラム A の固定アドレスが誤って命令10およびに分岐されているのではないかと思われます11

于 2010-03-09T20:48:19.193 に答える