跳转至

编程题

第二章 C语言概述 编程练习题

  1. 编写一个程序,调用一次printf()函数,把你的姓名打印在一行,再调用一次printf()函数,把你的姓名打印在两行。然后,再调用两次printf()函数,把你的姓名打印在一行。打印形式如下:

    Soler HO 第一次的内容

    Soler 第二次打印

    HO 第三次打印

    Soler HO 第三次 和第四次 打印

#include <stdio.h>

int main(void)
{
    printf("my name is : Soler HO\n");/*第一次打印内容*/
    printf("my first name is : Soler \n");/*第二次打印 ---- 名字*/
    printf("my last name is : HO \n"); /*第三次打印 ---- 姓*/
    printf("total name is : Soler HO \n"); /*第三次和第四次打印*/
    return 0;
}
2. 编写一个程序,打印你的姓名和地址
#include <stdio.h>
int main(void)
{
    char name[] = "Soler HO";
    char address[] = "Shanghai";

    printf("my name is %s , Address is %s .\n",name,address);

    return 0;
}
3. 编写一个程序,把你的年龄转换成天数,并显示这两个值,不用考虑闰年的问题
/*编写一个程序,把你的年龄转换成天数,并显示这两个值,不用考虑闰年的问题*/
#include <stdio.h>
int main(void)
{
    int age,days;
    int oneYear = 365; /*此处默认一年为365天*/

    printf("please input your age : \n");
    scanf("%d",&age);
    days = age * oneYear;
    printf("you live %d days\n",days);
    return 0;

}

  1. 编写一个程序,生成以下输出:

    1
    2
    3
    4
    For he's a jolly good fellow!
    For he's a jolly good fellow!
    For he's a jolly good fellow!
    Which nobody can deny!
    
    除了main()函数以外,该程序还要调用两个自定义函数:一个名为jolly(),用于打印前3条消息,调用一次打印一条:另一个函数名为deny(),打印最后一条消息。
    #include <stdio.h>
    
    void jolly(void);
    void deny(void);
    
    int main(void)
    {
        for(int i=1;i<=3;i++)
            jolly();
        deny();
    }
    void jolly(void)
    {
        printf("For He's a jolly good fellow!\n");
    }
    
    void deny(void)
    {
        printf("which nobody can deny!\n");
    }
    

  2. 编写一个程序,生成以下输出:

    1
    2
    3
    Brazil, Russia, India, China
    India, China,
    Brazil, Russia
    
    除了main()以外,该程序还要调用两个自定义函数:一个名为br(),调用一次打印一次"Brazil,Russia";另一个名为ic(),调用一次打印一次"India,China"。其他内容在main()函数中完成。
    /*编程练习题05*/
    #include <stdio.h>
    
    void br(void);
    void ic(void);
    
    int main(void)
    {
        br();
        printf(",");
        ic();
        printf("\n");
        ic();
        printf(",\n");
        br();
        printf("\n");
        return 0;
    }
    
    void br(void)
    {
        printf("Brazil,Russia");
    }
    
    void ic(void)
    {
        printf("India,China");
    }
    

  3. 编写一个程序,创建一个整型变量toes,并将toes设置为10.程序中还要计算toes的两倍和toes的平方。该程序应打印3个值,并分别描述以示区分。

    #include <stdio.h>
    
    int main(void)
    {
        int toes = 10;
        int twotimes = 2 * toes;
        int square = toes * toes;
        printf("origin toes is %d \n",toes);
        printf("double toes is %d \n",twotimes);
        printf("square toes is %d \n",square);
    
        return 0;
    }
    

  4. 编写一个程序,生成以下格式的输出:

    1
    2
    3
    Smile!Smile!Smile!
    Smile!Smile!
    Smile!
    
    该程序要定义一个函数,该函数要被调用一次打印一次“Smile!”,根据程序的需要使用该函数。
    #include <stdio.h>
    
    void smile(void);
    
    int main(void)
    {
        /*具体优化后续重新优化*/
        for(int i = 3;i>=1;i--)
            smile();
            printf("\n");
        for(int i = 1;i<3;i++)
            smile();
            printf("\n");
        smile();
        printf("\n");
        return 0;
    }
    void smile(void)
    {
        printf("Smile!");
    }
    

  5. 在C语言中,函数可以调用另一个函数。编写一个程序,调用一个名为one_three()的函数。 该函数在一行打印单词"one",再调用第2个函数two(),然后在另一行打印单词“three”。 two()函数在一行显示单词“two”。main()函数在调用one_three()函数前要打印短语“starting now:”, 并在调用完毕后显示短语“done!”。因此,该程序的输出应如下所示:
    1
    2
    3
    4
    5
    starting now:
    one
    two
    three
    done!
    
    #include <stdio.h>
    
    void one_three(void);
    void two(void);
    
    int main(void)
    {
        printf("starting Now : \n");
        one_three(); /*函数嵌套*/
    
        printf("Done!\n");
        return 0;
    }
    void one_three(void)
    {
        printf("one \n");
        two();
    }
    
    void two(void)
    {
        printf("two\n");
        printf("three\n");
    }