欢迎光临散文网 会员登陆 & 注册

C Primer Plus Test2

2019-08-24 10:47 作者:啷个里格朗郎郎  | 我要投稿

练习题来源:C Primer Plus(第6版)中文版

2.1

/**

编写一个程序:

调用一次printf函数,将名与姓打印在同一行,在调用一次,将名与姓分别打印在两行,再调用两次,将名与姓打印在一行。

Gustav Mahler
Gustav
Mahler
Gustav Mahler
*/
#include<stdio.h>
#include<stdlib.h>
int main(){

    printf("Gustav Mahler\n");
    printf("Gustav\nMahler\n");
    printf("Gustav");
    printf("Mahler\n");
    system("pause");
    return 0;

}

2.2

/*
    将年龄转换成天数,并显示出来,不考虑闰年问题
*/
#include<stdio.h>
#include<stdlib.h>
int main(){
    
    int age = 24;
    int day;
    day = age * 365;
    printf("age = %d, day = %d\n", age, day);
    system("pause");
    return 0;
}

2.3

/*
    编写一个程序,生成以下输出
    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,调用一次打印一条
    deny函数,打印最后一条
*/
#include<stdio.h>
#include<stdlib.h>

void jolly(){
    
    printf("For he's a jolly good fellow!\n");
}

void deny(){
    
    printf("Which nobody can deny!\n");
}

int main(){
    
    jolly();
    jolly();
    jolly();
    deny();
    system("pause");
    return 0;
}

2.4

/*
    编写一个程序,生成以下输出
    Brazil, Russia, India, China
    Brazil, Russia
    India, China
    两个自定义函数,一个为br(),调用一次打印一次‘Brzail, Russia’;
    另一个为ic(),调用一次打印一次‘India, China’
    其他内容在main函数中完成
*/
#include<stdio.h>
#include<stdlib.h>

void br(){
    
    printf("Brazil, Russia");
}

void ic(){
    
    printf("India, China");
}

int main(){
    
    br();
    ic();
    printf("\n");
    br();
    printf("\n");
    ic();
    printf("\n");
    system("pause");
    return 0;
}

2.5

/*
    创建一个整型变量toes,并将oes设置为10,。
    程序中还要计算toes的两倍和平方,并将这三个值打印出来。
*/
#include<stdio.h>
#include<stdlib.h>
int main(){
    
    int toes = 10;
    int toes_double, toes_sqirt;
    toes_double = toes * 2;
    toes_sqirt = toes * toes;
    printf("toes = %d\ntoes_double = %d\ntoes_sqirt = %d\n", toes, toes_double, toes_sqirt);
    system("pause");
    return 0;
}

2.6

/*
    生成以下格式的输出
    Smile!Smile!Smile!
    Smile!Smile!
    Smile!
    定义一个函数,该函数被调用一次打印一次Smile!,根据程序的需要使用该函数。
*/
#include<stdio.h>
#include<stdlib.h>

void Smile(){
    
    printf("Smile!");
}

int main(){
    
    int i;
    int j;
    
    for( i = 0; i < 2; i++){
        for( j = i; j < 2; j ++){

            Smile();
        }
        printf("\n");
        
    }
    
    system("pause");
    return 0;
}

2.7

/*
    调用一个名为one_three()的函数。该函数在打印一行单词one,在调用第二个函数two,
    然后在另一行打印单词three。two函数在一行显示two,main函数在调用one_three函数前打印短语
    starting now,并在调用完毕后显示短语done!程序输出如下
    starting now:
    one
    two
    three
    done!
*/
#include<stdio.h>
#include<stdlib.h>

void two(){
    
    printf("two\n");
}

void one_three(){
    
    printf("one\n");
    two();
    printf("three\n");
}

int main(){
    
    printf("starting now:\n");
    one_three();
    printf("done!\n");
    system("pause");
    return 0;
}

C Primer Plus Test2的评论 (共 条)

分享到微博请遵守国家法律