编写函数模板 void change(T &i,T &j),能够实现整型数据、实型和字符串的交换,并编写测试的主函数。
第1题:
有以下程序,其中函数的功能是将多个字符串按字典顺序排序: # include<string.h> void f(char*p[],int n) {char *t; int i,j; for(i=0;i<n-1;i++) for(j=i+1;j<n;j++) if(strcmp(p[i],p[j])>0){t=p[i];p[i]=p[j]p[j]=t;} } main() {char *p[5]={"abc","aabdfg","abbd","dcdbe","cd"}; f(p,5); printf("%d\n",strlen(p[1])); } 程序运行后的输出结果是 ______。
A.2
B.3
C.6
D.4
第2题:
第3题:
请补充函数fun(),该函数的功能是:把字符串str中的字符按字符的ASCⅡ码降序排列,处理后的字符串仍然保存在原串中,字符串及其长度作为函数参数传入。
例如,如果输入“cdefgh”,则输出为“hgfedc”。
注意:部分源程序给出如下。
请勿改动主函数main和其他函数中的任何内容,仅在函数fun()的横线上填入所编写的若干表达式或语句。
试题程序:
include <stdio.h>
define N 80
void fun (char s [], int n)
{
int i, j;
char ch;
for (i=0; i<n; i++)
for(j=【 】;j<n;j++)
if (s[i]<s [j])
{
ch=s [j];
【 】;
s [i] =ch;
}
main ( )
{
int i=0, strlen=0;
char str [N];
clrscr ();
printf ("\nInput a string: \n");
gets (str);
while (str [i] !=' \0')
{
strlen++;
i++;
}
fun (str, strlen);
printf ("\n***display string ***\n");
puts (str);
}
第4题:
阅读下列函数说明和C代码,将应填入(n)处的字句写在对应栏内。
【说明】
函数QuickSort是在一维数组A[n]上进行快速排序的递归算法。
【函数】
void QuickSort( int A[ ],int s,int t)
{ int i=s,j=t+1,temp;
int x=A[s];
do{
do i ++ ;while (1);
do j -- ;while(A[j]>x);
if(i<j){temp=A[i];(2);(3);}
}while(i<j);
A[a] =A[j];A[j] =x;
if(s<i-1) (4);
if(j+1<t) (5);
}
第5题:
请补充main函数,该函数的功能是:把一维数组中的元素逆置。结果仍然保存在原数组中。
注意:部分源程序给出如下。
请勿改动主函数main和其他函数中的任何内容,仅在 main函数的横线上填入所编写的若干表达式或语句。
试题程序:
include <stdio.h>
define N 10
main ( )
{
int i, j, t;
int bb[N];
clrscr ();
for (i=0; i<N; i++)
bb[i]=i;
printf("\n*** original list ***\n");
for (i=0;i<N; i++)
printf ("%4d",bb [i] );
for(【 】; j<=i;【 】)
{
t=bb [j ];
bb [j ] =bb [i];
bb[i]=t;
}
printf("\n****** new list ******\n");
for (i=0; i<N; i++)
printf ("%4d",bb [i] );
}
第6题:
请补充函数fun(),该函数的功能是:按行统计N×N维矩阵元素中的最大值(均为整数),并把这些值按从小到大的顺序保存在数组b中。矩阵的维数在主函数中输入,并赋予随机数。
注意:部分源程序给出如下。
请勿改动主函数main和其他函数中的任何内容,仅在函数fun的横线上填入所编写的若干表达式或语句。
试题程序:
include<stdio.h>
include<conio.h>
include<stdlib.h>
define N 20
void fun(【 】)
{
int i j;
int t;
for(i=0;i<n;i++)
for(j=0;j<n;j++)
if(【 】)
b[i]=a[i][j];
for(i=0;i<n;i++)
{
for(j=0;i<n;j++)
if(【 】)
{
t=b[i];
b[i]=b[j];
b[j]=t;
}
}
}
main()
{
int a[N][N];
int b[N];
int n;
int i,j;
clrscr();
printf("*****Input the dimension of array N*****\n");
scanf("%d",&n);
printf("*****The array *****\n");
for(i=0;i<n;i++)
{
for(j=0;i<n;j++)
{
a[i][j]=rand()%20;
while(a[i][j]==0)
a[i][j]=rand()%30;
printf("%4d",a[i][j]);
}
printf(”\n\n”);
}
for(i=0;i<n;i++)
b[i]=0;
fun(a,b,n);
printf("***** THE RESULT *****\n");
for(i=0;i<n;i++)
printf(“%d”,b[i]);
}
第7题:
有以下程序 #include <stdio.h> #include <string.h> void fun(char s[][10],int n) { char t; int i j; for (i=0; i<n-1; i++) for 0--i+l; j<n; j++) /*比较字符串的首字符大小,并交换字符串的首字符*/ if(s[i][0] > s[j][0]) { t = s[i][0]; s[i][0] = s[j][0]; s[j][0] = t;} } main() { char ss[5][10]= {"bcc", "bbcc", "xy", "aaaacc", "aabcc" }; fun(ss, 5); printf("%s,%s\n", ss[0],ss[4]); } 程序的运行结果是
A.xy, aaaacc
B.aaaacc,xy
C.xcc,aabcc
D.acc,xabcc
第8题:
有以下程序,其中函数f的功能是将多个字符串按字典顺序排序: #include<string.h> void f(char *p[],int n) {char *t;int i,j; for(i=O;i<n-1;i++) for(j=i+1;j<n;j++) if strcmp(p[i],p[j])>0{t=p[i] p[i]=p[j]; p[j]=t; } } main() {char* p[5]=("abc","aabdfg","abbd","dcdbe","cd"}; f(p,5); printf("%d\n",strlen(p[1])); } 程序运行后的输出结果是 ______。
A.2
B.3
C.6
D.4
第9题:
有以下程序 #include<stdio.h> #include<string.h> void fun(char s[][10],int n) { char t;int i,j; for(i=0;i<n-1;j++) for(j=i+1,j<n;j++) /*比较字符串的首字符大小,并交换字符串的首字符*/ if(s[0])>s[i][c]{t=s[i][o];s[i][o]=s[j][o];s [j][0]=t;} } main { char ss[5][10]="bcc","bbcc","xy","aaaacc"," aabcc"} fun(ss,5);printf("%s,%s",ss[0],ss[4]); } 程序运行结果是( )。
A.xy,aaaacc
B.aaaacc,xy
C.xcc,aabcc
D.acc,xabcc
第10题:
请补充函数fun(char *s),该函数的功能是把字符串中的内容逆置。
例如:字符串中原有的字符串为abcde,则调用该函数后,串中的内容变为edcba。
注意;部分源程序给出如下。
请勿改动主函数main和其他函数中的任何内容,仅在函数fun()的横线上填入所编写的若干表达式或语句。
试题程序:
$include<string.h>
include<conio.h>
include<stdio.h>
define N 81
void fun(char*s)
{
int i=0,t,n=strlen(s);
for(;【 】;i++)
{
t=*(s+i);
【 】;
【 】;
}
}
main()
{
char a[N];
clrscr();
printf("Enter a string:");
gets(a);
printf("The original string is:");
puts(a);
fun(a);
printf("\n");
printf("The string after modified:");
puts(a);
}
第11题:
以下程序中函数f( )的功能是将n个字符串按由大到小的顺序进行排序。 #include<string.h> void f(char p[ ][10],int n) { char t[20]; int i,j; for(i =O;i <n-1 ;i ++ ) for(j =i + 1 ;j < n;j ++ ) if(strcmp(p [i] ,p[j] ) <0) { strcpy(t,p[i]);strcpy(p[i] ,p[j] );strcpy(p[j] ,t); } } main ( ) { char p [ ] [ 10 ] = { "abc","aabdfg","abbd","dcdbe", "cd" }; int i; f(p,5); printf("%d\n",strlen(p[O] ) ); } 程序运行后的输出结果是
A.6
B.4
C.5
D.3
第12题:
根据下面函数原型编写一个函数,求出并返回由字符指针a所指向的字符串中包含的字符’a’和’A’的总个数。int void fun(char* a);
略
第13题:
请编写函数fun(),其功能是:将s所指字符串中下标为奇数的字符删除,串中剩余字符形成的新串放在t所指数组中。
例如,当s所指字符串中的内容为siegAHdied,则在t所指数组中的内容应是seAde。
注意:部分源程序给出如下。
请勿改动主函数main和其他函数中的任何内容,仅在函数fun的花括号中填入所编写的若干语句。
试题程序:
include<conio.h>
include<stdio.h>
include<string.h>
void fun(char*S,char t[])
{
}
main()
{
char s[100],t[100];
clrscr();
printf("\nPlease enter string s:");
scanf("%S",S);
fun(S,t);
printf("\nThe result is:%s\n",t);
}
第14题:
阅读以下说明和C语言函数,将应填入(n)处的语句写在对应栏内。
【说明】
设串s和串t采用顺序存储结构,编写函数实现串s和串t的比较操作,要求比较结果包括大于、小于和等于3种情况。
【函数】
int StrCompare(SStrType s, SStrType t)
{
int n=s.length, m=(1), i,j,tag;
i=0; j=0;
while((2))
{
if((3))
{
i++;
j++;
}
else if(s.str[i]>t.str[j])
{
tag=1;
return tag;
}
else
{
tag=-1;
return tag;
}
}
if(n==m)
tag=0;
else if((4))
tag=1;
else if(n<m)
tag=-1;
(5);
}
第15题:
假定输入的字符串中只包含字母和*号。请编写函数 fun(),它的功能是:除了尾部的,:号之外,将字符串中其他*号全部删除。形参p已指向字符串中最后一个字母。在编写函数时,不得使用C语言的字符串函数。
例如,若字符串中的内容为****A*BC*DEF*G******,删除后,字符串中的内容应当是ABCDEFG******。
注意:部分源程序给出如下。
请勿改动主函数main和其他函数中的任何内容,仅在函数fun的花括号中填入所编写的若干语句。
试题程序:
include<conio. h>
include<stdio. h>
void fun(char *a, char *p)
{
}
main ( )
char s[81],*t;
printf ("Enter a string: \n ");
gets (s);
t=s;
while (*t)
t++;
t--; /*指针t指向字符串尾部* /
while (*t== '*' )
t--; /*指针t指向最后一个字母*/
fun (s, t);
printf ("The string after deleted: \n");
puts (s);
}
第16题:
函数f_str(char *str,char del)的功能是;将非申字符串str分割成若干个子字符串并输出,del表示分割时的标志字符。例如,若str的值为“66981636666257”,del的值为“6”,调用此函数后,将输出3个子字符串,分别为“981”、“3”和“257”。请将函数f_str中(6)~(8)空缺处的内容填写完整。
[函数]
void f_str(char *str,char del)
{ int i,j,len;
len = strlen(str);
i = 0;
while (i<len) {
while ( (6) )
i++; /* 忽略连续的标志字符 */
/* 寻找从srt[i]开始直到标志字符出现的一个子字符串 */
j = i+1;
while (str[j] !=del && str[j] !='\0')
j++;
(7)="\0"; /* 给找到的字符序列置字符串结束标志 */
printf (" %s\t", & str [i]);
(8);
}
}
第17题:
请编写函数fun(),该函数的功能是:将M行N列的二维数组中的字符数据,按列的顺序依次放到一个字符串中。
例如,若二维数组中的数据为:
W WWW
S S S S
H H H H
则字符串中的内容应是WSHWSHWSHWSH。
注意:部分源程序给出如下。
请勿改动主函数main和其他函数中的任何内容,仅在函数fun的花括号中填入所编写的若干语句。
试题程序:
include<stdio.h>
define M 3
define N 4
void fun(char (*s)[N],char *b)
{
}
main()
{
char a[100],w[M][N]={{ 'W', 'W', 'W', 'W'},
{'S', 'S', 'S', 'S'},{'H', 'H', 'H', 'H'}};
int i,j;
printf("The matrix:\n");
for(i=0;i<M;i++)
{ for(j=0;j<N;j++)
printf("%3c",w[i][j]);
printf("\n");
}
fun(w,a);
printf("The A string:In");
puts(a);
printf("\n\n");
}
第18题:
请编写函数fun(),该函数的功能是:将放在字符串数组中的M个字符串(每串的长度不超过N),按J顷序合并组成一个新的字符串。
例如,若字符串数组中的M个字符串为
AAAA
BBBBBBB
CC
则合并后的字符串内容应该是AAAABBBBBBBCC
注意:部分源程序给出如下。
请勿改动主函数main和其他函数中的任何内容,仅在函数fun的花括号中填入所编写的若干语句。
试题程序:
include <conio. h>
define M 3
define N 20
void fun (char a [M] [N], char *b)
{
}
main ( )
{
char w [M] [N] ={"AAAA", "BBBBBBB", "CC"}, i;
char a[100]={" "};
printf ("The string: \n ");
for (i=0; i<M; i++)
puts (w[i]);
printf (" \n ");
fun (w, a);
printf ("The A string: \n ");
printf("%s ",a);
printf("\n\n ");
}
第19题:
有以下程序,其中函数f的功能是将多个字符串按字典顺序排序:#include <stdio, h>#inelude <string, h>void f(char * p[ ] ,int n) char * t;int i,j; for(i=0;i<n-1;i++) for(j=i+1 ;j<n;j ++ ) if(strcmp(p[i], p[j])>0) { t =p[i];p[i] =p[j] ;p[j] =t;}}main( ){ char * p [5] = { "abe"." aabdfg"." abbd"," dcdbe"," cd" }; f(P,5 ); printf("% d\n", strlen(p[1]) );}程序运行后的输出结果是( )。
A.2
B.3
C.60
D.4
第20题:
有以下程序,其中函数f的功能是将多个字符串按字典顺序排序( )。 #include<string.h> void f(char*p[],int n) { char*t;int i,j; for(i=0;i<n-1;i++) for(j=i+1;j<n;j++) if(strcmp(p[i],p[j]>0){t=p[i];p[i]=p[j];p[j] =t;} } main { char *P[5]={"abe","aabdfg","abbd","dcd- be","cd"}; f(p,5); printf("%d\n",strlen(p[1])); } 程序运行后的输出结果是( )o
A.2
B.3
C.6
D.4
第21题:
有以下程序,其中函数f的功能是将多个字符串按字典顺序排序: #include<string.h> voidf(char*p[],intn) { char*t;int i,j; for(i=0;i<n-1;i++) for (j=i+1;j<n;j++) if(strcmp(p[i],p[j])>0) { t=p[i]; p[i]=p[j]; p[j]=t; } } main() { char*p[5]={"abc"
A.2
B.3
C.6
D.4
第22题:
下列给定程序中,函数fun()的功能是将字符串s中位于偶数位置的字符或ASCII码为奇数的字符放入字符串t中(规定第一个字符放在第0位中)。
例如:字符串中的数据为ADFESHDI,则输出应当是 AFESDI。
请改正程序中的错误,使它能得到正确结果。
注意;不要改动main函数,不得增行或删行,也不得更改程序的结构。
试题程序:
include <conio.h>
include <stdio.h>
include <string.h>
define N 80
/************found*************/
void fun(char s,char t[])
{
int i,j=0;
for(i=0;i<strlen(s);i++)
/*********found*+************/
if(i%2=0 || s[i]%2!=0)
t[j++]=s[i];
t[j]='\0';
}
main()
{
char s[N],t[N];
clrscr();
printf("\nPlease enter string s:");
gets(s);
fun(s,t);
printf("\nThe result is:%s\n",t);
}
第23题:
写出模板函数实现数值型数组元素值按从小到大排序的程序。
include<iostream>
using namespace std;
template <class T>
void sort(T b[],int n)
{
T temp;
int i,j;
T *a=new T[n];
for (i=0;i<n;i++){a[i]=b[i];}
for(i=0;i<n-1;i++)
{
for(j=i+1;j<n;j++)
{ if(a[i]>a[j])
{temp=a[i];
a[i]=a[j];
a[j]=temp;
}
}
}
for(i=0;i<n;i++){cout<<a[i]<<" ";}
cout<<endl;
delete []a;
}
void main()
{
int i,n=6;
int a[]={5,1,9,10,3,8};
____________;
for(i=0;i<n;i++)
{cout<<a[i]<<" ";}
cout<<endl;
}