C++程序设计实验4

 

【问题描述】

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

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

【输出形式】There are 4 integers:234 700 346 267
【样例输入】sum=abc+234;while(abc==700)tab{ass=346++;bss=abc+267;

【样例输出】There are 4 integers:234 700 346 267

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

#include<bits/stdc++.h>
using namespace std;
void check (char*a)
{
	char *end=NULL;
	char temp[1000];
	string number[1000];
	int n=0;
	for(int i;;i++)
	{
		while(*a!='\0')
		{
			if((*a>='0'&&*a<='9'))
			{
				end=a+1;
				while((*end>='0'&&*end<='9'))
				{
					end++;
				}
				int t=0;
				while(a<end)
				{
					temp[t]=*a;
					t++;
					a++;
				}
				temp[t]='\0';
				number[n++]=temp;
				break;
			}
			a++;
		}
		if(*a=='\0')
		{
			break;
		}
	}
	cout<<"There are "<<n<<" integers:";
	for(int i=0;i<n;i++)
	{
		cout<<number[i];
		if(i<n-1)
		{
			cout<<" ";
		}
	}
}
int main()
{
	char a[1000];
	cin.getline(a,100);
	check(a);
}

【问题描述】

输入一个字符串,含有数字和非数字字符,如“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<bits/stdc++.h>
using namespace std;
void check (char*a)
{
	char *end=NULL;
	char temp_1[1000];
	char temp_2[1000];
	string word[1000];
	string number[1000];
	char other[1000];
	int w=0,n=0,o=0;
	for(int i;;i++)
	{
		while(*a!='\0')
		{
			if((*a>='a'&&*a<='z')||(*a>='A'&&*a<='Z'))
			{
				end=a+1;
				while((*end>='a'&&*end<='z')||(*end>='A'&&*end<='Z'))
				{
					end++;
				}
				int t=0;
				while(a<end)
				{
					temp_1[t]=*a;
					t++;
					a++;
				}
				temp_1[t]='\0';
				word[w++]=temp_1;
				break;
			}
			else if((*a>='0'&&*a<='9'))
			{
				end=a+1;
				while((*end>='0'&&*end<='9'))
				{
					end++;
				}
				int t=0;
				while(a<end)
				{
					temp_2[t]=*a;
					t++;
					a++;
				}
				temp_2[t]='\0';
				number[n++]=temp_2;
				break;
			}
			else
			{
				if(*a!=' ')
				{
					other[o++]=*a;
				}
			}
			a++;
		}
		if(*a=='\0')
		{
			break;
		}
	}
	cout<<"There are "<<n<<" integers:";
	for(int i=0;i<n;i++)
	{
		cout<<number[i];
		if(i<n-1)
		{
			cout<<" ";
		}
	}
	cout<<endl;
	cout<<"There are "<<w<<" words:";
	for(int i=0;i<w;i++)
	{
		cout<<word[i];
		if(i<w-1)
		{
			cout<<" ";
		}
	}
	cout<<endl;
	cout<<"There are "<<o<<" symbles:";
	for(int i=0;i<o;i++)
	{
		cout<<other[i];
		if(i<o-1)
		{
			cout<<" ";
		}
	}
}
int main()
{
	char a[1000];
	cin.getline(a,100);
	check(a);
}

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

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值