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

發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章