鱼C论坛

 找回密码
 立即注册
查看: 2363|回复: 1

[原创] 小甲鱼:输入任意空格数字求和

[复制链接]
发表于 2013-8-4 14:34:35 | 显示全部楼层 |阅读模式

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

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

x
   前段时间看了算法珠玑这本书受益颇多,最近看c++视频有这么一道题,试着用边界扫描的思想编了一下。   以下是本人代码:
   #include "stdio.h"#include "string.h"
#include "ctype.h"

#define MAXSIZE 100
//最大的输入字节数

int main(void){
    char str[MAXSIZE];
    int sum;
    printf("请输入任意数量的空格和数字,对其进行求和\n");
    while(gets(str) != NULL){
        int i;
        int starthere = 0;
         int sum = 0;

        for(i = 0;i <strlen(str)+1;i++){

            if(isspace(str[i]) || (str[i] == '\0') ) {  //对于空格进行边界扫描,重新定位
                sum +=starthere;
                starthere = 0;
                continue;
            }
            else {
                starthere = starthere *10 +(str[i] - '0');
                //计算两个空格间的数字的大小
                }


            }
            printf("%d.\n",sum);
        }

    }




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

使用道具 举报

发表于 2013-8-5 12:00:55 | 显示全部楼层
楼主加油,鱼C加油!我们都看好你哦!
想知道小甲鱼最近在做啥?请访问 -> ilovefishc.com
回复 支持 反对

使用道具 举报

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

本版积分规则

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

GMT+8, 2024-5-6 03:48

Powered by Discuz! X3.4

© 2001-2023 Discuz! Team.

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