C++ 如何在c+中传输字符串数据类型等参数+;
我正试图将参数传输到我的函数中以执行 这是我的stack.h文件C++ 如何在c+中传输字符串数据类型等参数+;,c++,C++,我正试图将参数传输到我的函数中以执行 这是我的stack.h文件 #ifndef stackarray_h #define stackarray_h #include<iostream> #include<string> using namespace std; const int MAX_SIZE = 100; typedef string Type; class Stack{ private: Type item[MAX_SIZE]; int to
#ifndef stackarray_h
#define stackarray_h
#include<iostream>
#include<string>
using namespace std;
const int MAX_SIZE = 100;
typedef string Type;
class Stack{
private:
Type item[MAX_SIZE];
int top;
public:
Stack();
bool isEmpty()const;
void push(Type value);
void pop();
Type getTop();
};
Stack::Stack(){
top = -1;
}
bool Stack::isEmpty()const{
if (top == -1)
return true;
else
return false;
}
void Stack::push(Type value){
item[++top] = value;
}
void Stack::pop(){
if (top != -1){
top = top - 1;
}
}
Type Stack::getTop(){
return item[top];
}
#endif
\ifndef stackarray\u h
#定义堆栈数组
#包括
#包括
使用名称空间std;
const int MAX_SIZE=100;
typedef字符串类型;
类堆栈{
私人:
类型项目[最大尺寸];
int top;
公众:
堆栈();
bool isEmpty()常量;
无效推送(类型值);
void pop();
类型getTop();
};
Stack::Stack(){
top=-1;
}
布尔堆栈::isEmpty()常量{
如果(顶部==-1)
返回true;
其他的
返回false;
}
void Stack::push(类型值){
项目[++顶部]=值;
}
void Stack::pop(){
如果(顶部!=-1){
top=top-1;
}
}
类型Stack::getTop(){
返回项目[顶部];
}
#恩迪夫
这是我的source.cpp文件。我的问题是,我无法将参数字符串传输到堆栈中的push函数
#include<iostream>
using namespace std;
#include<string>
#include<fstream>
#include"stack.h"
void readFile(char* file,string &str);
void convertFromInfixToPostfix(string &str);
int evaluatingPostfix(string str);
int convert(int value);
string convertToString(int value);
int main(){
string str;
readFile("Text.txt",str);
cout<<str<<endl;
convertFromInfixToPostfix(str);
cout << "Output: ";
cout << str << endl;
cout << "Result: ";
cout << evaluatingPostfix(str) << endl;
return 0;
}
void readFile(char* file,string &str){
ifstream read(file);
if (!read.is_open()){
cout << "Error!.File wasn't opened." << endl;
}
else
{
getline(read, str);
}
read.close();
}
void convertFromInfixToPostfix(string &str){
Stack _stack;
string output="";
int i = 0;
while (i < str.length()){
if (str[i] == '(' || str[i] == '+' || str[i] == '-'
|| str[i] == '*' || str[i] == '/' || str[i] == '%'){
_stack.push(str[i]);
}
else {
//excute character )
if (str[i] == ')'){
while (!_stack.isEmpty()){
char top = _stack.getTop();
output += top;
_stack.pop();
}
}else
//excute character digit
output = output + str[i];
}
// excute end of string
if (_stack.getTop() == '('){
_stack.pop();
}
i++;
}
str = output;
}
int evaluatingPostfix(string str){
int result;
Stack _stack;
int op1, op2;
int i = 0;
int index;
string temp;
char next_ch;
while (i < str.length()){
next_ch = str[i];
if (next_ch == '+' || next_ch == '-'
|| next_ch == '*' || next_ch == '/'
){
//excute operator
op1 = _stack.getTop();
_stack.pop();
// convert op1 to integer
op1 = convert(op1);
op2 = _stack.getTop();
_stack.pop();
// convert op2 to integer
op2 = convert(op2);
if (next_ch == '+'){
index = op2 + op1;
// convert index from integer to char
temp = convertToString(index);
_stack.push(temp);
}
if (next_ch == '-'){
index = op2 - op1;
// convert index from integer to char
temp = convertToString(index);
_stack.push(temp);
}
if (next_ch == '*'){
index = op2 * op1;
// convert index from integer to char
temp = convertToString(index);
_stack.push(temp);
}
if (next_ch == '/'){
index = op2 / op1;
// convert index from integer to char
temp = convertToString(index);
_stack.push(temp);
}
}
else {
// excute integer
_stack.push(next_ch);
}
i++;
}
// push the result onto the _stack
result = _stack.getTop();
// _stack is empty
_stack.pop();
return result;
}
int convert(int value){
int result;
result = value - 48;
return result;
}
string convertToString(int value){
string result ;
int n = value;
do{
n = n % 10;
result = result + (char)(n + 48);
} while (n>0);
return result;
}
#包括
使用名称空间std;
#包括
#包括
#包括“stack.h”
void readFile(char*文件、字符串和str);
void convertFromInfixToPostfix(字符串和str);
int求值后缀(字符串str);
int转换(int值);
字符串转换字符串(int值);
int main(){
字符串str;
readFile(“Text.txt”,str);
你会得到什么错误?“错误被警告到IDE”-错误不是什么商业秘密。当你收到错误时,在你的问题*中发布完整且*准确的错误文本。既然你已经发布了一个示例,完成它以便重现该错误。“什么是错误?”完全正确。请描述您在问题中遇到的错误。仅仅说“我不能那样做”对于诊断您的问题来说是模糊的。似乎您正试图将字符串推送到堆栈中,而它期望的是StackItemType类型。是@ivan。我在typedef string“type”行将表单“StackItemType”编辑为“type”。但这不是我的问题。使用方法push(字符串值)。我尝试将项(字符串项)推到堆栈上,但出现错误