请C++高手帮忙解决一下!!谢谢!!

nacie 2007-11-13 06:14:21
想把要一个被当作字符串处理的运算符再恢复到原来可运算的功能该怎么做?
比如: string s="*";
int i=3,j=6;
怎么样运算3*6呢?
...全文
147 11 打赏 收藏 转发到动态 举报
写回复
用AI写文章
11 条回复
切换为时间正序
请发表友善的回复…
发表回复
AlwaysSLH 2007-11-17
  • 打赏
  • 举报
回复
学习中
另外很喜欢shunwent的头像呀,鸣人那刚毅的眼神总能给人力量
从蓝田到元谋 2007-11-17
  • 打赏
  • 举报
回复
6楼的没那么复杂吧
shunwent 2007-11-14
  • 打赏
  • 举报
回复
回复9楼还有10楼的
楼主应该不是这个意思吧
如果输入字符串"1+2*(3-1)"
按照你们的方法 应该可很难得到答案吧
何哀何欢 2007-11-13
  • 打赏
  • 举报
回复
string s= "*";
int i=3,j=6;

if(s=="*") return i*j;
if(s=="+") return i+j
if...
jiaqiangscut00 2007-11-13
  • 打赏
  • 举报
回复
当你处理 A*B的时候你可以 检测到*时 你就把 AB*先 存储起来 然后 你回调的时候 检测到*的时候就可以 用int atoi ( const char * str ); 把前面的俩个字符转化为 数字进行计算了
shunwent 2007-11-13
  • 打赏
  • 举报
回复
可以的不过我有的功能不多
只是有这些 sin cos + - * / id ( ) 运算符而已
代码如下
我是采用算符优先方法写的
#include <iostream>
#include <string>
#include <cmath>
using namespace std;
char s[200]; // 表达式
struct node
{
int kind;
double num;
bool mark;
}mystack[100];
int len;
int lens;
bool end;
// sin cos + - * / id ( ) $
int con[10][10]= // 1 is '>' , 0 is '=' , -1 is '<' , -2 is invalid
{
-1, -1, 1, 1, 1, 1, -1, -1, 1, 1,
-1, -1, 1, 1, 1, 1, -1, -1, 1, 1,
-1, -1, 1, 1, -1, -1, -1, -1, 1, 1,
-1, -1, 1, 1, -1, -1, -1, -1, 1, 1,
-1, -1, 1, 1, 1, 1, -1, -1, 1, 1,
-1, -1, 1, 1, 1, 1, -1, -1, 1, 1,
-2, -2, 1, 1, 1, 1, -2, -2, 1, 1,
-1, -1, -1, -1, -1, -1, -1, -1, 0, -2,
-2, -2, 1, 1, 1, 1, -2, -2, 1, 1,
-1, -1, -1, -1, -1, -1, -1, -1, -2, -2
};
// 找出最近的一个终结符号
int findnear()
{
for(int i=lens-1;i>=0;i=i-2)
{
// 找出从右边数起第一个终结符号
if(mystack[i].mark==false)
return i;
}
}
bool process(int now)
{
// near保存右边数起第一个终结符号
int nearid=findnear();
int nearone=mystack[nearid].kind;
if(nearone==9 && now==9) return true;
int h=con[nearone][now];
if(h==-1 || h==0)
{
mystack[lens].kind=h;
mystack[lens+1].kind=now;
mystack[lens+1].mark=false; // 新符号赋值为终止符号
lens=lens+2;
}
else if(h==2)
{
cout<<"该表达式不合法"<<endl;
return false;
}
else if(h==1)
{
int j;
switch(nearone)
{
case 0: // sin
mystack[nearid].num=sin(mystack[nearid+2].num);
mystack[nearid].mark=true;
lens=lens-2;
process(now);
break;
case 1: // cos
mystack[nearid].num=cos(mystack[nearid+2].num);
mystack[nearid].mark=true;
lens=lens-2;
process(now);
break;
case 2: // +
mystack[nearid-2].num=mystack[nearid-2].num+mystack[nearid+2].num;
mystack[nearid-2].mark=true;
lens=lens-4;
process(now);
break;
case 3: // -
mystack[nearid-2].num=mystack[nearid-2].num-mystack[nearid+2].num;
mystack[nearid-2].mark=true;
lens=lens-4;
process(now);
break;
case 4: // *
mystack[nearid-2].num=mystack[nearid-2].num*mystack[nearid+2].num;
mystack[nearid-2].mark=true;
lens=lens-4;
process(now);
break;
case 5: // /
mystack[nearid-2].num=mystack[nearid-2].num/mystack[nearid+2].num;
mystack[nearid-2].mark=true;
lens=lens-4;
process(now);
break;
case 6: // id
mystack[lens-1].mark=true;
process(now);
break;
case 7:
return false;
case 8:
if(mystack[nearid-4].kind!=7)
return false;
else
{
mystack[nearid-4].num=mystack[nearid-2].num;
mystack[nearid-4].mark=true;
lens=lens-4;
process(now);
}
break;
}
}
return true;
}
void readdata()
{
cout<<"输入表达式:"<<endl;
cin>>s;
len=strlen(s);
int i,j,now;
// 初始化
mystack[0].kind=9;
mystack[0].mark=false;
lens=1;
// for(i=0;i<100;i++) mystack[i].mark=false;
end=false;
// 逐个字符判断
for(i=0;i<len;i++)
{
double sum=0.0;
if(s[i]<='9' && s[i]>='0')
{
for(j=i;j<len;j++)
{
if(s[j]<='9' && s[j]>='0')
{
sum=sum*10.0+s[j]-'0';
if(j==len-1)
{
mystack[lens].kind=-1;
mystack[lens+1].kind=6;
mystack[lens+1].mark=false;
mystack[lens+1].num=sum;
lens+=2; now=9;
process(now);
break;
}
}
else
{
i=j-1;
mystack[lens].kind=-1;
mystack[lens+1].kind=6;
mystack[lens+1].mark=false;
mystack[lens+1].num=sum;
lens+=2; now=6;
break;
}
}
}
else
{
switch(s[i])
{
case 's':
if(i+2>=len) end=true;
else if(s[i+1]=='i' && s[i+2]=='n')
{
i=i+2; now=0;
}
else end=true;
break;
case 'c':
if(i+2>=len) end=true;
else if(s[i+1]=='o' && s[i+2]=='s')
{
i=i+2; now=1;
}
else end=true;
break;
case '+':
now=2; break;
case '-':
now=3; break;
case '*':
now=4; break;
case '/':
now=5; break;
case '(':
now=7; break;
case ')':
now=8; break;
default:
return;
}
if(lens==1)
{
mystack[lens].kind=-1;
mystack[lens+1].kind=now;
mystack[lens+1].mark=false;
lens=lens+2;
}
else if(process(now)==false)
end=true;
}
}
now=9;
if(process(now)==false)
end=true;
}
int main()
{
char w='y';
while(w=='y')
{
readdata();
if(end) cout<<"表达式非法"<<endl;
else cout<<"结果是: "<<mystack[2].num<<endl;
cout<<"继续输入请按'y',否则按其他键"<<endl;
cin>>w;
}
}
nacie 2007-11-13
  • 打赏
  • 举报
回复
回复四楼:
谢谢啊 我就是在做编译原理实验呢 现在这里遇到问题了 我把*当字符串处理了 可是现在想要计算了 结果它还是字符串格式 我不知道该怎么样再找回它原来的功能 如果你有代码 还是麻烦你发我看下吧 谢谢!!
shunwent 2007-11-13
  • 打赏
  • 举报
回复
楼主的意思是这样吗
比如输入字符串“1+2*2” 然后输出的结果就是5
是不是这样啊
这个是相当复杂的一个程序来的
涉及到编译原理的知识吧

这个我当时有做过 使用的是算符优先的方法来做的
基本原理就是维护一个算符优先表
比如字符串"1+2*2" 涉及到的算符有
value(表示数值) '+' 以及 '*'
他们的算符优先表如下:

value '+' '*'
value > >
+ < > <
* < > >

所以当有运算式子为"1+2*2"
可以添加算符优先如下:
在头部加上'<',在尾部加上'>',变成
<1+2*2>
然后再在中间根据算符优先表,添加合适的算符,变成
<1>+<2>*<2>
然后对于每一个结构为<>的形式进行归约,变成
<temp1+temp2*temp3>
不考虑中间变量tempi
则原式子剩下的是
<+*>
添加算符后,变成
<+<*> 由于 + < *
也就是 <temp1+<temp2*temp3>
然后又归约<temp2*temp3>变成:
<temp1+temp2>
这样就可一归约这个,最终得出最终的结果

如果你要代码,我可以给你
独孤过儿 2007-11-13
  • 打赏
  • 举报
回复
不好意思,看错题了,回复作废...

那就像楼上的方法吧
loops 2007-11-13
  • 打赏
  • 举报
回复
if (s=="*") cout<<3*6;
独孤过儿 2007-11-13
  • 打赏
  • 举报
回复
int atoi ( const char * str );  <cstdlib> 

Convert string to integer

Parses the C string str interpreting its content as an integral number, which is returned as an int value.

The function first discards as many whitespace characters as necessary until the first non-whitespace character is found. Then,

starting from this character, takes an optional initial plus or minus sign followed by as many numerical digits as possible,

and interprets them as a numerical value.

The string can contain additional characters after those that form the integral number, which are ignored and have no effect on

the behavior of this function.

If the first sequence of non-whitespace characters in str is not a valid integral number, or if no such sequence exists because

either str is empty or it contains only whitespace characters, no conversion is performed.


Parameters
str
C string beginning with the representation of an integral number.

Return Value
On success, the function returns the converted integral number as an int value.
If no valid conversion could be performed, a zero value is returned.
If the correct value is out of the range of representable values, INT_MAX or INT_MIN is returned.

Example
/* atoi example */
#include <stdio.h>
#include <stdlib.h>

int main ()
{
int i;
char szInput [256];
printf ("Enter a number: ");
fgets ( szInput, 256, stdin );
i = atoi (szInput);
printf ("The value entered is %d. The double is %d.\n",i,i*2);
return 0;
}


Output:

Enter a number: 73The value entered is 73. The double is 146.


64,282

社区成员

发帖
与我相关
我的任务
社区描述
C++ 语言相关问题讨论,技术干货分享,前沿动态等
c++ 技术论坛(原bbs)
社区管理员
  • C++ 语言社区
  • encoderlee
  • paschen
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告
  1. 请不要发布与C++技术无关的贴子
  2. 请不要发布与技术无关的招聘、广告的帖子
  3. 请尽可能的描述清楚你的问题,如果涉及到代码请尽可能的格式化一下

试试用AI创作助手写篇文章吧