|
初めまして。本日、学校でこのようなプログラムをやりました。
#include<stdio.h>
#include<stdlib.h>
#include<ctype.h>
/* 構造体の定義 */
struct cell {
double num;
struct cell *next;
};
/* リストの先頭アドレスを指すポインタを Global で定義 */
struct cell *listhead = NULL;
/* 関数のプロトタイプ宣言 */
void push(double);
void pop(double *);
int main(void){
int c;
double op, op1, op2, val;
while ( ( c = getchar() ) != '\n' ){
if( isdigit(c) ){
ungetc( c, stdin );
scanf("%lf",&val);
push(val);
}
else{
switch(c){
case '+' :
pop(&op2);
pop(&op1);
push(op1+op2);
break;
case '-' :
pop(&op2);
pop(&op1);
push(op1-op2);
break;
case '*' :
pop(&op2);
pop(&op1);
push(op1*op2);
break;
case '/' :
pop(&op2);
pop(&op1);
if(op2 == 0){
printf("0 による除算はできません。\n");
exit(1);
}
push(op1/op2);
break;
}
}
}
if(!listhead->next){
pop(&op);
printf("答えは %f です.\n",op);
}
else{
printf("ERROR/n");
}
return 0;
}
void push(double val){
struct cell *p;
p = listhead;
listhead = (struct cell *)malloc(sizeof(struct cell));
listhead->num = val;
listhead->next = p;
}
void pop(double *p){
struct cell *q;
if(listhead){
*p = listhead->num;
q = listhead;
listhead = listhead->next;
free((void *)q);
}
else{
printf("スタックにデータがありません。\n");
exit(1);
}
}
このプログラムで
if( isdigit(c) )
の部分と
ungetc( c, stdin );
の部分がよくわかりませんでした。
このif文の条件はどのような意味なるでしょうか?
また、ungetcに使われているstdinとは何なのでしょうか?
かなり初歩的な質問かもしれませんが、答えていただけるとありがたいです。
宜しくお願いします。
|