C 不是 运行 我的 switch 语句
C isnt running my switch statement
我在 C 语言中遇到 switch case 问题
#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]) != '[=11=]' )
{
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);
}`
操作码始终是一个整数,它是 运行 开关的正确整数。
但出于某种原因,它跳过所有行的开关 运行s 并且从不从开关盒内输出任何内容。
加载的文件是
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
我不太确定 if 有什么问题,我也不确定有什么问题我已经尝试了很多方法,但无法在网上找到任何帮助。任何输入将不胜感激,如果您不能编译和 运行 我将提供所有代码的程序。由于代码超过 200 行,我觉得不适合 post 全部,但如果必须的话,我会的。
问题出在行opcode=Cnnn[a];
opcode 是 int
而 Cnnn[a]
是 char
当您将 int 分配给 char 时,它会根据 ASCII 编码分配字符的 int 值。示例:
#include<stdio.h>
int main()
{
int a;
char b = '1';
a = b;
printf("%d", a);
return 0;
}
这会打印出 49,因为根据 ascii,字符“1”对应于 49
编辑:
我不确定这是否是解决您问题的 "Correct" 方法,但由于我们注意到我们得到的 int 值比我们想要的 int 值多 48,我们可以 opcode = (Cnnn[a] - 48);
我在 C 语言中遇到 switch case 问题
#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]) != '[=11=]' )
{
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);
}`
操作码始终是一个整数,它是 运行 开关的正确整数。 但出于某种原因,它跳过所有行的开关 运行s 并且从不从开关盒内输出任何内容。
加载的文件是
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
我不太确定 if 有什么问题,我也不确定有什么问题我已经尝试了很多方法,但无法在网上找到任何帮助。任何输入将不胜感激,如果您不能编译和 运行 我将提供所有代码的程序。由于代码超过 200 行,我觉得不适合 post 全部,但如果必须的话,我会的。
问题出在行opcode=Cnnn[a];
opcode 是 int
而 Cnnn[a]
是 char
当您将 int 分配给 char 时,它会根据 ASCII 编码分配字符的 int 值。示例:
#include<stdio.h>
int main()
{
int a;
char b = '1';
a = b;
printf("%d", a);
return 0;
}
这会打印出 49,因为根据 ascii,字符“1”对应于 49
编辑:
我不确定这是否是解决您问题的 "Correct" 方法,但由于我们注意到我们得到的 int 值比我们想要的 int 值多 48,我们可以 opcode = (Cnnn[a] - 48);