对于一个字符型(char)变量,可以根据ascii码值的范围来判断是否为字母或数字。其原理为,在ascii码中,大写字母,小写字母和数字,分别都是连续的。所以对于char c, 如果满足 c>= '0' && c <= '9' 那么就是数字。大小写字母类似。如果经常使用这样的判断,可以封装为宏定义。判断数字:#define SHUZI(c) (c>= '0' && c <= '9' )#define DAXIE(c) (c>= 'A' && c <= 'Z' )#define XIAOXIE(c) (c>= 'a' && c <= 'z' )如果不关心大小写,只是判断字母可以写作:#define ZIMU(c) (DAXIE(c) || XIAOXIE(c))其含义为,如果c是大写字母或者是小写字母,那么c就是字母。
你好,由于不大清楚你的诉求,我就帮你写了一个简单的加法计算工具 希望能对你有一些帮助 //再次修正版 #include<iostream>#include<cstdlib>#include<cstring>using namespace std;int main(int argc,char *argv[]){int flag ,i;double a,b;char num[3][80]={"",""}; if(argc!=3){cout<<"int main(int argc,char *argv[]) 请输入两个参数,程序即将返回。\n";system("Pause");return 1;}for(int s=1;s<=2;s++){ flag = 0;i=0;for(;i<strlen(argv[s]);i++){ if(argv[s][i]=='0')strcat(num[s],"0"); else if(argv[s][i]=='1')strcat(num[s],"1"); else if(argv[s][i]=='2')strcat(num[s],"2"); else if(argv[s][i]=='3')strcat(num[s],"3"); else if(argv[s][i]=='4')strcat(num[s],"4"); else if(argv[s][i]=='5')strcat(num[s],"5"); else if(argv[s][i]=='6')strcat(num[s],"6"); else if(argv[s][i]=='7')strcat(num[s],"7"); else if(argv[s][i]=='8')strcat(num[s],"8"); else if(argv[s][i]=='9')strcat(num[s],"9"); else if(argv[s][i]=='.'&&(i>0)&&(flag<2)){ strcat(num[s],".");flag++;} else {cout<<"请输入正确的数据类型,程序即将返回。\n";system("Pause");return 1;} }} if(atoi(num[1])&&(atoi(num[1])==atof(num[1]))){cout<<"int类型,其值为:" <<atoi(num[1]);a=atoi(num[1]);} else if(atof(num[1])){cout<<"double类型,其值为:"<<atof(num[1]);a=atof(num[1]);} if(atoi(num[2])&&(atoi(num[2])==atof(num[2]))){cout<<"\nint类型,其值为:" <<atoi(num[2]);b=atoi(num[2]);} else if(atof(num[2])){cout<<"\ndouble类型,其值为:"<<atof(num[2]);b=atof(num[2]);}cout<<"\na + b = "<<(a+b)<<"\n";system("Pause"); return 0;}