一个简单的计数器程序及使用指针参数按引用传递的实例

#include <stdio.h>
#include <stdlib.h>/*for atof()*/
#define MAXOP 100 /*Max size of operator or operand*/
#define NUMBER '0'/*signal that a number was found*/

int getop(char []);
void push(double);
double pop(void);

/*reverse polish calculator*/
main()
{
   int type;
   double op2;
   char s[MAXOP];
   
   while((type = getop(s) != EOF)
   {
     switch (type)
	 {
	    case NUMBER:
		push(atof(s));
		break;
		
		case '+':
		push(pop() + pop());
		break;
		
		case '*';
		push(pop() * pop())
		break;
		
		case '-':
		op2 = pop();
		push(pop() - op2);
		break;
		
		case '/':
		op2 = pop();
		if(op2 != 0.00)
		push(pop() / op2);
		else 
		printf("Error: zero divisor\n");
		break;
		
		case '\n':
		printf("\t%.8g\n",pop());
		break;
		
		default:
		printf("error: unknown command %s\n", s0;
		break;
	 }
	 return 0;
}

//push和pop必须共享的栈顶指针和栈定义在这两个函数外部
int sp = 0;/*栈顶指针,下一个空栈位置*/
double val[MAXVAL];/*利用数组声明一个堆栈及其长度*/

/*利用数组实现压栈操作
  栈顶是数组的末尾元素*/
void push(double f)
{
   if(sp < MAXVAL)
   val[sp++] = f;
   else
   printf("error: stach full, can't push any more\n");
}

/*出栈操作*/
double pop(void)
{
  if(sp > 0)
  /*这里不应该是val[sp--]么?*/
  /*符合堆栈的后进先出*/
  return val[--sp];
  else
  {
    printf("error : stack empty\n");
	return 0.0;
  }
}

#include <ctype.h>
#include <stdio.h>
#define NUMBER '0'

/*getc()是字符输入函数,返回stream的下一个字符*/
int getc(void);
/*ungetc(int)是把C写回到stream中,下次对该流进行读操作时,将返回该字符*/
/*对每个流只能写回一个字符,且字符不能是EOF*/
void ungetc(int);

/*getop:get next charactor or numeric operand*/
int getop(char s[])
{
  int i, c;
  /*跳过空格和制表符*/
  while((s[0] = c = getc()) == ' ' || c == '\t')
  ;
  /*如果下一个字符不是数字也不是小数点,则返回*/
  if(!isdigit(c) && c != '.')
  return c;
  i = 0;
  if(isdigit(c))
  while (isdigit(s[++i] = c = getc()))
  ;
  if(c = '.')
  while(isdigit(s[++i] = c = getc()))
  ;
  s[i] = '\0';
  if(c != EOF)
  ungetc(c);
  return NUMBER;/*标志数已经收集起来了*/
}

/*getch and ungetch*/
#define BUFSIZE 100
//缓冲区 buffer for ungetch
char buf[BUFSIZE];
//下标变量, next free position in buf.
int bufp = 0;

int getch(void)
{
  return ( bufp > 0) ? buf[--bufp] : getchar();
}
void ungetch(int c)
{
  if(bufp >= BUFSIZE)
    printf("ungetch: too many characters\n");
  else
    buf[bufp++] = c;
}

 

C语言函数的参数传递一般是按值传递的。

指针参数使得被调用函数能够访问和修改主调函数中对象的值,例如:

下面的这个函数getint(),接受自由格式的输入,并执行转换,将输入的字符流分解成整数,且每次调用得到一个整数。

getint()需要返回转换后得到的整数,并且,在到达输入结尾时要返回文件结束标记EOF。

EOF可以用任何值表示,当然也可用一个输入的整数表示。

 

#include <stdio.h>
#include <ctype.h>
#include <string.h>
#define SIZE 10

main()
{
  int i, n, a[SIZE], getint(int *);
  for(n=0; n<SIZE && getint(&a[n])!=EOF; n++)
  ;
  for(i = 0; i < n; i++)
  {
    printf("%d, ", a[i]);
  }
}

int getint(int *pn)
{
  int sign, c;
  while(isspace(c = getch()))
  ;
  if((c=getch() != EOF) && (!isdigit(c)) && c != '+' && c != '-')
  {
    ungetch(c);
    return 0;
  }
  sign = (c == '-')? -1 : 1;
  if(c == '+' || c == '-')
  c = getch();
  for(*pn = 0; isdigit(c); c = getch())
  *pn = *pn * 10 + (c - '0');
  *pn *= sign;
  if(c != EOF)
  ungetch(c);
  return *pn;
}



 

  • 1
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值