用函数实现模块化程序设计 习题

作者在 2012-02-24 15:08:10 发布以下内容
1.写两个函数,分别求两个整数的最大公约数和最小公倍数,用主函数调用这两个函数,并输出结果。两个整数由键盘输人。
/*
时间:2011年11月29日11:54:34
题目:习题7.1写两个函数,分别求两个整数的最大公约数和最小公倍数
要求:用主函数调用这两个函数,并输出结果。两个整数由键盘输人。
备注:最小公倍数(Least Common Multiple)简写为LCM;最大公约数(greatest common divisor,简写为gcd
公式:两整数的乘积 等于 其最大公约数*最小公倍数
*/
# include <stdio.h>
int a,b;
int lcm,gcd;

void sort(void);
void Least_Common_Multiple (void);
void greatest_common_divisor (void);

int main()
{
    printf("输入两个正整数:");
    scanf("%d%d",&a,&b);    
    sort();
    Least_Common_Multiple();
    greatest_common_divisor();
    return 0;
}

void sort (void)
{
    int t;
    if(a>b)
    {
        t=a;
        a=b;
        b=t;
    }
    return;
}

void Least_Common_Multiple (void)
{
    int n=1;
    do
    {
        lcm = n*a;
        ++n;
    }while(0!=lcm%b);
    printf("最小公倍数是%d\n",lcm);
}

void greatest_common_divisor (void)
{
    gcd = a*b/lcm;
    printf("最大公约数是%d\n",gcd);
    return;
}
/*
在VC++6.0中的输出结果为:
————————————
输入两个正整数:12 18
最小公倍数是36
最大公约数是6
Press any key to continue
————————————
*/
 
2.求方程ax^2+bx+c=0的根,用3个函数分别求当:b^2-4nc大于0、等于0和小于0
时的根并输出结果。从主函数输入a,b,c的值。
/*
时间:2011年11月29日11:29:30
题目:习题7.2 求方程ax^2+bx+c=0的根,
要求:用3个函数分别求当:b^2-4nc大于0、等于0和小于0时的根并输出结果。
*/
# include <stdio.h>
# include <math.h>
double a,b,c;
double delta;
double x1,x2;

void input(void);
void cal_delta (void);
void positive (void);
void negative (void);
void zero (void);

int main()
{
    input();
    cal_delta();
    if(delta>0)
    {
        positive();
    }
    else if(0==delta)
    {
        zero();
    }
    else
    {
        negative();
    }
    return 0;
}

void input(void)
{
    printf("输入方程的系数:\n");
    printf("a= ");
    scanf("%lf",&a);
    printf("b= ");
    scanf("%lf",&b);
    printf("c= ");
    scanf("%lf",&c);
    return;
}

void cal_delta (void)
{    
    delta=b*b-4*a*c;
    return;
}

void positive (void)
{
    x1 = (-b + sqrt(delta))/(2*a);
    x2 = (-b - sqrt(delta))/(2*a);
    printf("方程有两个实根:\nx1=%0.2lf,x2=%0.2lf\n",x1,x2);
    return;
}

void negative (void)
{
    double real,image;
    real = -b/(2*a);
    image = sqrt(-delta)/(2*a);    
    printf("方程有两个共轭的虚根:\nx1=%0.2lf+%0.2lfi,x2=%0.2lf-%0.2lfi\n",real,image,real,image);
    return;
}

void zero (void)
{
    x1 = (-b + sqrt(delta))/(2*a);
    printf("方程有一个实根:\nx=%0.2lf\n",x1);
    return;
}

/*
在VC++6.0中的输出结果为:
————————————
输入方程的系数:
a= 1
b= 2
c= 1
方程有一个实根:
x=-1.00
Press any key to continue
-------------------------
输入方程的系数:
a= 1
b= 2
c= 3
方程有两个共轭的虚根:
x1=-1.00+1.41i,x2=-1.00-1.41i
Press any key to continue
----------------------------
输入方程的系数:
a= 1
b= 3
c= 2
方程有两个实根:
x1=-1.00,x2=-2.00
Press any key to continue
————————————
*/
 
3.写一个判素数的函数,在主函数输入一个整数,输出是否为素数的信息。
/*
时间:2011年11月29日10:51:21
题目:习题7.3 写一个判素数的函数,在主函数输入一个整数,输出是否为素数的信息
*/
# include <stdio.h>
int Is_Prime (int number);

int main()
{
    int n;
    printf("输入一个整数: ");
    scanf("%d",&n);
    if(Is_Prime(n))
    {
        printf("这个数是素数\n");
    }
    else
    {
        printf("这个数不是素数\n");
    }
    return 0;
}

int Is_Prime (int number)
{
    int i;
    for(i=2; i<number; ++i)
    {
        if(0==number%i)
        {
            return 0;
        }
    }
    if(i==number)
    {
        return 1;
    }
}

/*
在VC++6.0中的输出结果为:
————————————
输入一个整数: 2
这个数是素数
Press any key to continue
---------------------------
输入一个整数: 4
这个数不是素数
Press any key to continue
————————————
*/
 
4.写一个函数,使给定的一个3×3的二维整型散组转置,即行列互换。
/*
时间:2011年11月29日10:40:55
题目:习题7.4 写一个函数,使给定的一个3×3的二维整型散组转置,即行列互换。
*/
# include <stdio.h>
# define N 3

void input (int a[N][N]);
void convert (int a[N][N]);
void output (int a[N][N]);

int main()
{
    int a[N][N];
    input(a);
    convert(a);
    output(a);
    return 0;
}

void input (int a[N][N])
{
    int i,j;
    printf("输入%dX%d阶矩阵的值:\n",N,N);
    for(i=0; i<N; ++i)
    {
        for(j=0; j<N; ++j)
        {
            scanf("%d",&a[i][j]);
        }
    }
    return;
}

void convert (int a[N][N])
{
    int i,j;
    int t;
    for(i=0; i<N; ++i)
    {
        for(j=i; j<N; ++j)
        {
            t=a[i][j];
            a[i][j]=a[j][i];
            a[j][i]=t;
        }
    }
    return;
}

void output (int a[N][N])
{
    int i,j;
    printf("转置后的矩阵:\n");
    for(i=0; i<N; ++i)
    {
        for(j=0; j<N; ++j)
        {
            printf("%d ",a[i][j]);
        }
        printf("\n");
    }
    return;
}
/*
在VC++6.0中的输出结果为:
————————————
输入3X3阶矩阵的值:
1 2 3
4 5 6
7 8 9
转置后的矩阵:
1 4 7
2 5 8
3 6 9
Press any key to continue
————————————
*/
 
5.写一个函数,使输人的一个字符串按反序存放,在主函数中输入和输出字符串。
/*
时间:2011年11月23日13:32:09
题目:习题7.5 写一个函数,使输人的一个字符串按反序存放,在主函数中输入和输出字符串。
*/
# include <stdio.h>
# include <string.h>
# define N 100
void inverse (char str[]);

int main()
{
    char str[N];
    printf("输入字符串\n");
    gets(str);
    inverse(str);
    printf("逆序后的字符串:\n");
    puts(str);
    
    return 0;
}

void inverse (char str[])
{
    int i,j;
    char t;
    
    for(i=0; i<(strlen(str)/2); ++i)
    {
        j=strlen(str)-1-i;
        t=str[i];
        str[i]=str[j];
        str[j]=t;
    }
    return;
}
/*
在VC++6.0中的输出结果为:
————————————
输入字符串
123abc
逆序后的字符串:
cba321
Press any key to continue
————————————
*/
 
6.写一个函数,将两个字符串连接。
/*
时间:2011年11月29日13:17:03
题目:习题7.6 写一个函数,将两个字符串连接。
*/
# include <stdio.h>
# include <string.h>
# define N 100
void connect (char str1[],char str2[]);

int main()
{
    char str1[N],str2[N];
    gets(str1);
    gets(str2);
    connect(str1,str2);
    return 0;
}    

void connect (char str1[],char str2[])
{
    int i;
    char str[2*N];
    int len = strlen(str1)+strlen(str2);
    for(i=0; i<strlen(str1); ++i)
    {
        str[i]=str1[i];
    }
    for(i=strlen(str1); i<len; ++i)
    {
        str[i]=str2[i-strlen(str1)];
    }
    for(i=0; i<len; ++i)
    {
        printf("%c",str[i]);
    }
    printf("\n");
}
/*
在VC++6.0中的输出结果为:
————————————
12345
abcde
12345abcde
Press any key to continue
————————————
*/
 
7.写一个函数,将一个字符串中的元音字母复制到另一字符串,然后输出。
/*
时间:2011年11月23日14:55:05
题目:习题7.7 写一个函数,将一个字符串中的元音字母复制到另一字符串,然后输出
*/
# include <stdio.h>
# include <string.h>
# define N 100
int vowel (char alphabet);

int main()
{
    char str1[N],str2[N]={NULL};
    int i,j=0;
    
    printf("输入字符串:\n");
    gets(str1);
    for(i=0; i<strlen(str1); ++i)
    {
        if(vowel(str1[i]))
        {
            str2[j]=str1[i];
            ++j;
        }
    }
    printf("输出元音字母字符串:\n");
    puts(str2);
    return 0;
}

int vowel (char alphabet)
{
    switch (alphabet)
    {
    case 'a' :
    case 'A' :    
    case 'e' :
    case 'E' :    
    case 'i' :
    case 'I' :
    case 'o' :
    case 'O' :    
    case 'u' :
    case 'U' :            
        return 1;
    default :
        return 0;
    }
}
/*
在VC++6.0中的输出结果为:
————————————
输入字符串:
I Love You
输出元音字母字符串:
Ioeou
Press any key to continue
————————————
*/
 
8.写一个函数,输人一个4位数字,要求输出这4个数字字符.但每两个数字间空一个空格。如输人1990.应输出“l 9 9 0”。
/*
时间:2011年11月23日15:28:51
题目:习题7.8 写一个函数,输人一个4位数字
要求:输出这4个数字字符.但每两个数字间空一个空格。如输人1990.应输出"l 9 9 0"。

*/
# include <stdio.h>
# include <string.h>
# define N 100
void insert (char str[]);

int main()
{
    char str[N]={NULL};
    
    printf("输入四个数字:\n");
    gets(str);
    insert(str);
    printf("输出插入空格后的数字:\n");
    puts(str);
    return 0;
}

void insert (char str[])                //将字符串数组中第2个元素开始以后的元素挪到下标乘2的位置,在其前面插入空格
{
    int i;
    for(i=strlen(str)-1; i>0; --i)        //注意从后往前挪动
    {
        str[i*2]=str[i];
        str[i*2-1]=' ';
    }
    return;
}
/*
在VC++6.0中的输出结果为:
————————————
输入四个数字:
1990
输出插入空格后的数字:
1 9 9 0
Press any key to continue
————————————
*/
 
9.编写一个函数,由实参传来一个字符串,统计此字符串中字母、数字、空格和其他字符的个数.在主函数中输人字符串以及输出上述的结果。
/*
时间:2011年11月23日15:52:42
题目:习题7.9 编写一个函数,由实参传来一个字符串,统计此字符串中字母、数字、空格和其他字
符的个数.在主函数中输人字符串以及输出上述的结果。
*/
# include <stdio.h>
# include <string.h>
# include <ctype.h>
# define N 100
int space=0;
int digit=0;
int alpha=0;
int other=0;
void count (char str[]);

int main()
{
    char str[N];
    
    printf("输入字符串:\n");
    gets(str);
    count(str);
    printf("空格%d个\n数字%d个\n字母%d个\n其他字符%d个\n",space,digit,alpha,other);
    return 0;
}

void count (char str[])
{
    int i;
    for(i=0; i<(strlen(str)); ++i)
    {
        if(isalpha(str[i]))
        {
            ++alpha;
        }
        else if(isdigit(str[i]))
        {
            ++digit;
        }                
        else if(isspace(str[i]))
        {
            ++space;
        }
        else
        {
            ++other;
        }
    }
    return;
}
/*
在VC++6.0中的输出结果为:
————————————
输入字符串:
I love you 1314!
空格3个
数字4个
字母8个
其他字符1个
Press any key to continue
————————————
*/
 
10.写一个函数.输入一行字符,将此字符串中最长的单词输出。
/*
时间:2011年11月28日9:50:18
题目:习题7.10 写一个函数.输入一行字符,将此字符串中最长的单词输出。
*/
# include <stdio.h>
# include <string.h>
# include <ctype.h>
# define N 100
void longest (char str[]);

int main()
{
    char str[N];
    printf("输入一行字符:\n");
    gets(str);
    longest(str);
    return 0;
}

void longest (char str[])
{
    int n=0;
    int len=0;
    int i,j;
    for(i=0; i<strlen(str); ++i)
    {
        if(isalpha(str[i]))
        {
            ++n;
            if(n>len)
            {
                len=n;
                j=i;
            }
        }
        else
        {
            n=0;    
        }    
    }
    printf("最长单词是:");
    for(i=j-len+1; i<=j; ++i)
    {
        printf("%c",str[i]);
    }
    printf("\n");
    return;
}
/*
在VC++6.0中的输出结果为:
————————————
输入一行字符:
I love you
最长单词是:love
Press any key to continue
————————————
*/
 
11.写一个函数,用“起泡法”对输人的10个字符按由小到大顺序排列。
/*
时间:2011年11月28日10:13:10
题目:习题7.11 写一个函数,用"起泡法"对输人的10个字符按由小到大顺序排列。
*/
# include <stdio.h>
# include <string.h>
# define N 10
void sort(char string[]);

int main()
{
    char str[N];
    printf("输入10个字符:");
    gets(str);
    while(10!=strlen(str))
    {
        printf("输入的不是10个字符,请重新输入:");
        gets(str);
    }
    sort(str);
    return 0;
}

void sort(char string[])
{
    int i,j;
    char t;
    for(i=0; i<N-1; ++i)
    {
        for(j=i+1; j<N; ++j)
        {
            if(string[i]>string[j])
            {
                t=string[i];
                string[i]=string[j];
                string[j]=t;
            }
        }
    }
    printf("排序后的字符为:\n");
    for(i=0; i<N; ++i)
    {
        printf("%c",string[i]);
    }
    printf("\n");
    return;
}
/*
在VC++6.0中的输出结果为:
————————————
输入10个字符:13579
输入的不是10个字符,请重新输入:2468013579
排序后的字符为:
0123456789
Press any key to continue
————————————
*/
 
12.用牛顿迭代法求根。方程为。ax^3+bx^2+cx+d,系数a,b,c,d的值依次为1。2.3,4,由主函数输人。求方程在1附近的一个实根。求出根后由主函数输出
/*
时间:2011年11月28日10:47:11
题目:习题7.12 用牛顿迭代法求根。方程为。ax^3+bx^2+cx+d,系数a,b,c,d的值依次为1。2.
3,4,由主函数输人。求方程在1附近的一个实根。求出根后由主函数输出
*/
# include <stdio.h>
# include <math.h>
double solut(double a, double b, double c, double d);

int main()
{
    double a,b,c,d;
    double x;
    
    printf("输入方程参数:\n");
    printf("a= ");
    scanf("%lf",&a);
    printf("b= ");
    scanf("%lf",&b);
    printf("c= ");
    scanf("%lf",&c);
    printf("d= ");
    scanf("%lf",&d);
    x=solut(a,b,c,d);
    printf("方程的根是:%lf\n",x);
    return 0;
}

double solut(double a, double b, double c, double d)
{
    double x1=1.0;
    double x0;
    double f,f1;
    do
    {
        x0=x1;
        f=((a*x0+b)*x0+c)*x0+d;
        f1=(3*a*x0+2*b)*x0+c;
        x1=x0-f/f1;
    }while(fabs(x1-x0)>=1e-6);
    return x1;
}
/*
在VC++6.0中的输出结果为:
————————————
输入方程参数:
a= 1
b= 2
c= 3
d= 4
方程的根是:-1.650629
Press any key to continue
————————————
*/
 
13 用递归法求n阶勒让德多项式的值
/*
时间:2011年11月28日11:12:56
题目:习题7.13 用递归法求n阶勒让德多项式的值
公式:Pn(X) =1  (n=0)
            =x  (n=1)
            =((2n-1)x-Pn-1(X)-(n-1)Pn-2(x))/n  (n>=1)
*/
# include <stdio.h>
double p(int n, int x);

int main()
{
    int n;
    int x;
    double f;
    printf("输入勒让德多项式的阶数: ");
    scanf("%d",&n);
    printf("输入x的值: ");
    scanf("%d",&x);
    f=p(n,x);
    printf("P%d(%d)=%.2lf\n",n,x,f);
    return 0;
}

double p(int n, int x)
{
    double f;
    switch (n)
    {
    case 0:
        f=1;
        break;
    case 1:
        f=x;
        break;
    default:
        f=((2*n-1)*x-p(n-1,x)-(n-1)*p(n-2,x))/n;
        break;
    }
    return f;
}
/*
在VC++6.0中的输出结果为:
————————————
输入勒让德多项式的阶数: 0
输入x的值: 2
P0(2)=1.00
Press any key to continue
------------------------
输入勒让德多项式的阶数: 1
输入x的值: 2
P1(2)=2.00
Press any key to continue
-------------------------
输入勒让德多项式的阶数: 2
输入x的值: 2
P2(2)=1.50
Press any key to continue
————————————
*/
 
14.输入10个学生5门课的成绩,实现以下功能
    1.计算每个学生的平均分
    2.计算每门课的平均分
    3.找出所有50个分数中最高的分数对应的学生和课程
    4.计算平均分方差
/*
时间:2011年11月29日10:01:45
题目:习题7.14 输入10个学生5门课的成绩,实现以下功能
功能:1.计算每个学生的平均分
      2.计算每门课的平均分
      3.找出所有50个分数中最高的分数对应的学生和课程
      4.计算平均分方差
*/
# include <stdio.h>
# define N 10
# define M 5

struct student
{
    double score[5];
};
double aver_stu[N];        //计算方差时要用,改为全局变量

void input (struct student stu[]);
void average_stu (struct student stu[]);
void average_cour (struct student stu[]);
void highest (struct student stu[]);
void variance (struct student stu[]);

int main()
{
    struct student stu[N];
    input(stu);
    average_stu(stu);
    average_cour(stu);
    highest(stu);
    variance(stu);
    return 0;
}

void input (struct student stu[])
{
    int i,j;
    for(i=0; i<N; ++i)
    {
        printf("输入第%d位学生的成绩: ",i+1);
        for(j=0; j<M; ++j)
        {
            scanf("%lf",&stu[i].score[j]);
        }
    }
    return;
}

void average_stu (struct student stu[])
{
    int i,j;
    double sum;
    for(i=0; i<N; ++i)
    {
        sum=0;
        for(j=0; j<M; ++j)
        {
            sum+=stu[i].score[j];
        }
        aver_stu[i]=sum/M;
        printf("第%d位学生的平均成绩=%0.2lf\n",i+1,aver_stu[i]);
    }
    return;
}

void average_cour (struct student stu[])
{
    int i,j;
    double aver_cour[M];
    double sum;
    for(j=0; j<M; ++j)
    {
        sum=0;
        for(i=0; i<N; ++i)
        {
            sum+=stu[i].score[j];
        }
        aver_cour[j]=sum/N;
        printf("第%d门课程的平均成绩=%0.2lf\n",j+1,aver_cour[j]);
    }
    return;
}

void highest (struct student stu[])
{
    int i,j;
    int a,b;
    double max;
    max=stu[0].score[0];
    for(j=0; j<M; ++j)
    {
        for(i=0; i<N; ++i)
        {
            if(max<=stu[i].score[j])
            {
          
默认分类 | 阅读 1826 次
文章评论,共0条
游客请输入验证码
浏览11299次
文章分类
文章归档
最新评论