您的位置:首页 > 编程语言 > C语言/C++

华为:通过键盘输入100以内正整数的加、减运算式,请编写一个程序输出运算结果字符串

2013-04-19 11:29 1066 查看
/*这道题目暴露出很多问题 编程时头脑一定要清醒 否则会出现低级失误 比如字符串之间直接复制 和直接比较的问题*/
/**************************************************************************************
题目描述:
通过键盘输入100以内正整数的加、减运算式,请编写一个程序输出运算结果字符串。
输入字符串的格式为:“操作数1 运算符操作数2”,“操作数”与“运算符”之间以一个空格隔开。
补充说明:
1. 操作数为正整数,不需要考虑计算结果溢出的情况。
若输入算式格式错误,输出结果为“0”。
要求实现函数:
void arithmetic(const char *pInputStr, longlInputLen, char *pOutputStr);
【输入】 pInputStr: 输入字符串
lInputLen: 输入字符串长度
【输出】 pOutputStr:输出字符串,空间已经开辟好,与输入字符串等长;
【注意】只需要完成该函数功能算法,中间不需要有任何IO的输入输出
示例
输入:“4 + 7” 输出:“11”
输入:“4 - 7” 输出:“-3”
输入:“9 ++ 7” 输出:“0” 注:格式错误
***************************************************************************************/
#include<iostream>
#include<cstdio>
#include<cstdlib>
#include<cstring>
#include<assert.h>
using namespace std;
/*bug:对于操作数和操作符之间多余1个空格的情况 程序不报错 继续运行 不和题意 待debug*/
void arithmetic_cpy(const char *pInputStr, long lInputLen, char *pOutputStr)
{
/*因为分割字符串函数strtok的第一个参数不能是指针常量,所以复制
原字符串为另外的不相干的副本,记得要free,不是delete ,因为函数内部分配空间用的是malloc*/
char *pstr=strdup(pInputStr);
char  *out=pOutputStr;
assert(NULL!=pInputStr);
assert(NULL!=pOutputStr);
assert(lInputLen>0);
char temp[100]={0};
char table[3][15];  //  存储两个操作数和一个操作符,二维数组
int num1=0,num2=0,res=0;
char *p=strtok(pstr," ");   //开始分割字符串 因为分隔符只有两个,也可以不用循环
if(p==NULL)
{
cout<<"input error!"<<endl;
strcpy(out,"0");
return ;
}

//table[i++]=p; error C2440: '=' : cannot convert from 'char *' to 'char [15]'
//上边的错误是说指针复制应该strcpy  不应该只是改变指向

/*取第一个操作数*/
strcpy(table[0],p);
cout<<"table[0]= "<<table[0]<<endl;
num1=atoi(table[0]);
cout<<"num1= "<<num1<<endl;

/*去操作符并判断其合法性*/
p=strtok(NULL," ");
strcpy(table[1],p);
cout<<"table[1]= "<<table[1]<<endl;
//if(table[1]!="+"||table[1]!="-")    严重低级错误 字符串不能直接逻辑比较
if(strcmp(table[1],"+")!=0&&strcmp(table[1],"-")!=0)
{
cout<<"操作符不对!!"<<endl;
strcpy(out,"0");
return ;
}
/*取第二个操作数*/
p=strtok(NULL," ");
strcpy(table[2],p);
cout<<"table[2]= "<<table[2]<<endl;
num2=atoi(table[2]);
cout<<"num2= "<<num2<<endl;

if(strcmp(table[1],"+")==0)   //字符串之间不能直接比较
res=num1+num2;
else
res=num1-num2;	//字符串之间不能直接比较

/***************************
if(table[1]=="+")   字符串之间不能直接比较
res=num1+num2;
else
res=num1-num2;	字符串之间不能直接比较
************************/
cout<<"res="<<res<<endl;
itoa(res,temp,10);
strcpy(out,temp);
free(pstr);
}
/******用循环的方式********/
void arithmetic(const char *pInputStr, long lInputLen, char *pOutputStr)
{
char *pstr=strdup(pInputStr);
char  *out=pOutputStr;
assert(NULL!=pInputStr);
assert(NULL!=pOutputStr);
assert(lInputLen>0);
char temp[100]={0};
char table[3][15];
int num1=0,num2=0,res=0;
char *p=strtok(pstr," ");
if(p==NULL)
{
cout<<"input error!"<<endl;
strcpy(out,"0");
return ;
}
int i=0;
while(p!=NULL)
{
if(i>=3)      //要先判断 i值   否则将向未知内存写入数据  发生内存越界
{
strcpy(out,"0");
cout<<"i>=3!"<<endl;
return;
}
//table[i++]=p; error C2440: '=' : cannot convert from 'char *' to 'char [15]'
strcpy(table[i],p);
cout<<"table[i]= "<<table[i]<<endl;
p=strtok(NULL," ");
i++;
}
//if(table[1]!="+"&&table[1]!="-")  经典错误 字符串 不能直接相比较
if(strcmp(table[1],"+")!=0&&strcmp(table[1],"-")!=0)
{
cout<<"操作符不对!!"<<endl;
strcpy(out,"0");
return ;
}
num1=atoi(table[0]);
cout<<"num1= "<<num1<<endl;
num2=atoi(table[2]);
cout<<"num2= "<<num2<<endl;
if(strcmp(table[1],"+")==0)   //字符串之间不能直接比较
res=num1+num2;
else
res=num1-num2;	//字符串之间不能直接比较

/***************************
if(table[1]=="+")   字符串之间不能直接比较
res=num1+num2;
else
res=num1-num2;	字符串之间不能直接比较
************************/

itoa(res,temp,10);
strcpy(out,temp);
free(pstr);
}
int main()
{
char i[]="99 + 7";
char *o=new char[strlen(i)+1];
arithmetic(i,strlen(i),o);
cout<<"o= "<<o<<endl;
delete []o;
}
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签:  笔试面试 C C++ 算法
相关文章推荐