| 网站首页 | 业界新闻 | 小组 | 威客 | 人才 | 下载频道 | 博客 | 代码贴 | 在线编程 | 编程论坛
欢迎加入我们,一同切磋技术
用户名:   
 
密 码:  
共有 596 人关注过本帖
标题:ungetc函数的疑惑
只看楼主 加入收藏
周章明
Rank: 1
等 级:新手上路
帖 子:16
专家分:0
注 册:2013-4-28
结帖率:50%
收藏
 问题点数:0 回复次数:0 
ungetc函数的疑惑
# include <stdio.h>
# include <ctype.h>
# include <stdbool.h>
# include <string.h>

const size_t LENGTH  = 50;

void eatspace(void);
bool getinteger(int *n);
char *getname(char *name,size_t length);
bool isnewline(void);

int main(void)
{
int number;
char name[50];
printf("Enter a sequence of integers and alphabetic names:\n");
while(!isnewline())
        if(getinteger(&number))
                printf("\nInteger value:%8d",number);
        else if(strlen(getname(name,LENGTH)) > 0)
                printf("\nName:%s",name);
        else
        {
                printf("\nInvalid input:");
                return 1;
        }
        return 0;
}
bool isnewline(void)
{
char ch = 0;
if ((ch = getchar()) == '\n')
{
        return true;
}
ungetc(ch,stdin);//这里的ungetc有什么作用?
return false;

}
void eatspace(void)
{
char ch = 0;
while(isspace(ch = getchar()));
ungetc(ch,stdin);//为什么要将非空格字符返回到输入流中?

}
bool getinteger(int *n)
{
eatspace();
int value = 0;
int sign = 1;
char ch = 0;

if((ch = getchar()) == '-')
        sign = -1;
else if(isdigit(ch))
        value = 10*value + (ch - '0');
else if(ch != '+')
        {
        ungetc(ch , stdin);//这里的ungetc函数又是如何工作的呢?
        return false;
        }
while(isdigit(ch = getchar()))
        value = 10*value + (ch - '0');
ungetc(ch,stdin);
*n = value*sign;
return true;

}
char *getname(char *name,size_t length)
{
eatspace();
size_t count = 0;
char ch = 0;
while (isalpha(ch = getchar()))
{
        name[count++] = ch;
        if(count == length - 1)
                break;
}
name[count] = '\0';
if(count<length-1)
        ungetc(ch,stdin);//这里有为什么要调用这个函数呢?
return name;

}
希望哪位大侠帮帮忙。
搜索更多相关主题的帖子: void name alphabetic sequence include 
2013-05-09 09:48
快速回复:ungetc函数的疑惑
数据加载中...
 
   



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

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