Skip to content

Instantly share code, notes, and snippets.

@dcrystalj
Last active August 29, 2015 14:17
Show Gist options
  • Save dcrystalj/4bf6db6724d42c78bb30 to your computer and use it in GitHub Desktop.
Save dcrystalj/4bf6db6724d42c78bb30 to your computer and use it in GitHub Desktop.
fun errror
#include <stdio.h>
#include <iostream>
#include <string.h>
#include <algorithm>
#define N 10000
using namespace std;
int filter(char* str, char* out, int out_pos){
int len = strlen(str);
int curr = 0;
int should_be = 0;
while (curr < len){
if(curr+1 < len && str[curr] == '.' && str[curr+1] == '.') {
//go to prev dir
should_be = curr;
out_pos-=2;
while (out[out_pos] != '/'){
out_pos--;
}
if(curr+2 < len && str[curr+2] != NULL)
curr += 2;
else
curr += 3;
}
else
out[out_pos++] = str[curr++];
}
out[out_pos] = 0;
return out_pos;
}
int main() {
int n;
cin >> n;
char command [N];
char curr_str [N];
int curr_pos = 0;
while (n >= 0) {
n--;
cin.getline(command, N);
if (string(command, 2) == "cd"){
if (command[3] == '/'){
curr_pos = 0;
}
else if (curr_str[curr_pos] != '/')
curr_str[curr_pos++] = '/';
//(command[3] != '.') {
if (command[3] == '.'){
curr_pos--;
curr_pos = filter(command+2, curr_str, curr_pos);
}
else
curr_pos = filter(command+3, curr_str, curr_pos);
//printf("\n curr_str: %s\n", curr_str);
//}
}
else if (string(command, 3) == "pwd"){
if (curr_pos <= 0)
printf("/\n");
else
printf("%s/\n", curr_str);
}
}
return 0;
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment