第6题 (10.0分) ???????题号:5 ???????难度:中 ???????第8章
/*-------------------------------------------------------
【程序改错】
---------------------------------------------------------
题目:下列给定程序中函数fun的功能是:将长整型数中各位上为奇数的数依次取出,构
??????成一个新数放在t中。高位仍在高位,低位仍在低位。
例如:当s中的数为87653142时,t中的数为7531。
-------------------------------------------------------*/
#include <stdio.h>
void fun (long ?s, long *t)
{
????????int ??d;
????????long ?sl=1;
/***********FOUND***********/
????????t = 0;
????????while ( s > 0)
????????{
????????????????d = s%10;
/***********FOUND***********/
????????????????if (d%2 == 0)
????????????????{
????????????????????????*t = d * sl + *t;
????????????????????????sl *= 10;
????????????????}
????????????????s /= 10;
????????}
}
main()
{ ?
????????long s, t;
????????printf("\nPlease enter s:");
????????scanf("%ld", &s);
????????fun(s, &t);
????????printf("The result is: %ld\n", t);
}
第7题 (10.0分) ???????题号:60 ???????难度:中 ???????第8章
/*-------------------------------------------------------
【程序改错】
---------------------------------------------------------
题目:下列给定程序中,函数fun的功能是:实现两个整数的交换。
例如:给a和b分别输入60和65,输出为:a=65 b=60。
-------------------------------------------------------*/
#include <stdio.h>
#include <conio.h>
#include <stdlib.h>
/***********FOUND***********/
void fun(int a,b)
{
????????int t;
/***********FOUND***********/
????????t=b;b=a;a=t;
}
void main()
{
????????int a,b;
????????system("CLS");
????????printf("Enter a, b: ");
????????scanf("%d%d",&a,&b);
????????fun(&a, &b);
????????printf("a=%d ?b=%d\n ", a,b);
}
第8题 (10.0分) ???????题号:17 ???????难度:中 ???????第8章
/*-------------------------------------------------------
【程序改错】
---------------------------------------------------------
题目:下列给定程序中函数fun的功能是:将长整型数中各位上为偶数的数依次取出,构
??????成一个新数放在t中。高位仍在高位,低位仍在低位。
例如:当s中的数为87653142时,t中的数:8642。
-------------------------------------------------------*/
#include <stdio.h>
void fun (long ?s, long *t)
{
????????int ??d;
????????long ?sl=1;
????????*t = 0;
????????while ( s > 0)
????????{
????????????????d = s%10;
/***********FOUND***********/
????????????????if (d%2=0)
????????????????{ ?
????????????????????????*t=d* sl+ *t;
????????????????????????sl *= 10;
????????????????}
/***********FOUND***********/
????????????????s \= 10;
????????}
}
main()
{ ?
????????long ??s, t;
????????printf("\nPlease enter s:");
????????scanf("%ld", &s);
????????fun(s, &t);
????????printf("The result is: %ld\n", t);
}
第9题 (10.0分) ???????题号:61 ???????难度:中 ???????第8章
/*-------------------------------------------------------
【程序改错】
---------------------------------------------------------
题目:下列给定程序中,函数fun的功能是:求出数组中的最大数和次最大数,并把最
??????大数和a[0]中的数对调、次最大数和 a[1] 中的数对调。
-------------------------------------------------------*/
#include ??<conio.h>
#include ??<stdio.h>
#define ???N ??20
/***********FOUND***********/
void fun(int a,int n);
{
????????int ?i, ?m, t, k;
????????for(i=0; i<n;i++)
????????{
/***********FOUND***********/
????????????????m=0;
????????????????for(k=i+1; k<n; k++)
????????????????????????if(a[k]>a[m])
????????????????????????????????m=k;
????????????????????????t=a[i];
????????????????????????a[i]=a[m];
????????????????????????a[m]=t;
????????}
}
main()
{
????????int b[N]={11,5,12,0,3,6,9,7,10,8},n=10,i;
????????system("CLS");
????????for(i=0; i<n; i++) ?
????????????????printf("%d ",b[i]);
????????printf("\n");
????????fun(b,n);
????????for(i=0; i<n; i++) ?
????????????????printf("%d ", b[i]);
????????printf("\n");
}
第10题 (10.0分) ???????题号:64 ???????难度:中 ???????第8章
/*-------------------------------------------------------
【程序改错】
---------------------------------------------------------
题目:下列给定的程序中,fun函数的功能是:将p所指的字符串中每个单词的最后一个字
??????母改成大写(这里的"单词"是指有空格隔开的字符串)。
例如:若输入:"I am a student to take the examination",
??????则应输出:"I aM A studenT tO takE thE examinatioN"。
-------------------------------------------------------*/
#include ?<stdlib.h>
#include ?<string.h>
#include ?<conio.h>
#include ?<ctype.h>
#include ?<stdio.h>
void fun(char *p)
{
????????int k=0;
????????for ( ;*p;p++)
????????????????if (k)
????????????????{
????????/***********FOUND***********/
????????????????????????if (p==' ') ?
????????????????????????{
????????????????????????????????k=0;
????????????????????????/***********FOUND***********/
????????????????????????????????*p=toupper( *(p-1));
????????????????????????}
????????????????}
????????????????else
????????????????????????k=1;
}
void main()
{
????????char ?chrstr[64];
????????int d;
????????system("CLS"); ???
????????printf("\nPlease enter an English sentence within 63 letters: ");
????????gets(chrstr);
????????d=strlen(chrstr); ???
????????chrstr[d]=' ';
????????chrstr[d+1]=0;
????????printf("\nBofore changing:\n ?%s",chrstr);
????????fun(chrstr);
????????printf("\nAfter changing:\n ?%s",chrstr);
}