C++ 从键盘读取输入
我需要从键盘(stdin)读取以下文本。请注意,用户只能通过键盘以这种格式输入C++ 从键盘读取输入,c++,string,stdin,C++,String,Stdin,我需要从键盘(stdin)读取以下文本。请注意,用户只能通过键盘以这种格式输入 #the total size of physical memory (units are B, KB, MB, GB) 512MB 2 #the following are memory allocations { abc = alloc(1KB); { y_ = alloc(128MB);
#the total size of physical memory (units are B, KB, MB, GB)
512MB 2 #the following are memory allocations
{
abc = alloc(1KB);
{
y_ = alloc(128MB);
x1= alloc(128MB);
y_ = alloc(32MB);
for (i = 0; i < 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);
}
其中abc是对象名,1KB是分配的内存大小。
x1.next=x2,
这里x1指向x2
for (i = 0; i < 256; i++) abc[i] =alloc(512kB);
(i=0;i<256;i++)abc[i]=alloc(512kB);
for循环操作系统以这种格式输入,它可以具有相同的行命令,也可以具有嵌套的for循环
我有下面的代码在某种程度上解决了这个问题。我想知道如何改进它。请帮忙
我的代码是:
#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 the input main memory and 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 and physical memory to int and report error if not a number */
gen = atoi(sGen.c_str());
if(gen ==0) {
cerr << "Generation must be a number"<<endl;
exit(0);
}
pMem_int = atoi(pMem.c_str());
// cout<< gen<<" "<<pMem_int<<endl;
/* Now that the number from pMem is removed, get its unit B,MB,KB */
extract(pMem); /* returns val(string) */
/* convert the given physical memory to Byte. input: pMem_int*/
toByte(pMem_int, val); /* return(pMem_ulong)*/
// move pMem_ulond to another location to keep address intact
/* read rest of the inputs */
/* Ignore comment lines before the 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 the input stream for 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 the allocate function here with 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 the allocate function here with 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 the allocate function here with 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 the allocate function here with name: id, size: id_size_ulong
}
if(((cBrckt-oBrckt)) == gBrckt)
break;
}
}
/*---------------------- Function definitions --------------------------------*/
/* Function to extract the 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 the physical memory to 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 the 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 and GB of input, returns val*/
id_size_int = atoi(id_size.c_str());
/* Convert object size to 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 and GB of input, returns val*/
id_size_int = atoi(id_size.c_str());
/* Convert object size to 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 and 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 and GB of input, returns val*/
id_size_int = atoi(id_size.c_str());
/* Convert object size to 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;
}
#包括
#包括
#包括
#包括
#包括
#包括
#包括
使用名称空间std;
使用std::stringstream;
字符串pMem、sGen、comment、val、input、input\u for、id\u size、id、init\u str1、init\u str2、inc\u str、id\u dummy、s\u out、sss、id\u 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;
无符号长pMem_ulong=0,id_size_ulong;
chart[20],m[256],init1[10],init2[10],inc[10];
未签名的pos_开始,pos,pos_开始=0,pos_结束=0;
字符串提取(字符串pMem_提取);
无符号长toByte(int pMem_int_func,字符串val);
无效注释忽略(字符串和输入);
void func_insert();
void func_insert_for();
流出来;
void commentIgnore_for(字符串和输入_for);
int main()
{
/*读取输入主存储器和代数*/
/*忽略注释行*/
cin>>pMem;
如果(pMem==“#”){
cin.clear();
pMem.clear();
getline(cin,评论);
cin>>pMem;
}
如果(pMem==“#”){
cin.clear();
pMem.clear();
getline(cin,评论);
cin>>pMem;
}
如果(pMem==“#”){
cin.clear();
pMem.clear();
getline(cin,评论);
cin>>pMem;
}
/*读取输入生成*/
cin>>sGen;
如果(sGen==“#”){
cin.clear();
sGen.clear();
getline(cin,评论);
cin>>sGen;
}
如果(sGen==“#”){
cin.clear();
sGen.clear();
getline(cin,评论);
cin>>sGen;
}
如果(sGen==“#”){
cin.clear();
sGen.clear();
getline(cin,评论);
cin>>sGen;
}
/*将sGen和物理内存转换为int,如果不是数字,则报告错误*/
gen=atoi(sGen.c_str());
如果(gen==0){
cerr我的建议是,您应该编写一个适当的标记器-一段代码,它可以理解什么属于一起(例如“单词”),以及在哪里拆分(例如“(,),{,}”)。标记器将返回一个枚举,类似这样:
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
};
一旦你有了一个令牌,你就需要了解这个令牌是什么。有一个“已知”令牌(也称为“关键字”)表可能是有意义的,比如“mem”、“alloc”、“for”等等。如果某个东西不是关键字,那它就是一个符号的名称,比如一个变量。你将它们存储在一个表中,以便以后可以引用它们
你将不得不使用某种形式的堆栈来记录你的位置,这样当你完成时,你就可以回到你原来的位置
编写一个泛型解析器并不十分困难,而且在这样做时,您很可能会得到比当前代码少得多的代码
当然,你可以摆脱所有的:
if(input == "#"){
cin.clear();
input.clear();
getline(cin,comment);
cin >> input;
}
只需让解析器检查是否有一个“#”
作为输入,然后跳到行的末尾(如果有,则完成当前标记,如果没有,则继续进行)。IIUC,输入不是面向行的,因此
使用std::getline
可能不适用。我自己的方法是:
- 插入过滤流buf以删除注释
- 使用某种基于正则表达式的lexer(例如
) 将输入分解为令牌flex
- 定义语法;一旦我走到这一步,我会使用
, 为了生成语法解析代码,只需一个简单的 递归下降解析器应该不会太难编写bison
我已经解析了它,所以很难给出更精确的建议。我认为这可能更适合@CaptainObvlious:我在那里看到一个问题有一个问题……我被要求使它与空格兼容。这意味着输入也可以在一行中输入。就像一行中的两个分配。我认为我没有注意到这一点f、 我确实需要帮助。@Matstpeterson。你的方法非常糟糕。编写一个给你一个标记的函数会更好。我会写一个答案。有免费的工具可以生成标记器和解析器;这些工具应该被使用,而不是手工编写。为什么
clear()
在看到注释的开头之后?只需向前跳,直到看到行的下一个结尾。(这可能是标记器中的规则,也可以在输入流中插入过滤流buf。)基本上,我分配给定的physicalmemory并将其除以生成数。当我遇到alloc时,我在物理内存中分配alloc指定的内存。在for循环中,我递归分配。这是我正在研究的垃圾收集算法。x1.net=x2使x1指向x2。它是一个链接的l是的。
if(input == "#"){
cin.clear();
input.clear();
getline(cin,comment);
cin >> input;
}