静态存储持续性 无链接性 (例子)

#include <iostream>

const int ArSize=10;

void strcount(const char *str);
//////////////////////////////////////////////////////////////////////////
int main(void)
{
 using namespace std;
 char input[ArSize];
 char next;

 cout<<"enter a line: \n";
 cin.get(input,ArSize);//读取字符串

 while(cin) //当为空行时 cin 返回 false
 {
    cin.get(next);  //丢弃余下字符 
    while(next!='\n')
     cin.get(next);//结束丢弃实现代码

    strcount(input);
    cout<<"enter next line(empty to quit):\n";
    cin.get(input,ArSize);
 }
 cout<<"bye.\n";

 return 0;
}
//////////////////////////////////////////////////////////////////////////
void strcount(const char *str)
{
   using namespace std;
   static int total=0;
   int count=0;
  
   cout<<"\""<<str<<"\" contains ";
   while(*str) //计算字符出现
   {
    if(*str!=' ')
     count++;
    str++;
   }     //结束计算
   total+=count;
   cout<<count<<" chars,";
   cout<<total<<" total chars"<<endl;
}

posted @ 2007-02-11 01:48  Edward Xie  阅读(126)  评论(0)    收藏  举报