C Primer Plus 第六版第七章编程练习答案

部分习题代码丢失,需要请联系博主。
编译环境:Visu部分习题代码丢失,需要请联系博主。
编译环境:Visual Studio 2017al Studio 2017

#include<stdio.h>
#include<stdlib.h>
#define JB 10.00
#define JBT 40
#define JBB 1.5
#define S300 0.15
#define SX150 0.2
#define SX 0.25
int main(void)
//1
{
    char c;
    int num_b=0, num_n=1, num_o=0;
    while ((c=getchar())!='#')
    {
        if (c ==' ')
            num_b++;
        else if (c =='\n')
            num_n++;
        else
            num_o++;
    }
    printf("num_b=%d\tnum_n=%d\tnum_o=%d\n",num_b,num_n,num_o);
    system("pause");
    return 0;


}
/*遇到的问题:https://blog.csdn.net/qq_28796345/article/details/51296011
打回车程序不会立即开始执行*/
//2
{
    char c;
    int i=0;
    while ((c=getchar())!='#')
    {
        i++;
        if (i % 8 == 0)
            printf("\n");
        if (c == '\n')
            printf("-%d ", c, c);
        else

        printf("%c-%d ", c, c);

    }
    getchar();
    getchar();
    system("pause");
    return 0;
}
/*一打回车,程序就开始执行怎么办?
getchar有一个int型的返回值,当程序调用getchar时,程序就等着用户按键,用户输入的字符被存放在键盘缓
冲区中,直到用户按回车为止(回车字符也放在缓冲区中)。当用户键入回车之后,getchar才开始
从stdio流中每次读入一个字符。getchar函数的返回值是用户输入的字符的ASCII码,如出错返回 - 1,
且将用户输入的字符回显到屏幕。如用户在按回车之前输入了不止一个字符,其他字符会保留在键盘缓存区中,
等待后续getchar调用读取。也就是说,后续的getchar调用不会等待用户按键,而直接读取缓冲区中的字符,
直到缓冲区中的字符读完为后,才等待用户按键。
简单来说:在需要连续输入回车的情况下,刚输入完一个字符串,后面还需要输入另一个,
需要加一个getchar(),用它来抵消那回车键,要不第二个字符串会有问题。*/

{
    char ch = 0;
    int i = 0;

    printf("Please enter text to be analyzed:(# to terminate):");
    while ((ch = getchar()) != '#')
    {
        if ((i % 8 == 0) && (i != 0))
        {
            putchar('\n');
        }
        i++;
        printf("%c:%d ", ch, ch);
    }

    return;
}//第二题的答案
//3
{
    int a, sum_o,sum_j,n_o,n_j,o,j;
    float ave;
    n_o = 0;
    n_j = 0;
    sum_o = 0;
    sum_j = 0;
    while (scanf("%d", &a) == 1)
    {
        if (a == 0)
            break;

        if (a % 2 == 0)
        {
            n_o++;
            o = a;
            sum_o += o;
        }
        else
        {
            n_j++;
            j = a;
            sum_j += j;
        }

    }
    printf("o=%d\tj=%d\tsum_o=%d\t;sum_j=%d\n",n_o,n_j,sum_o,sum_j);
    system("pause");
    return 0;
}
//4.
{
    int i=0;
    char c;
    while ((c=getchar())!='#')              
    {
        if (c == '.')
        {
            putchar('!');
            i++;
        }
        else if (c == '!')
        {
            printf("!!");
            i++;
        }
        else putchar(c);

    }
    printf("%d", i);
    system("pause");
    return 0;
}
//5
{
    int i=0;
    char c;
    while ((c=getchar())!='#')
    {
        switch (c)
        {
        case '.':i++;
                 putchar('!');
                 break;
        case '!':i++;
            putchar('!');
            putchar('!');
            break;
        default:putchar(c);
            break;
        }
    }
    system("pause");
    return 0;
}
//6
{
    char per='e', c;
    int i = 0;
    while ((c=getchar())!='#')//注意优先级!
    {
        if (per == 'e'&&c == 'i')
            i++;
        else per = c;
    }
    printf("%d\n", i);
    system("pause");
    return 0;

}
//7
{
int hour;
double sum, shui, jing;
scanf("%d", &hour);
if (hour < JBT)
    sum = hour * JB;
else sum = (hour - JBT)*1.5*JB + 40 * JB;
if (sum <= 300)
shui = sum * S300;
else if (sum <= 450)
shui = 300 * S300 + (sum - 300)*SX150;
else shui = 300 * S300 + 150 * SX150 + (sum - 450)*SX;
jing = sum - shui;
printf("sum=%lf,shui=%lf,jing=%lf", sum, shui, jing);
system("pause");
return 0;
}
//9
{
int a, b, i,c;
scanf("%d", &a);
for (i = 2; i < a; i++)
{
    c = 0;
    for (b = 2; b*b< i; b++)
    {
        if (i%b == 0)
            c=1;
    }
    if (c == 0)
        printf("%d ", i);
}
system("pause");
return 0;
}
//10
{
    int a, b;
    double c=0;
    printf("Enter the corresponding to the desired pay rate or action:\n");
    printf("1).单身\t2).户主\n");
    while(scanf("%d %d", &a,&b)==1)
    {
        switch (a)
        {
        case 1: if (b <= 100)
            c = 0.15;
                else c = 100 * 0.15 + (b - 100)*0.28;
                break;
        case 2:if (b <= 200)
            c = b * 0.15;
               else c = 200 * 0.15 + (b - 100)*0.28;
               break;

        }
      printf("%lf\n",c);
        printf("Enter the corresponding to the desired pay rate or action:\n");
        printf("1).单身\t2).户主\n");
    }
        system("pause");
        return 0;
}
{
int a, b;
while (scanf("%d%d",&a,&b)==1)
{
    printf("%d\n", a+b);
}
system("pause");
return 0;
}
//11
{
int i, a=0, b=0, c=0, sum_a,sum_b,sum_c,n;
double sum,p_a=0,p_b=0,p_c=0,p,z=0,yun;
sum_a = sum_b = sum_c = 0;
while ((scanf("%d",&i))==1)
{
    switch (i)
    {
    case 1:printf("Please input yangqi num:\n");
        scanf("%d", &a);
        sum_a += a;
        p_a = sum_a * 2.05; 
        break;
    case 2:printf("Please input tiancai num:\n");
        scanf("%d", &b);
        sum_b += b;
        p_b = sum_b * 1.15;
        break;
    case 3:printf("Please input huluobu num:\n");
        scanf("%d", &c);
        sum_c += c;
        p_c = sum_c * 1.09;
        break;
    default: printf("try again!\n");
        break;
    }
}
p = p_a + p_b + p_c;
if (p >= 100)
{
    z = 0.05;
    p = p * 0.95;
}
sum = sum_a + sum_b + sum_c;
if (sum <= 5)
yun = 6.5;
else if (sum <= 20)
yun = 14;
else yun = 14 + (sum - 20)*0.5;

printf("yangji=%d,tiancai=%d,huluovo=%d\n", sum_a, sum_b, sum_c);
if (z ==0.05)
printf("p=%lf", p);
else
printf("没有优惠,p=%lf\n" ,p);
printf("yun=%lf\n", yun);
printf("zong=p+yun=%lf", p + yun);
system("pause");
return 0;

}
//o
{
int i=1;
printf("Think a number in your mind,I will gues it.\n");
printf("Input a y if I'm right and an n if I am wrong\n");
while (getchar() != 'y')
{
    printf("The number is %d, right?\n", i++);
    while (getchar() != '\n')
        continue;
}
system("pause");
return 0;
}

2015.6.1

猜你喜欢

转载自blog.csdn.net/qq_38967295/article/details/80532248