小坏蛋_千千 2016-01-08 07:51 采纳率: 0%
浏览 1521

C语言一个小小的问题,望解答

请问下面代码中为什么多一个输出语句程序是对的,去掉就不可以运行了呢?
一定和输入缓存区有关系么?加一个fflush也可以,就是不知道为什么???

 #include <stdio.h>
#include <string.h>
#include<stdlib.h>
long int sum,tsum,tnum;
struct word
{
    char str[105];
    int num;
} A[2][10005];
void chuli(char s[],int k)
{
    int i,j,flag=0;
    for(i=0; i<=sum; i++)
    {
        if(strcmp(A[k][i].str,s)==0)
        {
            A[k][i].num++;
            flag=1;
            sum++;
        }
    }
    if(flag==0)
    {
        for(j=0; j<105; j++)
            A[k][sum].str[j]=s[j];
        A[k][sum].num++;
        sum++;
        if(k==0)tnum++;
        else tsum++;
    }
}
void paixu2()
{
    int i,j;
    char c[105];
    struct word a;
    for(i=0; i<sum; i++)
    {
        for(j=i+1; j<sum; j++)
            if(A[1][i].num<A[1][j].num)
            {
                a=A[1][j];
                A[1][j]=A[1][i];
                A[1][i]=a;
            }
            else if(A[1][i].num==A[1][j].num&&strcmp(A[1][i].str,A[1][j].str)>0)
            {
                strcpy(c,A[1][j].str);
                strcpy(A[1][j].str,A[1][i].str);
                strcpy(A[1][i].str,c);
            }
    }
}
void chong()
{
    int i,j;
    for(i=0; i<tsum; i++)
    {
        for(j=0; j<tnum; j++)
        {
            if(strcmp(A[0][j].str,A[1][i].str)==0)A[1][i].num=0;
        }
    }
}
void input2(int k)
{
    char ch,s[105]= {'\0'};
    int i;
    sum=0;
    for(i=0; i<10005; i++)
        A[k][i].num=0;
    scanf("%*s%*c");
    while(ch!=-1&&ch!='<')
    {
        for(i=0; i<105; i++)s[i]='\0';
        ch=getchar();
        if(ch>='A'&&ch<='Z')ch+=32;
        if((65<=ch&&ch<=90)||(ch>=97&&ch<=122))
        {
            for(i=0;; i++)
            {
                s[i]=ch;
                ch=getchar();
                if(ch>='A'&&ch<='Z')ch+=32;
                if((65<=ch&&ch<=90)||(ch>=97&&ch<=122))continue;
                else break;
            }
            chuli(s,k);
        }
    }
    scanf("%*s%*c");
    if(k==1)
    {
        chong();
        paixu2();
    }
}
int main()
{
    int n,i;
    scanf("%d%*c",&n);
    while(n--)
    {
        for(i=0; i<10005; i++)
            for(int k=0; k<2; k++)
                memset(A[k][i].str,'\0',sizeof(A[k][i].str));
        tnum=0,tsum=0,sum=0;
        input2(0);
                //fflush(stdin);
        printf("****\n");/*********这个输出一定要有,并且不可以什么也不输出,不懂为什么*/
        input2(1);
        for(i=0; i<tsum&&i<10; i++)
        {
            if(A[1][i].num==0)continue;
            printf("%s\n",A[1][i].str);
        }
        //fflush(stdin);
        if(n!=0)printf("\n");
    }
    return 0;
}


********下面是测试数据***********

* **** Sample Input****
2
<oldwords>
how aRe you
</oldwords>
<article>
--How old are you?
--Twenty.
</article>
<oldwords>
google cn huluobo net i
</oldwords>
<article>
文章内容:
I love google,dropbox,firefox very much.
Everyday I open my computer , open firefox , and enjoy surfing on the inter-
net.
But these days it's strange that searching "huluobo" is unavail-
able.
What's wrong with "huluobo"?
</article>


* ******Sample Output******
old
twenty

firefox
open
s
able
and
but
computer
days
dropbox
enjoy

  • 写回答

1条回答 默认 最新

  • ysuwood 2016-01-08 08:13
    关注
     void input2(int k)
    {
        char ch,s[105]= {'\0'};
        int i;
        sum=0;
        for(i=0; i<10005; i++)
            A[k][i].num=0;
        scanf("%*s%*c");
        while(ch!=-1&&ch!='<') // ch未赋值
    
    评论

报告相同问题?

悬赏问题

  • ¥15 如何实现H5在QQ平台上的二次分享卡片效果?
  • ¥15 python爬取bilibili校园招聘网站
  • ¥30 求解达问题(有红包)
  • ¥15 请解包一个pak文件
  • ¥15 不同系统编译兼容问题
  • ¥100 三相直流充电模块对数字电源芯片在物理上它必须具备哪些功能和性能?
  • ¥30 数字电源对DSP芯片的具体要求
  • ¥20 antv g6 折线边如何变为钝角
  • ¥30 如何在Matlab或Python中 设置饼图的高度
  • ¥15 nginx中的CORS策略应该如何配置