递归处理反序问题,十进制转二进制
#include<stdio.h> void to_binary(unsigned long); int main(void){ printf("Enter an integer (q to quit)!\n"); unsigned long number; while(scanf("%lu",&number)==1){ to_binary(number); printf("\n"); } return 0; } void to_binary(unsigned long n){ int r; r = n%2; if(n>=2) to_binary(n/2); putchar(r?'1':'0');//非0为true }
#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);
#include <stdio.h> #include "hotel.h" int menu(void){ int code,status; printf("%s\n",STARS); printf("Enter the number of the desired hotel:\n"); printf("1)80.00 2)125.00 3)155.00 4)200.00\n"); printf("5)quit\n"); printf("\n%s\n",STARS); while((status=scanf("%d",&code))!=1 || (code<1 || code>5)){ if(status!=1){ scanf("*s"); printf("Enter an ineger 1 to 5\n"); } } return code; } int getnights(void){ int nights; printf("How many nights are needed?"); while(scanf("%d",&nights)!=1){ scanf("*s"); printf("Enter an integer"); } return nights; } void showprice(double rate,int nights){ int n; double total=0.0,factor=1.0; for(n=1;n<=nights;n++,factor*=DISCOUNT){ //printf("n=%d rate=%f factor=%f",n,rate,factor); total += rate*factor; } printf("The total cost will be %0.2f.\n",total); }
#include <stdio.h> #include "hotel.h" int main(void){ int nights; double hotel_rate; int code; while((code = menu())!=QUIT){ switch(code){ case 1:hotel_rate = HOTEL1; break; case 2:hotel_rate = HOTEL2; break; case 3:hotel_rate = HOTEL3; break; case 4:hotel_rate = HOTEL4; break; default:hotel_rate=0.0; break; } nights = getnights(); showprice(hotel_rate,nights); } printf("Think you and goodbye!!!\n"); return 0; }
root@sky-VirtualBox:~/GccDoc# gcc -c hotel.c root@sky-VirtualBox:~/GccDoc# gcc -c usehotel.c root@sky-VirtualBox:~/GccDoc# gcc -o h hotel.o usehotel.o root@sky-VirtualBox:~/GccDoc# ./h