| 网站首页 | 业界新闻 | 小组 | 威客 | 人才 | 下载频道 | 博客 | 代码贴 | 在线编程 | 编程论坛
欢迎加入我们,一同切磋技术
用户名:   
 
密 码:  
共有 749 人关注过本帖
标题:关于强制类型转换运算符的问题请教大家。。。
只看楼主 加入收藏
timmy26
Rank: 1
等 级:新手上路
帖 子:3
专家分:0
注 册:2008-8-23
收藏
 问题点数:0 回复次数:2 
关于强制类型转换运算符的问题请教大家。。。
我写了一个用一个数组来实现两个stack的程序,其中一个是用来存放偶数,一个存放奇数。调用pop,push这样的函数时,我的设想是从终端(键盘)输入字符串(odd,even),然后运用强制类型转换运算符,将该字符串转换为指针类型(ptostack),从而实现调用。但真正做起来始终有错误。请问各位这样的做法有可行性么?还是只是我的代码的问题。
谢谢!
搜索更多相关主题的帖子: 运算符 类型 
2008-11-14 16:26
forever74
Rank: 20Rank: 20Rank: 20Rank: 20Rank: 20
来 自:CC
等 级:版主
威 望:58
帖 子:1705
专家分:4345
注 册:2007-12-27
收藏
得分:0 
没看到你的代码之前,不知道有没有问题。
不用谢!

对宇宙最严谨的描述应该就是宇宙其实是不严谨的
2008-11-14 16:42
timmy26
Rank: 1
等 级:新手上路
帖 子:3
专家分:0
注 册:2008-8-23
收藏
得分:0 
以下是头文件Stack.h
#ifndef stack
struct stack;
typedef struct stack *ptostack;
typedef int topofstack;
ptostack createstack(int which);/* the parameter "which" helps us decide which stack we are going to create*/
void push(ptostack s,int newn,int move);
void pop(ptostack s,int move);
int isfull(ptostack s1,ptostack s2);
int how_many(char a[]);
#endif

以下是程序:
#include<stdio.h>
#include"C:\Win-TC\projects\Stack.h"
#define N 9
int num[N+1];
struct stack
{
topofstack top;
int *array;
int capacity;
};



ptostack createstack(int which)
{
int tmp;
ptostack s;
s=malloc(sizeof(struct stack));

tmp=N-N*which;/*which==0 means we create this stack to store even numbers,1 means odd numbers.They will use the same array but start from different point*/

s->array=&num[tmp]; /* even stack will start from num[N],odd stack will start from num[0]*/
s->top=tmp;
s->capacity=0;     /*we use this variable to record the amount of the numbers that in the stack at the present time*/
return s;
}


void push(ptostack s,int newn,int move)
{
s->top=s->top+move;
s->array[s->top]=newn;                              /* move may be 1 or -1*/
s->capacity=s->capacity+1;
}

int isfull(ptostack s1,ptostack s2)   /*use this function to prevent collision between two stack*/
{
if(s1->capacity+s2->capacity==N+1)
return 1;
else return 0;
}


void pop(ptostack s,int move)
{
s->top=s->top+move;
s->capacity=s->capacity-1;
}

int top(ptostack s)
{
return s->array[s->top];
}

main()
{
/*in this case we will input some integers and use functions defined above to seperate them into two stacks according to that the number is odd or even */
int i,j,temp,move;
char g[4];
ptostack odd,even;
odd=createstack(1);
even=createstack(0);
printf("Please input 10 integers:\n");
i=0;
while(i<N+1)
    {
    scanf("%d",&temp); /*Do you remember that num is a global array?*/
    if(temp%2==0) {push(even,temp,-1);}
    else {push(odd,temp,1);}
    i=i+1;
    }


printf("the top of the odd stack is %3d\n",top(odd));
printf("the top of the even stack is %3d\n",top(even));

printf("Now we are going to test the pop function.\n");
printf("which stack do you want to perform this operation?\n");
scanf("%s",g);
if(!strcmp(g,"odd"))
 move=-1;
else
 move=1;

pop((ptostack)g,move);
printf("the top of the %s stack is %d\n",g,top((ptostack)odd));
getch();

}
2008-11-14 18:33
快速回复:关于强制类型转换运算符的问题请教大家。。。
数据加载中...
 
   



关于我们 | 广告合作 | 编程中国 | 清除Cookies | TOP | 手机版

编程中国 版权所有,并保留所有权利。
Powered by Discuz, Processed in 0.081321 second(s), 9 queries.
Copyright©2004-2024, BCCN.NET, All Rights Reserved