c++ - reading input from keyboard -
i required read keyboard(stdin), following text. pl note entered user keyboard in format only.
#the total size of physical memory (units b, kb, mb, gb) 512mb 2 #the following memory allocations { abc = alloc(1kb); { y_ = alloc(128mb); x1= alloc(128mb); y_ = alloc(32mb); (i = 0; < 256; i++) abc[i] =alloc(512kb); x1 = alloc(32mb); x2 = alloc(32mb); x3 = alloc(32mb); x1.next = x2, x2.next = x3, x3.next = x1; } abc = alloc(256mb); }
so let me break down. line beginning # sign considered comment , ignored. first 2 allocations physical memory size , number of generations. global bracket opened. , may followed line called
abc = alloc(1kb);
where abc object name, 1kb memory size allocated. x1.next = x2,
here x1 points x2.
for (i = 0; < 256; i++) abc[i] =alloc(512kb);
the loop os entered in format , can have same line command or can have nested loops.
i have following code takes care of somewhat. want know improve on it. please help.
and code this:
#include <iostream> #include <algorithm> #include <string> #include <iomanip> #include <limits> #include <stdio.h> #include <sstream> using namespace std; using std::stringstream; string pmem,sgen, comment,val,input,input_for,id_size,id,init_str1, init_str2, inc_str, id_dummy,s_out,sss, id_dummy1; int gen=0, pmem_int=0,i=0, gbrckt =0,cbrckt=0, obrckt=0, id_size_int,v1,v2, for_obrckt=0,for_cbrckt=0,y=0, y1=0, g=0; unsigned long pmem_ulong =0, id_size_ulong; char t[20], m[256], init1[10],init2[10],inc[10]; unsigned pos_start, pos,pos_strt=0,pos_end=0; string extract(string pmem_extract); unsigned long tobyte(int pmem_int_func, string val); void commentignore(string& input); void func_insert(); void func_insert_for(); stringstream out; void commentignore_for(string& input_for); int main() { /* reading input main memory , num of generations */ /* ignoring comment line */ cin >> pmem; if(pmem == "#") { cin.clear(); pmem.clear(); getline(cin,comment); cin >> pmem; } if(pmem == "#") { cin.clear(); pmem.clear(); getline(cin,comment); cin >> pmem; } if(pmem == "#") { cin.clear(); pmem.clear(); getline(cin,comment); cin >> pmem; } /* reading input generations */ cin>> sgen; if(sgen == "#") { cin.clear(); sgen.clear(); getline(cin,comment); cin >> sgen; } if(sgen == "#") { cin.clear(); sgen.clear(); getline(cin,comment); cin >> sgen; } if(sgen == "#") { cin.clear(); sgen.clear(); getline(cin,comment); cin >> sgen; } /* convert sgen , physical memory int , report error if not number */ gen = atoi(sgen.c_str()); if(gen ==0) { cerr << "generation must number"<<endl; exit(0); } pmem_int = atoi(pmem.c_str()); // cout<< gen<<" "<<pmem_int<<endl; /* number pmem removed, unit b,mb,kb */ extract(pmem); /* returns val(string) */ /* convert given physical memory byte. input: pmem_int*/ tobyte(pmem_int, val); /* return(pmem_ulong)*/ // move pmem_ulond location keep address intact /* read rest of inputs */ /* ignore comment lines before global bracket */ cin >> input; if(input == "#"){ cin.clear(); input.clear(); getline(cin,comment); cin >> input; } if(input == "#"){ cin.clear(); input.clear(); getline(cin,comment); cin >> input; } if(input == "#"){ cin.clear(); input.clear(); getline(cin,comment); cin >> input; } if(input.compare("{") ==0) gbrckt=1; else { cerr<< "syntax error\n"; exit(0); } /* clearing input stream next input */ cin.ignore(numeric_limits<streamsize>::max(), '\n'); cin.clear(); input.clear(); //cout<<"input: "<<input<<endl; while( getline(cin,input)) { if(input == "ctrl-d") break; commentignore(input); //cout<<"inputloop: "<<input<<endl; /* if input = '{' or '}'*/ if(input.compare("{") ==0) obrckt = obrckt + 1; if (input.compare("}") ==0) cbrckt = cbrckt + 1; if (((input.find("alloc"))!= string::npos) && (input.find("alloc") < input.find("for"))) { func_insert(); //call allocate function here name: id, size: id_size_ulong } if ((input.find("for")) != string::npos) { sscanf(input.c_str(), "for (%s = %d; %s < %d; %[^)])", init1, &v1, init2, &v2, inc); init_str1 = init1, init_str2 = init2, inc_str = inc; cout<<init1<<" ="<< v1<<" "<<init_str1<<" < " << v2<< " "<< inc_str<<endl; cout << input <<endl; if(init_str1 != init_str2) { cerr << "error!\n"; exit(0); } if ((input.find("alloc"))!= string::npos) { // unsigned pos = (input.find("alloc")); if((input.find(";")) != string::npos) { pos_start = (input.find(")")+1); string alloc_substr = input.substr(pos_start); cout<<"substring alloc: "<< alloc_substr<<endl; func_insert(); //call allocate function here name: id, size: id_size_ulong } else { cerr << "error: syntax\n"; exit(0); } } // cin.ignore(); while(getline(cin,input_for)) { commentignore_for(input_for); if ((input_for.find("{") != string::npos)) { pos = input_for.find("{"); for_obrckt = for_obrckt+1; string for_brckt = input_for.substr(pos,pos); cout<< "found: " << for_obrckt<<endl; } if ((input_for.find("}") != string::npos)) { pos = input_for.find("}"); for_cbrckt = for_cbrckt+1; string for_brckt = input_for.substr(pos,pos); cout<< "found: " << for_cbrckt<<endl; } if (((input_for.find("alloc"))!= string::npos) && (input_for.find("alloc") < input_for.find("for"))) { func_insert_for(); //call allocate function here name: id, size: id_size_ulong } if(for_obrckt == for_cbrckt) break; } cout<<"out of break"<<endl; } if (((input.find(".next"))!= string::npos) && (input.find(".next") < input.find("for"))) { func_insert(); //call allocate function here name: id, size: id_size_ulong } if(((cbrckt-obrckt)) == gbrckt) break; } } /*---------------------- function definitions --------------------------------*/ /* function extract string part of physical memory */ string extract(string pmem_extract) { i=0; const char *p = pmem_extract.c_str(); for(i=0; i<=(pmem_extract.length()); i++) { if (*p=='0'|| *p=='1'|| *p=='2'|| *p=='3'|| *p =='4'|| *p=='5'|| *p=='6'|| *p=='7'|| *p=='8'|| *p=='9') *p++; else { val = pmem_extract.substr(i); return(val); } } } /* convert physical memory bytes. return(pmem_ulong);*/ unsigned long tobyte(int pmem_int_func, string val) { if (val == "kb") pmem_ulong = (unsigned long) pmem_int_func * 1024; else if (val == "b") pmem_ulong = (unsigned long) pmem_int_func; else if (val == "gb") pmem_ulong = (unsigned long) pmem_int_func * 1073741824; else if (val == "mb") pmem_ulong = (unsigned long) pmem_int_func * 1048576; else { cerr<<"missing value in memory, b, kb, mb, gb\n"; exit(0); } return(pmem_ulong); } /*ignoring comment line*/ void commentignore(string& input) { unsigned found = input.find('#'); if (found!=std::string::npos) input= input.erase(found); else return; return; } void func_insert() { sscanf(input.c_str(), "%s = alloc(%[^)]);", t, m); id =t; id_size =m; cout<<"tag: "<<id <<" memory: "<<id_size<<endl; extract(id_size); /* separates b,mb,kb , gb of input, returns val*/ id_size_int = atoi(id_size.c_str()); /* convert object size b */ tobyte(id_size_int, val); /* return(pmem_ulong) */ id_size_ulong = pmem_ulong; } void func_insert_for() { sscanf(input_for.c_str(), "%s = alloc(%[^)]);", t, m); id =t; id_size =m; if(!((id.find("[")) && (id.find("]")) != string::npos)) { cout<<"tag: "<<id <<" memory: "<<id_size<<endl; extract(id_size); /* separates b,mb,kb , gb of input, returns val*/ id_size_int = atoi(id_size.c_str()); /* convert object size b */ tobyte(id_size_int, val); /* return(pmem_ulong) */ id_size_ulong = pmem_ulong; // allocate here return; } else { if(inc_str.find("++")) y1 =1; if(inc_str.find("=")) { sss = inc_str.substr(inc_str.find("+") +1); y1 = atoi(sss.c_str()); cout<<"y1:"<<y1<<endl; } pos_strt = id.find("["); pos_end = id.find("]") -1; cout<<"positions start , ebd: " << pos_strt<<pos_end<<endl; id_dummy = id.substr(0,pos_strt); id = id_dummy; cout<<"tag: "<<id_dummy <<" memory: "<<id_size<<endl; extract(id_size); /* separates b,mb,kb , gb of input, returns val*/ id_size_int = atoi(id_size.c_str()); /* convert object size b */ tobyte(id_size_int, val); /* return(pmem_ulong) */ id_size_ulong = pmem_ulong; //allocate here cout<<"v1: " << v1 << " " << v2<<endl; // g = 0; for(y = v1; y < v2; y= y+y1) { // allocate here } } return; } void commentignore_for(string& input_for) { unsigned found = input_for.find('#'); if (found!=std::string::npos) input_for= input_for.erase(found); else return; return; }
also required make whitespace compatible. means input can entered in 1 line well. 2 allocations in 1 line. have not been able taken care. need that.
my suggestion write proper tokenizer - piece of code understands belongs (such "words"), , split (e.g. "(, ), {, }"). tokenizer return enum, this:
enum token { token_unknown, // error indiciation. token_leftparen, token_rightparen, token_leftbracket, token_rightbracket, token_comma, token_semicolon, token_equal, token_word, // sequence of alphanumerics };
once have token, need understand token is. may make sense have table of "known" tokens (aka "keywords"), such "mem", "alloc", "for", , on. if isn't keyword, it's name of symbol, such variable. store in table, can refer them later.
you have use sort of stack of are, can came when finish.
writing generic parser isn't terribly hard, , end lot less code current code in doing so.
certainly, you'd able rid of the:
if(input == "#"){ cin.clear(); input.clear(); getline(cin,comment); cin >> input; }
just let parser check if there '#'
input, , skip on end of line (completing current token if have one, if not, keep going).
Comments
Post a Comment