栈是著名的后进先出(LIFO)数据结构。
1、判断字符串是否是回文字符串。
2、十进制向多进制的转换。
1、代码
using System;
using System.Collections.Generic;
using System.Text;
using System.Collections;
namespace CStack
{
class Program
{
static void Main(string[] args)
{
Console.WriteLine(CStack.IsPalindrome("lixuejiao").ToString());
Console.WriteLine(CStack.IsPalindrome("sees").ToString());
Console.ReadLine();
}
}
class CStack
{
private int p_index;
private ArrayList list;
private int count;
public int Count
{
get { return list.Count; }
}
public CStack()
{
list = new ArrayList();
p_index = -1;
}
public void Push(Object item)
{
list.Add(item);
p_index++;
}
public object Pop()
{
object obj = list[p_index];
list.RemoveAt(p_index);
p_index--;
return obj;
}
public void Clear()
{
list.Clear();
p_index = -1;
}
public object Peek()
{
return list[p_index];
}
//判断字符串是否是回文字符串
//2010.04.25
public static bool IsPalindrome(string word)
{
CStack alist = new CStack();
string ch;
bool isPalindrome = true;
for (int num = 0; num < word.Length; num++)
{
alist.Push(word.Substring(num,1));
}
int pos = 0;
while (alist.Count > 0)
{
ch = alist.Pop().ToString();
if (ch != word.Substring(pos, 1))
{
isPalindrome = false;
break;
}
pos++;
}
if (isPalindrome)
{
Console.WriteLine(word + " is a palindrome");
}
else
{
Console.WriteLine(word +" is not a palindrome");
}
return isPalindrome;
}
}
}
using System.Collections.Generic;
using System.Text;
using System.Collections;
namespace CStack
{
class Program
{
static void Main(string[] args)
{
Console.WriteLine(CStack.IsPalindrome("lixuejiao").ToString());
Console.WriteLine(CStack.IsPalindrome("sees").ToString());
Console.ReadLine();
}
}
class CStack
{
private int p_index;
private ArrayList list;
private int count;
public int Count
{
get { return list.Count; }
}
public CStack()
{
list = new ArrayList();
p_index = -1;
}
public void Push(Object item)
{
list.Add(item);
p_index++;
}
public object Pop()
{
object obj = list[p_index];
list.RemoveAt(p_index);
p_index--;
return obj;
}
public void Clear()
{
list.Clear();
p_index = -1;
}
public object Peek()
{
return list[p_index];
}
//判断字符串是否是回文字符串
//2010.04.25
public static bool IsPalindrome(string word)
{
CStack alist = new CStack();
string ch;
bool isPalindrome = true;
for (int num = 0; num < word.Length; num++)
{
alist.Push(word.Substring(num,1));
}
int pos = 0;
while (alist.Count > 0)
{
ch = alist.Pop().ToString();
if (ch != word.Substring(pos, 1))
{
isPalindrome = false;
break;
}
pos++;
}
if (isPalindrome)
{
Console.WriteLine(word + " is a palindrome");
}
else
{
Console.WriteLine(word +" is not a palindrome");
}
return isPalindrome;
}
}
}
2、代码
class Program
{
static void Main(string[] args)
{
int num, baseNum;
Console.WriteLine("Enter a decimal number: ");
num = Convert.ToInt32(Console.ReadLine());
Console.WriteLine("Enter a base: ");
baseNum = Convert.ToInt32(Console.ReadLine());
Console.Write(num+" converts to ");
Console.WriteLine("Base "+baseNum+" is: ");
MulBase(num, baseNum);
Console.ReadLine();
}
public static void MulBase(int n, int b)
{
Stack Digits = new Stack();
do
{
Digits.Push(n%b);
n/=b;
}while(n!=0);
while(Digits.Count>0)
{
Console.Write(Digits.Pop());
}
}
}
{
static void Main(string[] args)
{
int num, baseNum;
Console.WriteLine("Enter a decimal number: ");
num = Convert.ToInt32(Console.ReadLine());
Console.WriteLine("Enter a base: ");
baseNum = Convert.ToInt32(Console.ReadLine());
Console.Write(num+" converts to ");
Console.WriteLine("Base "+baseNum+" is: ");
MulBase(num, baseNum);
Console.ReadLine();
}
public static void MulBase(int n, int b)
{
Stack Digits = new Stack();
do
{
Digits.Push(n%b);
n/=b;
}while(n!=0);
while(Digits.Count>0)
{
Console.Write(Digits.Pop());
}
}
}