关于c++中char char*赋值以及函数的传值
这个问题通过创造一个类来体现
首先是头文件
#define STACK_H_
class Stack
{
private:
char a[10][30];建立一个二位字符数组
int top;设置栈顶并规定上述二维数组的行数
public:
Stack();创造一个Stack类
bool empty();检测是不是空栈
bool full();检测是不是满栈
bool push();在栈顶放入元素
bool pop(char *b);将栈顶元素传值给char型指针b
};
#endif
接下来是定义各种函数的cpp
#include<iostream>
#include"stack.h"
using namespace std;
Stack::Stack()//创造一个栈类
{
top=0;
}
bool Stack::empty()//判断是不是空栈
{
return (top==0);
}
bool Stack::full()//判断是不是满栈
{
return (top==5);
}
bool Stack::push()//进栈,注意观察这个函数是如何判断输入结束以及进行赋值的
{
if(top<5)
{
cout<<"Plese enter: ";
int j=0;
while((a[top][j]=cin.get())!="
")
{
j++;
}
a[top][j]=" ";
top++;
cout<<endl;
}
else
return false;
return true;
}
bool Stack::pop(char *b)//出栈,同样注意观察这个函数是如何给char*赋值的
{
if(top>=0)
{
top--;
b=a[top];
while(*b!=" ")
{
cout<<*b;
b++;
}
cout<<endl;
top--;
}
else
return false;
return true;
}
重点观察这个函数注释的部分
最后一个便是执行cpp啦
#include<iostream>
#include"stack.h"
using namespace std;
int main()
{
Stack ac;
char *b;
ac.push();
ac.push();
ac.push();
ac.pop(b);
ac.pop(b);
return 0;
}
重点看第二个
- 上一篇:没有了
- 下一篇:没有了