我是c ++的初学者,我有一个简单计算器的代码,但它有一些错误。
int' from
float'
在第68行(copy = operand1)。如何修复这些错误? 事先感谢您的帮助;
#include <iostream>
#include <iomanip>
using namespace std;
struct Node{
float number;
Node *next;
};
Node* push(Node *stack, float data){
Node *utility;
utility = new Node;
utility -> number = data;
utility -> next = stack;
return utility;
}
Node* pop(Node *stack, float &data){
Node *temp;
if (stack != NULL){
temp = stack;
data = stack -> number;
stack = stack -> next;
delete temp;
}
else cout << "\nERROR: Empty stack.\n";
return stack;
}
int main()
{
float answer=0.f, operand1=0.f, operand2=0.f;
char ch = ' ';
int neg=0,cont=0,copy;
Node *utility, *top;
utility = new Node;
utility -> number = 0.f;
utility -> next = NULL;
top = new Node;
top -> number = 0.f;
top -> next = utility;
cout << "Enter the postfix operation: ";
while(ch != '\n')
{
cin >> noskipws >> ch;
cont++;
if(cont<4&&ch=='-'){
neg=-1;
ch=' ';
}
int operand = 0;
while(ch == ' '){
cin >> ch;
}
if((ch >= '0')&&(ch <= '9')){
while(ch != ' '){
operand = operand*10 + (ch-48);
cin >> ch;
if(neg==-1 && operand1>=0 ){
operand=operand * neg;
}
}
neg=0;
top = push(top, operand);
}else{
****copy=operand1;****
top=pop(top, operand1);
top=pop(top, operand2);
if(operand2<0&©>0){
operand2*=-1;
if(cont>4)
operand2*=-1;
}
switch(ch){
case '+': answer = operand2 + operand1;break;
case '-': answer = operand2 - operand1;break;
case '*': answer = operand2 * operand1;break;
case '/': answer = operand2 / operand1;break;
}
top=push(top, answer);
}
}
pop(top, answer);
cout << "Answer: " << answer << endl;
system("pause");
return 0;
}
答案 0 :(得分:0)
违规行是第68行,这是GCC 4.9.0的输出:
E:\ test.cpp:在函数&#39; int main()&#39;:
E:\ test.cpp:68:15:错误:一元&#39; *&#39; 的无效类型参数(有&#39; int&#39;)** **复制=操作数; ****
E:\ test.cpp:68:31:错误:不匹配&#39;运营商*&#39; (操作数类型是&#39; Node&#39;)**** copy = operand1; ****
错误表明单元运算符*无法应用于int(int没有单位运算符*,只有二进制)
第68行:**** copy = operand1; ****
开头和结尾处的四个*是问题,已删除。