c语言one two 3,one+two=3 此题感觉没什么头绪,所以发帖求指教求交流!

#include

#include

#define N 10

#define M 10

#define MAXSTRING 30

typedef struct LinkList

{

char words[M];

struct LinkList *next;

}*WordsList;

const char *s[N] = {

"zero", "one", "two", "three", "four","five",

"six", "seven", "eight", "nine"

};

int check_loc(char *);

int words_plus(char *, WordsList );

void print_list(WordsList );

int main()

{

char input[MAXSTRING], check;

WordsList wordslist, list_p, list_q;

int index, ct, result;

result = 0;

do{

puts("输入等式(输入zero + zero = 结束)");

list_p = wordslist = (WordsList )malloc(sizeof(struct LinkList));

list_q = (WordsList )malloc(sizeof(struct LinkList)); //构造链表

fgets(input, MAXSTRING, stdin);

for(ct = index = 0; input[index] != '\0'; index++)  //将字符装入链表一个节点中,遇到空格结束,即每个节点装一个单词

{

check = input[index];

if(check != ' ')

{

list_q->words[ct] = check;

ct++;

if(check == '=')            //如果是‘=’,装完后直接跳出了

{

list_q->words[ct] = '\0';

list_p->next = list_q;

list_p = list_q;

break;

}

}

else

{

list_q->words[ct] = '\0'; //链表后移的操作

list_p->next = list_q;

list_p = list_q;

ct = 0;

list_q = (WordsList )malloc(sizeof(struct LinkList));

}

}

list_p->next = NULL;

print_list(wordslist);   //打印表达式

result = words_plus(input, wordslist);

if(result != 0)

{

printf("%d\n", result);

}

free(wordslist);

}while(result != 0);

return 0;

}

int check_loc(char *p)        //将单词转化为数字

{

int index;

for(index = 0; index < N; index++)

{

if(strcmp(p, s[index]) == 0)

{

return index;

}

}

}

void print_list(WordsList wordlist)

{

WordsList list_p = wordlist->next; //跳过 ‘+’

while(list_p != NULL)

{

printf("%s ", list_p->words);

list_p = list_p->next;

}

}

int words_plus(char *input, WordsList wordslist) //实现数字相加

{

int index, n1, n2;

WordsList list_p = wordslist->next;

n1 = n2 = 0;

while(strcmp(list_p->words, "+") != 0)

{

n1 = n1 * 10 + check_loc(list_p->words);

list_p = list_p->next;

}

list_p = list_p->next;

while(strcmp(list_p->words, "=") != 0)

{

n2 = n2 * 10 + check_loc(list_p->words);

list_p = list_p->next;

}

return (n1+n2);

}

//不知为什么贴代码贴不上,lz凑合着看吧,代码没规范写,没有精简、、没有容错(只能zero + zero =结束,不然出错。而且单词输入错误也没有提示以及重新输入!当然,lz想有更好的效果,自己写写吧)

算法就是这样吧。。

这是网上的 和楼主一起学习!

评分

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值