设为首页 加入收藏

TOP

计算机等级考试二级C语言程序设计专项训练题——程序设计题(一)(一)
2023-07-23 13:32:22 】 浏览:177
Tags:计算机 程序设 计专项 计题

1、请写函数proc(),其功能是:求正整数x和y的最大公约。

例如,程序执行时,若输入的两个正整数为12,24,则它们的最大公约数为12,最小公倍数为24。

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

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

#include <stdio.h>
int proc(int x,int y)
{
}
int main()
{
    int num1,num2,gcd;
    printf("\nInput two numbers:\n");
    scanf("%d %d",&num1,&num2);
    gcd=proc(num1,num2);
    printf("Greatest common divisor:%d\n",gcd);
    printf("Least common multiple:%d\n",num1*num2/gcd);   
    return 0;
}
int r;
    while (x%y!=0)         
    { 
        r=x%y;      
        x=y;
        y=r;
    } 
    return y;
参考程序

2、请编写函数proc(),其功能是:将str所指字符串中除下标为偶数、同时ASCIl码值为奇数的字符外,其余的字符都删除,串中剩余字符所形成的一个新串放在t所指的数组中。例如,若str所指字符串中的内容为ABCDEFG12345,其中字符B的ASCIl码值为偶数,所在元素的下标为奇数,因此必须删除;而字符A的ASCII码值为奇数,所在数组中的下标为偶数,因此不应当删除。依此类推,后t所指的数组中的内容应是ACEG。

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

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

#include <stdio.h> 
void proc(char *str,char t[]) 
{ 
} 
int main()
{
    char str[100],t[100];
    printf("\nPlease enter string str:");
    scanf("%s",str);
    proc(str,t);
    printf("\nThe result  is:%s\n",t);
    return 0;
}
int i,j=0;
    for (i=0;str[i]!='\0';i++)  // 从数组的第一个元素开始,到其后一个
    {
        if (i%2==0 && str[i]%2!=0) // 下标为偶数、同时ASCIl码值为奇数的字符
             t[j++]=str[i];   // 如果成立,则把它放到t数组中
    }
    t[j]='\0';  //  字符串结束标志为'\0'
参考程序

3、下列给定程序中,函数proc()的功能是:从字符串str中,删除所有大写字母’F’。

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

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

#include <stdio.h> 
void proc(char *str) 
{ 
} 
int main()
{
    char  str[80];
    printf("\Enter a string:");
    gets(str);
    printf("The original string:");
    puts(str);
    proc(str);
    printf("The string after deleted:");
    puts(str);
    printf("\n");
    return 0;
}
int i,j;
    for (i=j=0;str[i]!='\0'; i++) 
       if (str[i]!='F') 
          str[j++]=str[i];
    str[j]='\0';
参考程序

4、假定输入的字符串中只包含字母和*号。请编写函数proc(),它的功能是:将字符串中的前导*号全部删除,中间和后面的*号不删除。

例如,若字符串中的内容为****a*bc*def*g****,删除后,字符串中的内容应当是a*bc*def*g****。

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

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

#include <stdio.h>
void proc(char *str) 
{ 
} 
int main()
{
    char  str[81];
    printf("Enter a string:");
    gets(str);
    proc(str);
    printf("The string after deleted:");
    puts(str);  
    return 0;
}
    char *p=str,*q=str;
    while (*p=='*') p++; // 通过p的移动来达到使p指向首个不是*号的字符
    for (;*p!='\0';p++,q++) 
       *q=*p;
    *q='\0';
参考程序

5、请编写函数proc(),其功能是:将str所指字符串中下标为偶数的字符删除,串中剩余字符形成的新串放在t所指字符数组中。

例如,当str所指字符串中的内容为abcdefg,则在t所指数组中的内容应是bdf。

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

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

#include <stdio.h>
void proc(char *str,char t[]) 
{ 
} 
int main()
{
    char  s[81],t[81];
    printf("Enter a string:");
    gets(s);
    proc(s,t);
    printf("The result is::");
    puts(t);  
    return 0;
}
    int i,j=0;
    for(i=0; str[i]!='\0';i++)
        if (i%2!=0) t[j++]=str[i];
    t[j]='\0';
参考程序

6、请编写函数proc(),它的功能是计算:s=(In(1)4+ln(2)4+ln(3)+…+In(m))0.5

C语言中可调用log(n)函数求ln(n)。

例如,若m的值为30,则proc()函数值为8.640500。

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

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

#include <stdio.h>
#include <math.h>
double proc(int m) 
{ 
} 
int main()
{
    printf("%lf\n",proc(30));
    return 0;
}
    int i;
    double s=0.0;
    for (i=1;i<=m; i++) 
       s=s+log(i); 
    return sqr
首页 上一页 1 2 下一页 尾页 1/2/2
】【打印繁体】【投稿】【收藏】 【推荐】【举报】【评论】 【关闭】 【返回顶部
上一篇计算机等级考试二级C语言程序设计.. 下一篇OpenGL ES glut 下载和使用

最新文章

热门文章

Hot 文章

Python

C 语言

C++基础

大数据基础

linux编程基础

C/C++面试题目