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

 通过键盘输入100以内正整数的加、减运算式,请编写一个程序输出运算结果字符串。
      输入字符串的格式为:“操作数1 运算符 操作数2”,“操作数”与“运算符”之间以一个空格隔开。
      补充说明:
      1. 操作数为正整数,不需要考虑计算结果溢出的情况。
      2. 若输入算式格式错误,输出结果为“0”。
      要求实现函数:
      void arithmetic(const char *pInputStr, long lInputLen, char *pOutputStr);
      【输入】 pInputStr:  输入字符串
      lInputLen:  输入字符串长度         
      【输出】 pOutputStr: 输出字符串,空间已经开辟好,与输入字符串等长;
      【注意】只需要完成该函数功能算法,中间不需要有任何IO的输入输出
      示例
      输入:“4 + 7”  输出:“11”
      输入:“4 - 7”  输出:“-3”

      输入:“9 ++ 7”  输出:“0” 注:格式错误


重点:strlen可以求出包含' '在内的字符串长度

还有就是字符串和整形的变换!!!


#include<iostream>
using namespace std;
void add(char *str,int len,char *outstr);
int main()
{
	char str[20];
	gets(str);
	int len=strlen(str);
	char outstr[20];
	add(str,len,outstr);
	cout<<outstr<<endl;
	system("pause");
	return 0;
}
void add(char *str,int len,char *outstr)
{
	int op1=0,op2=0,result;//操作数1 和 2
	int j;
	char hao;       //运算符.
	char temp[20],m=0;
	int k=0;
	
	int i=0;
		while(str[i]!=' ')
		{
			op1=op1*10+(str[i]-'0');
			i++;
		}
		//cout<<"op1="<<op1<<endl;//调试

		hao=str[++i];
		//cout<<"hao="<<hao<<endl;//调试

		if (str[i+1]!=' ')
		{
			outstr[0]='0';
			outstr[1]='\0';
			return;
		}
		i++;//空格位置
		i++;//
		while(str[i]!='\0')
		{
			op2=op2*10+(str[i]-'0');
			i++;
		}		
		//cout<<"op2="<<op2<<endl;//调试
	switch(hao)
	{
		case'+':result=(op1+op2);break;
		case'-':result=(op1+op2);break;
	}
	while(result)
	{
		temp[k]=result%10+'0';
		k++;
		result=result/10;
	}
	outstr[k]='\0';
	while(k)
	{
		outstr[m++]=temp[k-1];
		k--;
	}
}

没有考虑到负数的情况:补充:

   if(result < 0)  
   {  
   result = -result;  
   pOutputStr[k++] = '-';  
   }  

使用sprintf函数,可以大大缩短整形转换成字符串的过程,

sprintf函数用法参考:http://blog.csdn.net/qqyuanhao163/article/details/47735455

补充:

	while(result)
	{
		temp[k]=result%10+'0';
		k++;
		result=result/10;
	}
	outstr[k]='\0';
	while(k)//刚刚转化过来的字符串是逆序的,必须把它反转过来
	{
		outstr[m++]=temp[k-1];
		k--;
	}


	sprintf(outstr,"%d",result);
等价


参考答案:

   /************************************************************************/
      /*
      编程题第三题
      */
      /************************************************************************/
      /*
      【输入】 pInputStr:  输入字符串
      lInputLen:  输入字符串长度         
      【输出】 pOutputStr: 输出字符串,空间已经开辟好,与输入字符串等长;
      【注意】只需要完成该函数功能算法,中间不需要有任何IO的输入输出
      */
      void arithmetic(const char *pInputStr, long lInputLen, char *pOutputStr)
      {
      pOutputStr[0] = '0';
      pOutputStr[1] = 0;
      if(!pInputStr)
      {
      return;
      }
      // 获得第一个操作数
      int operand1 = 0;
      long index = 0;
      while(pInputStr[index] >= '0' && pInputStr[index] <= '9')
      {
      operand1 = operand1 * 10 + (pInputStr[index++] - '0');
      if(index >= lInputLen) return;
      }
      if(pInputStr[index++] != ' ' || index >= lInputLen)
      {
      return;
      }
      // 获得操作符
      char oper;
      if(pInputStr[index] == '+' || pInputStr[index] == '-')
      {
      oper = pInputStr[index++];
      }
      else
      {
      return;
      }
      if(index >= lInputLen || pInputStr[index++] != ' ')
      {
      return;
      }
      if(index >= lInputLen) return;
      // 获得第二个操作数
      int operand2 = 0;
      while(pInputStr[index] >= '0' && pInputStr[index] <= '9')
      {
      operand2 = operand2 * 10 + (pInputStr[index++] - '0');
      if(index >= lInputLen) return;
      }
      if(pInputStr[index] != 0)
      {
      return;
      }
      // 输入都是合法的
      int result;
      switch(oper)
      {
      case '+':
      result = operand1 + operand2;
      break;
      case '-':
      result = operand1 - operand2;
      break;
      }
      int k = 0;
      if(result < 0)
      {
      result = -result;
      pOutputStr[k++] = '-';
      }
      itoa(result, pOutputStr + k, 10);
      }



  • 1
    点赞
  • 3
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值