Sei sulla pagina 1di 6

Assignment 2 Question 2(b) i)

#include <stdio.h> void main() { double v=300, i=2.5; printf("The value of voltage = %.2f\n",v); printf("The value of current = %.2f\n",i); printf("The value of resistance = %.2f\n\n",v/i); }

ii)

#include <stdio.h> #define LAST 10 int main() { int i, sum=0; for(i=1;i<=LAST;i++) { printf("%d\n",i); sum+=i; } printf("The sum of all numbers = %d\n",sum); return 0; }

iii)

#include <stdio.h> void main() { char name[20]; int age; int year; printf("\nPlease enter your name : "); scanf("%s",&name); printf("\nPlease enter your age : "); scanf("%d",&age); printf("\nPlease enter your year of birth (yyyy) : "); scanf("%d",&year); printf("\nYour name is %s.\nYour year of birth is %d.\nAnd that means you are %d years old\n\n",name, year, age); }

Question 2(c) 1.
#include <stdio.h> typedef unsigned short BYTE; BYTE *display = (BYTE *) 0xE010; BYTE num[10] = { 0x3f, 0x06, 0x5b, 0x4f, 0x66, 0x6d, 0x7d, 0x07, 0x7f, 0x6f }; void main( ) { int i; for(;;) { display[0] = 0;display[1] = 0;display[2] = 0; scanf("%d",&i); if(i==0) display[3]=num[0]; else if(i==1) display[3]=num[1]; else if(i==2) display[3]=num[2]; else if(i==3) display[3]=num[3]; else if(i==4) display[3]=num[4]; else if(i==5) display[3]=num[5]; else if(i==6) display[3]=num[6]; else if(i==7) display[3]=num[7]; else if(i==8) display[3]=num[8]; else if(i==9) display[3]=num[9]; else { display[3]=0; printf("INVALID CODE\n"); } } }

2.

#include <stdio.h> typedef unsigned char BYTE; BYTE *leds = (BYTE *) 0xE003; void main(void) { int i; for (;;) { scanf("%d",&i); if(i==1) *leds=0xFF; else if(i==2) *leds=0xAA; else if(i==3) *leds=0xC3; else *leds=0x0; } }

3.

#include <stdlib.h> #include <stdio.h> #include <string.h> #include "drawpad.h" #define IRQ 4 void *timerint = 0x00000040; void *mouseint = 0x00000100; DRAWPAD *drawpad = 0x0000E020; void main(void) { char buf[16]; drawpad->ctrl = RED << 4; gotoxy(130, 20); textout("DANGER ZONE", YELLOW, 2); gotoxy(30, 192); textout("DANGER ZONE", YELLOW, 2); gotoxy(0, 97); textout("Start", BLACK, 1); gotoxy(240, 185); textout("End", BLACK, 1); gotoxy(0, 110); rect(47,10, WHITE, WHITE, 2); gotoxy(37, 47); rect(10,68, WHITE, WHITE, 2); gotoxy(41, 47); rect(85,10, WHITE, WHITE, 2); gotoxy(123, 47); rect(10,68, WHITE, WHITE, 2); gotoxy(123, 110); rect(10,75, WHITE, WHITE, 2); gotoxy(125, 175); rect(129, 10, WHITE, WHITE, 2); } void gotoxy(int x, int y) { drawpad->x = x; drawpad->y = y; drawpad->ctrl = 0x0080; } void ellipse(int w, int h, int color, int border, int bw) { drawpad->x += w; drawpad->y += h; drawpad->ctrl = 0x1000 | (color << 8) | (border << 4) | bw; } void rect(int w, int h, int color, int border, int bw) { drawpad->x += w;

drawpad->y += h; drawpad->ctrl = 0x2000 | (color << 8) | (border << 4) | bw; } void textout(char *s, int color, int size) { while (*s) drawpad->ctrl = 0x8000 | (*s++ << 8) | (color << 4) | size; } interrupt void buttonproc(void) { short x = drawpad->x, y = drawpad->y; if (drawpad->iflags & LBUTTONDOWN) { gotoxy(25, 40); ellipse(10, 10, RED, RED, 1); } else if (drawpad->iflags & LBUTTONUP) { gotoxy(25, 40); ellipse(10, 10, BLUE, BLUE, 1); } else if (drawpad->iflags & RBUTTONDOWN) { gotoxy(25, 90); ellipse(10, 10, GREEN, GREEN, 1); } else if (drawpad->iflags & RBUTTONUP) { gotoxy(25, 90); ellipse(10, 10, BLUE, BLUE, 1); } drawpad->iflags = 0; gotoxy(x, y); } interrupt void progressproc(void) { short x = drawpad->x, y = drawpad->y; static int len; static int color = CYAN; gotoxy(0, 190); rect(len++, 10, color, color, 1); if (len > 300) { len = 0; color = (color == CYAN) ? BLUE : CYAN; } gotoxy(x, y); }

Potrebbero piacerti anche