作为flowchart练习
根据流程图写代码:
#include <stdio.h>
int getline(char *buf,int n)
{
//return 0 means EOF
//return & (Ctr+Z) by keyboard generates EOF
//break from for loop make i less by one than the nomal exit
char c;
int i;
for(i = 0;i < n-1;i++)
{
c = getchar();
if(c == '\n' )
{
buf[i] = c;
break;
}else if(c == EOF)
{
break;
}
else
buf[i] = c;
}
if(c == '\n')
buf[++i] = '\0';
else
buf[i] = '\0';
return i;
}
int main(int argc,char *argv[])
{
char buf[26];
printf("%d\n",EOF);
while(getline(buf,26) > 0)
{
printf(buf);
}
printf(buf);
return 0;
}