鱼C论坛

 找回密码
 立即注册
查看: 3178|回复: 2

关于一道作业题。。有一部分完全没有思路 (string cpy ,cat cmp等 不能用指针)

[复制链接]
发表于 2016-10-4 11:19:53 | 显示全部楼层 |阅读模式
100鱼币
题目:
If you look at a newspaper you will see that the writing is justified (spread out) to fit into the columns.  Write a program that reads in two lines of text and justifies the lines to fit into a column width of 50.  Your program should include three functions:
- readline that reads in a line of text of not more than 50 characters, and returns its length
- countgaps that counts and returns the number of gaps between words in a line of text
- display that prints out a justified version of a line of text

When your program is running, the screen should look something like this:
Enter first line of text: Good morning how are you?
Enter second line of text: I’m good thanks, and how are you?

12345678901234567890123456789012345678901234567890
Good        morning       how       are       you?
I’m    good    thanks,    and    how    are   you?
The justification is done by counting the number of gaps in the text.  In the above examples, there are 4 and 6 gaps respectively in the two lines.  Then each gap must have an equal share of spaces added to it with any left over spaces being distributed one per gap until they run out.  In the first line of the example, the first gap has 8 spaces and the other three gaps have 7 spaces each.  In the second line of the example, the first five gaps have 4 spaces each and the last gap has 3 spaces.

Notes:
1.        If the text is longer than the column you must report an error – don't try and break it into two lines!
2.        Assume that the lines of text will have more than one word in them.
3.        Assume that all gaps are always typed in with only one space.
4.        Do not store the justified strings.  Simply display one character at a time in the justified format.
5.        Note the header line consisting of 123456789012345678....  This is useful to check your result.
我自己写了一半, display 部分完全没有头绪。。。哪位大神能帮帮我!~


题目翻译:如果你看一份报纸,你会发现,其内容是展开的,以适应列宽。写一个程序,读取两行文本,并调整该行的列宽度为50。你的程序应该包括三个功能:
- readline读取一行不超过50个字符的文本,并返回它的长度
- countgaps计数和返回一个文本行之间的间隙(空格)的个数
-display 实现结果显示
当你的程序运行时,屏幕应该看起来像这样:
输入第一行文字:Good morning how are you?
输入第二行文字: I’m good thanks, and how are you?
实现结果类似于下(应该是每个单词间插入空格使之到达50字符宽)
12345678901234567890123456789012345678901234567890
Good        morning       how       are       you?
I’m    good    thanks,    and    how    are   you?
调整过程通过计数字符串空格来实现。在上述例子中,两行中分别有4和6个间隙(空格)。然后每一个空格必须有一个相同的空间添加到它与任何留下的空间被分配一个每一个空格,直到他们用完。在第一行的例子中,如果第一个空格有8个空格,其他三个空格有7个空格。在第二行的例子中,前五个空格有4个空格,最后一个空格有3个空格。
注意:
1。如果文本过长(超过50字符),你必须报告一个错误,不要试图将其分为两行!
2。假设文本行中有一个以上的字。
三.假设所有输入的字符中 每个间隙都只输入一个空格。
4。Do not store the justified strings.  Simply display one character at a time in the justified format.
5。注意标题行由123456789012345678个……这是有用的检查你的结果。(翻译是有些错漏,哪位大神给点思路。。关于display 部分。。)
#include<stdio.h>
#include<string.h>
int readline(char sentence[1024]);
int countgaps(char sentence[1024],int length);
int length1;
int length2;
int gap1;
int gap2;
int main(){
    
    char sentence1[1024];
    char sentence2[1024];
    while(1){
        printf("Enter first line of text:");
        gets(sentence1);
        length1=readline(sentence1);
        if(length1!=-1){
            break;}
    }
    while(1){
        printf("Enter second line of text:");
        gets(sentence2);
        length2=readline(sentence2);
        if(length2!=-1){
            break;}
    }
    printf("%d,%d,",length1,length2);
    gap1=countgaps(sentence1,length1);
    gap2=countgaps(sentence2,length2);
    printf("%d,%d",gap1,gap2);
    
}

int readline( char sentence[1024]){
    int len;
    len=strlen(sentence);
    if(len>50){
        printf("Error! The text is longer than the column,");
        len=-1;
    } else{
        len=strlen(sentence);
        
    }
    return len;
}
int countgaps(char sentence[1024],int length){
    int gaps;
    int i;
    gaps=0;
    for(i=0;i<length;i++){
        if(sentence[i]==' '){
            gaps++;
        }
    }
    
    return gaps;
}

想知道小甲鱼最近在做啥?请访问 -> ilovefishc.com
回复

使用道具 举报

 楼主| 发表于 2016-10-4 12:38:41 | 显示全部楼层
感觉自己弄出来了。。
自己好蠢。。
//Qi Ji
//student No. 14209913

#include<stdio.h>
#include<string.h>
int readline(char sentence[1024]);
int countgaps(char sentence[1024],int length);
void display(char sentence[1024],int length,int gaps);
int length1;
int length2;
int gap1;
int gap2;
int main(){
    
    char sentence1[1024];
    char sentence2[1024];
    while(1){
        printf("Enter first line of text:");
        gets(sentence1);
        length1=readline(sentence1);
        if(length1!=-1){
            break;}
    }
    while(1){
        printf("Enter second line of text:");
        gets(sentence2);
        length2=readline(sentence2);
        if(length2!=-1){
            break;}
    }
    //printf("%d,%d,",length1,length2);
    gap1=countgaps(sentence1,length1);
    gap2=countgaps(sentence2,length2);
    //printf("%d,%d\n",gap1,gap2);
    display(sentence1,length1,gap1);
    printf("\n");
    display(sentence2,length2,gap2);
    printf("\n");
}

int readline( char sentence[1024]){
    int len;
    len=strlen(sentence);
    if(len>50){
        printf("Error! The text is longer than the column.");
        len=-1;
    } else{
        len=strlen(sentence);
        
    }
    return len;
}
int countgaps(char sentence[1024],int length){
    int gaps;
    int i;
    gaps=0;
    for(i=0;i<length;i++){
        if(sentence[i]==' '){
            gaps++;
        }
    }
    
    return gaps;
}
void display(char sentence[1024],int length,int gaps){
    int i;int extra;int extraspace;int extraadd;
    extra=50-length;
    if(gaps==0){        //avoid gaps=0,then extra/0 cause error!
        extraspace = -1;
        extraadd=-1;}
    else{
        extraspace=extra/gaps;//
        extraadd=extra%gaps;  //
    }
    int j;
    for(i=0;i<50;i++){
        printf("%c",sentence[i]);
        if(sentence[i]==' '){
            for(j=0;j<extraspace;j++){
                printf(" ");
            }
            if(extraadd>0){
                printf(" ");
                extraadd--;
            }
            
        }
        
    }
    
    
}
想知道小甲鱼最近在做啥?请访问 -> ilovefishc.com
回复

使用道具 举报

发表于 2017-1-1 19:09:06 | 显示全部楼层
你可以把它的要求写入文本然后进行读取再进行判断即可!我丫不是很懂!
想知道小甲鱼最近在做啥?请访问 -> ilovefishc.com
回复

使用道具 举报

您需要登录后才可以回帖 登录 | 立即注册

本版积分规则

小黑屋|手机版|Archiver|鱼C工作室 ( 粤ICP备18085999号-1 | 粤公网安备 44051102000585号)

GMT+8, 2024-11-27 20:29

Powered by Discuz! X3.4

© 2001-2023 Discuz! Team.

快速回复 返回顶部 返回列表