面向对象程序设计作业3--C++计算器初始部分
Github 链接:https://github.com/luojingzhao/object-oriented/tree/master/calculate
代码规范
因为上次计算机的代码并没有按照老师要求的规范来写,在老师要求的第二次的代码强调,于是更改了一下代码的格式与规范(代码规范请参照链接)
以下是修改好的代码:
代码
main.cpp
/************************************************************
FileName: main.cpp
Author:Sonnypp Version :1.0 Date:16/02/25
Description:实现对输入的四则表达式进行
Version:Dev-C++ 5.10
Function List:
1. 处理输入的字符串;
2. 输出处理后的字符串;
History:
<author> <time> <version > <desc>
luojingzhao 16/02/27
***********************************************************/
#include<iostream>
#include<stdlib.h>
#include<string>
#include<queue>
#include"Scan.h"
#include"Print.h"
using namespace std;
/* run this program using the console pauser or add your own getch, system("pause") or input loop */
int main(int argc, char** argv)
{
Dispose *p=new Dispose(); /*申请内存 */
Print *t=new Print();
string _input, strGetInputRet; /*实例化string*/
/*处理输入的字符串*/
cin>>_input;
p->setinput(_input);
strGetInputRet = p->getinput();
p->ToStringQueue(strGetInputRet);
t->output(); /*输出处理完的字符串*/
/*释放内存 ,并使其指向空指针*/
delete p;
p=NULL;
delete t;
t=NULL;
return 0;
}
Scan.h
#ifndef __SCAN_H__
#define __SCAN_H__
#include<iostream>
#include<stdlib.h>
#include<string>
#include<queue>
using namespace std;
class Dispose
{
public:
void setinput(string &_input); /*函数的封装 */
string getinput();
void ToStringQueue(string &input); /*主要的处理函数 */
private:
string input;
};
#endif //__SCAN_H__
Print.h
#ifndef __PRINT_H__
#define __PRINT_H__
#include<iostream>
#include<stdlib.h>
#include<string>
#include<queue>
#include"Scan.h"
using namespace std;
class Print
{
public:
void output(); /*字符串的输出*/
};
#endif //__PRINT_H__
Scan.cpp
#include<iostream>
#include<stdlib.h>
#include<string>
#include<queue>
#include"Scan.h"
using namespace std;
queue<string>data; //定义队列的类型并实例化
string str; //实例化string类型
int count; //用来数输入的数的位数
//得到私有成员
void Dispose::setinput(string &_input)
{
input=_input;
}
string Dispose::getinput()
{
return input;
}
//主要的处理函数
void Dispose::ToStringQueue(string &input)
{
int n=input.length();
int i;
for (i=0;i<n;i++)
{
if (count>10)
{
cout<<"Error"<<endl; /*假如输入的字符串长度超过10,则输出错误 */
break;
}
else if (input[i]=='+'||input[i]=='-'||input[i]=='*'
||input[i]=='/'||input[i]=='='||input[i]=='('||input[i]==')')
{
count=0; /*将位数归零 */
data.push(str); /*将之前的数存入*/
str.clear(); /*清空*/
str=input[i]; /*将符号再次存入*/
data.push(str);
str.clear();
}
else if (count<=10)
{
count++; //计数
str+=input[i];
}
}
if (count<=10)
{
data.push(str); /*处理若末尾是未超过的数的输出*/
str.clear();
}
}
Print.cpp
#include<iostream>
#include<stdlib.h>
#include<string>
#include<queue>
#include"Print.h"
#include "Scan.h"
using namespace std;
extern queue<string>data; /*全局变量的声明*/
extern int count;
void Print::output()
{
if (count<=10)
{
while (data.empty()==0)
{
if (data.size()==1) /*避免输出多余的换行*/
{
cout<<data.front();
data.pop();
}
else
{
cout<<data.front()<<endl;
data.pop();
}
}
}
}
困难与感受
当然了,修改代码的时候也遇到了不少的困难,也向西瓜学长请教,还有其他的途径。像在定义全局变量的时候就不会加上`extern`,在将代码两个头文件改成两个类分别定义的头文件没有使用`#ifndef __PRINT_H__ #define __PRINT_H__`,以至于编译往往出错,不能通过。总的来说,虽然代码是写出来了,但是要成为一个合格的程序员,代码的风格和规范是必不可少的,这也然我体会到了要写好一个真正的好的代码,不是简简单单的将代码敲出来就好了的。