{ int n; cin >> n; int i, j, k; i = j = k = 1;
int a[15][15];//随机!
for (i = 1; i <= n; i++) { for (j = 1; j <= n; j++) cout << setw(10) << a[i][j]; cout << '\n'; } cout << '\n'; int b[15][15] = {0}; for (i = 1; i <= n; i++) { for (j = 1; j <= n; j++) cout << setw(3) << b[i][j]; cout << '\n'; } cout << '\n'; int c[15][15] = {{1,1,1,1},{1,1,1,1}}; for (i = 1; i <= n; i++) { for (j = 1; j <= n; j++) cout << setw(3) << c[i][j]; cout << '\n'; } cout << '\n';
int d[15][15]; memset(d, 0, sizeof(d)); //a内所有元素变为0 for (i = 1; i <= n; i++) { for (j = 1; j <= n; j++) cout << setw(3) << c[i][j]; cout << '\n'; } } /******* * 0 0 0 0
0 0 0 0
0 0 0 0
0 251559597 32614 0
0 0 0 0
0 0 0 0
0 0 0 0
0 0 0 0
1 1 1 1
1 1 1 1
0 0 0 0
0 0 0 0
0 0 0 0
0 0 0 0
0 0 0 0
0 0 0 0 ******/
int n, a[15][15]; memset(a, 0, sizeof(a));//a内所有元素变为0 int n, a[15][15]={0};//a内所有元素变为0 int n, a[15][15]=0; //错误
//将课后例题上机!!
一、动态内存
1.malloc的使用
int*p=(int*) malloc (sizeof(int)*128) 及分配128个整形存储单元,并将这128个连续的整形存储单元的首地值存储在指针变量p中; 其中 (int*) 为强制类型妆换为指针类型 sizeof(int)为整型长度的空间
unsigned int* res = (unsigned int*)malloc(sizeof(unsigned int) * n); /* 之后直接用res[1], res[2],详见is_prime函数 */
//还有图领班3次linklist、5次的array_stack、array_queue.
/*以下为结构体数组*/ Node* arrayOfNode = (Node*)malloc( sizeof(Node) * len); for(int i = 0; i < len; i++){ arrayOfNode[i].data1 = i; (arrayOfNode+i)->data2 = 2*i; } //注意使用方法,详见图灵班第三次课
//另外数组笔记也有,去那边更新!! //将0000042输出为42,将a[7]变为a[2],maxn=7 for(i=maxn-1;i>0;i--) if(a[i]>0) break;//将7往右,是0 ,7就减去1;至第六个数不是0的,7-1-1-1-1-1=2 printf(" i is %i\n",i);//i=2 for(;i>0;i--) printf("%d",a[i]);将42输出为24,见下 //另外 int i; int a[3]= {0,4,2};//a[0]=0;a[1]=4,a[2]=2,我们要输出24 for(i=2;i>0;i--) printf("%i",a[i]);//输出成24 for(i=1;i<=2;i++) printf("%i",a[i]);//输出42
一维数组
类型标识符 数组名[常量表达式];
例如
int a[10];
它表示数组名为 a,此数组为整型,有 10 个元素。表示 a 数组有 10 个元素,下标从 0 开始,这 10 个元素是:
a[0],a[1],a[2],a[3],a[4],a[5],a[6],a[7], a[8],a[9]。 注意最后一个元素是 a[9]而不是a[10]。在对全部数组元素赋初值时,可以不指定数组长度。 例如
int a[5]={1,2,3,4,5};
可以写成 int a[]={1,2,3,4,5};
输入输出
#include <iostream> using namespace std; int main( ) {int i,a[10]; for (i=0;i<=9;i++) a[i]=i; for (i=9;i>=0;i--) cout<<a[i]<<″ ″; cout<<endl; return 0; } 运行结果如下: 9 8 7 6 5 4 3 2 1 0
二维数组 1定义二维数组
的一般形式为 类型标识符 数组名[常量表达式][常量表达式] 例如 float a[3][4],b[5][10];
定义 a 为 3×4(3 行 4 列)的单精度数组
3.
int a[3][4]={{1,2,3,4},{5,6,7,8},{9,10,11,12}};int a[3][4]={{1},{},{5,6}};
数组元素为
1 0 0 0
0 0 0 0
5 6 0 04.如果对全部元素都赋初值(即提供全部初始数据),则定义数组时对第一维的长度可以不指定,但第二维的长度不能省。 如
int a[3][4]={1,2,3,4,5,6,7,8,9,10,11,12};
可以写成
int a[][4]={1,2,3,4,5,6,7,8,9,10,11,12};
系统会根据数据总个数分配存储空间,一共 12 个数据,每行 4 列,当然可确定为 3 行。将一个二维数组行和列元素互换,存到另一个二维数组中。 例如 a= 1 2 3 b= 1 4 4 5 6 2 5 3 6 程序如下: #include <iostream> using namespace std; int main( ) { inta[2][3]={{1,2,3},{4,5,6}}; intb[3][2],i,j; cout<<″array a:″<<endl; for (i=0;i<=1;i++) { for (j=0;j<=2;j++) { cout<<a[i][j]<<″ ″; b[j][i]=a[i][j]; } cout<<endl; } cout<<″array b:″<<endl; for (i=0;i<=2;i++) { for(j=0;j<=1;j++) cout<<b[i][j]<<″ ″; cout<<endl; } return 0; } 运行结果如下: array a://i=0 1 2 2行 j=0 1 2 3列 1 2 3// 4 5 6// array b: 1 4//i=0 1 2 共3行 2 5//j=0 1 共2列 3 6
例5.5 有一个3×4的矩阵,要求编程序求出其中值最大的那个元素的值,以及 其所在的行号和列号。
#include<iostream> using namespace std; int main() { int i,j,row=0,column=0,max; int a[3][4]={{5,12,23,56},{19,28,37,46},{-12,-34,6,8}}; for(i=0;i<=2;i++) for(j=0;j<=3;j++) if(a[i][j]>max) { max=a[i][j]; row=i; column=j; } cout<<"max="<<max<<",row="<<",column="<<column<<endl; }
#include<iostream> using namespace std; int main(){ int max_value(int a,int b); int i,j,row=0,column=0,max ; int a[3][4]={{5,12,23,56},{19,28,37,46},{-12,-34,6,8}}; max=a[0][0]; for(i=0;i<=2;i++){ for(j=0;j<=3;j++) {
max_value(a[i][j],max);
max=max_value(a[i][j],max);
if(max==a[i][j]) { row=i; column=j; } } } cout<<"max="<<max<<",row="<<row<<",column="<<column<<endl; } int max_value(int a,int b) { if(a>b) return a; else return b; }#include<iostream> using namespace std; int main() { int max_value(int array[][4]); int a[3][4]={{5,12,23,56},{19,28,37,46},{-12,-34,6,8}}; max_value(a); cout<<"max vale is"<<max_value(a)<<endl; } int max_value(int array[][4]) { int i,j,max; max=array[0][0]; for(i=0;i<3;i++) for(j=0;j<4;j++){ if(array[i][j]>max) max=array[i][j]; } return max; }
头文件 string.h ctring 针对c、c++中字符数组 string 针对c++中string类
字符数组、字符串常量
1.字符数组的输入输出可以有两种方法:
(1)
逐个字符输入输出char[a];
for(i=0;i<n;i++)
for(j=0;j<n;j++)
cin>>a[i][j];
for(i=0;i<n;i++)
for(j=0;j<n;j++)
cout<<a[i][j];
(2)//字符数组 放字符串
将整个字符串一次输入或输出。例如有以下程
序段char str[20];
cin>>str;
cout<<str;
char c[80]; cin.getline(c,80); cout<<c<<endl;(3)cin.getline使用
a[12];
使用cin.getline(a, 14);!!必须!落谷p1055
(4) 用字符数组放字符串
char mod[12] = "0123456789X"; //必须12 11错! 字符串数组必须有\0 !!!#include <iostream> #include<string.h> using namespace std; int main() { char str0[80]; cout<<"str0="; cin.getline(str0,80); cout<<endl<<"str0="<<str0<<endl;//str0=deautiful girl ;str0=deautiful girl char str1[20]; cout<<"input str1=";//input str1=beautiful girl ; str1=beautiful cin>>str1; cout<<"str1="; cout<<str1; cout<<endl; cout<<"str1[1]="<<str1[1];//e char str2[]="I an fine"; cout<<endl<<"str2="<<str2<<endl; char c[6]={'h','e','l','l','o','\0'};//c[i]= hello char u[6]={'h','e','l','l','o'};//u[i]=hello 如果数值个数5小于数组长度6,后面的字节全部为空字符(即‘\0’) char w[ ]={'h','e','l','l','o'};//w[i]=helloytyhf 超过5的随机安排 char x[ ]={'h','e','l','l','o','\0'};//x[i]=hello 必须加\0 char v[6]={"hello"};//字符串自动补\0 必须6!!!! cout << "u[i]="; for (int i = 0; i <= 5; i++) cout << u[i]; cout<<endl; char f[3][2] = {{'f','d'}, {'o'}}; cout << "f[i]="<<endl; for(int i=0;i<=2;i++) { for(int j=0;j<=1;j++) cout<<f[i][j]; cout<<endl; } char g[10]; // cout<<"g[10]="<<g[10]<<endl;//越界了!!!最多a[9]
/*****以上 f[i]= fd o ********/ }
字符串链接用字符数组 //pta 3-16
#include<iostream> using namespace std; int main() { char s1[101],s2[101]; int i=0; int j=0; cin>>s1;//s1=012 cin>>s2; while(s1[i]!='\0') { i++;//i=1 2 3 多了一个! } while(s2[j]!='\0') { s1[i]=s2[j];//第一个直接赋!!!!! i++; j++; } s1[i]='\0';//!!!!!!!!!!!!!!!! cout<<s1;//!!!! }
其他方法
using namespace std; int main() { string s1,s2; cin>>s1>>s2; cout<<s1+s2; }
课件补充
以下正确
![]()
以下等价!
非法
合法
输入三个字符串按大小输出。 int main() { char s1[80],s2[80],s3[80]; cout<<"Input 3 strings:\n"; cin.getline(s1); cin.getline(s2); cin.getline(s3); if(strcmp(s1,s2)>0) changed(s1,s2); if(strcmp(s1,s3)>0) changed(s1,s3); if(strcmp(s2,s3)>0)changed(s2,s3); cout<<"sorted:"<<endl<<endl; cout<<s1<<endl<<s2<<endl<<s3<<endl; return 0; } void changed(char str1[],char str2[]) { char str3[80]; strcpy(str3,str1); strcpy(str1,str2); strcpy(str2,str3); } 输入10个字符串按大小输出。 int main() {char ss[10][80]; int i,j; cout<<"Input 10 strings:\n"; for(i=0;i<10;i++) cin.getline (ss[i],80); for(i=0;i<9;i++)//冒泡排序 for(j=0;j<9-i;j++) if(strcmp(ss[j],ss[j+1])>0) changed(ss[j],ss[j+1]); cout<<"sorted:"<<endl<<endl; for(i=0;i<10;i++) cout<<ss[i]<<endl; } void changed(char str1[],char str2[]) {char str3[80]; strcpy(str3,str1); strcpy(str1,str2); strcpy(str2,str3); }
字符串函数
字符串库函数
使用字符串函数需要 #include <cstring>
字符串函数都根据'\0'来判断字符串结尾
形参为char []类型,则实参可以是char数组或字符串常量
字符串拷贝
strcpy(char dest [],char src[]); //拷贝src到dest
字符串比较大小
int strcmp(char s1[],char s2[]); //返回0则相等 ;如果字符串 1>字符串 2,函数值为一正整数。
求字符串长度
int strlen(char s[]); //不算结尾的'\0'
字符串拼接
strcat(char s1[],char s2[]); //s2拼接到s1后面
字符串转成大写
strupr(char s[]);
字符串转成小写
strlwr(char s[]);#include <iostream> #include <string.h> using namespace std; int main() { char str1[20] = "I love you "; //不能str1[] char str2[] = "ME too"; cout << "strcmp(str1,str2)=" << strcmp(str1, str2) << endl; if (strcmp(str1, str2) > 0) cout << "yes"; cout << "strcat(str1,str2)=" << strcat(str1, str2) << endl; cout << "str1=" << str1 << endl; cout << "str2=" << str2 << endl; cout << "strcpy(str1,str2)=" << strcpy(str1, str2) << endl; cout << "str1=" << str1 << endl; cout << "str2=" << str2 << endl;
//cout<<"strcpy(str1,str2,2)="<<strcpy(str1,str2,2);//错? /**** * strcmp(str1,str2)=-4 strcat(str1,str2)=I love you ME too str1=I love you ME too str2=ME too strcpy(str1,str2)=ME too str1=ME too str2=ME too **/ }strlen 是 string length(字符串长度)的缩写。 它是测试字符串长度的函数。 其 函数的值为字符串中的实际长度,不包括′\0′在内。 如 char str[10]=″China″; cout<<strlen(str); 输出结果不是 10,也不是 6,而是 5。
strcpy例子
char str1[10],str2[]=″China″; strcpy(str1,str2); 执行后,str2 中的 5 个字符″China″和′\0′(共 6 个字符)复制到数组 str1 中。strcmp、例子
#include <iostream> #include <string> using namespace std; int main() { void max_string(char str[][30], int i); // 函数声明 int i; char country_name[3][30]; for (i = 0; i < 3; i++) cin >> country_name[i]; // 输入 3 个国家名 max_string(country_name, 3); // 调用 max_string 函数 return 0; } void max_string(char str[][30], int n) { int i; char string[30]; strcpy(string, str[0]); // 使 string 的值为 str[0] 的值 for (i = 0; i < n; i++) if (strcmp(str[i], string) > 0) // 如果 str[i]>string strcpy(string, str[i]); // 将 str[i] 中的字符串复制到 string cout << endl <<″the largest string is : ″<< string << endl; } 运行结果如下 : CHINA↙ GERMANY↙ FRANCH↙ the largest string is : GERMANYstrcat例子
#include <iostream> using namespace std; int main() { char s1[60], s2[40]; cout << "input string1:"; cin >> s1; cout << "input string2:"; cin >> s2; strcat(s1, s2); cout << "The new string is:" << s1 << endl; return 0; }
字符串变量 string
1.不能用'\0'!!!!!!!! 书p62!,
string a;
n=a.size();
while(j<=n);
while(a[j]!='\0');错误!!!!
2.只能用cin输入,但cin遇到空格就截止;用getline 报错
3.处理实际是数组
string1 ="STAY"; int n=string1.size(); for(int i=0;i<=n-1;i++) cout<<string1[i];#include <iostream> #include <string.h> using namespace std; int main(){ string string1; string1 ="STAY"; cout<<"string1=";//用数组输入 int n=string1.size(); for(int i=0;i<=n-1;i++) cout<<string1[i]; cout<<endl; string string2=" WITH"; cout<<"string1="<<string1<<endl; cout<<"string2="<<string2<<endl; string1[2]='a'; cout<<"string1="<<string1<<endl; string string3; cout<<"use cin string3,no space="; cin>>string3; cout<<"string3="<<string3<<endl; string string4; cout<<"use cin string4,some space="; cin>>string4; cout<<"string4="<<string4<<endl; /*******编译不通过 * string string5; cout<<"use getline string4,some space="; cin.getline(string5,80); cout<<"string5="<<string5<<endl; *****/ string1=string2; cout<<"string1="<<string1<<endl; cout<<"string2="<<string2<<endl; string2=string1+string2; cout<<"string2="<<string2<<endl; if(string1>string2) cout<<"string1>string2"<<endl; //string1=STAY string1=STAY string2= WITH string1=STaY use cin string3,no space=student string3=student use cin string4,some space=I am a string4=I string1= WITH string2= WITH string2= WITH WITH }
字符串数组
string name[ 50],num[50];
for(i=0,i<n,i++)
cin>>name[i]>>num[i];
string name[5]={"zhang","LI","sun","wang","tan"}; /******错误!! * string name[5]; * name[5]={"zhang","LI","sun","wang","tan"};//类似a[5]={1,2,3,4,5}; * **/ cout<<"name[5]="<<name<<endl;//会错!!!!!是地址
cout<<"name[5]=xunhuan"<<endl; for(int i=0;i<=4;i++) cout<<name[i]<<endl; }
//name[5]=0x72fcc0 name[5]=xunhuan zhang LI sun wang tan
字符串的输入
只判断一行
字符变量、 本例子
一维字符数组、本例子
字符串string 课后5-12(3) 课后5-15 1.不能用'\0'!!!!!!!! 2.只能用cin输入,但cin遇到空格就截止;
另外 若判断两行
string 字符串循环n遍 课后15!
字符串数组、书p149 例 5.12 课后17 //不能用!!!每个数组元素是一个字符串!!!!不能运算的!
二维字符数组,课后5-10 书例5-10!!
译密码。 。 例题
可以按以下规律将电文变成密码: 将字母A变成字母 E,a 变成 e, 即变成其后的第 4 个字母,W 变成 A,X 变成 B,Y 变成 C,Z 变 成 D。 。 见图 3.20。 。
字母按上述规律转换, 非字母字符不变。 。 如 "Wonderful!" 转换为"Asrhivjyp!"。 。 输入一行字符,变为原来的,
//字符变量 #include<iostream> using namespace std; int main() { char c; while(c=getchar(),c!='\n') { if((c>='a'&&c<='z')||(c>='A'&&c<='Z')) { c=c+4; if((c>='Z')&&(c<'Z'+4)||c>'z') c=c-26; } cout<<c; } cout<<endl; return 0; } /*I will go to Beijing M ampp ks xs Fimnmrk
//字符数组 放字符串 #include<iostream> using namespace std; int main() { char c[80]; cin.getline(c,80); cout<<c<<endl;//!!!!!!!!!!!!!!!!!! int j=0; while(c[j]!='\0') { if((c[j]>='a'&&c[j]<='z')||(c[j]>='A'&&c[j]<='Z')) { c[j]=c[j]+4; if((c[j]>'Z')&&(c[j]<='Z'+4)||c[j]>'z') c[j]=c[j]-26; } cout<<c[j]; j++; } } /*I will go to Beijing M ampp ks xs Fimnmrk */
//string类 字符串 用cin,有局限。#include<iostream> #include<cstring> using namespace std; int main() { string stringc; cin>>stringc;// cin 空格错!!! cout<<"stringc="<<stringc<<endl; int j=0; int n=stringc.size(); cout<<"n="<<n<<endl;
while(j<=n) //while(stringc[j]!='\0')出错 不用j<=n-1???
{ if((stringc[j]>='a'&&stringc[j]<='z')||(stringc[j]>='A'&&stringc[j]<='Z')) { stringc[j]=stringc[j]+4; if((stringc[j]>'Z')&&(stringc[j]<='Z'+4)||stringc[j]>'z') stringc[j]=stringc[j]-26; } cout<<stringc[j]; j++; } } /** * 用cin string 只第一个有效!!! * I am a student stringc=I n=1 result=M*******///string 字符串类 定长#include<iostream> #include<cstring> using namespace std; int main() { string stringc="I will go to Beijing"; int j=0; int n=stringc.size(); cout<<"n="<<n<<endl; cout<<"result="; while(j<=n)//\0 错! { if((stringc[j]>='a'&&stringc[j]<='z')||(stringc[j]>='A'&&stringc[j]<='Z')) { stringc[j]=stringc[j]+4; if((stringc[j]>'Z')&&(stringc[j]<='Z'+4)||stringc[j]>'z') stringc[j]=stringc[j]-26; } cout<<stringc[j]; j++; } } /**n=20 result=M ampp ks xs Fimnmrk*///字符串数组 不能用的吧。。。。。。 不懂 ******/书课后5-10
//二维数组 #include<iostream> #include<stdio.h> using namespace std; int main() { char text[3][80]; int i,j,upper,lower,digit,space,other; i=j=lower=upper=digit=space=0; for(i=0;i<=2;i++) { cout<<"input line"<<i+1<<endl; //gets(text[i]); cin.getline(text[i],80); for(j=0;j<80&&text[i][j]!='\0';j++) { if(text[i][j]==' ') space++; else if((text[i][j]>='0')&&(text[i][j]<='9')) digit++; else if((text[i][j]>='A')&&(text[i][j]<='Z')) upper++; else if((text[i][j]>='a')&&(text[i][j]<='z')) lower++; else other++; } } cout<<"space="<<space<<",digit="<<digit<<",upper="<<lower; } //一维数组 #include<iostream> #include<stdio.h> using namespace std; int main() { char text[80]; int i,j,upper,lower,digit,space,other; i=j=lower=upper=digit=space=0; for(i=0;i<=2;i++) { cout<<"input line"<<i+1<<endl; cin.getline(text,80); for(j=0;j<80&&text[j]!='\0';j++) { if(text[j]==' ') space++; else if((text[j]>='0')&&(text[j]<='9')) digit++; else if((text[j]>='A')&&(text[j]<='Z')) upper++; else if((text[j]>='a')&&(text[j]<='z')) lower++; else other++; } } cout<<"space="<<space<<",digit="<<digit<<",upper="<<lower; } //input line1 I am a boy input line2 You are a girl input line3 12+34=46 space=6,digit=6,upper=16
字符串有三种形式。
1. 用双引号括起来的字符串常量,如"CHINA" , "C++ program "。
2. 存放于字符数组中,以‘\0’字符(ASCII码为0)结尾
3. string对象。string是C++标准模板库里的一个类,专门用于处理字符串.
字符串常量
1.字符串常量占据内存的字节数等于字符数目加1,多出来的是结尾字符‘\0’。
长度=n;内存、字节=n+1;!!!
2. 字符串 "C program" 在内存中的布局: C p r o g r a m \0
3、字符串的长度不包含’\0’如果字符串常量中包含双引号,则双引号应写为‘\"’。而‘\’字符在
字符串中出现时,须连写两次,变成‘\\’。例如:
cout << "He said: \"I am a stu\\dent.\"";
=> He said: "I am a stu\dent."
用cin读入字符串
用cin可以将字符串读入字符数组
cin会自动添加结尾的'\0'
cin读入到空格为止
char line[100];
cin>>line;cout<<line;
在数组长度不足的情况下,scanf可能导致数组越界
char line[5];
cin>>line
若输入"12345",则数组越界!
cin.getline(char buf[], int bufSize);
读入一行(行长度不超过bufSize-1,按enter键一行结束)或 bufSize-1个字符 到buf,看哪个条件先满足自动添加'\0'
回车换行符不会写入buf,但是会从输入流中去掉char line[10];
cin.getline(line, sizeof(line));
//或 cin.getline(line,10); 读入最多9个字符到 line
cout << line;
一维or二维
用一维循环
打印柱状图
将text统计字符
用二维
ppt10个字符串排序
将text统计字符
char vs string char c[80]="hello"; string string1="hello"; int n=strlen(c); int n=string1.size(); for(i=0..n) for(i=0..n) 或者 不能!while(c[j]!='\0') while(c[i]!='\0') char b[80]; string string1; cin>>b;//cin>>b[80]报错,在结构体那里! cin>>string1; cout<<b;//cout<<b[80]也会报错! cout<<string1; char b[80]; 不能!string string1; cin.getline(b,80); 不能 !cin.getline(string1,80); cout<<b;
数组长度处理 像素模糊处理,信息学 int main() { int m, n; int i, j; int a[1001][1001]; cin >> n >> m; //输入图像行数、列数 for (i = 1; i <= n; i++) //输入每个像素点灰度 for (j = 1; j <= m; j++) cin >> a[i][j]; } 鞍点 const int n = 4, m = 5; for (i = 1; i <= n; i++) //输入每个像素点灰度 for (j = 1; j <= m; j++) cin >> a[i][j]; 排序 int seletion(int a[], int length) { for (i = 0; i <= length - 1; i++) { } }
只能字母、数字、下划线 只能下划线、字母开头 ,数字不行! Q3. 下列符号中,可以作为标识符的是(单选,2分) 3isReady _1_2A 对 who? x+y Q5.下列表达式中不正确的是(单选,2分) px 3 (x) (p-i)=39 (p-i)=39错误, 表达式不能赋值,只能变量赋值。
Q 与y=(!x ? 0 : (x>0? 1 : -1)); 等效的if语句是 if (x= =0) y=0; else if (x>0) y=1; else y= -1;
Q18.要使字符串变量str具有值“hello!”,正确的定义语句是(单选,2分) char str[6]={‘h’,’e’,’l’,’l’,’o’,’!’}; char str[6]=”hello!”; char str[ ]={‘h’,’e’,’l’,’l’,’o’,’!’,’\0’}; char str[]={”hello!”}; 选 cd a:没有\0只是字符数组,不是字符串,不能用字符串函数 b:要赋值str[7],char str[7]=”hello!”; 否则没有地方放\0 cd对 Q33.下列对字符数组s初始化错误的是(单选,2分) char s[5]={"abc"}; char s[5]={ 'a',‘b', 'c'}; char s[5]= " "; char s[5]="abcde";错误, Q34.对两个数组a和b进行初始化, char a[ ]="abcde"; char b[ ]= {'a',b', 'c','d',e'}; a数组比b数组长度长, 但是字符串长度一样,用strlen输出一样! Q20. 与a3+3a3b+3ab2+b3 对应的C++表达式是(2分) a^3+3*a^3*b+3*a*b^2+b^3 0分 正确答案: a*a*a+3*a*a*a*b+3*a*b*b+b*b*b Q27. 下列数组说明和初始化正确的是(单选,2分) int a[5]=0;错 int b[3]={1,2,3,4}; float c[ ]={1,2,3};对 float d={5.3,6.0}; 35.若有定义char s[10]= "program",t[ ]= "test ";,则对字符串的操作错误的是(单选,2分)
strcpy(s,t)
cout<<strlen(s);
strcat(s,t)
cin>>t;