处理堆栈和接口的问题
Problems with handling a Stack along with a Interface
所以我的 Console Application
有问题。我正在尝试使用某些方法在我的 class 中实现一个接口。但我真的无法掌握它。
我应该用 class.
[push method
将 push
个字母 (char
) 放入我的堆栈中=52=]
我需要从 stack
中 pop
它们并将 stack
中的每个 char
写到控制台中。我还应该使用 isEmpty
方法检查 Stack
是否为空,我认为这是正确完成的 ATM。
我也在 class
中做了一个 stack
,因为我正在尝试用 methods
做一些事情。但它似乎并不影响程序中创建的堆栈。
我遇到的问题是什么也没有发生。当我启动应用程序时,控制台弹出并运行代码并等待 Console.Read()
发生。
希望您能理解,代码比较简单。我会 post Interface, Class and program code.
接口代码:
public interface IStackInterface
{
char peek();
char pop();
void push(char nytt);
bool isEmpty();
}
Class代码:
public class StackClass : IStackInterface
{
Stack<char> stacken = new Stack<char>();
public StackClass()
{
}
public bool isEmpty()
{
if (stacken.Count == 0)
{
return false;
}
else
{
return true;
}
}
public char peek()
{
return stacken.Peek();
}
public char pop()
{
return stacken.Pop();
}
public void push(char nytt)
{
stacken.Push(nytt);
}
}
程序代码:
static void Main(string[] args)
{
StackClass stacken = new StackClass();
try
{
stacken.push('t'); stacken.push('s');
stacken.push('ä'); stacken.push('b');
stacken.push(' ');
stacken.push('r'); stacken.push('ä');
stacken.push(' '); stacken.push('m');
stacken.push('o'); stacken.push('g');
stacken.push('a'); stacken.push('L');
}
catch (InvalidOperationException e)
{
Console.WriteLine(e.Message);
}
while (!stacken.isEmpty())
{
Console.WriteLine(stacken.pop());
}
Console.Read();
}
此致。
您的代码没有按照您的预期运行,纯粹是因为您对 isEmpty
的实现方式不对。当计数为零时,它应该 return true
。
您的代码中存在逻辑错误...isEmpty returns 与您想要的相反。另外,你可以这样优化它:
public bool isEmpty()
{
return (stacken.Count == 0);
}
甚至:
public bool isEmpty()
{
return stacken == null ? true : (stacken.Count == 0);
}
所以我的 Console Application
有问题。我正在尝试使用某些方法在我的 class 中实现一个接口。但我真的无法掌握它。
我应该用 class.
[push method
将push
个字母 (char
) 放入我的堆栈中=52=]我需要从
stack
中pop
它们并将stack
中的每个char
写到控制台中。我还应该使用isEmpty
方法检查Stack
是否为空,我认为这是正确完成的 ATM。我也在
class
中做了一个stack
,因为我正在尝试用methods
做一些事情。但它似乎并不影响程序中创建的堆栈。
我遇到的问题是什么也没有发生。当我启动应用程序时,控制台弹出并运行代码并等待 Console.Read()
发生。
希望您能理解,代码比较简单。我会 post Interface, Class and program code.
接口代码:
public interface IStackInterface
{
char peek();
char pop();
void push(char nytt);
bool isEmpty();
}
Class代码:
public class StackClass : IStackInterface
{
Stack<char> stacken = new Stack<char>();
public StackClass()
{
}
public bool isEmpty()
{
if (stacken.Count == 0)
{
return false;
}
else
{
return true;
}
}
public char peek()
{
return stacken.Peek();
}
public char pop()
{
return stacken.Pop();
}
public void push(char nytt)
{
stacken.Push(nytt);
}
}
程序代码:
static void Main(string[] args)
{
StackClass stacken = new StackClass();
try
{
stacken.push('t'); stacken.push('s');
stacken.push('ä'); stacken.push('b');
stacken.push(' ');
stacken.push('r'); stacken.push('ä');
stacken.push(' '); stacken.push('m');
stacken.push('o'); stacken.push('g');
stacken.push('a'); stacken.push('L');
}
catch (InvalidOperationException e)
{
Console.WriteLine(e.Message);
}
while (!stacken.isEmpty())
{
Console.WriteLine(stacken.pop());
}
Console.Read();
}
此致。
您的代码没有按照您的预期运行,纯粹是因为您对 isEmpty
的实现方式不对。当计数为零时,它应该 return true
。
您的代码中存在逻辑错误...isEmpty returns 与您想要的相反。另外,你可以这样优化它:
public bool isEmpty()
{
return (stacken.Count == 0);
}
甚至:
public bool isEmpty()
{
return stacken == null ? true : (stacken.Count == 0);
}