鱼C论坛

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

求指教

[复制链接]
发表于 2019-4-14 00:19:46 | 显示全部楼层 |阅读模式

马上注册,结交更多好友,享用更多功能^_^

您需要 登录 才可以下载或查看,没有账号?立即注册

x
#include <stdio.h>

int main()
{
        char str[1024];
        char *p = str;          // 用于间接寻址
        char *pos[1024] = {0};  // 记录每个单词的地址
        int len = 0;
        int cChar = 0, cWord = 0; // cChar 统计字符数, cWord 统计单词数
        int max = 0, i = 0, j;

        printf("请输入一个英文句子:");
        // 接收输入,顺带统计用户实际输入了多少个字符
        while ((str[len++] = getchar()) != '\n' && len + 1 < 1024)                             这个while 条件真的时候到底怎么执行,网上搜说没大括号执行第一句,但这样看程序真的不通,我自己用大括号括起来也也执行不对。。                                             
                ;
        str[len-1] = '\0'; // str[len]存放的是'\n',将其替换为'\0'

        if (*p != ' ')
        {
                pos[i++] = p; // 记录第一个单词的地址
                cWord++;
        }

        while (len--)
        {
                if (*p++ == ' ')
                {
                        // 判断最大字符数
                        max = cChar > max ? cChar : max;
                        cChar = 0;

                        // 到底了,退出循环
                        if (*p == '\0')
                        {
                                break;
                        }

                        // 单词数加一
                        if (*p != ' ')
                        {
                                pos[i++] = p;
                                cWord++;
                        }
                }
                else // 没有else会把空格统计进去
                {
                        cChar++;
                }
        }
        max = --cChar > max ? cChar : max; // 最后会算多一个'\0',所以减去

        // 申请可变长数组,max+1,否则'\0'放不下
        char result[cWord][max+1];

        // 将分割好的单词放进二维数组里
        for (i = 0; i < cWord; i++)
        {
                for (j = 0; *(pos[i]+j) != ' ' && *(pos[i]+j) != '\0'; j++)
                {
                        result[i][j] = *(pos[i]+j);
                }
                result[i][j] = '\0';
        }

        // 打印结果
        printf("分割结果已存放到result[%d][%d]的二维数组中...\n", cWord, max+1);
        printf("现在依次打印每个单词:\n");
        for (i = 0; i < cWord; i++)
        {
                printf("%s\n", result[i]);
        }

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

使用道具 举报

发表于 2019-4-14 02:04:07 From FishC Mobile | 显示全部楼层
本帖最后由 舞劲繁华 于 2019-4-14 02:09 编辑

while (1)
{
        str[len] = getchar();
   
    // 还记得甲鱼说过的短路规则吧 &&
    // while ((str[len++] = getchar()) != '\n' && len + 1 < 1024)
        if (str[len++] == '\n')
            break;
        if (len+1 >= 1024)
            break;
}
想知道小甲鱼最近在做啥?请访问 -> ilovefishc.com
回复 支持 反对

使用道具 举报

发表于 2019-4-14 10:36:37 | 显示全部楼层
本帖最后由 jackz007 于 2019-4-14 16:15 编辑
        while ((str[len ++] = getchar()) != '\n' && len + 1 < 1024)  ;  // 循环从键盘读取字符,直到键入回车结束,这时,如果键入的字符数量不到 1024,那么,就加以保持,如果超出,就截取前面的1024个字符,最后保存到 str[] 中。
      楼主的代码确实有一些问题,我已经调通,楼主可以参考一下。
#include <stdio.h>

int main()
{
        char str[1024]                                                                                                            ;
        char * p = str                                                                                                            ;
        char * pos[1024] = {0}                                                                                                    ;
        int len = 0                                                                                                               ;
        int cChar = 0 , cWord = 0                                                                                                 ;
        int max = 0 , i = 0 , j                                                                                                   ;

        printf("请输入一个英文句子:")                                                                                            ;
        while ((str[len ++] = getchar()) != '\n' && len + 1 < 1024)                                                               ;
        str[len - 1] = '\0'                                                                                                       ; 
        if (* p != ' ') {
                pos[i ++] = p                                                                                                     ;
                cWord ++                                                                                                          ;
        }
        while (len --) {
                if (* p ++ == ' ') {
                        max = cChar > max ? cChar : max                                                                           ;
                        cChar = 0                                                                                                 ;
                        if (* p == '\0') break                                                                                    ;
                        if (* p != ' ') {
                                pos[i ++] = p                                                                                     ;
                                cWord ++                                                                                          ;
                        }
                } else {
                        cChar ++                                                                                                  ;
                }
        }
        max = --cChar > max ? cChar : max                                                                                         ;
        char result[cWord][max + 1]                                                                                               ;
        for (i = 0 ; i < cWord ; i ++) {
                for (j = 0 ; * (* (pos + i) + j) != ' ' && * (* (pos + i) + j) != '\0' ; j ++) result[i][j] = * (* (pos + i) + j) ;
                result[i][j] = '\0'                                                                                               ;
        }
        printf("分割结果已存放到result[%d][%d]的二维数组中...\n" , cWord , max + 1)                                               ;
        printf("现在依次打印每个单词:\n")                                                                                        ;
        for (i = 0 ; i < cWord ; i ++) printf("%s\n" , result[i])                                                                 ;
}
      感觉这个代码线索不够清晰,于是,又重新改写了一遍,谨供楼主参考。
#include <stdio.h>

int main()
{
        char str[1024] , * p , * pos[1024] = {0}                                                                                  ;
        int cc , cm , cw , i , j , len                                                                                            ;

        printf("请输入一个英文句子:")                                                                                            ;
        for(len = 0 ; (str[len] = getchar()) != '\n' && len < 1024 ; len ++)                                                      ;
        str[len] = '\0'                                                                                                           ;
        for(cm = 0 , cw = 0 , p = str ; * p ;) {
                for(; * p && * p == ' ' ; p ++)                                                                                   ;
                if (* p) pos[cw ++] = p                                                                                           ;
                for(cc = 0 ; * p && * p != ' ' ; p ++) cc ++                                                                      ;
                if (cc > cm) cm = cc                                                                                              ;
        }
        char result[cw][cm + 1]                                                                                                   ;
        for (i = 0 ; i < cw ; i ++) {
                for (j = 0 ; * (* (pos + i) + j) != ' ' && * (* (pos + i) + j) != '\0' ; j ++) result[i][j] = * (* (pos + i) + j) ;
                result[i][j] = '\0'                                                                                               ;
        }
        printf("分割结果已存放到result[%d][%d]的二维数组中...\n" , cw , cm + 1)                                                   ;
        printf("现在依次打印每个单词:\n")                                                                                        ;
        for (i = 0 ; i < cw ; i ++) printf("%s\n" , result[i])                                                                    ;
}
想知道小甲鱼最近在做啥?请访问 -> ilovefishc.com
回复 支持 反对

使用道具 举报

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

本版积分规则

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

GMT+8, 2024-10-3 14:29

Powered by Discuz! X3.4

© 2001-2023 Discuz! Team.

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