Skip to content

Instantly share code, notes, and snippets.

@pcyu16
Created June 26, 2010 02:09
Show Gist options
  • Select an option

  • Save pcyu16/453692 to your computer and use it in GitHub Desktop.

Select an option

Save pcyu16/453692 to your computer and use it in GitHub Desktop.
#include <cstdio>
#include <map>
#include <iostream>
#include <string>
#include <fstream>
#include <vector>
#include <algorithm>
#define MAX_LEN 128
using namespace std;
const char *cmt = "* One instruction's place is reserved here";
int main(int argc, char *argv[])
{
char ifn[] = "output.tm";
char ofn[] = "sort.tm";
char *src_name;
char *dst_name;
char tmp[MAX_LEN];
if(argc == 1) src_name = ifn;
else src_name = argv[1];
if(argc == 3 ) dst_name = argv[2];
else dst_name = ofn;
fstream fp(src_name,ios::in);
vector<string> ins;
map<int,string> table;
while(fp.getline(tmp,MAX_LEN)!=NULL){
int val;
if(tmp[0] == '*'){
ins.push_back(tmp);
continue;
}
sscanf(tmp,"%d", &val);
table[val] = tmp;
ins.push_back(tmp);
}
fp.close();
fp.open(dst_name,ios::out);
int lineno = 0;
vector<string>::iterator it;
for(it=ins.begin();it!=ins.end();it++){
if( *it == cmt){
fp << table[lineno] << endl;
ins.erase(find(ins.begin(),ins.end(),table[lineno]));
lineno++;
}else{
lineno += (*it)[0]=='*'?0:1;
fp << *it << endl;
}
}
fp.close();
return 0;
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment