C++简单应用题

1.函数调用

1.1拆分三位数

简单应用题

请编写一个函数sortnum(int num),参数num是一个三位的整数,该函数将num的百位、十位和个位的数字进行重排,并返回由上述的三个数字组成的最大的三位数。

注意:部分源程序已存在文件test28_2.cpp中。

如输入456后,输出结果如下:

654

请勿修改主函数main和其他函数中的任何内容,仅在函数sortnum 的花括号中填写若干语句。

文件test28_2.cpp的内容如下:

#include<iostream.h>

int sortnum(int num)

{   

}

void main()

{

int num;

int result=0;

cout<<"请输入一个三位数";

cin>>num;

cout<<sortnum(num)<<endl;

}

【答案】

int number[3],i,j,temp;

for(i=0;i<3;i++)

{

     number[i]=num%10;

     num=num/10;

}

for (i=0;i<2;i++)

{

     for (j=i+1;j<3;j++)

         if (number[j]<=number[i])

         {

             temp=number[j];

             number[j]=number[i];

             number[i]=temp;

         }

}

num=number[2]*100+number[1]*10+number[0];

return num;

 

1.2月份转换

40.22.简单应用题

请编写一个函数 printdate(int year,int month,int day),该函数实现将输入的3个数字转换成英语数字纪年输出的功能,如输入March 9,1978,则输出1978 3 9。注意:使用switch结构实现该函数的基本功能并应该能够判断错误的输入。部分源程序已存在文件test40_2.cpp中。请勿修改主函数main和其他函数中的任何内容,仅在函数printdate的花括号中填写若干语句。

源程序文件test40_2.cpp清单如下:

#include<iostream.h>

void printdate(int year,int month,int day)

{

}

void main()

{

printdate(1978,3,9);

}

【答案】

void printdate(int year,int month,int day)

{

if(year<0||month<1||month>12||day<1||day>31)

{

     cout<<"ERROR";

     return;

}

switch(month)

{

case 1:cout<<"January";break;

case 2:cout<<"February";break;

case 3:cout<<"March";break;

case 4:cout<<"April";break;

case 5:cout<<"May";break;

case 6:cout<<"June";break;

case 7:cout<<"July";break;

case 8:cout<<"Auguest";break;

case 9:cout<<"September";break;

case 10:cout<<"October";break;

case 11:cout<<"November";break;

case 12:cout<<"December";break;

}

cout<<" "<<day<<","<<year<<endl;

}

 

 

 

1.3平方根

29.22. 编程题

编写函数fun(),它的功能是求n以内(不包括n)同时能被37整除的所有自然数之和的平方根s,并做为函数值返回。

例如:n1000时,函数值应为s=153.909064

注意:部分源程序给出如下。

请勿改动主函数main和其他函数中的任何内容,仅在函数fun的花括号中填入所编写的若干语句。

试题程序:

#include    <conio.h>

#include    <math.h>

#include    <stdio.h>

double  fun(int n)

{

 

}

 

main()

{

clrscr();

printf("s=%f\n", fun(1000));

}

【答案】

double  fun(int n)

{double  s=0.0;

int  i;

for(i=0; i<n;i++)     /*从0~n中找到既能被3整除同时又能被7整除的数,并将这些数求和*/

   if(i%3==0&&i%7==0)    

      s=s+i;

s=sqrt(s);            /*对s求平方根*/

return  s;

}

 

 

1.4求幂一

25.22.简单应用题

请编写两个函数int sum_of_powers(int k, int n), powers(int m, int n),求16k次方的和,sum_of_powers中参数kn分别表示k次方和所求数列中最大的一个自然数,最后返回所求值,powers中参数mn分别表示m为底数n为指数,最后返回所求值。要求使用for循环和函数嵌套(int sum_of_powers中调用powers)实现算法。输出结果如下:

sum of 4 powers of intergers from 1 to 6=2275

注意:部分源程序已存在文件test25_2.cpp中。

请勿修改主函数main和其他函数中的任何内容,仅在函数sum_of_powerspowers的花括号中填写若干语句。

文件test25_2.cpp的内容如下:

#include<iostream.h>

const int k(4);

const int n(6);

int sum_of_powers(int k, int n), powers(int m, int n);

void main()

{

cout<<"sum of "<<k<<" powers of intergers from 1 to "<<n<<"=";

cout<<sum_of_powers(k,n)<<endl;

}

int sum_of_powers(int k, int n)

{

}

int powers(int m, int n)

{

}

【答案】

1)int sum_of_powers(int k, int n)

{

int sum(0);

for(int i(1); i<=n; i++)

     sum += powers(i,k);

return sum;

}

2)int powers(int m, int n)

{

int i,product(1);

for(i=1; i<=n; i++)

     product *= m;

return product;

}

1.5求幂二

 

30.22.简单应用题

请编写一个函数fun(int x,int n),该函数返回xn次幂的值,其中xn都是非负整数。xn次幂的计算方法是1x相乘n次,如x20次幂的计算为1x相乘20次。

注意:部分源程序已存在文件test30_2.cpp中。

请勿修改主函数main和其他函数中的任何内容,仅在函数fun的花括号中填写若干语句。

如输入34,输出结果如下:

3  4

81

文件test30_2.cpp清单如下:

#include<iostream.h>

double fun(int x,int n)

{

}

void main()

{

int x,n;

cin>>x>>n;

cout<<fun(x,n)<<endl;

}

【答案】

 if(x==0) return 0;

if(n==0) return 1;

int y=1;

for(int i=0;i<n;i++) y*=x;

return y;

 

30.22.简单应用题

请编写一个函数fun(int x,int n),该函数返回xn次幂的值,其中xn都是非负整数。xn次幂的计算方法是1x相乘n次,如x20次幂的计算为1x相乘20次。

注意:部分源程序已存在文件test30_2.cpp中。

请勿修改主函数main和其他函数中的任何内容,仅在函数fun的花括号中填写若干语句。

如输入34,输出结果如下:

3  4

81

文件test30_2.cpp清单如下:

#include<iostream.h>

double fun(int x,int n)

{

}

void main()

{

int x,n;

cin>>x>>n;

cout<<fun(x,n)<<endl;

}

【答案】

 if(x==0) return 0;

if(n==0) return 1;

int y=1;

for(int i=0;i<n;i++) y*=x;

return y;

 

1.6计算因子

33.22.简单应用题

请编写一个函数fun(),它的功能是计算并输出给定整数n的所有因子(不包括1与自身)之和(规定n的值不大于1000)

例如:主函数从键盘给输入n的值为856,则输出为sum=763

注意:部分源程序已存在文件test33_2.cpp中。

请勿修改主函数main和其他函数中的任何内容,仅在函数fun的花括号中填写若干语句。

文件test33_2.cpp清单如下:

#include<stdio.h>

#include<iostream.h>

int fun(int n)

{

}

void main()

{

int  n, sum;

cout<<"Input  n:"<<endl;

cin>>n;

sum=fun(n);

cout<<"sum=\n"<<sum<<endl;

}

【答案】

int fun(int n)

{int s=0,i;

for(i=2;i<=n-1;i++) 

   if(n%i==0)

     s+=i;          

return s;          

}

1.7累加一

37.22.简单应用题

请编写函数fun(),其功能是计算并输出下列多项式值

Sn=1+1/1!+1/2!+1/3!+1/4!+ +1/n!

例如:从键盘输入15,则输出为s=2.718282

注意:部分源程序以存在文件test37_2.cpp中。

请勿改动主函数main和其他函数中的任何内容,仅在函数fun的花括号中填入所编写的若干语句。

文件test37_2.cpp的内容如下:

#include<stdio.h>

#include<iostream.h>

double fun(int n)

{

}

void main()

{

int  n;  

double  s;

cout<<"Input  n:"<<endl;

cin>>n;

s=fun(n);

cout<<"s="<<s<<endl;

}

 

1.8累加二

38.22.简单应用题

请编写函数fun(),其功能是计算并输出当x<0.97时下列多项式的值,直到∣Sn-Sn-1<0.000001为止。

Sn=1+0.5x+0.5(0.5-1) x2/2! +0.5(0.5-1)(0.5-2) x3/3! + 0.5(0.5-1)(0.5-2)(0.5-n+1) xn/ n!

例如:主函数从键盘给x输入0.21后,则输出为s=1.100000

注意:部分源程序以存在文件test38_2.cpp中。

请勿改动主函数main和其他函数中的任何内容,仅在函数fun的花括号中填入所编写的若干语句。

文件test38_2.cpp的内容如下:

#include<stdio.h>

#include<iostream.h>

#include<math.h>

double fun(double x)

{

}

void main()

{

double x, s;

cout<<"Input  x:"<<endl;

cin>>x;

s=fun(x);

cout<<"s="<<s<<endl;

}

【答案】

double fun(double x)

{

double s1=1.0,p=1.0,sum=0.0,s0,t=1.0;

int n=1;

do

{s0=s1;      

sum+=s0;    

t*=n;        

p*=(0.5-n+1)*x;   

s1=p/t;          

n++;

}while(fabs(s1-s0)>=1e-6);  

return  sum;

}

1.9冒泡排序

23.22.简单应用题

请编写一个函数void bubble(double data[], int length),其中data是一维数组,存放比较的数据,length是数组中存放元素的个数,用冒泡法将数据(个数可变)排序后由小到大输出。冒泡法是常用的排序算法,这种算法执行效率不高,但比较简单,就是将相邻的两个数据作比较,把较小的数据交换到前面。纵向看来,交换过程中较小的数据就好像水中的气泡不断浮起。要求使用for循环实现算法。

注意:部分源程序已存在文件test23_2.cpp中。

请勿修改主函数main和其他函数中的任何内容,仅在函数bubble的花括号中填写若干语句。

文件test23_2.cpp的内容如下:

#include <iostream.h>

void bubble(double data[], int length)

{

}

void main()

{

int n;

cout << "请输入数据的个数: ";

cin >> n;

double* ddata = new double[n];

for(int i = 0; i < n; i++)

{

           cout << "No." << i+1 << ": ";

           cin >> ddata[i];

}

bubble(ddata, n);

cout << "排序后输出数据:"<<endl;

for(i = 0; i < n; i++)

{

           cout << "No." << i+1 << ": ";

           cout << ddata[i] << endl;

}

}

【答案】

void bubble(double data[], int length)

{

int segment;

int loop;

double temp;

for(segment = 0; segment <= length - 2; segment++)

{

      for(loop = length - 2; loop >= segment; loop--)

     if(data[loop + 1] < data[loop])

     {

         temp = data[loop];

         data[loop] = data[loop + 1];

         data[loop + 1] = temp;

     }

}

}

 

1.10判别素数

36.22.简单应用题

请编写一个函数 prim(int num),该函数实现判别参数num是否为素数,在主函数中利用prim()函数验证哥德巴猜想--任何比2大的偶数都可表示为两个素数之和基本功能,根据main函数的调用情况给出正确的返回值。

注意:部分源程序已存在文件test36_2.cpp中。

请勿修改主函数main和其他函数中的任何内容,仅在函数prim的花括号中填写若干语句。

文件test36_1.cpp的内容如下:

#include <iostream.h>

const LEN=100;

int prim(int num)

{

}

void main()

{

int a=7;

int cnt=0;

cout<<"a is 7:\n";

int *s;

s=new int[LEN];

for(int i=2;i<a;i++)

{

     if(!prim(i))

     {

         s[cnt]=i;

         cnt++;

     }

}

for (i=0;i<cnt;i++)

{

     for (int j=i+1;j<cnt;j++)

     {

         if(s[i]+s[j]==a)

             cout<<s[i]<<'\t'<<s[j]<<'\t'<<endl;

     }

}

}

【答案】

int prim(int num)

{

int half,flag;

flag =0;

half=num/2;

for (int i=2;i<=half;i++)

{

     if(num%i==0)

         flag=1;

     else

         continue;

}

if (flag==1)

     return 1;

else

     return 0;

}

 

1.11制数转换

32.22.简单应用题

请编写一个函数fun(char *num),该函数返回与传入的二进制数相应的十进制数,参数num指向存放8位二进制数的字符数组。二进制数转换为十进制数的方法是将二进制数的每一位乘以该位的权然后相加,如二进制数10010100=1*2+0*2+0*2+1*2+0*2+1*2+0*2+2*=148

注意:部分源程序已存在文件test32_2.cpp中。

请勿修改主函数main和其他函数中的任何内容,仅在函数fun的花括号中填写若干语句。

文件test32_2.cpp的内容如下:

#include<iostream.h>

int fun(char *num)

{

}

void main()

{

char num[8],ch;

cout<<"Enter an 8 bit binary number";

for(int i=0;i<8;i++)

{

     cin>>ch;

     num[i]=ch;

}

cout<<fun(num)<<endl;

}

【答案】

int fun(char *num)

{

 

 int result=0;

int middle;

for(int i=7;i>=0;i--)

{

     if (num[i]=='1')

     {

         middle=1;

         for(int k=1;k<=(7-i);k++)

         {

             middle=middle*2;

         }

         result=result+middle;

     }      

}

return result;

}

1.12最小公倍

13.22.简单应用题

请编写一个函数int fun(int nFirst,int nSecond),求两个数的最小公倍数并返回这个值。

注意:部分源程序已存在文件test13_2.cpp中。如输入78时,结果是56

请勿修改主函数main和其他函数中的任何内容,仅在函数fun的花括号中填写若干语句。

文件test13_2的内容如下:

#include<iostream.h>

int fun(int nFirst,int nSecond);

void main()

{

int nFirst, nSecond;

cout<<"Please input the first one\n";

cin>>nFirst;

cout<<"Please input the second one\n";

cin>>nSecond;

cout<<"最小公倍数:"<<fun(nFirst,nSecond)<<endl;

}

int fun(int nFirst,int nSecond)

{

}

【答案】

int fun(int nFirst,int nSecond)

{

int nMax, nMin;

if (nFirst>nSecond)

{

     nMax = nFirst;

     nMin = nSecond;

}

else

{

     nMax = nSecond;

     nMin = nFirst;

}

int nMod = nMax % nMin;

while(nMod)

{

     nMax = nMin;

     nMin = nMod;

     nMod = nMax % nMin;

}

int nMultiple = nFirst * nSecond / nMin;

return nMultiple;

}

2.字符串操作

2.1字符串输出

35.22.简单应用题

请编写一个函数display(),该函数要求用户先输入一字符串,然后在屏幕上再输出该字符串(假设该字符串长度小于100)。注意:部分源程序已存在文件test35_2.cpp中。

请勿修改主函数main和其他函数中的任何内容,仅在函数display()的花括号中填写若干语句。

如输入abc,输出结果如下:

please input string:

abc

abc

Press any key to continue

文件test35_2.cpp的内容如下:

#include <iostream.h>

#include <conio.h>

void display()

{   

}

void main()

{

cout<<"please input string:"<<endl;

display();

}

【答案】

void display()

{

 

char str[100],ch;

int i=0;

while ((ch=getche())!='\r')

{

     str[i]=ch;

     i++;

}

str[i]='\0';

cout<<endl<<str<<endl;

}

 

2.2字符串翻转

16.22.简单应用题

请编写一个函数void fun(char ss[]),该函数将字符串ss翻转,如ss为"123abc"则翻转后为"cba321"。注意:用数组方式及for循环来实现该函数。

注意:部分源程序已存在文件test16_2.cpp中。

请勿修改主函数main和其他函数中的任何内容,仅在函数fun的花括号中填写若干语句。

文件test16_2.cpp的内容如下:

#include<iostream.h>

#include<string.h>

void fun(char ss[]);

void main()

{

char s[80];

    cout<<"请输入字符串:";

cin>>s;

fun(s);

cout<<"逆序后的字符串:"<<s<<endl;

}

void fun(char ss[])

{

}

【答案】void fun(char ss[])

{

int n=strlen(ss);

for(int i=0;i<(n/2); i++)

{

     char c=ss[i];

     ss[i]=ss[n-1-i];

     ss[n-1-i]=c;

}

}

 

2.3字符串大小写转换

21.22.简单应用题

请编写一个函数char *change(char instr[]),将输入字符串中的所有小写字母转换为大写字母输出。要求使用for循环实现。如输入jinfeiteng,则输出结果是JINFEITENG

注意:部分源程序已存在文件test21_2.cpp中。

请勿修改主函数main和其他函数中的任何内容,仅在函数change的花括号中填写若干语句。

文件test21_2.cpp的内容如下:

char *change(char instr[]);

#include "iostream.h"

void main()

{

char instr[50];

char *outstr;

cout << "Input a string: " <<endl;

cin >> instr;

outstr=change(instr);

cout << "Over graded string: " << endl;

cout << outstr <<endl;

}

char *change(char instr[])

{

}

【答案】

char *change(char instr[])

{

char *outstr=new char[50];

const char delta = 'A' - 'a';

int i;

for( i = 0 ; instr[i] != '\0'; i++)

{

    if(instr[i] >= 'a' && instr[i] <= 'z')

     {

         outstr[i] = instr[i] + delta;

     }

     else

     {

         outstr[i] = instr[i];

     }

}

outstr[i] = '\0';

return outstr;

}

 

2.4字符串连接

4.22.简单应用题

常用字符串函数strcat(s1,s2)可将字符串s2添加到字符串s1的末端,但其使用必须保证字符串s1足够大,以便保存它自己的内容和字符串s2中的内容。请编写一个函数char *append(char *s1,char *s2),其可将字符串s2添加到字符串s1的末端,而且不受s1空间大小的限制。请利用常用字符串函数实现。

常用字符串函数说明:

strcpy(to,form):将form字符串复制到to字符串;

strcat(s1,s2):将字符串s2添加到字符串s1的末端,但必须保证字符串s1足够大;

strlen(s):返回字符串s的长度;

注意:部分源程序已存在文件test4_2.cpp中。

请勿修改主函数main和其他函数中的任何内容,仅在函数append的花括号中填写若干语句。

输出结果如下:

this is a string.

文件test4_2.cpp的内容如下:

#include<iostream.h>

#include<string.h>

char *append(char *s1,char *s2)

{

}

void main()

{

char *s,*s1,*s2;

s1="this is ";

s2="a string.";

s=append(s1,s2);

cout<<s<<endl;

}

【答案】

char *append(char *s1,char *s2)

{

char *tmp;

int length;

length=strlen(s1)+strlen(s2);

tmp=new char[length+1];

strcpy(tmp,s1);

strcat(tmp,s2);

return tmp;

}

 

2.5字符串中的数字

9.22.简单应用题

请编写一个函数int CalcDigital(char *str),该函数可返回字符串str中数字字符(即'0'-'9'10个数字)的个数,如字符串"olympic2008"中数字字符的个数为4。请用if条件判断语句与for循环语句来实现该函数。

注意:部分源程序已存在文件test9_2.cpp中。

请勿修改主函数main和其他函数中的任何内容,仅在函数find的花括号中填写若干语句。

文件test9_2.cpp的内容如下:

#include<iostream.h>

#include<string.h>

int CalcDigital(char *str);

void main()

{

char *str;

str=new char[255];

cout<<"输入字符串:";

cin>>str;

int num=CalcDigital(str);

cout<<str<<":"<<num<<endl;

}

int CalcDigital(char *str)

{

}

【答案】int CalcDigital(char *str)

{

if(str==NULL) return 0;

     int num_of_digital=0;

int len=strlen(str);

     for(int i=0;i<len;i++)

     if(str[i]<='9' && str[i]>='0')

         num_of_digital++;

return num_of_digital;

}

 

2.6字符串中最大的字符

15.22.简单应用题

请编写一个函数char MaxCharacter(char * str),该函数返回参数str所指向的字符串中具有最大ASCII码的那个字符(如字符串"world"中字符'w'具有最大的ASCII码)。当str所指向的字符串为空时,则返回空字符0x0或'\0'。

输出结果如下:

Good Morning!

Max charr

注意:部分源程序已存在文件test15_2.cpp中。

请勿修改主函数main和其他函数中的任何内容,仅在函数MaxCharacter的花括号中填写若干语句。

文件test15_2.cpp的内容如下:

#include<iostream.h>

#include<string.h>

char MaxCharacter(char * str);

void main()

{

char str[100];

strcpy(str,"Good Morning!");

char maxc=MaxCharacter(str);

cout<<str<<endl;

cout<<"Max char:"<<maxc<<endl;

}

char MaxCharacter (char *str)

{

}

【答案】

char MaxCharacter (char *str)

{

if(str==NULL)

     return 0x0;

char maxChar=0x0;

int len=strlen(str);

for(int i=0;i<len;i++)

{

     if(str[i]>maxChar)

         maxChar=str[i];

}

return maxChar;

}

 

2.7字符串删除一

18.22.简单应用题

编写函数fun(),该函数的功能是从字符串中删除指定的字符,同一字母的大、小写按不同字符处理。

例如:程序执行时输入字符串为turbo c and borland c++,从键盘上输入字符n,则输出后变为turbo c ad borlad  c++

如果输入的字符在字符串中不存在,则字符串照原样输出。

注意:部分源程序已存在文件test18_2.cpp中。

请勿改动主函数main和其他函数中的任何内容,仅在函数fun的花括号中填入所编写的若干语句。

文件test18_2.cpp的内容如下:

#include<stdio.h>

#include<iostream.h>

#include<conio.h>

void fun(char s[ ], int c)

{

}

void main()

{

static char str[ ]="turbo c and borland c++";

char ch;

cout<<"原始字符串:\n"<<str<<endl;

cout<<"输入一个字符:";

cin>>ch;

fun(str,ch);

cout<<"str="<<str<<endl;

}

【答案】

void fun(char s[ ], int c)

{

int i=0;

char *p;

p=s;       

while(*p)  

   {if(*p!=c)  

     {s[i]=*p; 

      i++;     

     }

    p++;      

   }

s[i]='\0';   

}

 

2.8字符串删除二

27.22.简单应用题

请编写函数fun(),其功能是将s所指字符串中除了下标为奇数、同时ASCII值也为奇数的字符之外,其余的所有字符都删除。字符串中剩余的字符所形成的一个新的字符串放在t所指的数组中。

例如:s所指字符串中的内容为ABCDEFG12345,其中字符AASCII码值虽为奇数,但元素所在的下标为偶数,因此必需删除;字符1ASCII码值为奇数,所在数组中的下标也为奇数,不删除,最后t所指的数组中的内容应是135

请勿修改主函数main和其他函数中的任何内容,仅在函数su的花括号中填写若干语句。

文件test27_2.cpp的内容如下:

#include <conio.h>

#include <stdio.h>

#include <iostream.h>

#include <string.h>

void fun(char *s,char t[ ])

{

}

void main()

{

char s[100],t[100];

cout<<"Please enter string S: "<<endl;

gets(s);

fun(s, t);

puts(t);

}

【答案】

void fun(char *s,char t[ ])

{

int i,j=0,n;

n=strlen(s);     

for(i=0;i<n;i++)

   if(i%2!=0&&s[i]%2!=0)  

      { t[j]=s[i];j++;}

t[j]='\0';               

}

 

2.9字符串查找

20.22.简单应用题

请编写一个函数int pattern_index(char substr[],char str[]),该函数执行含通配符"?"的字符串的查找时,该通配符可以与任一个字符匹配成功。当子串substrstr中匹配查找成功时,返回子串substrstr中的位置,否则返回值为0。要求使用for循环实现。输出结果如下:

子串起始位置:5

注意:部分源程序已存在文件test20_2.cpp中。

请勿修改主函数main和其他函数中的任何内容,仅在函数pattern_index的花括号中填写若干语句。

文件test20_2.cpp的内容如下:

#include<iostream.h>

int pattern_index(char substr[],char str[])

{

}

void main()

{

char *substring,*string;

int same;

substring="???gram";

string="this program return index of substring";

same=pattern_index(substring,string);

if(same)

cout<<"子串起始位置:"<<same<<endl;

else

cout<<"匹配不成功"<<endl;

}

【答案】

int pattern_index(char substr[],char str[])

{

int i,j,k;

 for(i=0;str[i];i++)

     for(j=i,k=0;(str[j]==substr[k])||(substr[k]=='?');j++,k++)

         if(!substr[k+1])

             return(i);

return(0);

}

 

2.10字符串排序

22.22.简单应用题

请编写函数fun(),对长度为7个字符的字符串,除首、尾字符外,将其余5个字符按ASCII值码降序排列。

例如:原来的字符串为CEAedca,则排序后输出为CedcEAa

注意:部分源程序已存在文件test22_2.cpp中。

请勿改动主函数main和其他函数中的任何内容,仅在函数fun的花括号中填入所编写的若干语句。

文件test22_2.cpp的内容如下:

#include <iostream.h>

#include <stdio.h>

#include <ctype.h>

#include <conio.h>

void int fun(char *s, int num)

{

 

 

}

void main()

{

char s[10];

printf("输入7个字符的字符串:");

gets(s);

fun(s,7);

cout<<s;

}

【答案】

int fun(char *s, int num)

{char t;

 int i, j;

 for(i=1;i<num-2;i++)   

    for(j=i+1;j<num-1;j++) 

        if(s[i]<s[j])      

          { t=s[i];       

            s[i]=s[j];

            s[j]=t;

          }

}

 

2.11回文数

26.22.简单应用题

请编写函数fun(),该函数的功能是判断字符串是否为回文,若是则函数返回1,主函数中输出YES;否则返回0,主函数中输出NO。回文是指顺读和倒读都一样的字符串。

例如:字符串LEVEL是回文,而字符串123312就不是回文。

注意:部分源程序已存在文件test26_2.cpp中。

请勿修改主函数main和其他函数中的任何内容,仅在函数fun的花括号中填写若干语句。

文件test26_2.cpp的内容如下:

#include<iostream.h>

#include<stdio.h>

#define  N  80

int fun(char *str)

{

}

void main()

{char s[N];

cout<<"Enter a string : "<<endl;

gets(s);

cout<<"\n\n";

puts(s);

if(fun(s))

cout<<"YES\n";

else     

cout<<"NO\n";

}

【答案】

int fun(char *str)

{int i,n=0,fg=1; 

 char *p=str;     

 while(*p)         

    {n++; p++;}

 for(i=0;i<n/2;i++) 

    if(str[i]==str[n-1-i]) ; 

    else

      {fg=0; break;}        

return fg;                  

}

 

3.数组操作

3.1数组中最大数

42.22.简单应用题

请编写一个函数 maxofarray(atype *p,int count),该函数从一个数组中找出其中的最大元素,并且数组中可以存放多种数据类型的元素。

注意:部分源程序已存在文件test42_2.cpp中。

请勿修改主函数main和其他函数中的任何内容,仅在函数maxofarray的花括号中填写若干语句。

文件test42_2.cpp清单如下:

#include<iostream.h>

#include<string.h>

#include<conio.h>

template<class atype>

void maxofarray(atype* p,int count)

{

}

void main()

{

int len=5;

char *p1;

cout<<"the char type array and it's length is 5:\n";

cout<<"the array element is a b c d e\n";

p1=new char[len];

for (int i=0;i<len;i++)

     p1[i]='a'+i;

maxofarray(p1,len);

}

【答案】

void maxofarray(atype* p,int count)

{

for (int j=0;j<count-1;j++)

{

     for (int k=0;k<count-1-j;k++)

         if(p[k]>p[k+1])

         {

             atype temp;

             temp=p[k];

             p[k]=p[k+1];

             p[k+1]=temp;

         }

}

cout<<"\nthe max element of this array is: "<<p[count-1]<<endl;

}

3.2计算平均数

12.22.简单应用题

请编写一个函数int Count(double a[], int n),统计出具有n个元素的一维数组中大于等于所有元素平均值的元素个数并返回这个值。注意:请使用for循环实现该函数。

注意:部分源程序已存在文件test12_2.cpp中。

请勿修改主函数main和其他函数中的任何内容,仅在函数Count的花括号中填写若干语句。

文件test12_2的内容如下:

#include<iostream.h>

int Count(double a[], int n)

{

}

void main()

{

double a[5];

cout<<"请输入5个double型的数字"<<endl;

for(int i=0;i<5;i++)

{

     cin>>a[i];

}

int result=Count(a,5);

cout<<"大于等于所有元素平均值的元素个数:"<<result<<endl;

}

【答案】int Count(double a[], int n)

{

double m=0;

int i;

    for(i=0;i<n;i++) m+=a[i];

    m=m/n;

    int c=0;

    for(i=0;i<n;i++)

        if(a[i]>=m) c++;

    return c;

}

 

3.3累加和

34.22.简单应用题

请编写一个函数sum(int array[],int len),该函数返回数组array的所有整数元素的和,其中len为数组array的长度。

注意:部分源程序已存在文件test34_2.cpp中。

请勿修改主函数main和其他函数中的任何内容,仅在函数sum的花括号中填写若干语句。

程序输出结果如下:

sum of array 15

文件test34_2.cpp的内容如下:

#include <iostream.h>

int sum(int array[],int len)

{   

}

void main()

{

static int a[5]={1,2,3,4,5};

int result=sum(a,5);

cout<<"sum of array "<<result<<endl;

}

【答案】

int sum(int array[],int len)

{

 

 int sum=0;

for (int i=0;i<len;i++)

     sum=sum+array[i];

return sum;

}

 

3.4数组排序

44.22.简单应用题

请编写一个函数 sort(int a[],int n),该函数输入的参数为一个数组串和该数组的长度,它实现将该数组从小到大重新排序的功能。注意:使用冒泡排序法实现该函数的基本功能。冒泡排序是一种简单的排序算法,它通过一系列的循环操作,每次将一个最大值移动到正确的位置在每次循环中,它比较每一对相邻的元素对,将其中较大的元素移动到上面。

注意:部分源程序已存在文件test44_2.cpp中。

请勿修改主函数main和其他函数中的任何内容,仅在函数sort的花括号中填写若干语句。

文件test44_2.cpp清单如下:

#include<iostream.h>

void sort(int a[],int n)

{

}

void main()

{

int a[]={3,8,6,4,2};

cout<<"Before sort:"<<endl;

for(int i=0;i<5;i++)

     cout<<a[i]<<" ";

cout<<endl;

sort(a,5);

cout<<"After sort:"<<endl;

for(i=0;i<5;i++)

     cout<<a[i]<<" ";

cout<<endl;

}

【答案】

void sort(int a[],int n)

{

int temp;

for(int i=1;i<n;i++)

     for(int j=0;j<n-i;j++)

         if(a[j]>a[j+1])

         {

             temp=a[j];

             a[j]=a[j+1];

             a[j+1]=temp;

         }

}

 

 

3.5数组查找一

19.22.简单应用题

请编写一个函数int SeqSearch(int list[], int start, int n, int key),该函数从start开始,在大小为n的数组list中查找key值,返回最先找到的key值的位置,如果没有找到则返回-1。请使用for循环实现。

注意:部分源程序已存在文件test19_2.cpp中。

请勿修改主函数main和其他函数中的任何内容,仅在函数SeqSearch的花括号中填写若干语句。

文件test19_2.cpp的内容如下:

#include <iostream.h>

int SeqSearch(int list[], int start, int n, int key)

{

}

void main()

{

int A[10];

int key, count=0, pos;

cout<<"Enter a list of 10 integers: ";

for(pos=0;pos<10;pos++)

{

     cin>>A[pos];

}

cout<<"Enter a key: ";

cin>>key;

pos=0;

while( (pos=SeqSearch(A,pos,10,key))!=-1)

{

     count++;

     pos++;

}

cout<<key<<" occurs "<<count<<(count!=1?" times":" time")<<" in the list."<<endl;

}

【答案】

int SeqSearch(int list[], int start, int n, int key)

{

for(int i=start;i<n;i++)

{

     if(list[i]==key)

     {

         return i;

     }

}

return -1;

}

 

3.6数组查找二

43.22.简单应用题

请编写一个函数 index(int x,int a[],int n),该函数实现先显示给定长度的一数组中所有元素,然后在其中查找一个数是否存在的功能。注意:使用for循环结构实现该函数的基本功能,根据main函数的调用情况给出正确的返回值。部分源程序已存在文件test43_2.cpp中。请勿修改主函数main和其他函数中的任何内容,仅在函数index的花括号中填写若干语句。

源程序文件test43_2.cpp清单如下:

#include<iostream.h>

bool index(int x,int a[],int n)

{

}

void main()

{

int a[]={1,2,3,4,5,6,7,8};

int num;

num=5;

cout<<"num=5\n";

if (index(num,a,8)) cout<<"true"<<endl;

else cout<<"false"<<endl;

}

【答案】

bool index(int x,int a[],int n)

{

for(int i=0;i<n;i++)

     cout<<a[i]<<" ";

for(i=0;i<n;i++)

     if (a[i]==x) return true;

return false;

}

 

3.7二维数组计算平均数

31.22.简单应用题

请编写一个函数fun(int score[][3],int num),该函数返回有一门成绩以上课程成绩在85分以上,其余课程成绩不低于70分的人数。数组score按行存放num名考生各自的三门期末考试成绩。

注意:部分源程序已存在文件test31_2.cpp中。

请勿修改主函数main和其他函数中的任何内容,仅在函数fun的花括号中填写若干语句。

程序输出结果如下:

3

Press any key to continue

文件test31_2.cpp清单如下:

#include <iostream.h>

int fun(int score[][3],int num)

{   

}

void main()

{

int score[4][3]={{70,89,92},{70,76,93},{80,86,98},{65,73,45}};

cout<<fun(score,4)<<endl;

}

【答案】

int fun(int score[][3],int num)

{

 

 

 int total=0;

int flag=0;

for (int i=0;i<num;i++)

{

     for(int j=0;j<3;j++)

     {

         if (score[i][j]<70)

         {

             flag=-1;

             j=3;

         }

         else if(score[i][j]>=85) flag=1;

     }

     if (flag==1) total=total+1;

     flag=0;    

}

return total;

}

 

3.8二维数组转置

39.22.简单应用题

请编写函数fun(),该函数的功能是将MN列的二维数组中的数据,按列的顺序依次放到一维数组中。

例如:二维数组中的数据为

33333333

44444444

55555555

则一维数组中的内容应是

334455334455334455334455

注意:部分源程序以存在文件test_2.cpp中。

请勿改动主函数main和其他函数中的任何内容,仅在函数fun的花括号中填入所编写的若干语句。

文件test39_2.cpp的内容如下:

#include<stdio.h>

#include<iostream.h>

void fun(int(*s)[10],int *b,int *n,int mm,int nn)

{

}

void main()

{

int w[10][10]={{33,33,33,33},{44,44,44,44},{55,55,55,55}},i,j;

int a[100]={0}, n=0;

cout<<"The matrix:\n";

for(i=0; i<3; i++)

     for(j=0; j<4; j++)

        cout<<w[i][j];

     cout<<endl;

   }

fun(w, a, &n, 3, 4);

cout<<"The A array:\n";

for(i=0; i<n; i++)

cout<<a[i];

cout<<"\n\n";

}

【答案】

void fun(int(*s)[10],int *b,int *n,int mm,int nn)

{

int i,j;

for(j=0;j<nn;j++)    

   for(i=0;i<mm;i++)

      {b[*n]=*(*(s+i)+j);*n=*n+1; }

}

4递归函数

4.1求和一

11.22.简单应用题

请编写一个函数 int sum(int n),该函数完成1+2+3++n的运算,并返回运算结果,其中n>0。注意:请使用递归算法实现该函数。

注意:部分源程序已存在文件test11_2.cpp中。

请勿修改主函数main和其他函数中的任何内容,仅在函数sum的花括号中填写若干语句。

文件test11_2.cpp的内容如下:

#include<iostream.h>

int sum(int n)

{

}

void main()

{

int n;

cout<<"输入n: ";

cin>>n;

int result=sum(n);

cout<<"结果为:"<<result<<endl;

}

 

4.2求和二

1.22.简单应用题

请编写一个函数long Fibo(int n),该函数返回nFibonacci数,规则如下:n等于1或者2时,Fibonacci数为1,之后每个Fibonacci数均为其前两个数之和,即:F(n)=F (n-1)+F(n-2)

注意:请使用递归算法实现该函数。

部分源程序已存在文件test1_2.cpp中。

请勿修改主函数main和其他函数中的任何内容,仅在函数Fibo的花括号中填写若干语句。如n=8时,结果是21

文件test1_2.cpp清单如下:

#include<iostream.h>

const int N=8;

long Fibo(int n);

void main()

{

long f=Fibo(N);

cout<<f<<endl;

}

long Fibo(int n)

{

}

【答案】

long Fibo(int n)

{

if(n==1) return 1L;

else if(n==2) return 1L;

else

return Fibo(n-1)+Fibo(n-2);

}

 

4.3求和三

10.22.简单应用题

请编写一个函数inline long sum(int n),用递归函数完成运算:sum(n)=1*1+2*2+• • •+n*n,递归表达式为sum(n)=sum(n-1)+n2

注意:部分源程序已存在文件test10_2.cpp中。

请勿修改主函数main和其他函数中的任何内容,仅在函数sum的花括号中填写若干语句。

文件test10_2.cpp的内容如下:

#include<iostream.h>

inline long sum(int n)

{   

}

void main()

{

int n;

cout<<"输入n:";

cin>>n;

cout<<"结果为:"<<sum(n)<<endl;

}

【答案】

inline long sum(int n)

{

if(n==1)

return 1;

else

     return n*n+sum(n-1);

}

4.4组合

41.22.简单应用题

请编写一个函数 comm(int n,int k),该函数将用递归算法计算从n个人中选择k个人组成一个委员会的不同组合数,由n个人里选k个人的组合数=由(n-1)个人里选k个人的组合数+由(n-1)个人里选(k-1)个人的组合数。

注意:部分源程序已存在文件test41_2.cpp中。

请勿修改主函数main和其他函数中的任何内容,仅在函数comm的花括号中填写若干语句。

源程序文件test41_2.cpp清单如下:

#include<iostream.h>

int comm(int n,int k)

{

}

void main()

{

int n=7,k=3;

cout<<"n=7,k=3"<<endl;

cout<<comm(n,k)<<endl;

}

【答案】

int comm(int n,int k)

{

if(k>n) return 0;

else if(n==k||k==0)

     return 1;

else

     return comm(n-1,k)+comm(n-1,k-1);

}

 

 

5.循环操作

5.1当循环

6.22.简单应用题

编写函数fun(),它的功能是利用以下所示的简单迭代方法求方程cos(x)-x=0的一个实根。

xn+1 =cos(xn)

迭代步骤如下:

(1)x1初值为0.0

(2)x0=x1,把x1的值赋给x0

(3)x1=cos(x0),求出一个新的x1

(4)x0-x1的绝对值小于0.000001, 则执行步骤(5),否则执行步骤(2)

(5)所求x1就是方程cos(x)-x=0的一个实根,做为函数值返回。

程序输出结果Root=0.739085

注意:部分源程序已存在文件test6_2.cpp中。

请勿改动主函数main和其他函数中的任何内容,仅在函数fun的花括号中填入所编写的若干语句。

文件test6_2的内容如下:

#include<conio.h>

#include<math.h>

#include<iostream.h>

float fun()

{

}

void main()

{

    cout<<"Root="<<fun()<<endl;

}

【答案】

float fun()

{ float x1=0.0,x0;   

do

 { x0=x1;                   

   x1=cos(x0);              

 }

 while(fabs(x0-x1)>=1e-6);  

return  x1;                 

}

 

5.2自然数的倒数之和

45.22.简单应用题

请编写函数fun(),它的功能是计算并输出n(包括n)以内能被59整除的所有自然数的倒数之和。

例如:从键盘给n输入20后,则输出为s=0.583333

注意:部分源程序以存在文件test45_2.cpp中。

请勿改动主函数main和其他函数中的任何内容,仅在函数fun的花括号中填入所编写的若干语句。

文件test_2.cpp的内容如下:

#include<stdio.h>

#include<iostream.h>

double fun(int n)

{

}

void main()

{

int   n;  

double  s;

cout<<"Input n"<<endl;

cin>>n;

s=fun(n);

cout<<"\ns="<<s<<endl;

}

【答案】

double fun(int n)

{ int i;

  double sum=0.0;

  for(i=1;i<=n;i++)

    if(i%5==0||i%9==0)

       sum+=1.0/i;

          return sum;

}

 

6指针操作

6.1拆分与合并

3.22.简单应用题

请编写函数fun,其功能是将两个两位数的正整数ab合并形成一个整数放在c中。合并的方式是将a数的个位和十位数依次在c数千位和十位上,b数的十位和个位数依次放在c数的个位和百位。

注意:部分源程序以存在文件test3_2.cpp中。

请勿修改主函数main和其他函数中的任何内容,仅在函数fun的花括号中填写若干语句。

文件test3_2.cpp的内容如下:

#include<iostream.h>

void fun(int a ,int b,long *c)

{

}

void main()

{int a,b;

long c;

cout<<"Input a,b;"<<endl;

cin>>a>>b;

fun(a,b,&c);

cout<<"The result is:\n"<<c<<endl;

}

【答案】

void fun(int  a, int b , long *c)

{

*c=(a%10)*1000+(b%10)*100+(a/10)*10+b/10;

}

 

6.2交换两个数

14.22.简单应用题

请编写一个函数void swap(int *x,int *y),用来交换两个数的值。

注意:部分源程序已存在文件test14_2.cpp中。

请勿修改主函数main和其他函数中的任何内容,仅在函数swap的花括号中填写若干语句。

文件test14_2.cpp的内容如下:

#include<iostream.h>

void swap(int *x,int *y);

void main()

{

int a=1,b=3;

swap(&a,&b);

cout<<"a="<<a<<" "<<"b="<<b<<endl;

}

void swap(int *x,int *y)

{

}

【答案】void swap(int *x,int *y)

{

int temp;

temp=*x;

*x=*y;

*y=temp;

}

 

 

7引用传参

7.1排序

2.22.简单应用题

请编写两个函数void sort(int &x, &y)void sort(int x,int y,int z),实现对2个和3个元素的排序并在屏幕上输出排序结果(数字之间使用跳格)。

注意:部分源程序已存放在文件test2_2.cpp中。

请勿修改主函数main和其他函数中的任何内容,仅在函数的花括号中填写若干语句。

输出结果如下:

3    4

2    3    4

文件test2_2.cpp的内容如下:

#include<iostream.h>

void sort(int &x,int &y)

{

    /**1**/

}

void sort(int x,int y,int z)

{

/**2**/

}

void main()

{

int a=4,b=3,c=2;

sort(a,b);

sort(a,b,c);

}

【答案】

1)void sort(int &x,int &y)

{

int t;

if (x>y)

{

t=x;

x=y;

y=t;

}

cout<<x<<'\t'<<y<<endl;

}

2)void sort(int x,int y,int z)

{

int t;

if(x>y){ t=x;  x=y;  y=t;}

if(x>z){ t=x;  x=z;  z=t;}

if(y>z){ t=y;  y=z;  z=t;}

cout<<x<<'\t'<<y<<'\t'<<z<<endl;

}

 

7.2交换两个数

5.22.简单应用题

请编写函数void swap(int *px,int *py) void swap(int &px,int &py),实现主程序中变量ab值的交换。

输出结果如下:

3   2

2   3

注意:部分源程序已存在文件test5_2.cpp中。

请勿修改主函数main和其他函数中的任何内容,仅在函数相应的花括号中填写若干语句。

文件test5_2.cpp的内容如下:

#include<iostream.h>

void swap(int *px,int *py)

{

/***1***/

}

void swap(int &px,int &py)

{

/***2***/

}

void main()

{

int a=2,b=3;

swap(a,b);

cout<<a<<" "<<b<<endl;

swap(&a,&b);

cout<<a<<" "<<b<<endl;  

}

【答案】

1)void swap(int *px,int *py)

{

int temp;

temp=*px;

*px=*py;

*py=temp;

}

2)void swap(int &px,int &py)

{

int temp;

temp=px;

px=py;

py=temp;

}

 

}

 

 

8.文件操作

7.22.简单应用题

编写一个函数int charnum(char fn[10]),该函数以只读方式打开文件fn,,通过统计,返回文件中字符的个数,请使用while循环实现计数功能。

注意:部分源程序已存在文件test7_2.cpp中。

请勿修改主函数main和其他函数中的任何内容,仅在函数charnum的花括号中填写若干语句。

文件test7_2.cpp的内容如下:

#include<iostream.h>

#include<fstream.h>

#include<stdlib.h>

int charnum(char fn[10]);

void main()

{

int num;

num=charnum("abc.txt");

cout<<"num="<<num<<endl;

}

int charnum(char fn[10])

{

}

【答案】

int charnum(char fn[10])

{

fstream file;

file.open(fn,ios::in);

if(!file)

{

     cout<<"abc.txt can't open"<<endl;

     abort();

}

char ch;

int i=0;

while(!file.eof())

{

     file.get(ch);

     i++;

}

file.close();

return i-1;

}

 

9.结构体

8.22.简单应用题

已知考生的记录由学号和学习成绩构成,N名考生的数据已存入a结构体数组中。请编写函数fun,该函数的功能是:找出成绩最低的考生记录,通过形参返回主函数(规定只有一个最低分)。已给予出函数的首部,请完成该函数。

注意:部分源程序已存在文件test8_2.cpp中。

请勿改动主函数main和其他函数中的任何内容,仅在函数fun的花括号中填入所编写的若干语句。

文件test8_2.cpp的内容如下:

#include<iostream.h>

#include<string.h>

#include<conio.h>

#define  N   10

typedef struct  ss

       {char  num[10];

        int  s; 

       } STU;

void fun(STU a[],STU *s)

{

 

}

void main( )

{STU 

a[N]={{"A01",81},{"A02",89},{"A03",66},{"A04",87},{"A05",77},{"A06",90},{"A07",79},{"A08",61},{"A09",80},{"A10",71}},m;

fun(a,&m);

cout<<"***** The original date *****"<<endl;

cout<<"The lowest :"<<m.num<<m.s<<endl;

}

【答案】

fun(STU a[],STU *s)

{

     int i, min;

     min=a[0].s;     

     for(i=0;i<N;i++)

       if(a[i].s<min) 

          {min=a[i].s;

           *s=a[i];   

          }

}

 

 

10.成员函数

 

17.22.简单应用题

编写类AA的成员函数int Compare(AA b),该函数用于比较*thisb的大小,若两者含有元素的个数n相同,并且数组中前n个元素值对应相同,则认为两者相等返回1,否则返回0。注意:用数组方式及for循环来实现该函数。输出结果如下:

a=b

a<>c

注意:部分源程序已存在文件test17_2.cpp中。

请勿修改主函数main和其他函数中的任何内容,仅在函数Compare的花括号中填写若干语句。

文件test17_2.cpp的内容如下:

#include<iostream.h>

#include<stdlib.h>

class AA {

int *a;

int n;

int MS;

public:

void InitAA(int aa[], int nn, int ms)

{

if(nn>ms)

{

cout<<"Error!"<<endl;

exit(1);

}

MS=ms;

n=nn;

a=new int[MS];

for(int i=0; i<n; i++) a[i]=aa[i];

}   

int Compare(AA b);

};

int AA::Compare(AA b)

{

}

void main()

{

AA a,b,c;

int x[]={1,2,3,4,5};

int y[]={1,2,3,6,7};

int z[]={1,2,5,7,9};

a.InitAA(x,3,5);

b.InitAA(y,3,5);

c.InitAA(z,3,5);

if (a.Compare(b))

cout<<"a=b"<<endl;

else

cout<<"a<>b"<<endl;

if (a.Compare(c))

cout<<"a=c"<<endl;

else

cout<<"a<>c"<<endl;

}

【答案】int AA::Compare(AA b)

{

if(n!=b.n) return 0;

for(int i=0; i<n; i++)

     if(a[i]!=b.a[i]) return 0;

return 1;

}

 

11.运算符重载

24.22.简单应用题

请编写类的成员函数char & CharArray::operator [](int i),将下标运算符[]重载,如果i没有为负数或超界则返回该字符,否则输出"Index out of range."并且返回0。要求使用if判断实现算法。输出结果如下:

Index out of range.

Index out of range.

string

Index out of range.

Index out of range.

6

注意:部分源程序已存在文件test24_2.cpp中。

请勿修改主函数main和其他函数中的任何内容,仅在函数CharArray::operator []的花括号中填写若干语句。

文件test24_2.cpp的内容如下:

#include<iostream.h>

class CharArray

{

public:

CharArray(int l)

{

     Length=l;

     Buff=new char[Length];

}

~CharArray()

{

      delete Buff;

}

int GetLength()

{

     return Length;

}

char & operator[] (int i);

private:

int Length;

char *Buff;

};

char & CharArray::operator [](int i)

{

}

void main()

{

int cnt;

CharArray string1(6);

char *string2="string";

for(cnt=0; cnt<8; cnt++)

     string1[cnt] = string2[cnt];

cout<<"\n";

for(cnt=0; cnt<8; cnt++)

     cout<<string1[cnt];

cout<<"\n";

cout<<string1.GetLength()<<endl;

}

【答案】

char & CharArray::operator [](int i)

{

static char ch=0;

if(i<Length && i>=0)

     return Buff[i];

else

{

     cout<<"\nIndex out of range.";

     return ch;

}

}

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值