How to write a C Program Function to read instructions from the file and obey them in C Programming Language ?
This C Program function to read instructions from the file and obey them.
Solution:
- #include <stdio.h>
- #include <stdlib.h>
- #include <stdbool.h>
- #include "display.h"
- typedef unsigned char byte;
- int getopcode(byte b){
- return ((b >> 6) & 0x3);
- }
- int getoperand(byte b, bool s){
- if(s && (b >> 5) & 1){
- return (-32) + (b & 0x1F);
- } else {
- return (b & 0x3F);
- }
- }
- struct state {
- int x;
- int y;
- int pen;
- };
- struct state DX(struct state s, int dx, display *d){
- if (s.pen == 1) line(d, s.x, s.y, s.x + dx, s.y);
- s.x = s.x + dx;
- return s;
- }
- struct state DY(struct state s, int dy, display *d){
- if (s.pen == 1) line(d, s.x, s.y, s.x, s.y + dy);
- s.y = s.y + dy;
- return s;
- }
- struct state DT(struct state s, int t, display *d){
- pause(d, t);
- return s;
- }
- struct state PEN(struct state s){
- if(s.pen == 1){
- s.pen = 0;
- } else {
- s.pen = 1;
- }
- return s;
- }
- void interpet(FILE *in, display *d) {
- struct state s = { 0, 0, 0 };
- byte b;
- bool sign = true;
- b = fgetc(in);
- while(! feof(in)) {
- int opcode = getopcode(b);
- printf("%d %d\n",opcode, operand);
- if (opcode == 0) s = DX(s, getoperand(b, sign), d);
- if (opcode == 1) s = DY(s, getoperand(b, sign), d);
- if (opcode == 2){
- sign = false;
- s = DT(s, getoperand(b, sign), d);
- }
- if (opcode == 3) s = PEN(s);
- b = fgetc(in);
- }
- }
- // TODO: upgrade this function to read instructions from the file and obey them
- void run(char *filename, bool testing) {
- FILE *in = fopen(filename, "rb");
- if (in == NULL) {
- printf("Can't open %s\n", filename);
- exit(1);
- }
- display *d = newDisplay(filename, 200, 200, testing);
- interpet(in, d);
- end(d);
- fclose(in);
- if (testing) printf("Sketch %s OK\n", filename);
- }
Learn More :
File
- COPY DATA FROM ONE FILE TO ANOTHER FILE USING C PROGRAM
- C Program File Input & Output Example
- C Program To Destruc Self Execution File ?
- C Program To Store Students Record In A Text File
- C Program To Write Data In A File And Search Data From File
- Make a copy of file given in argv[1] to file given in argv[2]
- C Program to copy a file to another file
- Client/Server C program to make client send the name of a file
- A small I/O bound program to copy N bytes from an input file to an output file.
- C Program to Read and Write FIFO
- File Number Control C Program Example
- C Program to opens and reads dictionary file specified in spellrc
- C Program to Recursively converts all file-names to lower-case
- C Program to Find the Size of File using File Handling Function
- C Program Recursive function that searches for a given file in a given folder
- C Program Read a char & print next char ( file to file )
- C Program to Input Student Details into a File For Two Subjects
- Identifies the architecture Windows PE (exe or dll) file was compiled C Program
- File Handling (console to file) in C Program
Function
- How to pass one dimensional array to function in c.
- Write a c program which passes two dimension array to function.
- Write overloaded function templates for finding the roots of the linear (a * x + b = 0) and square (a * x2 + b * x + c = 0) uravneniy.Zamechanie: in function to send coefficients of the equations.
- C Program Character toupper() Example
- C Program Function Example
- Napisać funkcję obliczającą funkcję geometryczną w tablicy NxM elementowej z elementów o wartościach parzystych znajdujących się pod główną i ponad przeciwną przekątną.
- C Program To Find LCM and HCF Of Two Number Using Function - 2
- C Program To Convert Temperature In Celsius To Fahrenheit, Using Function
- C Program To Find Simple Interest
- C Function to Check Vowel
- Factorial Program In C Using Function
- C Program For Prime Number Using Function
- C Function to xorSwap and addSwap in C Programming
- C Program to concatenate two strings without using string functions
- C program calculates a given function in range given by user, stores the data in arrays and displays the answer in a table.
- C Program to Implements a dictionary's functionality.
- = (int*)malloc(sizeof(int)) ??
- Design a C function that shortens a string to 8 characters
- C Program to Implements a dictionary's functionality
- C Program that prompts the user to input a string, (whitespaces allowed)
- Local sounds functions in C Program
- Function Get the resource that will be gathered from the zone name
- C Program to Find the Size of File using File Handling Function
- Average function in C
Read
- Sort Three Numbers - program reads in three Integers and displays them in ascending order.
- C or C++ program to read marks of 4 subjects and find how many students are pass and fail with division
- C Program readers/writers
- C Program to Demonstrates use of reader/writer lock
- C Program to Write/Read Dynamic Data Example
- A small I/O bound program to copy N bytes from an input file to an output file.
- Input reads in the array positions and range for random generation.
- C Program to Read and Write FIFO
- pthreads Readers/Writers Lock C Program
- C Program to opens and reads dictionary file specified in spellrc
- C Program reads in and spews out the attributes of a given .wav file.
- C Program Number of Judge and Score From Each Judge
- Read 10 real numbers using a vector and show the least of them C Program
- C Program To Read A Parenthesised Infix Expression From The User And Check Whether It Is Well Parenthesised Or Not
- C Program To Read The Adjecancy Matrix of Directed Graph And Convert It Into Adjecancy List
- C Program Read a char & print next char ( file to file )
- Menu Driven Program to Read Two Integers Find Sum, Difference and Product C Program
Instruction