描述 计算a加b。 输入 一行,用空格分开的两个整数a和b。其中0≤a, b≤10000。 输出 一个整数,为a加b的和。 样例输入 1 2 样例输出 3 My code // CPPConsoleApplication1.cpp : Defines the entry point for the console application. // #include "stdafx.h" #include <stdio.h> #include <stdlib.h> #include <string.h> #include <ctype.h> /* 3 4 7 */ #define BLANK 32 #define MAX 1024 extern int isdigit(int c); extern int isspace(int c); extern char* strcpy(char* _Dest, const char* _Source); extern char* strncpy(char* _Dest, const char* _Source, size_t _Count); extern long strtol(const char* _Str, char** _EndPtr, int _Radix); extern int strcmp(const char* s1, const char* s2); int islegalnum(int num) { return (num<=0 || num>=1000) ? 0 : 1; } bool islegalchar(char c) { if(isdigit(c) || isspace(c)) return true; return false; } int GetInputString(char* allocted) { char _str[MAX]; int len = 0; char temp; //get input string. do { _str[len] = getchar(); if(!islegalchar(_str[len])) { //clear getcahr buffer while(getchar()!='/n'); return -1; } }while(_str[len++]!='/n'); _str[len] = 0; strcpy(allocted, _str); return len; } int GetInputNumber(char* s, int* outNum1, int* outNum2) { char* tailptr; char* temphead; *outNum1 = strtol(s, &tailptr, 0); temphead = tailptr; *outNum2 = strtol(temphead, &tailptr, 0); temphead = tailptr; strtol(temphead,&tailptr,0); if(strcmp(temphead, tailptr)!=0) return -1; } int main() { char* s = (char*)malloc(sizeof(char)*MAX); int number1, number2; while(GetInputString(s)==-1) printf("You MUST input 2 numbers, and the value of them MUST between 0~10000/r/n"); if(GetInputNumber(s, &number1, &number2)==-1) { printf("You MUST input 2 numbers, and the value of them MUST between 0~10000/r/n"); return -1; } if(islegalnum(number1)&&islegalnum(number2)) { printf("Result: %d/r/n", number1 + number2); } else { printf("You MUST input 2 numbers, and the value of them MUST between 0~10000"); return -1; } return 0; }