Я возникли проблемы в C с переключателем случаеC Isnt работает мой переключатель заявление
#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);
}`
Опкод всегда является ИНТ, и это право ИНТ для запуска коммутатора. , но по какой-то причине он проскакивает через коммутатор, проходит через все линии и никогда не выводит ничего из корпуса коммутатора.
файл загруженный
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 строк, я чувствовал, что было бы нецелесообразно публиковать его все, но если я должен это сделать.
Если вы не прошли через свой код с помощью отладчика, вы еще не приложили достаточного усилия для решения этой проблемы , Прости. –
Я буду помнить об этом, спасибо за вход –
, что вы получаете, когда вы «printf opcode» в своем выходе? –