博客
关于我
强烈建议你试试无所不能的chatGPT,快点击我
UVA494 Kindergarten Counting Game
阅读量:7281 次
发布时间:2019-06-30

本文共 1479 字,大约阅读时间需要 4 分钟。

C语言程序员的一项重要工作就是封装功能函数。

问题链接:。

题意简述:幼儿园数单词游戏。输入若干句话,数一下每句有几个单词输出。

问题分析:实现方法有多种。可以用C语言的字符串函数strtok()来实现,也可以用字符流来实现。

程序说明用字符流实现时,封装了函数mygetchar()和mygetwords(),使得程序逻辑更加简洁,可阅读行强,通用行好。

AC的C语言程序如下:

/* UVA494 Kindergarten Counting Game */#include 
#include
#include
char mygetchar(){ char c; c = getchar(); if(c != '\n' && c != EOF) c = isalpha(c) ? c : ' '; return c;}int mygetwords(){ char c; int count = 0; while((c = mygetchar()) && c != '\n' && c != EOF) { if(isalpha(c)) count++; while(isalpha(c)) c = mygetchar(); } return (count == 0 && c == EOF) ? -1 : count;}int main(void){ int count; for(;;) { count = mygetwords(); if(count < 0) break; printf("%d\n", count); } return 0;}
另外一个AC的C语言程序如下:

/* UVA494 Kindergarten Counting Game */#include 
#include
#include
int mygets(char s[]){ int i = 0; char c; while((c = getchar()) && c != '\n' && c != EOF) s[i++] = isalpha(c) ? c : ' '; s[i] = '\0'; return i;}int main(void){ char buf[1024]; char delim[] = " "; char *p; int count; while(mygets(buf)) { count = 0; p = strtok(buf, delim); while(p) { count++; p = strtok(NULL, delim); } printf("%d\n", count); } return 0;}

转载于:https://www.cnblogs.com/tigerisland/p/7564376.html

你可能感兴趣的文章
spark textFile 困惑与解释
查看>>
除了首付,购房预算还须有这7项才能买到房!
查看>>
Dynamic attention in tensorflow
查看>>
python中的三元运算
查看>>
Swift:宏定义
查看>>
Linux(Ubuntu12.04)上玩儿STC单片机(转)
查看>>
Heroku免费版限制
查看>>
Struts2拦截器
查看>>
jQuery的$.extend和$.fn.extend作用及区别
查看>>
[C#] async 的三大返回类型
查看>>
数据结构之---C语言实现图的邻接表存储表示
查看>>
Node.js 把图片流送到客户端
查看>>
Android P 功能和 API
查看>>
php --with-mysql=mysqlnd
查看>>
登录(ajax提交数据和后台校验)
查看>>
谷歌中国的第一款产品“猜画小歌”
查看>>
HTTP 错误 500.19 - Internal Server Error
查看>>
序列起始值修改
查看>>
蓝点中文_Linux2.0 实验三 用户组管理
查看>>
php表单在提交之后再后退,表单的内容默认是被清空的
查看>>