 |
| C# Programming questions specific to the Microsoft C# language. See also the forum Beginning Visual C# to discuss that specific Wrox book and code. |
Welcome to the p2p.wrox.com Forums.
You are currently viewing the C# section of the Wrox Programmer to Programmer discussions. This is a community of software programmers and website developers including Wrox book authors and readers. New member registration was closed in 2019. New posts were shut off and the site was archived into this static format as of October 1, 2020. If you require technical support for a Wrox book please contact http://hub.wiley.com
|
|
|
|

April 23rd, 2005, 09:39 AM
|
|
Authorized User
|
|
Join Date: Apr 2005
Posts: 13
Thanks: 0
Thanked 0 Times in 0 Posts
|
|
reverse problem
Modify the following code, write a method "Reverse(char[] s1, char[] s2)", ths string s1 and s2 must be the same size, the string s1 should become a reverse copy of string s2. Internal to reverse, use the CharStack to perform the reversal.
using System;
public class CharStack
{
private const int EMPTY = -1;
private int maxLen;
private char[] s;
private int top;
public void SetStack(int maxSize)
{
s = new char[maxSize];
maxLen = maxSize;
top = EMPTY;
}
public void Reset() { top = EMPTY; }
public void Push(char c) { s[++top] = c; }
public char Pop() { return s[top--]; }
public char TopOf() { return s[top]; }
public bool Empty() { return (top == EMPTY); }
public bool Full() { return (top == maxLen - 1); }
}
class CharStackTest
{
public static void Main()
{
CharStack s = new CharStack();
string str = "My name is Don Knuth!";
char[] reStr = new char[str.Length];
int i = 0;
s.SetStack(str.Length); // stack can accommodate str
Console.WriteLine(str);
while ((i < str.Length) && !s.Full())
s.Push(str[i++]);
while (!s.Empty()) // print the reverse
Console.Write(s.Pop());
Console.WriteLine();
}
}
|
|

April 24th, 2005, 02:59 AM
|
 |
Wrox Author
|
|
Join Date: Jun 2003
Posts: 17,089
Thanks: 80
Thanked 1,576 Times in 1,552 Posts
|
|
What's this? Are you sharing useful code, or do you want us to do your homework?
Imar
---------------------------------------
Imar Spaanjaars
Everyone is unique, except for me.
|
|

April 24th, 2005, 06:44 AM
|
|
Authorized User
|
|
Join Date: Apr 2005
Posts: 13
Thanks: 0
Thanked 0 Times in 0 Posts
|
|
Same in this one, I couldn`t do the question, please help.
|
|

April 25th, 2005, 05:56 AM
|
|
Friend of Wrox
|
|
Join Date: Jul 2004
Posts: 623
Thanks: 0
Thanked 1 Time in 1 Post
|
|
well,have a look at the codes,
Code:
private void reverse(char[] s1,char[] s2)
{
if(s1.Length!=s2.Length)
throw new Exception("the lenghts should be equal");
CharStack stack=new CharStack();
stack.SetStack(s1.Length);
foreach(char ch in s1)
stack.Push(ch);
for(int i=0;i<s2.Length;i++)
s2[i]=stack.Pop();
}
private void ToUse()
{
string name="My name is Don Knuth!";
char[] s2=new char[name.Length];
this.reverse(name.ToCharArray(),s2);
foreach(char ch in s2)
textBox1.Text+=ch.ToString();
}
_____________
Mehdi.
software student.
|
|

April 25th, 2005, 07:52 AM
|
|
Authorized User
|
|
Join Date: Apr 2005
Posts: 13
Thanks: 0
Thanked 0 Times in 0 Posts
|
|
I got it, thank you very much!!
|
|

April 25th, 2005, 08:12 AM
|
|
Friend of Wrox
|
|
Join Date: Jul 2004
Posts: 623
Thanks: 0
Thanked 1 Time in 1 Post
|
|
no problem,
it's better to change the CharStack class
Code:
public class CharStack
{
private const int EMPTY = -1;
private int maxLen;
private char[] s;
private int top;
public CharStack(int maxSize)
{
s = new char[maxSize];
maxLen = maxSize;
top = EMPTY;
}
public void Reset() { top = EMPTY; }
public void Push(char c)
{
if(!this.Full())
s[++top] = c;
else
throw new Exception("stack is full");
}
public char Pop()
{
if(!this.Empty())
return s[top--];
else
throw new Exception("stack is empty");
}
public char TopOf() { return s[top]; }
public bool Empty() { return (top == EMPTY); }
public bool Full() { return (top == maxLen - 1); }
}
instead of SetStack you can have a constructor also when you want to pop or push something you should check the stack.
_____________
Mehdi.
software student.
|
|
 |