C++ #指针-token识别

【问题描述】

输入一个字符串,含有数字和非数字字符,如“sum=abc+234;while(abc==700)tab{ass=346++;bss=abc+267;}”,将其中连续的数字作为一个整数,依次存放到一个数组nums中。例如,234放在nums[0],700放在nums[1]……,统计共有多少个整数,并输出这些整数;将输入字符串中的连续的字母作为一个单词存放到一个数组words中;如上面的字符串中,sum放在words[0],abc放在words[1]……,统计共有多少个单词,并输出这些单词。其它所有字符存放到一个数组others中,如上面的字符串中,=放在others[0],+放在others[1]……,统计共有多少个字符,并输出这些字符。结合指针和自定义函数完成该题。

【输入形式】sum=abc+234;while(abc==700)tab{ass=346++;bss=abc+267;}

【输出形式】

There are 4 integers:234 700 346 267

There are 8 words:sum abc while abc tab ass bss abc

There are 16 symbles:= + ; ( = = ) { = + + ; = + ; }
【样例输入】sum=abc+234;while(abc==700)tab{ass=346++;bss=abc+267;}

【样例输出】

There are 4 integers:234 700 346 267

There are 8 words:sum abc while abc tab ass bss abc

There are 16 symbles:= + ; ( = = ) { = + + ; = + ; }

按照题目给定的输入输出完成。

#include <iostream>
#include <cstring>
#include <cstdlib>
using namespace std;

//函数声明,用于判断输入字符串中整数的个数,并将整数提取到数组中
int isN(const char* str, int nums[]);  
//函数声明,用于判断输入字符串中单词的个数,并将单词提取出来存储为动态分配的字符串指针
int isW(const char* str, char** words);  
//函数声明,用于判断输入字符串中非数字、非字母、非空格的符号个数,并将符号提取到数组中
int isO(const char* str, char others[]);  

int main()
{
    char STR[1000];
    //从标准输入读取一行字符串,最多读取999个字符(留一个位置给'\0'结束符),存入STR数组
    cin.getline(STR, 1000);  

    int nums[100];
    //调用isN函数统计字符串中的整数个数,并获取整数数组
    int numCount = isN(STR, nums);  
    cout << "There are " << numCount << " integers:";
    for (int i = 0; i < numCount; i++)
        cout << nums[i] << " ";
    cout << "\n";

    char* words[100];
    //调用isW函数统计字符串中的单词个数,并获取单词指针数组
    int wordCount = isW(STR, words);  
    cout << "There are " << wordCount << " words:";
    for (int i = 0; i < wordCount; i++)
        cout << words[i] << " ";
    cout << "\n";

    char others[1000];
    //调用isO函数统计字符串中非数字、非字母、非空格的符号个数,并获取符号数组
    int othersCount = isO(STR, others);  
    cout << "There are " << othersCount << " symbles:";
    for (int i = 0; i < othersCount; i++)
        cout << others[i] << " ";
    cout << "\n";

    //释放动态分配用于存储单词的内存空间,防止内存泄漏
    for (int i = 0; i < wordCount; i++)  
        delete[] words[i];

    return 0;
}

//函数定义,用于判断输入字符串中整数的个数,并将整数提取到nums数组中
int isN(const char* str, int nums[])
{
    int count = 0;  //用于统计提取到的整数个数
    const char* p = str;  //指针p用于遍历输入字符串

    while (*p!= '\0')    //只要字符串未结束,就继续遍历
    {
        if (isdigit(*p))  //如果当前字符是数字
            {
            int num = 0;  
            //循环处理连续的数字字符,将其转换为对应的整数
            while (isdigit(*p))
            {  
                num = num * 10 + (*p - '0');  //将数字字符转换为数字并累加到num
                p++;  //移动指针到下一个字符
            }
            nums[count++] = num;  //将转换好的整数存入nums数组,并更新个数统计
        }
        else
            p++;  //如果当前字符不是数字,直接移动指针到下一个字符继续判断
    }
    return count;  //返回提取到的整数个数
}

// 函数定义,用于判断输入字符串中单词的个数,并将单词提取出来存储为动态分配的字符串指针
int isW(const char* str, char** words)
{
    int count = 0;  //用于统计提取到的单词个数
    const char* p = str;  //指针p用于遍历输入字符串
    const char* start = p;  //用于记录单词的起始位置

    while (*p!= '\0')  //只要字符串未结束,就继续遍历
    {
        if (isalpha(*p))  //如果当前字符是字母,说明可能开始一个新单词
        {
            start = p;  //更新单词起始位置
            //循环找到单词结束的位置(即遇到非字母字符)
            while (isalpha(*p)) 
                p++;
            int wordLen = p - start;  // 计算单词的长度
            //动态分配内存空间来存储这个单词,长度为单词长度加1(用于存放'\0'结束符)
            char* newWord = new char[wordLen + 1];  
            //将单词从原字符串复制到新分配的内存空间中
            strncpy(newWord, start, wordLen);  
            newWord[wordLen] = '\0';  //添加字符串结束符
            words[count++] = newWord;  //将单词指针存入words数组,并更新个数统计
        }
        else
            p++;  //如果当前字符不是字母,直接移动指针到下一个字符继续判断
    }
    return count;  //返回提取到的单词个数
}

//函数定义,用于判断字符串中非数字、非字母、非空格的符号个数,并将符号提取到others数组中
int isO(const char* str, char others[])
{
    int count = 0;  //用于统计提取到的符号个数
    const char* p = str;  //指针p用于遍历输入字符串

    while (*p!= '\0')  //只要字符串未结束,就继续遍历
    {
        //如果当前字符既不是数字,也不是字母,并且不是空格,说明是符号
        if (!isdigit(*p) &&!isalpha(*p) && *p!= ' ')
        {  
            others[count++] = *p;  //将符号存入others数组,并更新个数统计
            p++;  //移动指针到下一个字符
        }
        else
            p++;  //如果当前字符不符合符号条件,直接移动指针到下一个字符继续判断
    }
    return count;  //返回提取到的符号个数
}

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值