第一次尝试:(此时题目尚未更改,我按要求把-
和数字连起来输出)
#include<stdlib.h>
#include<iostream>
#include<string>
#include<queue>
#include<cstdio>
using namespace std;
class Scan
{
// private section
public:
void ToStringQueue(string input);
};
void Scan::ToStringQueue(string input)
{
string s;//Define a string "s",and store(存储) something by using it.
queue<string>que;//Define a queue "que".
int m,n,i,j,t=0;
n=input.size();//Define int n,"n = string's length".
for(i=0;i<n;i++)//mistake:i=1;i<=n;
{
if(input[i]=='-')
{
t=1;
que.push(s);//
s.clear();//clear s
s+=input[i];
continue;
}
else
{
if(t==1)// '-'
{
if(input[i]=='('||input[i]==')'||input[i]=='+'||input[i]=='*'||input[i]=='/')
{
t=0;
que.push(s);
s.clear();
s+=input[i];
que.push(s);//que.push(input[i]);
s.clear();
}
else
{
s+=input[i];
continue;
}
}
else
{
if(input[i]=='('||input[i]==')'||input[i]=='+'||input[i]=='*'||input[i]=='/')
{
que.push(s);
s.clear();
s+=input[i];
que.push(s);//que.push(s);
s.clear();
}
else
{
s+=input[i];
continue;
}
}
}
}
m=s.size();
if(m!=0)que.push(s);//check s
s.clear();
while(que.empty()==0)//mistake:j<n;
{
cout<<que.front()<<endl;
que.pop();
}
}
int main()
{
int n=0,i,j;
Scan sc;//实例化对象miss
string b;
cin>>b;
sc.ToStringQueue(b);
return 0;
}
//example:
//-100+(98-97)*2
//-100+(98+97)*2
//100*(99-98)
//100*10+1000
//100*10-100*(98*97-1)+100
//
思路分析:
m=s.size();
if(m!=0)que.push(s);//check s
s.clear();
问题1:for(i=1;i<=n;i++)
问题2:que.push(input[i]);
问题3:主函数里未实例化对象。
第二次尝试(-
单独一行):
代码2:
#ifndef CALCULATOR_H
#define CALCULATOR_H
#include<string>
#include<queue>
#include<stdlib.h>
#include<iostream>
using namespace std;
/*
* No description
*/
class Print
{
public:
void pout();
};
class Scan
{
// private section
public:
void ToStringQueue(string input);
};
#endif // CALCULATOR_H
#include "calculator.h" // class's header file
#include<string>
#include<queue>
#include<stdlib.h>
#include<iostream>
using namespace std;
queue<string>que;
string s;
void Scan::ToStringQueue(string input)
{
int n=input.length(),m=0,i,j;//n是字符串input的长度
for(i=0;i<n;i++)//n has no define;
{
if(input[i]=='-'||input[i]=='+'||input[i]=='('||input[i]==')'||input[i]=='/'||input[i]=='*')
{
que.push(s);
s.clear();
s+=input[i];
que.push(s);
s.clear();
}
else
{
s+=input[i];
continue;
}
}
m=s.size();//利用m判断s是否为空串
if(m!=0)que.push(s);
s.clear();
}
void Print::pout()//输出函数pout单独声明在out类里
{
while(que.empty()==0)
{
cout<<que.front()<<endl;
que.pop();
}
}
int main()
{
Scan Sc;//实例化对象Sc
Print Put;//实例化对象Put
string input;
cin>>input;
Sc.ToStringQueue(input);
Put.pout();
return 0;
}
//example:-100+(98-97)*2
思路分析:
问题:
要求:
代码3:
.h
文件:
#ifndef CALCULATOR_H
#define CALCULATOR_H
#include<string>
#include<queue>
#include<stdlib.h>
#include<iostream>
using namespace std;
/*
* No description
*/
class Print
{
public:
void pout();
};
class Scan
{
// private section
public:
void ToStringQueue(string input);
};
#endif // CALCULATOR_H
.cpp
文件:
#include "calculator.h" // class's header file
#include<string>
#include<queue>
#include<stdlib.h>
#include<iostream>
using namespace std;
queue<string>que; // Define a <string>queue "que";
string s; // Define string s;用空串s存储字符
int t=0; // Define t=0; 来计算位数
// Using global variables here.
void Scan::ToStringQueue(string input)
{
int n=input.length(),m=0,i,j;
for(i=0;i<n;i++) // Error: n has no define;
{
if(t>10) // such as "10,000,000,000"
{
cout<<"Error"<<endl;
break;
}
if(input[i]=='-' || input[i]=='+' || input[i]=='(' || input[i]==')' || input[i]=='/' || input[i]=='*')
{
t=0; // stop the count
que.push(s);
s=""; // 相当于s.clear();
s+=input[i];
que.push(s);
s="";
}
else
{
t++;
s+=input[i];
continue;
}
}
if(t<=10) // ifnormal
{
m=s.size();
if(m!=0)que.push(s);
s="";
}
}
void Print::pout()
{
if(t<=10) // ifnormal
{
while(que.empty()==0) // "que" is not empty
{
cout<<que.front()<<endl;
que.pop();
}
}
}
int main()
{
Scan Sc; // Define a "Scan" object:"Sc".
Print Put; // Define a "Print" object "Put".
string input;
cin>>input;
Sc.ToStringQueue(input);
Put.pout();
return 0;
}
//-100+(98-97)*2
第三次尝试:
代码4:
.h
文件:
#ifndef CALCULATOR_H
#define CALCULATOR_H
#include<string>
#include<queue>
#include<stdlib.h>
#include<iostream>
using namespace std;
/*
* No description
*/
class Print
{
public:
void pout(queue<string>que);//type:queue<string>
};
class Scan
{
// private section
public:
queue<string>ToStringQueue(string input);//type:queue<string>
};
#endif // CALCULATOR_H
.cpp
文件:
#include "calculator.h" // class's header file
#include<string>
#include<queue>
#include<stdlib.h>
#include<iostream>
using namespace std;
int t=0;//Using global variable "t";
queue<string>Scan::ToStringQueue(string input)//type:queue<string>
{
int n=input.length(),m=0,i,j;
queue<string>que;
string s;
for(i=0;i<n;i++) // Error: n has no define;
{
if(t>10) // such as "10,000,000,000"
{
cout<<"Error"<<endl;
break;
}
if(input[i]=='-' || input[i]=='+' || input[i]=='(' || input[i]==')' || input[i]=='/' || input[i]=='*')
{
t=0; // stop the count
que.push(s);
s=""; // s.clear();
s+=input[i];
que.push(s);
s="";
}
else
{
t++;
s+=input[i];
continue;
}
}
if(t<=10) // ifnormal
{
m=s.size();
if(m!=0)que.push(s);
s="";
}
return que;
}
void Print::pout(queue<string>que)
{
if(t<=10) // ifnormal
{
while(que.empty()==0) // "que" is not empty
{
cout<<que.front()<<endl;
que.pop();
}
}
}
int main()
{
queue<string>que;//type:queue<string>
Scan Sc; // Define a "Scan" object:"Sc".
Print Put; // Define a "Print" object "Put".
string input;
cin>>input;
que=Sc.ToStringQueue(input);
Put.pout(que);
return 0;
}
//-100+(98-97)*2
问题探究:
样例测试:
t=0; // stop the count
que.push(s);
s=""; // s.clear();
s+=input[i];
que.push(s);
s="";
解决方法:
t=0; // stop the count
if(s!="")
que.push(s);
s=""; // s.clear();
s+=input[i];
que.push(s);
s="";
最终的代码:
.h
.文件:
#ifndef CALCULATOR_H
#define CALCULATOR_H
#include<string>
#include<queue>
#include<stdlib.h>
#include<iostream>
using namespace std;
/*
* No description
*/
class Input
{
public:
string Get();
};
class Print
{
public:
void pout(queue<string>que);//type:queue<string>
};
class Scan
{
// private section
public:
queue<string>ToStringQueue(string input);//type:queue<string>
};
#endif // CALCULATOR_H
.cpp
文件
#include "calculator2.h" // class's header file
#include<string>
#include<queue>
#include<stdlib.h>
#include<iostream>
using namespace std;
int t=0;//Using global variable "t";
string Input::Get()
{
string s;
cin>>s;
return s;
}
queue<string>Scan::ToStringQueue(string input)//type:queue<string>
{
int n=input.length(),m=0,i,j;
queue<string>que;
string s;
for(i=0;i<n;i++) // Error: n has no define;
{
if(t>10) // such as "10,000,000,000"
{
cout<<"Error"<<endl;
break;
}
if(input[i]=='-' || input[i]=='+' || input[i]=='(' || input[i]==')' || input[i]=='/' || input[i]=='*')
{
t=0; // stop the count
if(s!="")//avoid null string
que.push(s);
s=""; // s.clear();
s+=input[i];
que.push(s);
s="";
}
else
{
t++;
s+=input[i];
continue;
}
}
que.push(s);
s.clear();
/*if(t<=10) // ifnormal
{
m=s.size();
if(m!=0)que.push(s);
s="";
} */
return que;
}
void Print::pout(queue<string>que)
{
if(t<=10) // ifnormal
{
while(que.empty()==0) // "que" is not empty
{
cout<<que.front()<<endl;
que.pop();
}
}
}
int main()
{
queue<string>que;//type:queue<string>
Scan Sc; // Define a "Scan" object:"Sc".
Print Put; // Define a "Print" object "Put".
Input Ge;// Define a "Input" object "Ge".
string input;
input=Ge.Get();//receieve input.
que=Sc.ToStringQueue(input);//receieve que.
Put.pout(que);//printf que.
return 0;
}
//-100+(98-97)*2
2016/2/22
感谢 @Sxiaopeng
和 @chs97
朋友的帮助和指导