OJシリーズの100以内の正整数の加算・減算式


<pre name="code" class="cpp">// Arithmetic.cpp :              。
//

#include "stdafx.h"
#include <string.h>
#include <stdio.h>
/*
      100       、    ,                。
         :“   1        2”,“   ” “   ”         。

    :
1、       ,              。
2、         ,     “0”。

      : 
void arithmetic(const char *pInputStr, long lInputLen);

【  】 pInputStr:       
		 lInputLen:                  

【  】            ,        IO     

   
  :“4 + 7”    :“11”
  :“4 - 7”    :“-3”
  :“9 ++ 7”    :“0”  :    
*/
/*
* Function:arithmetic
* Usage:void arithmetic(const char *pInputStr, long lInputLen)
* -----------------------------------------------------
*/
void arithmetic(const char *pInputStr, long lInputLen)
{
	if (lInputLen<=0)
	{
		return ;
	}
	char AddOrSub[2] = {'+','-'};
	long i;
	long opera1,opera2;
	char temp;
	opera1 = opera2 = 0;

	for (i=0;i<lInputLen;i++)  //        
	{
		if (pInputStr[i]>'9'||pInputStr[i]<'0')
		{
			return ;
		}
		if (pInputStr[i] ==' ') //        
		{
			break;
		}		
		opera1 =opera1*10 + pInputStr[i]-'0';
		
	}
	temp = pInputStr[i+1]; //   
	if(pInputStr[i+2]==AddOrSub[0]|| pInputStr[i+2]==AddOrSub[1])
	{
		printf("%d
",0); return; // } i = i+3; // 3 while(i<lInputLen) // { if (pInputStr[i]>'9'||pInputStr[i]<'0') { return ; } opera2 =opera2*10 + pInputStr[i]-'0'; i ++; } int sum = 0; switch(temp) { case '+': sum = opera1 + opera2; printf("%d
",sum); break; case '-': sum = opera1 - opera2; printf("%d
",sum); break; default: return; } } int _tmain(int argc, _TCHAR* argv[]) { char pInputStr[1024]; gets(pInputStr); long lInputLen = strlen(pInputStr); arithmetic(pInputStr, lInputLen); getchar(); return 0; }