|
发表于 2022-10-4 22:55:48
|
显示全部楼层
请告诉我,我哪里做错了
- $ ls
- main.c
- $ cat main.c
- #include <stdio.h>
- using namespace std;
- int main()
- {
- char a[]= "U12a 34b, 56c";
- int b=0;
- char *c = new char [ sizeof(a)];
- char *d = new char [ sizeof(a)];
- for(int i=0;i<sizeof(a);i++)
- {
- if(a[i]==32)
- {
- c[i]='0';
- d[i]=a[i];
- }
- else
- {
- c[i]=a[i];
- d[i]='0';
- }
- b++;
- }
- c[ sizeof(a)]='\n';
- d[ sizeof(a)]='\n';
- printf("共有%d个字符:\n",b);
- printf("c == %s\n",c);
- printf("d == %s",d); // d尾部自带'\n'
- int *sum = new int [ b ];
- for(int i=0;i<b;i++)
- {
- if(d[i]=='0')
- sum[i]=1;
- else
- sum[i]=0;
- }
- printf("sum ");
- for(int i=0;i<b;i++)
- printf("%d",sum[i]);
- //找到空格数,和各段字符数时,就可以创建char a[x][y] 了;
- // ==0 && ==1 时,一定是字符 y列就可以读入字符;
- // ==0 && ==32时,一定是空格,是空格,x行,就可以换行了;
- delete [] c,d,sum;
- return 0;
- }
- $ gcc --version
- gcc (GCC) 12.2.0
- Copyright (C) 2022 Free Software Foundation, Inc.
- This is free software; see the source for copying conditions. There is NO
- warranty; not even for MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.
- $ gcc -g -Wall -o main main.c
- main.c:3:1: error: unknown type name ‘using’
- 3 | using namespace std;
- | ^~~~~
- main.c:3:17: error: expected ‘=’, ‘,’, ‘;’, ‘asm’ or ‘__attribute__’ before ‘std’
- 3 | using namespace std;
- | ^~~
- main.c: In function ‘main’:
- main.c:9:15: error: ‘new’ undeclared (first use in this function)
- 9 | char *c = new char [ sizeof(a)];
- | ^~~
- main.c:9:15: note: each undeclared identifier is reported only once for each function it appears in
- main.c:9:19: error: expected ‘,’ or ‘;’ before ‘char’
- 9 | char *c = new char [ sizeof(a)];
- | ^~~~
- main.c:10:19: error: expected ‘,’ or ‘;’ before ‘char’
- 10 | char *d = new char [ sizeof(a)];
- | ^~~~
- main.c:33:20: error: expected ‘,’ or ‘;’ before ‘int’
- 33 | int *sum = new int [ b ];
- | ^~~
- main.c:52:5: error: ‘delete’ undeclared (first use in this function)
- 52 | delete [] c,d,sum;
- | ^~~~~~
- main.c:52:13: error: expected expression before ‘]’ token
- 52 | delete [] c,d,sum;
- | ^
- $ ls
- main.c
- $
复制代码
你能分清C语言和C++吗?
|
|