C Primer Plus课后编程习题

【原创】所有程序都运行过,部分题目存在的问题已经标注,欢迎指正。

第二章

1.

//打印姓名
#include<stdio.h>
int main()
{
    printf("Gustav Mahler\n");
    printf("Gustav\n");
    printf("Mahler\n");
    printf("Gustav Mahler\n");
    return 0;
}

2.

//打印姓名和地址
#include<stdio.h>
int main()
{
    printf("Jack Ma\n");
    printf("CHINA\n");
    return 0;
}

3.

//输入年龄返回天数
#include<stdio.h>
int main()
{
    int i_age,i_days;
    printf("please type you age:\n");
    scanf("%d",&i_age);
    i_days = 365 * i_age;
    printf("convert to days is:%d",i_days);
    return 0;
}

4.

//调用函数进行打印
#include<stdio.h>

void jolly()
{   int i;
    for(i=0;i<3;i++)
        printf("For he is a jolly good fellow!\n");
}

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

int main()
{
    jolly();
    deny();
}

5.(两个子函数的内容不显示,已解决)【调用函数格式错误,前边不需要加void】

#include<stdio.h>

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

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

int main()
{
    printf("Brazil,Russia,India,China");
    void br();
    void ic();
}

6.(待排查,使用pow(toes,2)计算10的平方,输出为99;如果直接pow(10,2)正常,直接toes*toes也正常

#include<stdio.h>
#include<math.h>

int main()
{
    int toes = 10;
    int i_twice,i_square;
    i_twice = toes * 2;
    i_square = pow(toes,2);
    printf("The twice of toes is:%d\n",i_twice);
    printf("The square of toes is:%d\n",i_square);
    return 0;
}

7.

#include<stdio.h>

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

//换行
void nn()
{
    printf("\n");
}

int main()
{
    joker();joker();joker();nn();
    joker();joker();nn();
    joker();nn();
}

8.

#include<stdio.h>

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

}

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

int main()
{
    printf("starting now:\n");
    one_three();
    return 0;

}

第三章

1.

a).整型的上溢

#include<stdio.h>
int main()
{
    int big = 4294967296;
    int toobig;
    toobig = big + 1;
    printf("The value of big is %d\nThe value of toobig is %d\n",big,toobig);
    return 0;
}

输出为:

The value of big is 0
The value of toobig is 1

b).浮点数上溢

#include<stdio.h>
int main()
{
    float toobig = 3.4E38 * 100.0f;
    printf("%e\n",toobig);
    return 0;
}

输出:

1.#INF00e+000

 

c).浮点数下溢

(按照<C Primer Plus>的描述来编写程序,结果输出未发生下溢,可能是64位机的缘故。但如果增多小数位数,精度确实会发生改变)

#include<stdio.h>
int main()
{
    float toobig;
    toobig =  0.123456789E-10/10;
    printf("%e\n",toobig);
    return 0;
}

输出为:1.234568e-012

2.

#include<stdio.h>
int main()
{
    int i_number;
    printf("Please type a number<0~127>\n");
    scanf("%d",&i_number);
    printf("The ASCII of the number is %c",i_number);
    return 0;
}

3.

#include<stdio.h>
int main()
{
    printf("\aStartled by the sudden sound, Sally shouted,\n'By the Great Pumpkin, what was that!'");
    return 0;
}

4.(待排查,输出16进制计数法时,输出为0)

#include<stdio.h>
int main()
{
    float f_number;
    printf("Enter a floating-point value:\n");
    scanf("%f",&f_number);
    printf("fix-point notation: %f\n",f_number);
    printf("expoential notations: %e\n",f_number);
    printf("p notation: %X",f_number);
    return;
}

5.(待排查,数值较大,所以使用了long long int,但是输出还是不正常

#include<stdio.h>
int main()
{
    int age;
    long long int second;
    printf("Please inter your age:");
    scanf("%d",age);
    second = age * 3.156;
    printf("The second of your age is: %d",second);
    return 0;
}

6.(出错,等待解决,了解当数值很大或者很小时,应该真么定义)

#include<stdio.h>
int main()
{
    float quart, weight;
    long long int number;
    printf("Please Enter a number of the water:");
    scanf("%f", &quart);
    weight = quart * 950;
    printf("The weight of water is %f g", weight);
    number = weight / (3 * 10 ^ -23);
    printf("The number of the water is %d",number);
}

7.(该程序在visual  studio 2019中运行时报错,而在codeblocks中正常)

#include<stdio.h>
int main()
{
    float num1, num2;
    printf("请输入你的身高(英寸)");
    scanf("%f", &num1);
    num2 = num1 * 2.54 ;
    printf("你的身高是%f cm", num2);
    return 0;
}

 

8.

#include<stdio.h>
int main()
{
    float cup;
    printf("请输入杯数:");
    scanf("%f",&cup);
    printf("品脱数:%f\n",cup * 1/2);
    printf("盎司数:%f\n",cup * 8);
    printf("汤勺数:%f\n",cup * 16 );
    printf("茶勺数:%f\n",cup * 48);
    return 0;

}

 

 

第四章

1.

#include<stdio.h>
int main()
{
    char first_name[20],last_name[20];
    printf("请输入你的名字:");
    scanf("%s",&first_name);
    printf("请输入你的姓:");
    scanf("%s",&last_name);
    printf("I get is,your name is :%s %s",first_name,last_name);
    return 0;
}

2.

 

 

3.

posted @ 2019-12-29 11:39  今夕清欢  阅读(448)  评论(0编辑  收藏  举报