matlab 实现求string表达式的值

一.函数实现目标

写一个Matlab函数 function ret = myeval(str)计算表达式str的值。
str是一个算术表达式,即两个正整数的加法,减法,乘法和除法。
例如,myeval(’ 12+23 ‘)将执行返回35,myeval(’ 23-12 ‘)将返回11,myeval(’ 12*11 ‘)将返回132,myeval(’ 12/4 ')将返回3。

二.额外要求

(1) 其中可以多一个空格表达式,应该忽略它。
例如,“12 + 23”等于“12 + 23”。
(2)不能使用Matlab函数eval或num2str等现有的函数。

三.整体思路

核心思想:依靠ASCII码解决
step1:将输入的字符串转化成ASCII码
step2:检测有无空格,并将除了空格之外的字符的ASCII码保存到 ls[]
step3:迭代 ls[],将第一个数的各个位保存到num1[],当检测到操作符时,将其保存到operator[], 并且跳出循环。
step4: 将第二个数的(ls[]中除了第一个数和操作符)各个位保存到num2[]
step5: 根据num1和num2计算这两个数的真实值Number1, Number2
ASCII码表
根据ASCII码表,我们可以知道字符0对应的ASCII码是48,所以将字符的ASCII码减去48即可得到各个位的真实值,然后再根据在哪一位,计算出两个数字的真实值。
step6: 根据操作符operator[]判断是哪种运算并计算出结果rat

四.完整代码

function ret = myeval(str)
% MYEVAL: calculate the addition, subtraction, multiplication and division of two Numbers entered as a string 
% The core idea is to use ASCII code to distinguish between two numeric and operator types 

% Converts the input string to ASCII code
str = abs(char(str));
ls = [];
% The number of number of the ASCII code of the string except space and set it to 1 initially
lsEND = 1;
for i = str
   % All but Spaces (32 for ASCII code) are added to ls
   if i ~= 32
       ls(lsEND)= i ;
       lsEND = lsEND + 1;
   end
end

num1 = [];
% The number of number of the first number and set it to 1 initially
num1END = 1;
operator = [];
for i = ls
   % Store each digit of the first number in num1
   if i < 48 || i > 57
       % Save the ASCII code for the operator and jump out of the for-loop
       operator(1) = i;
       break
   end
   num1(num1END) = i;
   num1END = num1END + 1;
  
end

% Calculate the true value of the first number
Number1 = 0;
for m = 1:(num1END - 1)
    % The ASCII - 48 of the numeric character gets the true value of the number, and then multiplies the corresponding number of digits
    Number1 = Number1 + (num1(m) - 48) * 10^(num1END - 1 - m);
end

num2 = [];
% The number of number of the second number and set it to 1 initially
num2END = 1;
for j = ls(num1END + 1) : ls(end)
   % Store each digit of the second number in num2
   num2(num2END) = j;
   num2END = num2END + 1;
end

% Calculate the true value of the second number
Number2 = 0;
for n = 1:(num2END - 1)
    % The ASCII - 48 of the numeric character gets the true value of the number, and then multiplies the corresponding number of digits
    Number2 = Number2 + (num2(n) - 48) * 10^(num2END - 1 - n);
end

% According to the ASCII code of the operator, what operation is to be performed and operate
% '+': 43; '-': 45; '*': 42; '/': 47;
switch operator(1)
    case 43
        ret = Number1 + Number2;
    case 45
        ret = Number1 - Number2;
    case 42
        ret = Number1 * Number2;
    case 47
        ret = Number1 / Number2;
end
end
  • 2
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 0
    评论
MATLAB的正则表达式(Regular Expressions)是一种强大的文本搜索和处理工具,可以在字符串中匹配特定的模式。 MATLAB中的正则表达式可以使用以下函数进行处理: 1. regexp:用于在字符串中查找正则表达式的匹配项,并返回匹配项的位置和子字符串。 2. regexprep:用于在字符串中查找正则表达式的匹配项,并将其替换为指定的字符串。 3. regexpi:与regexp函数类似,但是不区分大小写。 4. regexprep:与regexprep函数类似,但是不区分大小写。 5. regextranslate:将MATLAB字符串转换为正则表达式字符串。 在MATLAB中,正则表达式的语法与其他语言中的正则表达式语法类似。以下是一些常用的正则表达式语法: 1. ^:匹配输入字符串的开始位置。 2. $:匹配输入字符串的结束位置。 3. .:匹配除换行符以外的任何单个字符。 4. *:匹配前面的字符零次或多次。 5. +:匹配前面的字符一次或多次。 6. ?:匹配前面的字符零次或一次。 7. []:表示一个字符集,匹配其中的任何一个字符。 8. [^]:表示一个反向字符集,匹配未在其中的任何一个字符。 9. ():表示一个子表达式,可以在表达式中引用。 例如,以下代码将查找字符串中所有以大写字母开头的单词,并将它们替换为“Word”: str = 'This is a Test String'; newStr = regexprep(str, '\<([A-Z][a-z]*)\>', 'Word'); 输出结果为: newStr = 'Word is a Word Word'
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

不务正业的小文同学

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值