- html - 出于某种原因,IE8 对我的 Sass 文件中继承的 html5 CSS 不友好?
- JMeter 在响应断言中使用 span 标签的问题
- html - 在 :hover and :active? 上具有不同效果的 CSS 动画
- html - 相对于居中的 html 内容固定的 CSS 重复背景?
我是 C 编程新手,我一直在编写一个虚拟计算机程序,该程序从 STDIN 获取输入,输入基本上表示任务虚拟计算机执行某个数字的倍数检查的命令 - 只是简单的东西。本质上,当我第一次编写这个程序时,我是使用指向文件流的文件指针从文件中读取输入,但是当我将流切换到 STDIN 时,它开始变得奇怪。
这个 STDIN 的有趣之处在于它是一个文件流重定向,所以我仍然在命令行参数中提供一个文件,但是因为我使用的编码平台具有允许文件重定向的命令,而无需实现实际的文件指针让我很困惑。
我开始遇到溢出错误,当我过去将文件指针指向命令行参数中提供的程序时,不会发生这种错误,我不知道为什么,因为我刚刚将文件指针流切换到 stdin`, 。如果有人能向我指出问题可能是什么,我将不胜感激,这是我从中收到溢出错误的代码:
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
/*Calling in the prototypes NOTE: I don't call execute/compile because they preceed the main method */
int printMemory(int* accumulator, int* instructionCounter, int* instructionRegister,int*operationCode,int* operand, int memory []);
int checkSegmentationFault(int *operand);
int checkWordFlow(int instructionCounter, int memory []);
/*
Function Name: compile
Parameters: A pointer to the file that we are processing, the memory array, a pointer to instructionCounter, instructionRegister, operationCode, and operand, so that we can carry operations on them
Return value(s): returns 1 if it compiles succesfully otherwise it would return a zero or terminate.
Partners: None
Description: This function reads in the file through it's pointer line by line and then converts it's data into 4 digit values, then they get stored into memory array. The function the proceeds to check for some compiling errors then it returns the result.
*/
int compile (FILE* fPointer , int memory [], int* instructionCounter , int* instructionRegister ,int*operationCode ,int* operand){
char s[80]; /* The buffer */
*instructionRegister=0;
*operationCode=0;
while(((*instructionRegister)=fscanf(fPointer,"%d %s %d", operationCode,s,operand)) != EOF){ /*Reads data line by line then stores the integer returned by fscanf to instructionRegister pointer so that I can check for formating */
if((*instructionRegister) ==3 ){ /*Checks for improper format by comparing the current instructionRegister count to 3, returns improper format otherwise */
if(*operand >9999|| *operand <0){ /* Checks for word overflow in compiler, makes sure that digits do not exceed 9999 */
printf("attempts to place a word in memory that is larger than 4 digits, or attempted to pass in a negative value\n ");
exit(0);
}
/*Compares the string section of the code and checks if it matches the following words and then it converts it to it's 4 digit value by adding into it the operand */
if(strcmp(s,"READ") == 0) {
memory[*operationCode] = 10 * 100 + *operand;
}
else if(strcmp(s,"WRIT") == 0) {
memory [*operationCode] = 11 * 100 + *operand;
}
else if(strcmp(s,"LOAD") ==0){
memory [*operationCode] = 20 * 100 + *operand;
}
else if(strcmp(s,"PRNT") ==0){
memory [*operationCode] = 12 * 100 + *operand;
}
else if(strcmp(s,"STOR") ==0){
memory [*operationCode] = 21 * 100 + *operand;
}
else if(strcmp(s,"SET") ==0){
memory [*operationCode] = *operand;
}
else if(strcmp(s,"ADD") ==0){
memory [*operationCode] = 30 * 100 + *operand;
}
else if(strcmp(s,"SUB") ==0){
memory [*operationCode] = 31 * 100 + *operand;
}
else if(strcmp(s,"DIV") ==0){
memory [*operationCode] = 32 * 100 + *operand;
}
else if(strcmp(s,"MULT") ==0){
memory [*operationCode] = 33 * 100 + *operand;
}
else if(strcmp(s,"MOD") ==0){
memory [*operationCode] = 34 * 100 + *operand;
}
else if(strcmp(s,"BRAN") ==0){
memory [*operationCode] = 40 * 100 + *operand;
}
else if(strcmp(s,"BRNG") ==0){
memory [*operationCode] = 41 * 100 + *operand;
}
else if(strcmp(s,"BRZR") ==0){
memory [*operationCode] = 42 * 100 + *operand;;
}
else if(strcmp(s,"HALT")==0){
memory [*operationCode] =9999;
}
else { /* Prints back to the user that the compiler did not recognize one of them commands as it was going through it */
printf ("This is an unknown command, commands are case sensitive, program will now exit \n");
exit(0);
}
}
else{ /* Returns improper format if instructionRegister does not match 3*/
printf("Improper Format, program will now exit \n");
exit(0);
}
}
/* Checks if the instruction data contains a HALT, if not it would terminate */
while(*instructionCounter<100){
if (memory[*instructionCounter] == 9999){
return 1;
}
else
(*instructionCounter)++;
}
printf("Halt was not found, program will now exit");
exit (0);
}
/*
Function Name : execute
Parameters : accumulator for storing in the arithematic operations , instructionCounter for counting the instructions, instructionRegisterfor storing the current instruction,
operationCode stores the first 2 digits so that it would recognize what command is currently being executed, operand stores the next 2 digits , and the memory to be able to loop through the computer 100 memory
Return value(s) : returns 1 if it executes fine, otherwise it would terminate if it finds an error. It also prints out HALT once found which shows the current state of the memory.
Partners : None
Description : This function processes the compiled instructions and start carrying out operations on them depending on their operation code, it also checks for error then it prints out the current memory state once it reaches HALT,
*/
int execute(int* accumulator , int* instructionCounter , int* instructionRegister ,int*operationCode ,int* operand, int memory [])
{
/* Resets the values to zero because they were used in the compiler */
*operand=0;
*operationCode=0;
*instructionRegister=0;
*instructionCounter=0;
*accumulator=0;
/* this loop starts looking at the 4 digit memory cells and executes them 1 by 1 */
while(*instructionCounter<100){
checkWordFlow(*instructionCounter, memory);
*instructionRegister=memory[*operand]; /*stores current instruction */
*operand=memory[*instructionCounter]%100; /* Stores the 2 right digits */
checkSegmentationFault(operand); /* checks that operand does not contain a negative value so that the problem does not throw a seg fault */
*operationCode=memory[*instructionCounter]/100; /* Stores the 2 left diigts */
if(*operationCode==10){ /*READ: inquires the user to provide input and stores it in specified address in the instructions*/
scanf("%d",&memory[*operand]);
}
else if(*operationCode==11 ){ /*WRIT*: prints out the data in a memory element when executed*/
printf("%d\n",memory[*operand]);
}
else if(*operationCode==12 ){ /*PRNT */
while(1){ /*Loops and prints the followiing values that prnt passes in as operand */
/*Checks if the ASCI values are within the correct range */
if((memory[*operand]/100 > 65 &&memory[*operand]/100 <90) || memory[*operand]/100 == 10 )
printf("%c",memory[*operand]/100);
else if (memory[*operand]/100 == 0){
break;
}
else {
printf("Unknown Character\n");
exit(0);
}
if((memory[*operand]%100>65 &&memory[*operand]%100 <90) || memory[*operand]%100 == 10)
printf("%c", memory[*operand]%100);
else if (memory[*operand]%100 == 0){
break;
}
else{
printf("Unknown Character\n");
exit(0);
}
(*operand)++;
}
*operand =0; /*Resets the value of operand since it was incremented, so that it wont mess up the HALT print*/
printf("\n");
}
else if(*operationCode==20 ){ /*LOAD : loads into the accumulator */
*accumulator = memory[*operand];
}
else if(*operationCode== 21){ /*STORE: stores the accumlator value into the specific memory cell*/
memory[*operand] = *accumulator;
}
else if(*operationCode==30 ){ /*ADD: adds into the accumulator the specificed memory address data*/
*accumulator+=memory[*operand];
}
else if(*operationCode==31 ){ /*SUB: substracts from the accumulator the specified memoery addres data*/
*accumulator= *accumulator - memory[*operand];
}
else if(*operationCode== 32){ /*DIV: divides from the accumulator the specificed memory address data */
if(memory[*operand] >0){ /* Handles division by zero error */
*accumulator= *accumulator/memory[*operand];
}
else {
printf("Division by zero was attempted\n program will now exit \n");
exit(0);
}
}
else if(*operationCode== 33){ /*MULT: multiplies to the accumulator the specified address data*/
*accumulator= *accumulator*memory[*operand];
}
else if(*operationCode== 34){ /*MOD: calculates the remainder to the accumulator the specified address data*/
*accumulator= *accumulator%memory[*operand];
}
else if(*operationCode ==40){ /*BRAN: jump memory execution to address given */
*instructionCounter= *operand;
(*instructionCounter)--; /* deduct counter by 1 so that the loop does not skip over instructions */
}
else if(*operationCode ==41){ /*BRNG: jumps memory execution to addres given only if accumulator is negative*/
if (*accumulator <0){
*instructionCounter=*operand;
(*instructionCounter)--; /* deduct counter by 1 so that the loop does not skip over instructions */
}
}
else if(*operationCode ==42){ /*BRZR jumps memory execution to memory location only if accumulator is zero*/
if(*accumulator ==0){
*instructionCounter=*operand;
(*instructionCounter)--; /* deduct counter by 1 so that the loop does not skip over instructions */
}
}
else if(*operationCode == 99){ /*HALT: terminates the program but prints out the memory state before it does that*/
printMemory(accumulator,instructionCounter,instructionRegister,operationCode,operand,memory); /*prints the memory out */
exit(0);
}
else if(*operationCode == 0){ /* Checks for empty elements and moves passt them */
(*instructionCounter)++;
continue;
}
else{ /* Handles unrecognized operation codes */
printf("Unknown Command, program will now exit\n");
exit(0);
}
(*instructionCounter)++; /* Increment loop by 1 and start the next round of looping */
}
return 1;
}
int main (int argc, char* argv[]){
FILE * fPointer=NULL;
char fileName[150];
int accumulator=0;
signed int instructionCounter=0;
signed int instructionRegister=0;
int operationCode=0;
int operand=0;
signed int memory [100];
/*Checks if the user passed in an argument at the command line*/
if(argc < 1){
puts("You didn't specify the arguments or parameters\n");
exit(0);
}
/*fill the buffer */
if(argc>1)
strcpy(fileName,argv[1]);
fPointer=stdin;
/* If file failed to open then this would throw an error */
if(fPointer == NULL){
puts("File failed to open\n");
exit(0);
}
/* This loop fills the memory array with zeros*/
for(instructionCounter =0;instructionCounter<100;instructionCounter++){
memory [instructionCounter] = 0;
}
instructionCounter=0;
/* Run the commands */
compile(fPointer, memory,&instructionCounter,&instructionRegister,&operationCode,&operand);
execute(&accumulator,&instructionCounter,&instructionRegister,&operationCode,&operand,memory);
return 1;
}
/*
Function Name : printMemory
Parameters : accumulator, instructionCounter, instructionRegister, operationCode, operand, memory array, to insure that we print out the correct state of the memory we have to pass them in
Return value(s) : returns 1, but prints out memory state before it does that
Partners : None
Description : Prints out the current state of the memory in a nicely formatted manner
*/
int printMemory(int* accumulator , int* instructionCounter , int* instructionRegister ,int*operationCode ,int* operand, int memory []){
printf("REGISTERS:\naccumulator %+05d\ninstructionCounter %02d\ninstructionRegister %+05d\noperationCode %02d\noperand %02d\n",
*accumulator, *instructionCounter, *instructionRegister, *operationCode, *operand);
*instructionCounter =0;
printf("MEMORY:\n 0 1 2 3 4 5 6 7 8 9\n");
for((*instructionCounter)=0;(*instructionCounter)<100;(*instructionCounter)+=10) /* loops through the memory array and outputs 10 elements in every row */
{
printf("%2d",*instructionCounter);
printf(" %+05d %+05d %+05d %+05d %+05d %+05d %+05d %+05d %+05d %+05d\n", memory[*instructionCounter], memory[*instructionCounter+1] , memory [*instructionCounter+2] , memory [*instructionCounter+3], memory[*instructionCounter+4],
memory[*instructionCounter+5],memory[*instructionCounter+6],memory[*instructionCounter+7],memory[*instructionCounter+8],memory[*instructionCounter+9] );
}
return 1;
}
/*
Function Name : checksegmentationFault
Parameters : operand.
Return value(s) : returns 0, terminates the program if it tried to access an unknown operand , likely a negative value.
Partners : None
Description : terminates the program if it tried to access an unknown operand , likely a negative value.,
*/
int checkSegmentationFault(int *operand)
{
if(*operand < 0 || *operand >100){
printf("SEGMENTATION FAULT: Attempted to access an unknown address \n");
exit(0);
}
return 0;
}
/*
Function Name : checkWordFlow
Parameters : instructionCounter for looping, and memory array
Return value(s) : 1 , terminates if it reaches a word overflow
Partners : None
Description : this function checks for the word overflow possible error at execution by checking that digit values do no surpass 9999,
*/
int checkWordFlow( int instructionCounter, int memory []) /*fix this shit */
{
instructionCounter=0;
while(instructionCounter<100){
if(memory[instructionCounter]>9999)
{ printf("Word Overflow at memory element %d\n program will exit\n", instructionCounter);
exit(0);
}
instructionCounter++;
}
return 1;
}
输入流看起来像这样:(就像我上面提到的,这个流正在使用 VIM 命令进行重定向,它欺骗程序从文件中读取数据,而无需实际实现文件指针)
01 READ 60
02 LOAD 60
03 SUB 61
04 STOR 60
05 BRNG 15
06 READ 70
07 LOAD 70
08 ADD 80
09 STOR 80
10 LOAD 60
11 SUB 61
12 STOR 60
13 BRNG 15
14 BRAN 6
15 WRIT 80
16 HALT 99
61 SET 1
80 SET 0
我已经挠头好几个小时了,我不明白为什么会这样做,正如我所说,我是 C 编程新手,我仍然不知道如何调试和执行 C 的东西,我来了来自Java背景。
编辑 1:用户没有将程序写入虚拟计算机,程序已经编写完毕,用户只需通过 VIM 中的命令使用 STDIN 进行重定向,如下所示 (./computer < prog1)
或(./computer < prog2)
。该程序应该成功编译,然后当计算机弄清楚它是什么类型的程序时,它会根据其任务提示用户输入。因此它可以要求用户输入值,然后如果这是重定向到它的程序,它会计算它们的平均值。
最佳答案
如果您从用户处获取输入,则代码将会不同。
有几个选项,
保留相同的代码,但要求用户在输入所有数据后按 Ctrl-Z
。仅当您是唯一输入数据的人时,此方法才有效,并且通常不可行。
每次循环后,您可以询问用户是否要输入更多数据。然后,您可以获得 y/n
形式的输入,并在用户输入 y
时循环返回。
下面是一个示例。
do
{
scanf("%d %79s %d",operationCode,s,operand));
// Other code here
printf("\n Do you want to enter another value (y/n) ");
scanf(" %c",&check);
} while(check != 'n");
关于c - 函数溢出错误,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/38873672/
我已经使用 vue-cli 两个星期了,直到今天一切正常。我在本地建立这个项目。 https://drive.google.com/open?id=0BwGw1zyyKjW7S3RYWXRaX24tQ
您好,我正在尝试使用 python 库 pytesseract 从图像中提取文本。请找到代码: from PIL import Image from pytesseract import image_
我的错误 /usr/bin/ld: errno: TLS definition in /lib/libc.so.6 section .tbss mismatches non-TLS reference
我已经训练了一个模型,我正在尝试使用 predict函数但它返回以下错误。 Error in contrasts<-(*tmp*, value = contr.funs[1 + isOF[nn]])
根据Microsoft DataConnectors的信息我想通过 this ODBC driver 创建一个从 PowerBi 到 PostgreSQL 的连接器使用直接查询。我重用了 Micros
我已经为 SoundManagement 创建了一个包,其中有一个扩展 MediaPlayer 的类。我希望全局控制这个变量。这是我的代码: package soundmanagement; impo
我在Heroku上部署了一个应用程序。我正在使用免费服务。 我经常收到以下错误消息。 PG::Error: ERROR: out of memory 如果刷新浏览器,就可以了。但是随后,它又随机发生
我正在运行 LAMP 服务器,这个 .htaccess 给我一个 500 错误。其作用是过滤关键字并重定向到相应的域名。 Options +FollowSymLinks RewriteEngine
我有两个驱动器 A 和 B。使用 python 脚本,我在“A”驱动器中创建一些文件,并运行 powerscript,该脚本以 1 秒的间隔将驱动器 A 中的所有文件复制到驱动器 B。 我在 powe
下面的函数一直返回这个错误信息。我认为可能是 double_precision 字段类型导致了这种情况,我尝试使用 CAST,但要么不是这样,要么我没有做对...帮助? 这是错误: ERROR: i
这个问题已经有答案了: Syntax error due to using a reserved word as a table or column name in MySQL (1 个回答) 已关闭
我的数据库有这个小问题。 我创建了一个表“articoli”,其中包含商品的品牌、型号和价格。 每篇文章都由一个 id (ID_ARTICOLO)` 定义,它是一个自动递增字段。 好吧,现在当我尝试插
我是新来的。我目前正在 DeVry 在线学习中级 C++ 编程。我们正在使用 C++ Primer Plus 这本书,到目前为止我一直做得很好。我的老师最近向我们扔了一个曲线球。我目前的任务是这样的:
这个问题在这里已经有了答案: What is an undefined reference/unresolved external symbol error and how do I fix it?
我的网站中有一段代码有问题;此错误仅发生在 Internet Explorer 7 中。 我没有在这里发布我所有的 HTML/CSS 标记,而是发布了网站的一个版本 here . 如您所见,我在列中有
如果尝试在 USB 设备上构建 node.js 应用程序时在我的树莓派上使用 npm 时遇到一些问题。 package.json 看起来像这样: { "name" : "node-todo",
在 Python 中,您有 None单例,在某些情况下表现得很奇怪: >>> a = None >>> type(a) >>> isinstance(a,None) Traceback (most
这是我的 build.gradle (Module:app) 文件: apply plugin: 'com.android.application' android { compileSdkV
我是 android 的新手,我的项目刚才编译和运行正常,但在我尝试实现抽屉导航后,它给了我这个错误 FAILURE: Build failed with an exception. What wen
谁能解释一下?我想我正在做一些非常愚蠢的事情,并且急切地等待着启蒙。 我得到这个输出: phpversion() == 7.2.25-1+0~20191128.32+debian8~1.gbp108
我是一名优秀的程序员,十分优秀!