第九章 C函数练习-C primer plus

#include<stdio.h>
void to_binary(unsigned long);
int main(void) {
    unsigned long number;
    printf("Enter an integer(q to quit):\n");
    while ((scanf("%ul", &number)) == 1)
    {
        printf("Binary equivalent:");
        to_binary(number);
        putchar('\n');
        printf("Enter an integer(q to quit):\n");
    }
    printf("Done\n");
    getchar();
    getchar();
    return 0;
}
void to_binary(unsigned long n) {
    int r;
    r = n % 2;
    if (n >= 2)
        to_binary(n / 2);
    putchar('0' + r);
    return;
} 


Enter an integer(q to quit):
9
Binary equivalent:1001
Enter an integer(q to quit):
255
Binary equivalent:11111111
Enter an integer(q to quit):
1024
Binary equivalent:10000000000
Enter an integer(q to quit):
q


/* 酒店收费程序*/
#include<stdio.h>
#include"hotel.h" /* 定义常量、声明函数*/
int main(void) {
    int nights;
    double houtel_rate;
    int code;
    while ((code=menu())!= QUIT)
    {
        switch (code)
        {
        case 1:houtel_rate = HOTEL1;
            break;
        case 2:houtel_rate = HOTEL2;
            break;
        case 3:houtel_rate = HOTEL3;
            break;
        case 4:houtel_rate = HOTEL4;
            break;
        default:houtel_rate = 0.0;
            printf("Oops!\n");
            break;
        }
        nights = getnights();
        showprice(houtel_rate, nights);
    }
    printf("Thank you and goodbye.");
    return 0;
}

/*hotel.h -- hotel.c中的常量定义和函数声明*/
#define QUIT 5
#define HOTEL1 80.00
#define HOTEL2 125.00
#define HOTEL3 155.00
#define HOTEL4 200.00
#define DISCOUNT 0.95
#define STARS "************************"
// 给出选项列表
int menu(void);
// 返回预定天数
int getnights(void);
//按饭店的星级和预定的天数计算价格并显示出来
void showprice(double, int);

/*hotel.c -- 旅馆管理函数*/
#include<stdio.h>
#include"hotel.h"
int menu(void) {
    int code, status;
    printf("");
    printf("\n%s%s\n",STARS,STARS);
    printf("Enter the number of the desired hotel:\n");
    printf("1) Fairfield Arms 2) Hotel Olympic\n");
    printf("3) Cherworthy Plaza 4) The Stockton\n");
    printf("5) quit\n");
    printf("%s%s\n", STARS, STARS);
    while ((status=scanf("%d",&code))!=1||(code<1||code>5))
    {
        if (status != 1)
            scanf("%*s");
        printf("Enter an integer from 1 to 5,please.\n");
    }
    return code;
}
int getnights(void) {
    int nights;
    printf("How many nights are needed?");
    while (scanf("%d", &nights) != 1) {
        scanf("%*s");
        printf("Please enter an integer, such as 2.\n");
    }
    return nights;
}
void showprice(double rate, int nights)
{
    int n;
    double total = 0.0;
    double factor = 1.0;
    for (n = 1; n <= nights; n++, factor *= DISCOUNT)
        total += rate*factor;
    printf("The hotel cost will be $%0.2f.\n", total);
} 

************************************************
Enter the number of the desired hotel:
1) Fairfield Arms 2) Hotel Olympic
3) Cherworthy Plaza 4) The Stockton
5) quit
************************************************
1
How many nights are needed?2
The hotel cost will be $156.00.

************************************************
Enter the number of the desired hotel:
1) Fairfield Arms 2) Hotel Olympic
3) Cherworthy Plaza 4) The Stockton
5) quit
************************************************


### 指针

#include<stdio.h>
void interchange(int *u, int *v);
int main(void) {
    int x = 5, y = 10;
    printf("Originally x = %d and y = %d.\n", x, y);
    interchange(&x, &y);  /*向函数传送地址*/
    printf("Now x = %d and y = %d.\n", x, y);
    getchar();
    getchar();
    return 0;
}
void interchange(int *u, int *v) {
    int temp;
    temp = *u;
    *u = *v;
    *v = temp;
}  

Originally x = 5 and y = 10.
Now x = 10 and y = 5.


#include<stdio.h>
double min(double a, double b);
int main(void) {
    double x = 0, y = 0;
    printf("Please input two number:");
    while ((scanf("%lf %lf", &x, &y)) != 2) {
        scanf("%*s");
        printf("please input double number,e.g 2.50\n");
    }
        printf("minner is %lf", min(x, y));
    printf("\n");
    getchar();
    getchar();
    return 0;
}
double min(double a, double b) {
    double min = 0;
    if (a < b)
        min = a;
    else
        min = b;
    return min;
}  



Please input two number:s
please input double number,e.g 2.50
2.4 2.8
minner is 2.400000




#include<stdio.h>
void chline(char ch, int i, int j);
int main(void) {
    char ch;
    int x = 0, y = 0;
    printf("Please input a char:");
    scanf("%c", &ch);
    printf("please input two int\n");
    scanf("%d %d", &x, &y);
    chline(ch, x, y);
    printf("\n");
    getchar();
    getchar();
    return 0;
}
void chline(char ch,int i,int j) {
    int k;
    for (k = 1; k < i; k++)
        printf(" ");
    for (; k <= j; k++)
        printf("%c", ch);
    printf("\n");
} 


Please input a char:a
please input two int
2 6
 aaaaa



#include<stdio.h>
void chline(char ch, int i, int j);
int main(void) {
    char ch=0;
    int x = 0, y = 0;
    printf("Please input a char:");
    scanf("%c", &ch);
    printf("Please input  column and row:\n");
    scanf("%d %d", &x, &y);
        chline(ch, x, y);
    getchar();
    getchar();
    return 0;
}
void chline(char ch, int i, int j) {
    int k = 0, m = 0;
    for (k = 0; k < j; k++) {
        for (m = 0; m < i; m++)
            printf("%c", ch);
        printf("\n");
    }
}

/* 与上一个题不相同的地方为,这是先控制列数在控制行数,已经更新条件,括号结束*/

Please input a char:a
Please input  column and row:
3 5
aaa
aaa
aaa
aaa
aaa



#include<stdio.h>
double get_average(double i, double j);
int main(void) {
    double x = 0, y = 0;
    printf("Please input two double number:");
    scanf("%lf %lf", &x, &y);
    get_average(x, y);
    getchar();
    getchar();
    return 0;
}
double get_average(double i, double j) {
    double ans = 0;
    ans = ((1 / i) + (1 / j)) / 2;
    return        printf("answer is %0.3lf", 1 / ans);
}

Please input two double number:4.5 5.5
answer is 4.950





没有输出

#include<stdio.h>
void larger_of(double *i, double *j);
int main(void) {
    double x = 0, y = 0;
    printf("Please input two double number:");
    scanf("%lf %lf", &x, &y);
    larger_of(&x, &y);
    printf("the result is: x = %0.3lf, y = %0.3lf",x,y);
    getchar();
    getchar();
    return 0;
}
void larger_of(double *i, double *j) {
    if (*i < *j)
        *i = *j;
    else
        *i = *i;
}


Please input two double number:2.3 2.6
the result is: x = 2.600, y = 2.600

#include<stdio.h>
#include<ctype.h>
int get_id(char ch);
int main(void) {
    char ch;
    printf("Please input a char:");
    scanf("%c", &ch);
    printf("the %c position of number is %d ", ch, get_id(ch));
    getchar();
    getchar();
    return 0;
}
int get_id(char ch) {
    int n;
//    if (('a' <= ch && ch >= 'z') || ('A' <= ch&&ch >= 'Z'))
//    n = 26 -(ch % 26);
    if (isalpha(ch))
        return tolower( ch) - 'a' + 1;
    else return -1;
    return n;
}

Please input a char:g
the g position of number is 7


#include<stdio.h>
double power(double  n, int p);
int main(void) {
    double m = 0;
    int exp= 0;
    printf("Enter a number and the positive integer power");
    printf("to which\nthe number will be raised. Enter q");
    printf(" to quit.\n");
    //if ((scanf("%lf %d", &m,&exp)) == 2)
    scanf("%lf %d", &m, &exp);
    //printf("%0.3g to the power %ld is %0.5g\n",m,exp, power(m,exp));
    printf("%.3g to the power %ld is %.5g\n",m,exp, power(m,exp));
    getchar();
    getchar();
    return 0;
}
double power(double  n,int p) {
    // double ans=0;  // 结果为0,因为做乘积,初始化为0 结果为0
    double ans=1;
    int i = 0;
    if (p > 0)
        for (i = 1; i <= p; i++)
            ans *= n;
    else if (p < 0)
        for (i = -1; i >= p; i--)
            ans /= n;
    else if(n!=0)
        ans = 1;
    else ans = 1/n; // 因为0的0次幂无意义
    return  ans;
}

Enter a number and the integer power to which
the number will be raised. Enter q to quit.
2 -2
2 to the power -2 is 0.25


  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值