北航研究生复试2014上机第三题:冒号对齐

题目:,排版题。输入若干行字符,表示某电影的演职员表,每行只有一个冒号,冒号前面是职位,冒号后面是姓名,要求把各行冒号对齐,删除多余空格后输出。先输入一个数字,表示排版要求的冒号位置,该位置号保证比各行冒号前的最大字符数还要大。再输入若干行字符,最多50行,每行最多100个字符,除空格、制表符和回车之外都是有效字符,要求每行的冒号处于格式要求的位置,冒号两边与有效单词之间各有一个空格,冒号前面的单词之间只有一个空格(删除多余的空格和制表符),在冒号左边右对齐,前面全由空格填充,冒号后面的单词之间也只有一个空格,在冒号右边左对齐,最后一个单词后不加空格直接换行。

#include<stdio.h>
#include<stdlib.h>
#include<string.h>


#define ROW 50
#define COL 200

void ReSetStr(char* str, int pos);      //通过':'的位置pos构建新的字符串
int GetMao(char* str ,int len);         //得到原字符串':'的位置 

/*
**  str原字符串 从[begin, end]处的字符 删除多余的空格和制表符  复制到
**  newstr新字符串从pos开始的位置,
**  tag是标记,tag=0,从pos向前复制;tag=1,从pos向后复制
*/
void strcpy_part(char str[COL],int begin,int end,char newstr[COL],int pos, int tag);

bool Judge(char c);                     //判断字符c是否为空格或者制表符

int main(){

    int posOfmao;
    scanf("%d", &posOfmao);
    getchar();
    char str[ROW][COL] = {0};
    int i = 0;
    int size = 0;
    char ch = getc(stdin);
    while(ch != '\n'){          //输入
        while(1){
            if(ch != '\n'){
                str[size][i++] = ch;
                ch = getc(stdin);
            }
            else{
                ++size; i = 0;
                ch = getc(stdin);
                break;
            }
        }
    }

    for(i = 0; i < size; i++)   //处理
        ReSetStr(str[i],posOfmao);

    for(i = 0; i < size; i++)   //输出
        printf("%s\n", str[i]);

    return 0;
}

void ReSetStr(char* str, int pos){
    int len = strlen(str);
    char newstr[COL];
    for(int i = 0; i < COL; i++)
        newstr[i] = ' ';
    int pos_be = GetMao(str ,len);
    int posbefore = pos_be;
    int posafter = len - pos_be - 1;

    newstr[pos - 1] = ':';
    //newstr[pos] = newstr[pos - 2] = ' ';
    strcpy_part(str,0,posbefore - 1,newstr,pos - 3,0);          //向前复制
    strcpy_part(str,posbefore + 1,len - 1 , newstr, pos + 1,1);//向后复制
    strcpy(str,newstr);                                        //整体复制
}   


int GetMao(char* str ,int len){
    for(int i = 0; i < len; i++)
        if(str[i] == ':')
            return i;
    return -1;
}

void strcpy_part(char str[COL],int begin,int end,char newstr[COL],int pos, int tag){
    if(tag == 0){//从;往前
        int del = 1;
        while(end >= begin){
            if(Judge(str[end]) == false){   //为空格或者制表符
                if(del == 1){   //已经写入一个空格 需要删除多余的空格或制表符
                    while(!Judge(str[end])){//循环找到不为空格或制表符的下标
                        --end;
                    }
                    del = 0;
                }else if(del == 0){         //还没有写入,写入一个空格
                    newstr[pos--] = ' ';
                    del = 1;
                    --end;
                }
            }else{                          //不为空格或者制表符,直接复制
                newstr[pos--] = str[end--];
                del = 0;
            }
        }
    }else if(tag == 1){//从;往后
        int del = 1;
        while(begin <= end){
            if(Judge(str[begin]) == false){ //为空格或者制表符
                if(del == 1){   //已经写入一个空格 需要删除多余的空格或制表符
                    while(!Judge(str[begin])){  //循环找到不为空格或制表符的下标
                        ++begin;
                    }
                    del = 0;
                }else if(del == 0){             //还没有写入,写入一个空格
                    newstr[pos++] = ' ';
                    del = 1;
                    ++begin;
                }
            }else{                              //不为空格或者制表符,直接复制
                newstr[pos++] = str[begin++];
                del = 0;
            }
        }
        newstr[pos] = '\0';                     //末尾结束,添加\0
    }
}

bool Judge(char c){
    if(c == ' ' || c == '\t')
        return false;
    return true;
}

输入:
50
we are family : hero dfghk
sfghfh giyio: gihikjk jnk
you rteo vc:fvjodf
hkdjlgf wreterh cxv:pgods
输出
这里写图片描述

  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值