这是一个计算器,我想用异常来处理输入字母的情况,但是弄不好,望回答(复制过来后所有的中文都变成符号了
#include<bits/stdc++.h>
#define A INT_MAX
#define B INT_MIN
int i,t;
int jiecheng(int a,int b){//½×³Ëº¯ÊýÉùÃ÷
int i,sum=1;
for(i=a;i<=b;i++)
{
sum=sum*i;
}
return sum;
}
int zdgys(int a,int b){//×î´ó¹«ÒòÊýº¯ÊýÉùÃ÷
if(a<b)
{
t=a;
a=b;
b=t;
}
for(i=a;i>=1;i--)
{
if(a%i==0&&b%i==0)
{
return i;
break;
}
}
}
int zxgbs(int a,int b){//×îС¹«±¶Êýº¯ÊýÉùÃ÷
if(a<b)
{
t=a;
a=b;
b=t;
}
for(i=a;i>0-10e10;i++)
{
if(i%a==0&&i%b==0)
{
return i;
break;
}
}
}
double pingjunshu(int a,int b){//ƽ¾ùÊýº¯ÊýÉùÃ÷
double c=(a+b)/2.0;
return c;
}
int main()
{
std::cout<<"ÌØÊâ˵Ã÷: 1¡¢½×³Ë·ûºÅΪ! 2¡¢×î´ó¹«ÒòÊý·ûºÅΪ# 3¡¢×îС¹«±¶Êý·ûºÅΪ@ 4¡¢ÊäÈë0 c 0Çå³ý¼ÆËãºÛ¼£ 5¡¢´Î·½¸ù·ûºÅΪ$ 6¡¢²ÎÓë¼ÆËãµÄ×î´óֵΪ"<<A<<" "<<"7¡¢²ÎÓë¼ÆËãµÄ×îСֵΪ"<<B<<" "<<"9¡¢Æ½¾ùÊý·ûºÅΪ& a";
try{
while(1)
{
char ch;
int b,c;
std::cin>>b>>ch>>c;
if(ch!='c'&&ch!='+'&&ch!='-'&&ch!='/'&&ch!='*'&&ch!='!'&&ch!='#'&&ch!='@'&&ch!='$'&&ch!='^'&&ch!='&')
std::cout<<"[Error] "<<ch<<"²»ÊǼÆËãÆ÷¹æ¶¨µÄÔËËã·ûºÅ a";
char y[27]{"qwertyuiopasdfghjklzxcvbnm"};
char a=b,d=c;
for(int i=0;i<26;i++)
if(a==y[i])
{
std::cout<<"[Error] "<<b<<"²»ÊÇÊý×Ö£¡ a";
throw -1;
}
else
if(d==y[i])
{
std::cout<<"[Error] "<<c<<"²»ÊÇÊý×Ö£¡ a";
throw -1;
}
if(ch=='c')
{
system("cls");
std::cout<<"ÌØÊâ˵Ã÷: 1¡¢½×³Ë·ûºÅΪ! 2¡¢×î´ó¹«ÒòÊý·ûºÅΪ# 3¡¢×îС¹«±¶Êý·ûºÅΪ@ 4¡¢ÊäÈë0 c 0Çå³ý¼ÆËãºÛ¼£ 5¡¢´Î·½¸ù·ûºÅΪ$ a";
}
if(ch=='+')
std::cout<<b<<" + "<<c<<"="<<b+c<<" a";
if(ch=='-')
std::cout<<b<<" - "<<c<<"="<<b-c<<" a";
if(ch=='*')
std::cout<<b<<" x "<<c<<"="<<b*c<<" a";
if(ch=='/')
std::cout<<b<<" ¡Â"<<c<<"="<<b/(double)c<<" a";
if(ch=='^')
std::cout<<b<<"µÄ"<<c<<"´Î·½ÊÇ"<<pow(b,c)<<" a";
if(ch=='$')
std::cout<<b<<"µÄ"<<c<<"´Î·½¸ùÔ¼ÊÇ"<<pow(b,1.0/c)<<" a";
if(ch=='!')
std::cout<<"´Ó"<<b<<"³Ëµ½"<<c<<"µÄ»ýÊÇ"<<jiecheng(b,c)<<" a";
if(ch=='#')
std::cout<<b<<"ºÍ"<<c<<"µÄ×î´ó¹«ÒòÊýÊÇ"<<zdgys(b,c)<<" a";
if(ch=='@')
std::cout<<b<<"ºÍ"<<c<<"µÄ×îС¹«±¶ÊýÊÇ"<<zxgbs(b,c)<<" a";
if(b+c>A||b>A||c>A)
std::cout<<"[Error] ²ÎÓë¼ÆËãÁ¿Òѳ¬¹ý×î´óÖµ a";
if(b+c<B||b<B||c<B)
std::cout<<"[Error] ²ÎÓë¼ÆËãÁ¿Òѳ¬¹ý×îСֵ a";
if(ch=='&')
std::cout<<b<<"ºÍ"<<c<<"µÄƽ¾ùÊýÊÇ"<<pingjunshu(b,c)<<" a";
}
}
catch(...){
return 0;
}
}
1。C++直接有判断数字(isdigit),字母(isalpha),大写(isupper),小写(islower)函数的,大不了也可以直接用范围判断的,如
if (ch>='a' && ch<='z') //判小写字母
不可能去用数组的
2。你的
char a=b,d=c;
for(int i=0; i<26; i++)
if(a==y[i])
判断不正确,要注意数字与数值的区别
你输入的如123456,它是数值,不能赋用字符来判断的,a=b是将也就是123456(且是截断的)变成ASCII码了,它不能与字母判断的