C++程序设计实验4

简介: 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

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

1. #include<bits/stdc++.h>
2. using namespace std;
3. void check (char*a)
4. {
5.  char *end=NULL;
6.  char temp[1000];
7.  string number[1000];
8.  int n=0;
9.  for(int i;;i++)
10.   {
11.     while(*a!='\0')
12.     {
13.       if((*a>='0'&&*a<='9'))
14.       {
15.         end=a+1;
16.         while((*end>='0'&&*end<='9'))
17.         {
18.           end++;
19.         }
20.         int t=0;
21.         while(a<end)
22.         {
23.           temp[t]=*a;
24.           t++;
25.           a++;
26.         }
27.         temp[t]='\0';
28.         number[n++]=temp;
29.         break;
30.       }
31.       a++;
32.     }
33.     if(*a=='\0')
34.     {
35.       break;
36.     }
37.   }
38.   cout<<"There are "<<n<<" integers:";
39.   for(int i=0;i<n;i++)
40.   {
41.     cout<<number[i];
42.     if(i<n-1)
43.     {
44.       cout<<" ";
45.     }
46.   }
47. }
48. int main()
49. {
50.   char a[1000];
51.   cin.getline(a,100);
52.   check(a);
53. }

【问题描述】

输入一个字符串,含有数字和非数字字符,如“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:= + ; ( = = ) { = + + ; = + ; }

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

1. #include<bits/stdc++.h>
2. using namespace std;
3. void check (char*a)
4. {
5.  char *end=NULL;
6.  char temp_1[1000];
7.  char temp_2[1000];
8.  string word[1000];
9.  string number[1000];
10.   char other[1000];
11.   int w=0,n=0,o=0;
12.   for(int i;;i++)
13.   {
14.     while(*a!='\0')
15.     {
16.       if((*a>='a'&&*a<='z')||(*a>='A'&&*a<='Z'))
17.       {
18.         end=a+1;
19.         while((*end>='a'&&*end<='z')||(*end>='A'&&*end<='Z'))
20.         {
21.           end++;
22.         }
23.         int t=0;
24.         while(a<end)
25.         {
26.           temp_1[t]=*a;
27.           t++;
28.           a++;
29.         }
30.         temp_1[t]='\0';
31.         word[w++]=temp_1;
32.         break;
33.       }
34.       else if((*a>='0'&&*a<='9'))
35.       {
36.         end=a+1;
37.         while((*end>='0'&&*end<='9'))
38.         {
39.           end++;
40.         }
41.         int t=0;
42.         while(a<end)
43.         {
44.           temp_2[t]=*a;
45.           t++;
46.           a++;
47.         }
48.         temp_2[t]='\0';
49.         number[n++]=temp_2;
50.         break;
51.       }
52.       else
53.       {
54.         if(*a!=' ')
55.         {
56.           other[o++]=*a;
57.         }
58.       }
59.       a++;
60.     }
61.     if(*a=='\0')
62.     {
63.       break;
64.     }
65.   }
66.   cout<<"There are "<<n<<" integers:";
67.   for(int i=0;i<n;i++)
68.   {
69.     cout<<number[i];
70.     if(i<n-1)
71.     {
72.       cout<<" ";
73.     }
74.   }
75.   cout<<endl;
76.   cout<<"There are "<<w<<" words:";
77.   for(int i=0;i<w;i++)
78.   {
79.     cout<<word[i];
80.     if(i<w-1)
81.     {
82.       cout<<" ";
83.     }
84.   }
85.   cout<<endl;
86.   cout<<"There are "<<o<<" symbles:";
87.   for(int i=0;i<o;i++)
88.   {
89.     cout<<other[i];
90.     if(i<o-1)
91.     {
92.       cout<<" ";
93.     }
94.   }
95. }
96. int main()
97. {
98.   char a[1000];
99.   cin.getline(a,100);
100.  check(a);
101. }
相关文章
|
20天前
|
算法 开发工具 计算机视觉
【零代码研发】OpenCV实验大师工作流引擎C++ SDK演示
【零代码研发】OpenCV实验大师工作流引擎C++ SDK演示
23 1
|
9天前
|
C++
C++ : 程序设计简单实例
C++ : 程序设计简单实例
13 3
|
9天前
|
安全 C++
C++:程序设计实例
C++:程序设计实例
12 2
|
21天前
|
C++
C++程序设计实践一上(题目来自杭州电子科技大学ACM)
C++程序设计实践一上(题目来自杭州电子科技大学ACM)
12 2
|
21天前
|
存储 搜索推荐 C++
C++课程设计实验杭州电子科技大学ACM题目(中)
C++课程设计实验杭州电子科技大学ACM题目(中)
15 1
|
21天前
|
C++
C++程序设计实践一下(题目来自杭州电子科技大学ACM)
C++程序设计实践一下(题目来自杭州电子科技大学ACM)
16 1
|
19小时前
|
存储 JavaScript 前端开发
程序与技术分享:C++程序设计实验考试准备资料(2019级秋学期)
程序与技术分享:C++程序设计实验考试准备资料(2019级秋学期)
|
1天前
|
C++
技术经验分享:C++程序设计的技巧
技术经验分享:C++程序设计的技巧
|
1月前
|
C++
【期末不挂科-C++考前速过系列P6】大二C++实验作业-模板(4道代码题)【解析,注释】
【期末不挂科-C++考前速过系列P6】大二C++实验作业-模板(4道代码题)【解析,注释】
【期末不挂科-C++考前速过系列P6】大二C++实验作业-模板(4道代码题)【解析,注释】
|
14天前
|
存储 算法 安全
面向对象程序设计C++
面向对象程序设计C++