一个简单的关于字符串匹配与转换的程序

本文介绍了一种将特定格式的字符串转换为数字的方法,并通过C语言实现了一个具体例子。主要解决的问题是如何将形如“onetwo*onetwo=”的字符串转换成对应的数学表达式并求值。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

    大家好,好久不见啦~~以后我会更加频繁地更新博客,欢迎大家关注哦~~大笑,我的目标是:只做原创

    好啦~切入正题。

    近期看到了一道题目,感觉挺有意思,想和大家分享交流。

                                                   题目重述

     输入一串字符串,字符串的内容形如“one two *one two =”,要求返回其结果(就“one two *one two =”而言,返回144)。

                                                          问题分析与程序结构

    读完题目,便可以分析出,该题目的根本难点与重点就在将字符串形式的数字转换为阿拉伯数字,并将各阿拉伯数字相乘,进而返回结果。
    于是,对于整个程序,便可以分为三部分: 获得输入字符串、对字符串进行识别并转换、输出结果。

                                                               重点算法讲解

    这个题的重点在于如何 把一个形如“one two three”的字符串转换为形如123的阿拉伯数字,那么,我们可以先设结果res=0;再将字符串进行遍历,如果遇到空格,则将之前扫描的字符串与one  two three four.......进行匹配,进而得到该位数的值m,将res的结果更新为res=res*10+m。那么,整个数就可以被恢复出来并被转换为阿拉伯数字啦~~
                                                                   C语言实现
    
    个人认为这个题的乐趣就在于于过程分析与实现,所以便选择了C语言来实现。
      代码如下:
      
#include<stdio.h>
#include<stdlib.h>
#include<string.h>
typedef struct data
{
char input;//输入的字符
struct data *next;
}data;
char match[10][10]={"zero","one","two","three","four","five","six","seven","eight","nine"};//匹配数组
data * head;//头结点
void get_input();//建立输入队列
int *result;//转换后队列,如:one two *one three =  则,result【0】=12,result【1】=13
int number_of_data=0;//转换后队列的长度
void get_match();//匹配过程
void print_res();//输出结果
int main()
{
    head=(data *)malloc(sizeof(data));
    head->next=NULL;
    char ch;
    get_input();
    result=(int *)calloc(sizeof(int),number_of_data);
    get_match();
    print_res();
}


void get_input()
{
    char ch;
    data *now,*t;
    now=head;
    while ((ch=getchar())!='\n')
    {
         t=(data *)malloc(sizeof(data));
         t->input=ch;
         t->next=NULL;
         now->next=t;
         now=t;
         if (ch == '*')
            number_of_data++;
    }
    number_of_data++;
}


void get_match()
{
    char ch[10],c;
    data *t;
    int i=0,index=0,res_temp=0;
    if (head->next!=NULL)
    {
       t=head->next;
       while ((c=t->input)!='=')
       {
           index=0;
           while ((c=t->input)!=' '&&c!='*'&&c!='=')
           {
               ch[index]=c;
               index++;
               t=t->next;
           }
           if (c==' ')
           {
           ch[index]='\0';
           int k;
           for ( k=0;k<10;k++) 
              if (strcmp(match[k],ch)==0)
                   break;
           res_temp=10*res_temp+k;
           int l=0;
           for ( l=0;l<index;l++)
               ch[l]='\0';
           }
           if (c=='*'||c=='=')
           {
              result[i]=res_temp;
              i++;
              res_temp=0;
              printf("no.%d  :  %d  \n",i,result[i-1]);
           } 
           if (c=='=')
              break;
           t=t->next;
       }
           result[i]=res_temp;
           i++;
           printf("no.%d  :  %d  \n",i,result[i-1]);
    }   
}


void print_res()
{
      int i=0;
      int re=1;
      for (i=0;i<number_of_data;i++)
        re=re*result[i];
      printf("%d\n",re);
}



     
                         
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值