int Calculate(char x)//计算偏移 { if(x>=97&&x<=122) return 0; switch(x) { case '!': case '^': case '(': case ')': return 1; case '|': case '-': return 2; case '<': return 3; default: return 1; } }这个代码的作用
时间: 2024-02-14 16:35:42 浏览: 16
`Calculate(char x)` 函数的作用是根据输入的字符 `x`(运算符或操作数)计算偏移量,即该字符在输出时占用的空格数。具体实现如下:
1. 如果 `x` 是操作数(即小写字母),则返回 0,因为操作数不需要占用空格。
2. 如果 `x` 是以下运算符之一:`!`(非)、`^`(异或)、`(`(左括号)或 `)`(右括号),则返回 1,因为它们占用一个空格。
3. 如果 `x` 是以下运算符之一:`|`(或)或 `-`(蕴含),则返回 2,因为它们占用两个空格。
4. 如果 `x` 是 `<`(双向蕴含),则返回 3,因为它占用三个空格。
5. 如果 `x` 不是以上任何一种情况,则返回 1,表示该字符占用一个空格。
这样,通过调用 `Calculate(char x)` 函数,就可以在输出命题公式时保证各个字符之间的间隔正确。
相关问题
#include <iostream> #include <stack> using namespace std; int calculate(int a,char b,int c) { switch(b) { case '+' : return a+c; case '-' : return a-c; case '*' : return a*c; case '/' : return a/c; } return 0; } int main() { stack<int>opnd; string s; int i; getline(cin,s); int num=0; for(i=0; s[i]!='\0'; i++) { if(s[i]=='@') break; else if(s[i]>='0'&&s[i]<='9') { num=num*10+(s[i]-48); } else if(s[i]=' '&&s[i-1]>='0'&&s[i-1]<='9') { opnd.push(num); num=0; } else if(s[i]=='+'||s[i]=='-'||s[i]=='*'||s[i]=='/') { int a=opnd.top(); opnd.pop(); int b=opnd.top(); opnd.pop(); opnd.push(calculate(b,s[i],a)); } } cout<<opnd.top(); return 0; }哪里有问题
在代码中,第10行的判断条件有问题,应该改为`s[i]==' '&&s[i-1]>='0'&&s[i-1]<='9'`,即如果当前字符是空格并且前一个字符是数字,则将数字累加到num中,并将num作为一个操作数压入栈opnd中。而当前代码中的判断条件是`s[i]=' '&&s[i-1]>='0'&&s[i-1]<='9'`,即将当前字符赋值为空格,而不是判断当前字符是否为空格。
正确的代码应该是这样的:
```
#include <iostream>
#include <stack>
using namespace std;
int calculate(int a, char b, int c) {
switch (b) {
case '+': return a + c;
case '-': return a - c;
case '*': return a * c;
case '/': return a / c;
}
return 0;
}
int main() {
stack<int> opnd;
string s;
int i;
getline(cin, s);
int num = 0;
for (i = 0; s[i] != '\0'; i++) {
if (s[i] == '@')
break;
else if (s[i] >= '0' && s[i] <= '9') {
num = num * 10 + (s[i] - 48);
}
else if (s[i] == ' ' && s[i - 1] >= '0' && s[i - 1] <= '9') {
opnd.push(num);
num = 0;
}
else if (s[i] == '+' || s[i] == '-' || s[i] == '*' || s[i] == '/') {
int a = opnd.top();
opnd.pop();
int b = opnd.top();
opnd.pop();
opnd.push(calculate(b, s[i], a));
}
}
cout << opnd.top();
return 0;
}
```
解释下面代码功能#include<iostream> #include<cstring> #include<cmath> using namespace std; int Node[50]; int number; int Calculate(char x)//计算偏移 { if(x>=97&&x<=122) return 0; switch(x) { case '!': case '^': case '(': case ')': return 1; case '|': case '-': return 2; case '<': return 3; default: return 1; } } int CalculateOut(string &Word,int n,int m)//输出||、->、<->,等等 { int i; if(m!=0) { for(i=1; i<m; i++)cout<<Word[n+i]; if(Word[i+n]!='\0') cout<<" "; } } int Input(string &Word)//输出数据 { int m,i,tag,q; number=0; for(i=0; Word[i]!='\0'; ) { q=i; if(Word[i]==' ') { i++; continue; } cout<<Word[i]; tag=Calculate(Word[i]); CalculateOut(Word,i,tag); i+=tag;//偏移 if(!tag)//数字计数 { Node[Word[q]-97]++; number++; if(Word[i+1]!='\0') cout<<" "; i++; } } cout<<endl; return number; } int Bin(int n,int m) { int bin[10000]; int a,b,i=0; do { a=n%2; n/=2; bin[i++]=a; } while(n>0); for(n=0; n<m-i; n++) { cout<<"0 "; } for(n=i-1; n>=0; n--) { cout<<bin[n]; if(n) cout<<" "; } } int Outnumber(int n) { int i,m=1; for(i=1; i<n; i++) { m=m*2+1; } for(i=m; i>=0; i--) { Bin(i,n); cout<<endl; } } void CoutNode() { int i,temp=0; for(i=0; i<50; i++) { if(temp==0&&Node[i]!=0) { cout.put(97+i); temp++; } else if(Node[i]!=0) { cout<<" "; cout.put(97+i); temp++; } } number=temp; cout<<endl; } int ReNode()//初始化 { for(int i=0; i<110; i++) Node[i]=0; } int Turn(char Map[400],string &Word) { int n,i; for(i=0,n=0;i<=strlen(Map);i++) { if(Map[i]!=' ') { Word[n]=Map[i]; n++; } } Word[n]='\0'; } int main() { int n,i; string Word; char Map[400]; while(gets(Map)!=NULL)//输入 { ReNode(); Turn(Map,Word); Input(Word); CoutNode(); Outnumber(number); } // Bin(2); }
这段代码主要是实现一个命题逻辑的计算器,可以输入一些命题公式,计算并输出结果。具体功能如下:
1. Calculate(char x):根据输入的字符(运算符或操作数)计算偏移量,返回一个整数值。
2. CalculateOut(string &Word,int n,int m):根据输入的字符串、起始位置和偏移量,输出相应的运算符。
3. Input(string &Word):根据输入的字符串,输出数据并返回操作数的个数。
4. Bin(int n,int m):将十进制数n转换为二进制,并输出m位二进制数。
5. Outnumber(int n):输出0~2^n-1的所有二进制数。
6. CoutNode():输出所有出现过的操作数。
7. ReNode():初始化操作数数组。
8. Turn(char Map[400],string &Word):将输入的字符数组转换为字符串。
9. main():主函数,循环读入命题公式,调用各函数进行计算和输出。
相关推荐
![7z](https://img-home.csdnimg.cn/images/20210720083312.png)
![rar](https://img-home.csdnimg.cn/images/20210720083606.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)