Last active
November 25, 2016 20:08
-
-
Save splitline/91eb44d9b517f6b3b81e2b15d8b7a1e8 to your computer and use it in GitHub Desktop.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
/* | |
[Input.txt] | |
10000 | |
9 | |
Set 0 String Hello, World, RRRRRR. | |
Set 15 int 2147483647 | |
Set 1000 short 65535 | |
Get 0 int | |
Get 15 short | |
Get 1000 int | |
Get 0 char | |
Get 1000 short | |
Get 0 String | |
500 | |
7 | |
Set 0 String Hello, | |
Set 6 int 48 | |
Set 7 String ZZZZZZZ | |
Get 0 String | |
Set 499 int 2147483647 | |
Get 499 char | |
Get 499 int | |
====================================== | |
[Ouput.txt] | |
1819043144 | |
-1 | |
65535 | |
72 | |
-1 | |
Hello, World, RR. | |
Hello,0ZZZZZZZ | |
Violation Access. | |
0 | |
Violation Access. | |
*/ | |
#include <stdio.h> | |
#include <stdlib.h> | |
#include <string.h> | |
#include <ctype.h> | |
int mem[65540]={0}; | |
int main(){ | |
int N; | |
while(~scanf("%d",&N)){ | |
memset(mem,0,sizeof(mem)); | |
int k,addr,val_n; | |
char cmd[5]={0},type[10]={0},val[1000]={0}; | |
scanf("%d",&k); | |
int max=-1; | |
for(int i=0;i<k;i++){ | |
scanf("%s%d%s",cmd,&addr,type); | |
if(addr>max)max=addr; | |
if(addr>N){ | |
printf("Violation Access.\n"); | |
continue; | |
} | |
if(!strcmp(cmd,"Set")){ | |
if(!strcmp(type,"String")){ | |
scanf(" "); | |
gets(val); | |
if(addr+strlen(val)>N){ | |
printf("Violation Access.\n"); | |
continue; | |
} | |
for(int j=addr;j<strlen(val)+addr;j++){ | |
mem[j]=(int)val[j-addr]; | |
} | |
mem[strlen(val)+addr]=0; | |
} | |
if(!strcmp(type,"char")){ | |
if(addr>N){ | |
printf("Violation Access.\n"); | |
continue; | |
} | |
scanf("%d",&mem[addr]); | |
} | |
if(!strcmp(type,"int")){ | |
if(addr+4>N){ | |
printf("Violation Access.\n"); | |
continue; | |
} | |
scanf("%d",&val_n); | |
for(int i=addr;i<addr+4;i++){ | |
mem[i]=val_n%256; | |
val_n/=256; | |
} | |
} | |
if(!strcmp(type,"short")){ | |
if(addr+2>N){ | |
printf("Violation Access.\n"); | |
continue; | |
} | |
scanf("%d",&val_n); | |
for(int j=addr;j<addr+2;j++){ | |
mem[j]=val_n%256; | |
val_n/=256; | |
} | |
} | |
} | |
if(!strcmp(cmd,"Get")){ | |
if(!strcmp(type,"String")){ | |
if(addr>N){ | |
printf("Violation Access.\n"); | |
continue; | |
} | |
for(int j=addr;mem[j]!=0;j++){ | |
printf("%c",mem[j]); | |
} | |
printf("\n"); | |
} | |
if(!strcmp(type,"char")){ | |
if(addr>N){ | |
printf("Violation Access.\n"); | |
continue; | |
} | |
printf("%d\n",mem[addr]); | |
} | |
if(!strcmp(type,"int")){ | |
if(addr+4>N){ | |
printf("Violation Access.\n"); | |
continue; | |
} | |
int tmp=0x00; | |
tmp=mem[addr]+mem[addr+1]*0x100+mem[addr+2]*0x100*0x100+mem[addr+3]*0x100*0x100*0x100; | |
printf("%d\n",tmp); | |
} | |
if(!strcmp(type,"short")){ | |
if(addr+2>N){ | |
printf("Violation Access.\n"); | |
continue; | |
} | |
short tmp=0x00; | |
tmp=mem[addr]+mem[addr+1]*0x100; | |
printf("%d\n",tmp); | |
} | |
} | |
//for(int i=0;i<50;i++)printf("%x;",mem[i]); | |
//printf("\n"); | |
} | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment