1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140
| #include <stdio.h> #include <limits.h> #include <string.h> #include <algorithm> using namespace std; #define MAX (10000) char szIntMax[20]; char szLine[MAX]; char szOne[MAX]; char szTwo[MAX]; char szOper[10];
char* MyItoa(int nNum, char* pszNum, int nBase) { int nLen = 0; while (nNum) { pszNum[nLen++] = nNum % nBase + '0'; nNum /= nBase; } reverse(pszNum, pszNum + nLen); pszNum[nLen] = '\0';
return pszNum; }
bool IsBigger(char* pszOne, int nLenOne, char* pszTwo, int nLenTwo) { if (nLenOne != nLenTwo) { return nLenOne > nLenTwo; } else { for (int i = 0; i < nLenOne; ++i) { if (pszOne[i] != pszTwo[i]) { return pszOne[i] > pszTwo[i]; } } return false; } }
int StripHeadZero(char* pszNum) { int nLen = strlen(pszNum); int i;
for (i = 0; i < nLen && pszNum[i] == '0'; ++i); if (i == nLen) { pszNum[0] = '0'; pszNum[1] = '\0'; nLen = 2; } else { char* pszWrite = pszNum; char* pszRead = pszNum + i; nLen = 0; while (*pszRead) { *pszWrite++ = *pszRead++; ++nLen; } *pszWrite = '\0'; }
return nLen; }
int main() { int nIntMax = INT_MAX; MyItoa(nIntMax, szIntMax, 10); int nLenMax = strlen(szIntMax);
while (gets(szLine)) { if (szLine[0] == '\0') { continue; }
sscanf(szLine, ";%s%s%s";, szOne, szOper, szTwo); printf(";%s %s %s\n";, szOne, szOper, szTwo); StripHeadZero(szOne); StripHeadZero(szTwo);
int nLenOne = strlen(szOne); int nLenTwo = strlen(szTwo); bool bFirst = false; bool bSecond = false;
if (IsBigger(szOne, nLenOne, szIntMax, nLenMax)) { printf(";first number too big\n";); bFirst = true; }
if (IsBigger(szTwo, nLenTwo, szIntMax, nLenMax)) { printf(";second number too big\n";); bSecond = true; }
if (bFirst || bSecond) { if (szOper[0] == '+' || (szOper[0] == '*' && szOne[0] != '0' && szTwo[0] != '0')) { printf(";result too big\n";); } } else { long long nOne, nTwo; sscanf(szLine, "%lld%s%lld", &nOne, szOper, &nTwo); long long nResult;
if (szOper[0] == '+') { nResult = nOne + nTwo; } else if (szOper[0] == '*') { nResult = nOne * nTwo; } if (nResult > INT_MAX) { printf(";result too big\n";); } } }
return 0; }
|