题目1010:A + B------------------------此种输入方法令我打开眼界

本文介绍了一种将英文单词表示的数字转换为实际数值的算法实现。通过使用C语言,该程序能够解析如“one+two=three”这样的字符串,并正确计算出结果。文章详细展示了如何构建查找表以及解析输入字符串的过程。

主要就是输入一长串的字符,如何从中摘出单个的字符换成数字:

AC:

/**********************************/
令人头疼的是如何输入这些:
one + two =
three four + five six =
zero seven + eight nine =
zero + zero =
通过下面这道题,学习了好多
/**********************************/


#include<stdio.h> #include<cstring> char str[10][8]={"zero","one","two","three","four","five","six","seven","eight","nine"}; int find(char *tem) { int i=0; for (i=0;i<10;i++) if (strcmp(tem,str[i])==0) return i; return 0; } int main() { char temp[10]; while(scanf("%s",&temp)!=EOF) { int t1=0,t2=0; while(strcmp(temp,"+")!=0) {t1=t1*10+find(temp); scanf("%s",&temp); } scanf("%s",&temp); while(strcmp(temp,"=")!=0) {t2=t2*10+find(temp); scanf("%s",&temp); } if (t1==0 && t2==0) break; else printf("%d\n",t1+t2); } return 0; }

 

转载于:https://www.cnblogs.com/jianrenguo/p/6489564.html

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值