/* ***************************************
* Filename:changedate2.c
* Author:hufeng Date:2007/11/12
* Version:v1.0
* ****************************************/
/* header file */
#include
#include
/* creat a struct */
typedef struct linkstack
{
int date;
struct linkstack *next;
}ls;
/* date change */
void change_date(ls *top, int num)
{
ls *p = NULL;
while (num != 0)
{
p = (ls*)malloc(sizeof(ls)); /* creat a node */
p->date = num % 8;
p->next = top->next; /* creat a stack */
top->next = p;
num = num /8;
}
}
/* pop_stack */
void pop_stack(ls *top)
{
ls *p = NULL;
while (top->next != NULL)
{
p = top->next;
printf("%d",p->date);
top->next = p->next;
free(p);
}
}
/* main program */
int main()
{
int num;
ls *top;
char key;
int flag = 1;
top = (ls*)malloc(sizeof(ls));
top->next = NULL;
while (flag == 1)
{
printf("please enter a number whilch will be change:\n");
scanf("%d", &num);
change_date(top, num);
pop_stack(top);
printf("\nPress 'Q' or 'q' to quit!\n");
key = getch();
if (key == 'q' || key == 'Q')
{
flag = 0;
}
}
getch();
return 0;
}
本程序在win-tc编译通过,如果是其他的编译器(turbo系列)请将倒数第三行的getch();去掉即可!
祝你编程愉快!
栈的基本操作函数你自己搞定.我的是实现将十进制数转化为八进制数的函数.
void conversion(){
initstack(s);//构造空栈
scanf("%d",&n);
while(n){
push(S,n%8);//插入元素为栈定新元素
n=n/8;}
while(!stackempty(s)){//判断栈s是否为空栈
pop(s,e);//删除栈定元素,用e返回该元素
printf("%d",e);}
}
不划算啊