计算任意两个正整数A与B的和.要求A和B的每一位数字由对应的英文单词表示参与计算

2024-11-19 19:41:32
推荐回答(2个)
回答1:

把字符串转成数字,然后计算

回答2:

#include
#include "stdafx.h"
#include
#include
#include
#include
#include
#include
#include
#include "mcstring.h"
using std::string;
using std::cin;
using std::cout;
using std::endl;
using std::set;
using std::vector;

unsigned ctu(const string&r){
static string num[]={"zero","one","two","three","four","five","six","seven","eight","nine"};
for(unsigned i=0;i<10;i++){
if(r==num[i]){
return i;
}
}
return 0x10;
}
int _tmain()
{
cout<<"输入:"< string str("0");
vector value;
while(!str.empty()){
do{
cin>>str;
}while(str.empty());
value.push_back(str);
if(str[0]=='=')break;
}
vector temp;
unsigned long a=0,b=0;
bool flag=false;
for(size_t i=0;i switch(value[i][0]){
case '+': if(flag){flag=false;temp.push_back(a);a=0;temp.push_back(0x10000000);}break;
case '-': if(flag){flag=false;temp.push_back(a);a=0;temp.push_back(0x20000000);}break;
case '*': if(flag){flag=false;temp.push_back(a);a=0;temp.push_back(0x30000000);}break;
case '/': if(flag){flag=false;temp.push_back(a);a=0;temp.push_back(0x40000000);}break;
case '&': if(flag){flag=false;temp.push_back(a);a=0;temp.push_back(0x50000000);}break;
case '|': if(flag){flag=false;temp.push_back(a);a=0;temp.push_back(0x60000000);}break;
case '=': if(flag){flag=false;temp.push_back(a);a=0;temp.push_back(0x70000000);}break;
default:
flag=true;
b=ctu(value[i]);
if(b&0x10){
cout<<"检测出一处错误:“"< b=0;
}
a=10*a+b;
}
}
for(size_t i=0;i if(temp[i]==0x30000000){
temp[i+1]=temp[i+1]*temp[i-1];
temp[i-1]=0x80000000;
i++;
}else if(temp[i]==0x40000000){
if(temp[i+1]==0){
cout<<"检测一处错误:除数为0,默认一处理"< temp[i+1]=1;
}
temp[i+1]=temp[i-1]/temp[i+1];
temp[i-1]=0x80000000;
i++;
}
}
for(size_t i=0;i if(temp[i]==0x10000000){
int j=1;
while(temp[i+j]&0xf0000000&&!(temp[i+j]&0xf000000))j++;
temp[i+j]=temp[i+j]+temp[i-1];
temp[i-1]=0x80000000;
i=i+j;
}else if(temp[i]==0x20000000){
int j=1;
while(temp[i+j]&0xf0000000&&!(temp[i+j]&0xf000000))j++;
temp[i+j]=temp[i-1]-temp[i+j];
temp[i-1]=0x80000000;
i=i+j;
}
}
for(size_t i=0;i if(temp[i]==0x50000000){
int j=1;
while(temp[i+j]&0xf0000000&&!(temp[i+j]&0xf000000))j++;
temp[i+j]=temp[i+j]&temp[i-1];
temp[i-1]=0x80000000;
}else if(temp[i]==0x60000000){
int j=1;
while(temp[i+j]&0xf0000000&&!(temp[i+j]&0xf000000))j++;
temp[i+j]=temp[i+j]|temp[i-1];
temp[i-1]=0x80000000;
}
}
vector tmp;
for(size_t i=0;i if((temp[i]&0xf0000000)){
if(temp[i]&0xf000000)tmp.push_back(temp[i]);
}
if(temp[i]==0x70000000){
tmp.push_back(temp[i]);
}
}
for(size_t i=0;i if(tmp[i]==0x70000000)cout<<"=";
else cout< }
cout< getchar();
return 0;
}
//参考下,搞了差不多两个小时,不想改了