C没有运行我的switch语句

时间:2015-09-15 01:02:29

标签: c switch-statement

我在使用开关案例的C中遇到问题

#include <stdio.h>
#include <strings.h>

void runprogram(void);  // parses the input file
void display(int, int); // prints out the requested lines
void clearmemory(void); // clears the choice for input file

int traceflag = 0;      //determines if the output should say everything
char memory[128];       //the file name for the input

void runprogram (void)
{
 FILE *ifp;                 //file loaded
 int total=0;               //accumulator total
 int temp3;
 int a, b, c, d, numcnt, operand, opcode, CNNN;
 char ch, ch2;
 printf("Current File: %s\n",memory);
 if(traceflag==0)
  printf("Trace is off\n");
 else
  printf("Trace is on\n");
 ifp = fopen(memory,"r");          //opens the file
 char line [128];
 char NNN[3];
 char Cnnn[4];
 char C[1];
 a=0;
 b=0;
 c=0;
 d=0;
 numcnt=0;
 while(fgets(line,sizeof line,ifp)!= NULL) // read a line from a file
 {
  while ((ch = line[b]) != '\0' )
   {
    if ( numcnt == 1 )
    {
     if (isdigit(ch))
     {
      ch2 = line [b + 1];
      if ( isdigit( ch2 ))
      {
       for (a = 0; a < 4; a++ )
       {
        Cnnn [ a ] = ch;
        b++;
        ch = line [ b ];
       }
       for (a=0;a<4;a++)
        {
         if (a==0)
          opcode=Cnnn[a];
         else
          NNN[a-1]=Cnnn[a];
        }
       numcnt++;
       b=0;
       a=0;
      }
     }
    }
    else if ( isdigit(ch))
     {
      ch2 = line [ b + 1 ];
      if ( ch2 == ' ' )
       numcnt++;
      }
     else
      ch = line [ b ];
     b++;
     ch = line [ b ];
   }
   fgets( line, sizeof(line), ifp);
   numcnt=0;
   b=0;
   d=0;
   for (c = 0; c < 3; c++)
    {
     NNN [ c ] = Cnnn [ d + 1 ];
     d++;
    }
   sscanf(NNN, "%d", &operand);
   opcode=opcode/10;
   b=0;
   d=0;
   /*if(traceflag==1)
    {
     printf("Full Line: %s\n",line);        //print the file
     printf("Opcode: %d\n", opcode);
   }*/
   printf("Opcode: %d Operand: %d\n", opcode, operand);
    switch ( opcode )
     {
      case 0:                  //0 - Halt
            printf("Run finished \n");
            break;
      case 1:                  //1 - Load: Copy memory nnn to accumulator
            total=operand;
            break;
      case 2:                  //2 - Store: Copy accumulator to memory nnn
            operand=total;
            break;
      case 3:                  //3 - Add memory nnn to accumulator
            total=total+operand;
            break;
      case 4:              //4 - Subtract memory nnn from accumulator
            total=total-operand;
            break;
      case 5:            
            //int temp3;         //temp to hold entered number
            printf("Enter a number: ");
            scanf(" %d",&temp3);
            total=temp3;
            break;
      case 6:         
            printf("Accumulator: %d\n",total);
            break;
      case 7:             
            if(total==0)
             opcode=operand/10;
            break;
       case 8:                  //8 ‐ Branch to nnn if the accumulator > 0
            if(total>0)
             opcode=operand/10;
            break;
       case 9:                  //9 – Branch to nnn
            opcode=operand/10;
            break;
       default:               
            total=0;
            break;
     }
 }
 printf("Run finished \n");
 fclose(ifp);
}`

操作码总是一个int,它是运行开关的正确的int。 但由于某种原因,它跳过开关贯穿所有线路,并且从不在开关盒内输出任何东西。

加载的文件是

0 Rd 5000 1 st n 2017 2 ld zero 1014 3 st sum 2016 4 L: ld n 1017 5 Add sum 3016 6 St sum 2016 7 Ld n 1017 8 Sub one 4015 9 St n 2017 10 Brgt L 8004 11 Ld sum 1016 12 Wr 6000 13 Stop 0000 14 Zero: 0 0000 15 One: 1 0001 16 Sum: 0 0000 17 N: 0 0000

我不太确定是否有错如果我不确定什么是错的我尝试了很多东西而且无法在网上找到任何帮助。任何输入将不胜感激,如果你不能编译和运行该程序,我将提供所有代码。由于代码超过200行,我觉得发布它是不合适的,但如果我必须的话。

1 个答案:

答案 0 :(得分:1)

问题在于opcode=Cnnn[a];行 操作码是intCnnn[a]char,当您为char分配int时,它根据ASCII编码分配字符的int值。例如:

#include<stdio.h>

int main()
{
    int a;
    char b = '1';
    a = b;
    printf("%d", a);
    return 0;
}

这打印出49因为根据ascii,字符'1'对应49

编辑:

我不确定这是否是解决问题的“正确”方法,但由于我们注意到我们获得的int值比我们想要的int值多48个opcode = (Cnnn[a] - 48);