个人作业 C primer plus 第4章 -第九章 9.9 答案

4.2

#include <stdio.h>
#include <string.h>
int main (void)
{
    char name[20];
    int weidth;

    printf("please enter your name: ");
    scanf("%s",name);
    weidth = strlen(name)+3;
    printf("\"%s\"\n",name);
    printf("%20s\n",name);
    printf("%-20s\n",name);
    printf("%*s\n",weidth,name);

    return 0 ;

}

4.3

#include <stdio.h>

int main (void)
{
    float height;
    char name[20];

    printf("please enter your name:\n");
    scanf("%s",name);
    printf("please enter your height:\n");
    scanf("%f",&height);
    printf("%s ,you are %.3f feet tall",name,height);

    return 0 ;

}


4.4
#include <stdio.h>

int main (void)
{
    float meter,tall;
    char name[20];

    printf("please enter your name:\n");
    scanf("%s",name);
    printf("please enter your height(cm):\n");
    scanf("%f",&tall);
    meter = tall/100;
    printf("%s ,you are %.3f meters tall",name,meter);

    return 0 ;

}

4.5
#include <stdio.h>
int main(void)
{
    float speed,size,time;

    printf("please input the speed(Mb/s): \n");
    scanf("%f",&speed);
    printf("please enter size of your file(/Mb):\n");
    scanf("%f",&size);
    time = size*8/speed;
    printf("at %.2f megabits per second, a file of"
           " %.2f megabytes\n",speed,size);
    printf("downloads in %.2f seconds",time);

    return 0 ;


}

4.6
#include <stdio.h>
#include <string.h>
int main(void)
{
    char first_name[20];
    char second_name[20];
    int f_length,s_length;

    printf("enter your first name: ");
    scanf("%s",first_name);
    printf("enter your second name:");
    scanf("%s",second_name);
    f_length = strlen(first_name);
    s_length = strlen(second_name);
    printf("%s %s\n",first_name,second_name);
    printf("%*d %*d\n",f_length,f_length,s_length,s_length);
    printf("%-*d %-*d",f_length,f_length,s_length,s_length);
    return 0;

}

4.7
#include <stdio.h>
#include <float.h>
int main(void)
{
    float f = 1.0/3.0;
    double d = 1.0/3.0;

    printf("float values: ");
    printf("%.6f %.12f %.16f\n",f,f,f);
    printf("double values: ");
    printf("%.6f %.12f %.16f\n",f,f,f);
    printf("FLT_DIG: %d\n",FLT_DIG);
    printf("DBL_DIG: %d\n",DBL_DIG);

    return 0;

}


5.1
#include <stdio.h>

int main (void)
{
    int mins,hour,min;
    const int min_translate=60;
    printf("please enter minutes and enter <0 to quit \n ");
    scanf("%d",&mins);
    while (mins>0)
    {
        hour = mins/min_translate;
        min = mins%min_translate;
        printf("the time is %02d : %d\n",hour,min);
        scanf("%d",&mins);
    }
    printf("end");

    return 0;

}

5.2
#include <stdio.h>
int main (void)
{
    int total;
    int number=0;


    printf("please enter  number ");
    scanf("%d",&total);

    while (number++<=10)
    {
        printf("%d\n",total++);

    }
    printf("end");

    return 0 ;
}
5.3

#include <stdio.h>
int main (void)
{
    int week,day,days;

    printf("please enter days: ");
    scanf("%d",&days);
    while(days>0)
    {
        week = days/7;
        day  = days%7;
        printf("%d days are %d weeks, %d days",days,week,day);
        scanf("%d",&days);
    }

    return 0;
}
5.4

#include <stdio.h>
int main (void)
{
    float cm,inches;
    int feet;
    printf("enter your height: \n");
    scanf("%f",&cm);
    while(cm>0)
    {
        feet = cm/30.48;
        inches = ((cm -feet*30.48)/30.48)*12 ;
        printf("%.1f cm =%d feet, %.1finches",cm,feet,inches);
        scanf("%f",&cm);
    }
    return 0 ;
}

5.7
#include <stdio.h>
void lifang(double n);
int main (void)
{
    double x;
    printf("enter a double number:\n");
    scanf("%f",&x);
    lifang(x);

    return 0;

}

void lifang(double n)
{
    double number;

    number = n*n*n;
    printf("the answer is %.1f",number);
}

5.8
#include <stdio.h>
int main (void)
{
    int f_number,s_number,modulu;

    printf("this problem computes moduli.\n");
    printf("Enter an integer to serve as the second operand: ");
    scanf("%d",&s_number);
    printf("now enter the first operand: ");
    scanf("%d",&f_number);
    while (f_number>0&s_number>0)
    {
        modulu = f_number%s_number;

        printf("%d %% %d is %d",f_number,s_number,modulu);

        printf("enter next number for first operand: \n");
        scanf("%d",&f_number);
    }
    printf("End\n");
    return 0;
}
5.9
#include <stdio.h>
void Temperatures(double h_temperature);

int main(void)
{
    double temp;

    printf("please enter a temperature and quit with a word q :");
    while(scanf("%lf",&temp)==1)
    {
        Temperatures(temp);
        printf("please enter another integer: ");
    }

    printf("done!");
    return 0;

}

void Temperatures(double h_temperature)
{

    const double a =5.0/9.0,b=32,c=273.16;

    printf("three temperature is h is %.2f,s is%.2f ,k is%.2f\n",h_temperature,a*(h_temperature-b),a*(h_temperature-b)+c);
}

6.1
#include <stdio.h>

int main(void)
{
    char word[26];
    int i;
    char ch ='a';

    for(i = 0; i < 26; i++)
    {
        word[i] = ch + i;
        printf("%c",word[i]);
    }

    return 0;
}

6.2

#include <stdio.h>

int main(void)
{
   int i, j;

   for (i = 0; i<5; i++)
   {
       for(j=0; j<=i; j++)
        printf("$");
       printf("\n");
   }
    return 0;
}

6.3
#include <stdio.h>

int main(void)
{
   int i,z;
   char j;
   for (i = 0; i<6; i++)
   {
       for(j ='F', z = 0; z<=i; z++, j--)
        printf("%c",j);
       printf("\n");
   }
    return 0;
}

6.4

#include <stdio.h>
int main(void)
{
    int i,j;
    char ch = 'A';

    for(i = 0; i<6; i++)
    {
        for(j = 0; j <= i; j++)
            printf("%c",ch++);
        printf("\n");
    }

    return 0;
}

6.5

#include <stdio.h>
int main(void)
{
    int i,j,k,x;
    char ch;

    printf("please enter a word: ");
    scanf("%c",&ch);

    for(i =0; i < (ch -'A')+1; i++)
    {
        for(x = 0; x< (ch -'A')-i; x++)
            printf("%c",' ');

        for(j = 0; j <= i; j++)
            printf("%c",'A'+j);
        for(k = 1; k <= i; k++)
            printf("%c",'A'+j-k-1);
        printf("\n");
    }


    return 0;
}

6.6

#include <stdio.h>
int main(void)
{
    int lower, uper;
    int i;

    printf("please enter the lower number: ");
    scanf("%d", &lower);
    printf("please enter the uper number: ");
    scanf("%d", &uper);
    for ( ; lower <= uper; lower++)
    {
        printf("%d   %d   %ld\n",lower,lower*lower,lower*lower*lower);

    }
}

6.7

#include <stdio.h>
#include <string.h>

int main(void)
{
    char word[50];
    int i ;

    printf("please enter a word: ");
    scanf("%s",word);
    for(  i = strlen(word); i >= 0 ; i--)
    {
        printf("%c",word[i-1]);
    }

    return 0;
}

6.8

#include <stdio.h>
int main(void)
{
    double x,y;

    printf("please enter two number with type of float: ");
    while(scanf("%lf %lf", &x, &y) == 2)
    {
        printf("the result is %f",(x-y)/(x*y));

    }

    return 0;
}

6.9

#include <stdio.h>
double xx(double x, double y);

int main(void)
{
    double i,j;

    xx(i,j);

    return 0;
}

double xx(double x, double y)
{
    printf("please enter two number with type of float: ");
    while(scanf("%lf %lf", &x, &y) == 2)
    {
        printf("the result is %f",(x-y)/(x*y));

    }

}
6.10
#include <stdio.h>

int main(void)
{
    int lower, upper, sum;


    printf("enter lower and upper integer limits: ");
    scanf("%d %d", &lower, &upper);
    while(lower != upper)
    {
        for(sum = 0 ; lower <= upper; lower++)
            sum += lower*lower;
        printf("the sums is %d\n",sum);
        printf("enter the next set of limits: ");
        scanf("%d %d", &lower, &upper);
    }

    printf("done!");
    return 0;
}

6.11
#include <stdio.h>

int main(void)
{
    int i,j,number[10];

    printf("please enter the number: ");
    for (i = 0; i < 8; i++)
        scanf("%d",&number[i]);
    for (j = 7; j >= 0; j--)
        printf("%d",number[j]);

    return 0;

}

6.12
#include <stdio.h>
int main(void)
{
    int times,index;
    double x,y;

    printf("please enter the times: ");
    while (scanf("%d",×)==1)
    {
        for (index = 1, x = 0; index <= times; index++)
            x += 1.0/index ;

        for (index = 1, y = 0; index <= times; index++)

            if (index % 2 == 0)
            {
                y += -1.0/index;
            }
            else
                y += 1.0/index ;

        printf("%lf , %lf", x, y);
        printf("please enter the times: \n");
    }
    
    return 0;
}

6.13
#include <stdio.h>
#define SIZE 8
int main(void)
{
    int i,k,number[SIZE];

    for(i = 0, k = 1; i< SIZE; i++)
       {
           number[i] = k;
            k *= 2;

       }
    i = 0;
    do{
            printf("%d ",number[i]);
            i++;
    } while(i < SIZE);

    return 0;
}

6.14
#include <stdio.h>
#define SIZE 8
int main(void)
{

    double number[SIZE], sums[SIZE];
    int i;
    double sum;

    printf("please enter the integetr: ");
    for ( i = 0, sum = 0; i < SIZE; i++)
    {
        scanf("%lf",&number[i]);
        sum += number[i];
        sums[i] = sum ;
    }

    for ( i = 0; i < SIZE; i++)
    {
        printf("%5.1f ",number[i]);
    }
    printf("\n");
    for ( i = 0; i < SIZE; i++)
    {
        printf("%5.1f ",sums[i]);
    }

    return 0;
}

6.15

#include <stdio.h>
#include <string.h>
int main(void)
{

    char word[255];
    int i=0;
    printf("please enter the word you want ro print: ");
    scanf("%c",&word[i]);
    do{
        i++;
        scanf("%c",&word[i]);
    }while (word[i] != '\n');

    for(i = strlen(word); i>=0; i--)
        printf("%c",word[i]);

    return 0;
}

6.16

#include <stdio.h>
#define Da_interest_rate 0.1
#define De_interest_rate 0.05

int main(void)
{
    int year = 0;
    double Da_money = 100.0, De_money = 100.0;

    do{
        year ++;
        Da_money += 100*Da_interest_rate;
        De_money += De_money*De_interest_rate;
    }while(Da_money >= De_money );

    printf("after %d years the  Deirdre moneys is more than Daphne\n ",year);
    printf("and they money are %lf , %lf",De_money,Da_money);

    return 0;
}

6.17

#include <stdio.h>
#define interst_rate 0.08
#define take_money 100000
int main(void)
{
    double money = 1000000;
    int year;

    for(year = 0; money >= 0; year++)
    {
        money += money*interst_rate - take_money;
    }
    printf("after %d years the count is empty",year);

    return 0;
}

6.18

#include <stdio.h>
#define Dunbar_number 150
int main(void)
{
    int week,friend_number = 5;

    for(week = 0; friend_number <=Dunbar_number; week++)
        friend_number = (friend_number-week)*2 -week;
    printf("after %d weeks the friends above Dunbar's number",week);

    return 0;
}

7.1
#include <stdio.h>
#include <ctype.h>
int main(void)
{
    long space = 0L;
    long huanhang = 0L;
    long others = 0L;
    char ch;

    while((ch = getchar()) != '#')
    {
        if (ch == '\n')
            huanhang ++;
        else if (isspace(ch))
            space ++;
        else
            others ++;
    }

    printf("%ld %ld %ld", space, huanhang, others);
    return 0;
}

7.2
#include <stdio.h>
#define limit 8
int main(void)
{
    char ch;
    int number = 0;

    while((ch = getchar()) !='#')
    {
        printf("%c-%d ", ch, ch);
        number ++;
        if (number % limit ==0)
            printf("\n");
    }
    printf("\n");
    printf ("done!");
    return 0;
}

7.3
#include <stdio.h>
#define limit 8
int main(void)
{
    int ch;
    int odd_number =0;
    int even_number =0;
    float odd_average =0.0f;
    float even_average =0.0f;

    while(scanf("%d",&ch) == 1 && ch !=0)
    {
        if (ch %2 ==0)
        {
            even_number++;
            even_average += ch;
        }
        else
            {odd_number++;
            odd_average += ch;
            }
    }

    printf ("even is %d and the average of them is %.1f",even_number,even_average/even_number);
    printf("\n");
    printf ("odd is %d and the average of them is %.1lf",odd_number,odd_average/odd_number);
    return 0;
}

7.4
#include <stdio.h>
int main(void)
{
    char ch;
    int i,number =0;

    printf("please enter your word: ");
    while((ch = getchar()) != '#')
       {
        if (ch == '.')
        {
            printf("!");
            number++;
        }
        else if (ch == '!')
            {
                printf("!!");
                number++;
            }
        else
                printf("%c", ch);
        }

    printf ("\n the number of change are %d",number);
    return 0;
}

7.5(注意中英文的感叹号是不同的)

#include <stdio.h>
int main(void)
{
    char ch;
    int i ;

    printf("please enter your word: ");
    while((ch = getchar()) != '#')
    {
        switch(ch)
        {
        case '.':
            printf("!");
            break;
        case '!':
            printf("!!");
            break;
        default :
            printf("%c",ch);
            break;
        }
    }
    return 0;
}

7.6
#include <stdio.h>
int main(void)
{
    char ch , ched ;
    int number = 0;

    printf("enter your word : ");
    while((ch = getchar()) !='#')
    {
        if (ched == 'e' && ch =='i')
            number++;
        ched = ch;

    }
    printf("the number of ei are %d",number);
    return 0;
}
7.7
#include <stdio.h>
#define first_rate 0.15
#define second_rate 0.2
#define last_rate 0.25
#define overtime 1.5

int main(void)
{
    double time ,sum_money, money;

    printf("please enter the time of this week you have done: ");
    scanf("%lf",&time);
    if (time > 40)
        time = overtime *time;
    sum_money = 10.00 * time;

    if (sum_money < 300)
        money = sum_money-sum_money *first_rate;
    else if(sum_money <450)
        money = sum_money -300.0*first_rate -(sum_money - 300.0)*second_rate;
    else
        money = sum_money -300.0*first_rate - 150.0 * second_rate - (sum_money -450.0)*last_rate;
    printf("the total money is %.1f and the net income is %.1f",sum_money, money);

    return 0;
}

7.8
#include <stdio.h>
#define first_rate 0.15
#define second_rate 0.2
#define last_rate 0.25
#define overtime 1.5

int main(void)
{
    double time ,sum_money, money,pay;
    int type;
    int sign = 1;//标志位用来使得输入不在范围内时,重新进入循环获得case值。

    printf("please enter the number of desired pay(1-4): ");
    while (scanf("%d",&type) == 1)
    {
       if (type == 5)
            break;
      while (sign != 0)
       {switch (type)
            {
            case 1:
                pay = 8.75;
                sign = 0;
                break;
            case 2:
                pay = 9.33;
                sign = 0;
                break;
            case 3:
                pay = 10.00;
                sign = 0;
                break;
            case 4:
                pay = 11.20;
                sign = 0;
                break;
            default :
                printf("please enter the right number(1-4)");
                scanf("%d",&type);
                sign = 1;
                break ;
            }
        }
        printf("please enter the time of this week you have done: ");
        scanf("%lf",&time);
        if (time > 40)
            time = overtime *time;
        sum_money = pay * time;

        if (sum_money < 300)
            money = sum_money-sum_money *first_rate;
        else if(sum_money <450)
            money = sum_money -300.0*first_rate -(sum_money - 300.0)*second_rate;
        else
            money = sum_money -300.0*first_rate - 150.0 * second_rate - (sum_money -450.0)*last_rate;
        printf("the total money is %.1f and the net income is %.1f",sum_money, money);

        printf("\nplease enter the number of desired pay: \n");
        sign = 1;
    }
    printf("done!");
    return 0;
}
7.9
#include <stdio.h>
int main(void)
{
    int number,i,j;
    int sign = 0;
    printf("please enter a number : ");
    while (scanf("%d",&number) == 1 && numbe > 0)    
    {                                   
        for ( i=2; i <= number; i++)      
        {
            for (j=2 ; j <= i; j++)
            {
                if (i % j == 0 && j != i )
                    {
                        sign = 1;
                        break;
                    }
            }
            if (sign == 0)
                printf("%d \n",i);

             sign = 0;
        }
    printf("please enter a number : ");
    }
    return 0;

}

7.10

7.11
#include <stdio.h>
#define yangji 2.05
#define beet 1.15
#define carrot 1.09
#define discount 0.05
#define first_rate 6.5
#define second_rate 14
#define third_rate 0.5

int main(void)
{
    double pound,n, discount_money, total_money, freight;
    char type;
    double total_weight = 0.0;
    double yangji_weight = 0.0;
    double beet_weight = 0.0;
    double carrot_weight = 0.0;

    printf("please enter the goods you want to (q to quit): ");
    while ((type = getchar()) != 'q')
    {
       if ( type == '\n')       //用来跳过输入重量时所按下的Enter键
            continue;
       while (getchar() !='\n')
            continue;           // 用来跳过输入行的剩余部分

        switch (type)
        {
        case 'a':
            printf("please enter the pound you want to buy: ");
            scanf("%lf",&n);
            yangji_weight += n;
            total_weight += n;
            break;
        case 'b':
            printf("please enter the pound you want to buy: ");
            scanf("%lf",&n);
            beet_weight += n;
            total_weight += n;
            break;
        case 'c':
            printf("please enter the pound you want to buy: ");
            scanf("%lf",&n);
            carrot_weight += n;
            total_weight += n;
            break;
        }

        printf("please enter another goods your want to : ");
    }

    total_money = yangji * yangji_weight + beet * beet_weight + carrot * carrot_weight;
    if (total_money > 100)
        discount_money = total_money * discount ;
    if (total_weight < 5)
        freight = first_rate;
    else if (total_weight < 20)
        freight = second_rate;
    else
        freight = second_rate + third_rate*(total_weight - 20.0);

    printf("the price of goods are yangji : %.1f   beet : %.2f  carrot :  %.2f  \n",yangji, beet, carrot);
    printf("your order form are    yangji : %.f   beet : %.1f  carrot :  %.1f  \n",yangji_weight, beet_weight, carrot_weight);
    printf("the total price is %.1f\n",total_money);
    printf("the discount is %.1f\n",discount_money);
    printf("the freight is %.1f\n",freight);
    printf("the finally money is %.3f",total_money + freight - discount_money);

    return 0;
}

8.1
#include <stdio.h>
int main(void)
{
    int number = 0;
    char ch;

    printf("please enter the word:");
    while((ch = getchar()) != EOF)
    {
        number++;  //用键盘输入时,在输出行的最后一行的开头处 CTRL +Z 来表示文件的结尾,这样就可以使用EOF标志了。
    }

    printf("end!");
    printf("you enter %d ",number);

    return 0;
}

8.2
#include <stdio.h>
int main(void)
{
    char ch;
    int number = 0;

    printf("please enter the word you want to print: ");
    while((ch = getchar() )!= EOF) 
    {
        switch (ch)
        {
        case '\n':
            printf("\\n");
            printf("%d  ",ch);
            break;
        case '\t':
            printf("\\t");
            printf("%d  ",ch);
             break;
        default:
            if(ch < ' ')
               {
                putchar('^');
                putchar(ch+64);
                printf(":%d ", ch);
               }
            else
               {
                putchar(ch);
                printf(":%d ", ch);
                 break;
               }
            }
       number++;
       if(number %10 == 0)
           putchar('\n');
    }
    return 0;
}

8.3
#include <stdio.h>
#include <ctype.h>
int main(void)
{
    int upper_number = 0;
    int lower_number = 0;
    char ch;

    printf("please enter words : ");
    while ((ch = getchar()) != EOF)
    {
        if (isupper(ch) != 0)
            upper_number++;
        else if (islower(ch) != 0)
            lower_number++;
    }
    printf("the alpha you enter have %d upper and %d lower ",upper_number
           ,lower_number);

    return 0;

}

8.4
#include <stdio.h>
#include <ctype.h>
int main(void)
{
    int word_number = 0;
    int number =0;
    int sum_number = 0;
    char ch;

    printf("please enter word: ");
    while((ch = getchar()) != EOF)
    {
        if ((ch == ' ') || (ch == '\n'))
        {
            number ++;  //字母个数
            sum_number += word_number;
            word_number = 0;
        }
        if ((isalpha(ch)) != 0)
        {
            word_number++;
        }
    }
    
    printf("these are %d words and have total %d alphas",number,sum_number);
    printf("the average of alpha number is %.1f",(float)sum_number/number);
    return 0;
}

8.5

#include <stdio.h>
int main(void)
{
    int guess = 50;
    int upper_limit = 100;
    int lower_limit = 0;
    char ch;

    printf("pick an integer from 1 to 100. i will try to guss");
    printf("it .\nRespond with a y if my guss is right and with");
    printf("\nan n if it is wrong .\n");
    printf("uh... is your number %d?\n",guess);
    printf("the number i guss is  bigger or smaller than the number( r or b or s )");
    while ((ch = getchar()) != 'y')
    {
        if (ch == '\n')
                continue;
        if (ch == 'b')
            {
              upper_limit = guess;
              guess = (lower_limit + upper_limit)/2;
            }
        if(ch == 's')
            {
              lower_limit = guess;
              guess = (lower_limit + upper_limit)/2;
            }
        if (ch == 'r')
            break;
        printf("well, then is it %d?\n",guess);
    }
    printf("i knew i could do it !\n");
    return 0;
}

8.6
#include <stdio.h>
char get_first(void);
int main (void)
{
    char ch;

    printf("please enter a word: ");
    ch = get_first();
    printf("%c",ch);

    return 0;
}
char get_first(void)
{
    char ch;

    ch = getchar();
    while(getchar() != '\n')
        continue;

    return ch;
}

8.7
#include <stdio.h>
#define first_rate 0.15
#define second_rate 0.2
#define last_rate 0.25
#define overtime 1.5

int main(void)
{
    double time ,sum_money, money,pay;
    char type;
    
    printf("please enter the number of desired pay(a-d):\n ");
    printf("a) $8.75/hr          ");
    printf("b) $9.33/hr\n");
    printf("c) $10.00/hr          ");
    printf("d) $11.20/hr\n");
    printf("q) quit\n");
    while (scanf("%c",&type) == 1)
    {
       if (type == 'q')
            break;
           switch (type)
            {
            case 'a': pay = 8.75; break;
            case 'b': pay = 9.33; break;
            case 'c': pay = 10.00;break;
            case 'd': pay = 11.20; break;
            default : printf("please enter the right number(a-d)");
                      continue;
            }
        printf("please enter the time of this week you have done: ");
        scanf("%lf",&time);
        if (time > 40)
            time = overtime *time;
        sum_money = pay * time;

        if (sum_money < 300)
            money = sum_money-sum_money *first_rate;
        else if(sum_money <450)
            money = sum_money -300.0*first_rate -(sum_money - 300.0)*second_rate;
        else
            money = sum_money -300.0*first_rate - 150.0 * second_rate - (sum_money -450.0)*last_rate;
        printf("the total money is %.1f and the net income is %.1f",sum_money, money);
        printf("\nplease enter the number of desired pay: \n");
    }
    printf("done!");
    return 0;
}

8.8
#include <stdio.h>
char  get_choice(void);
int check_first_input (void);
int check_second_input (void);
float add(void);
float subtract(void);
float multiply(void);
float divide(void);
int main(void)
{
    char choice;
    while ((choice = get_choice()) != 'q')
      {
        switch(choice)
        {
            case 'a':  add(); break;
            case 's':  subtract(); break;
            case 'm':  multiply(); break;
            case 'd':  divide(); break;
            default :printf("please enter the right word.\n");continue;
        }
      }
    printf("done!");

    return 0;
}

char get_choice(void)
{
    char choice;

    printf("Enter the operation of your choice: \n");
    printf("a. add           s. subtract \n");
    printf("m. multiply      d. divide\n");
    printf("q. quit\n");
    choice = getchar();
    while(getchar() !='\n')
        continue;

    return choice;
}

float add(void)
{
    float first_number,second_number;
    float result;

    first_number = check_first_input();
    second_number = check_second_input();
    result = first_number + second_number;
    printf("%.1f + %.1f = %.1f\n",first_number,second_number,result);

    return result;
}

float subtract(void)
{
    float first_number,second_number;
    float result;

    first_number = check_first_input();
    second_number = check_second_input();
    result = first_number - second_number;
    printf("%.1f - %.1f = %.1f\n",first_number,second_number,result);

    return result;
}

float multiply(void)
{
    float first_number,second_number;
    float result;

    first_number = check_first_input();
    second_number = check_second_input();
    result = first_number * second_number;
    printf("%.1f * %.1f = %.1f\n",first_number,second_number,result);

    return result;
}
float divide(void)
{
    float first_number,second_number;
    float result;

    first_number = check_first_input();
    second_number = check_second_input();
    while (second_number == 0)
    {
        printf("enter a number other than 0: ");
        second_number = check_second_input();
    }
    result = first_number / second_number;
    printf("%.1f / %.1f = %.1f\n",first_number,second_number,result);

    return result;
}
int check_first_input (void)
{
    int  input;
    char ch;

    printf("please enter the first number: ");
    while ((scanf("%d",&input)) == 0)
    {
        while ((ch = getchar()) != '\n')   //这里的getchar()获得是上次input的值,并不是再从键盘获取新的值
            putchar(ch);
        printf(" is not an number.\n");
        printf("please enter a number,such as 2.5,-1.78E8,or 3 : ");
    }
    while(getchar() !='\n')  //我之前想法是这边的不等于改成等于,但是一旦这样修改那么Enter将会进入while
        continue;            // 那么这个enter键将被使用而不会将输入送入缓存区。
    return input;
}

int check_second_input (void)
{
    int input;
    char ch;

    printf("please enter the second number: ");
    while ((scanf("%d",&input)) == 0)
    {
        while ((ch = getchar()) != '\n')
            putchar(ch);
        printf(" is not an number.");
        printf("please enter a number,such as 2.5,-1.78E8,or 3  :");
    }
    while(getchar() !='\n')
        continue;
    return input;
}


9.1
#include <stdio.h>
double min(double x,double y);
int main(void)
{
    double x,y,mins;

    printf("please enter two number: \n");
    scanf("%lf %lf",&x,&y);
    mins = min(x,y);
    printf("the min number is %.2lf",mins);

    return 0;
}

double min (double x , double y)
{
    return (x > y)? y:x;
}

9.2.3
#include <stdio.h>
void chline (char ch, int i, int j);
int main (void)
{
    char ch;
    int i,j;

    printf("please enter the word and lines you want to like (a 10 2)\n");
    scanf("%c %d %d",&ch,&i,&j);
    chline(ch,i,j);

    return 0;
}
void chline (char ch, int i, int j)
{
    int x,y;

    for ( x = 1; x <= j ; x++)
    {
        for (y = 1 ; y <= i; y++)
            putchar(ch);
        putchar('\n');
    }

    return;
}

9.4

#include <stdio.h>
double avg_number(double x,double y);
int main(void)
{
    double x,y;

    printf("please enter two number (no 0)\n");
    scanf("%lf %lf",&x,&y);
    printf("the result is %.1f",avg_number(x,y));

    return 0;
}

double avg_number(double x,double y)
{
     double result;

     while((x == 0) || (y == 0))
     {
         printf("error the number can't be 0 enter agin:");
         scanf("%lf %lf",&x,&y);
     }
     result = 1/((1/x + 1/y)/2);

     return result;
}

9.5

#include <stdio.h>
void  larger_of (double * x, double * y);
int main (void)
{
    double x,y;

    printf("enter two number\n");
    scanf("%lf %lf",&x,&y);
    larger_of(&x,&y);
    printf("the big numberis %.1f %.1f",x,y);

    return 0;
}
void larger_of (double * x,double * y)
{
    double temp;

    temp = (*x >*y)? *x:*y;
    *x = temp;
    *y = temp;
}

9.6

#include <stdio.h>
void  larger_of (double * x, double * y,double *z);
int main (void)
{
    double x,y,z;

    printf("enter three number\n");
    scanf("%lf %lf %lf",&x,&y,&z);
    printf("befor:  x : %.1f y: %.1f z : %.1f\n",x,y,z);
    larger_of(&x,&y,&z);
    printf("after:  x : %.1f y: %.1f z : %.1f",x,y,z);

    return 0;
}
void larger_of (double * x,double * y,double *z)
{
    double temp_max,temp_min,temp_mid;

    if (*x > *y)
    {
        if (*z > *x)
        {
          temp_max = *z;
          temp_min = *y;
          temp_mid = *x;
        }
        else
        {   temp_max = *x;
            temp_min = (*y > *z)? *z:*y;
            temp_mid = (*y > *z)? *y:*z;
        }
    }
    else
    {
        if (*z > *y)
        {
          temp_max = *z;
          temp_min = *x;
          temp_mid = *y;
        }
        else
        {   temp_max = *y;
            temp_min = (*x > *z)? *z:*x;
            temp_mid = (*x > *z)? *x:*z;
        }
    }
    *x = temp_min;
    *y = temp_mid;
    *z = temp_max;
}

9.9

#include <stdio.h>
double power(double n, int p);
int main(void)
{
    double x, xpow;
    int exp;

    printf("Enter a number and the positive integer power");
    printf(" to which\nthe number will be raised. Enter q");
    printf(" to quit.\n");
    while (scanf("%lf%d", &x, &exp) == 2)
    {
        xpow = power(x,exp);
        printf("%.3g to the power %d is %.5g\n", x, exp, xpow);
        printf("Enter next pair of numbers or q to quit.\n");
    }
    printf("Hope you enjoyed this power trip -- bye!\n");

    return 0;
}

double power(double n, int p)
{
    double pow = 1;
    int i;

    if (p == 0 )
    {
        if (n ==0)
        {
            printf("the 0^0 is not define and as the value is 1\n");
                pow = 1;
        }
        else
            pow = 1;
    }

    else
    {
       if (p == 1)
        pow = n;
       else
       {
        pow = power(n,p-1)*n;
       }
    }

    return pow;
}

猜你喜欢

转载自blog.csdn.net/qq_36324796/article/details/78993656