void to_base_n(int num, int base) { int r; if(num>0){ r=num%base; to_base_n(num/base, base); printf("%d", r); } }
#include<stdio.h> #include<ctype.h> #include"ex9.h" //* 9-1 */ double two_min(double x,double y){ return(x>y?y:x); } void func_9_1(void){ double x=3,y=4; printf("the smaller between %lf and %lf is %lf\n",x,y,two_min(x,y)); } //* 9-2 */ void chline(char ch,int i,int j){ for (int l = 0; l < j; l++) { if(l<i)putchar(' '); else putchar(ch); } } void func_9_2(void){ chline('*',2,5); printf("\n"); } //* 9-3 */ void chlinerow(char ch,int i,int j){ for (int l = 0; l < i; l++) { for(int m=0;m<j;m++)putchar(ch); printf("\n"); } } void func_9_3(void){ chlinerow('*',2,5); printf("\n"); } //* 9-4 */ double cmeans(double x,double y){ return(1/(1.0/2*(1/x+1/y))); } void func_9_4(void){ double x=3,y=4; printf("the cmeans between %lf and %lf is %lf\n",x,y,cmeans(x,y)); } //* 9-5 */ void larger_of(double *x,double *y){ *x = *y = *x>*y?*x:*y; } void func_9_5(void){ double x=3,y=4; larger_of(&x,&y); printf("x = %lf, y = %lf\n",x,y); } //* 9-6 */ int find_a(char ch){ int num = -1; if (isupper(ch)) num = ch - 'A' +1; if (islower(ch)) num = ch - 'a' +1; return(num); } void func_9_6(void){ char ch; int num; printf("please input your stings:\n"); while((ch=getchar()) != EOF){ num = find_a(ch); if(num >= 0)printf("%c is %d\n",ch,num); else printf("ch is not a alpha.\n"); } } //* 9-7 */ double power_re(double x,int i){ double num=1; if (x == 0 || i == 0) num = 1; else{ if (i>0) for(int j=0;j<i;j++)num *= x; else for(int j=0;j<-i;j++)num *= 1/x; } return(num); } void func_9_7(void){ double x =2.0; int i=2,j=-2; printf("%.2lf^%d = %.2lf\n",x,i,power_re(x,i)); printf("%.2lf^%d = %.2lf\n",x,j,power_re(x,j)); } //* 9-8 */ double power_8(double x,int i){ double num=1; if (x == 0 || i==0) num = 1; else { if (i>0) num = power_8(x,i-1) * x ; else num = 1/x * power_8(x,i+1); } return(num); } void func_9_8(void){ double x =2.0; int i=2,j=-2; printf("%lf^%d = %lf\n",x,i,power_8(x,i)); printf("%lf^%d = %lf\n",x,j,power_8(x,j)); } //* 9-9 */ void to_base_n(int base,int num){ int n=0; if (num < base) n = num; else{ n = num%base; to_base_n(base,num/base); } printf("%d",n); }
void to_base_n(unsigned int para,unsigned short int para1) { if(para < para1){ printf("%u",para); return; } to_base_n((para / (unsigned int)para1),para1); printf("%u",(unsigned int)para % para1); }
void to_base_n(unsigned long n, unsigned int base) { int r =0; r = n % base; if(n >= base) to_base_n(n/base, base); putchar('0' + r); return; }
void to_base_n(int n, int range) { int r; r = n % range; if(n >= range) to_base_n(n/range,range); printf("%d",r); //putchar('0'+r); }
int main(void) { int number = 0, base = 0; while(!feof(stdin)){ printf("\nEnter the number: "); scanf("%d", &number); printf("Enter the base to convert to: "); scanf("%d", &base); if(base<BINARY || base>DECIMAL){ printf("The base has to be in the [2,10] range. Try again!\n"); continue; } else printf("%d in base %d is: ", number, base); to_base_n(number,base); putchar('\n'); } return 0; }
int main(void) { unsigned long number; unsigned int base; printf("Enter an inreger and a base(q to quit):\n"); while(scanf("%lu %u",&number,&base) == 2) { if(base < 2 || base > 10) { printf("Please enter base range of 2 to 10!\n"); printf("Enter an inreger and a base(q to quit):\n"); continue; } printf("%lu's base %u equivalent:",number,base); to_base_n(number,base); while(getchar() != '\n'); putchar('\n'); printf("Enter an inreger and a base(q to quit):\n"); } return 0; }
int main() { int number; int binary; printf("Enter two integer (q to quit):\n"); while(scanf("%d%d",&number,&binary) == 2) { printf("num=%d,binary=%d\n",number,binary); while(binary<LIMIT1 || binary>LIMIT2) { printf("%d is beyond the range,enter a num between %d to %d:",binary,LIMIT1,LIMIT2); scanf("%d",&binary); } printf("Binary equivalent:"); to_base_n(number,binary); putchar('\n'); printf("Enter two integer (q to quit):\n"); } printf("Bye!\n"); return 0; }
void func_9_9(void){ int base =8,num=129; printf("your number is:\n"); to_base_n(base,num); printf("\n"); }
int main(void) { to_base_n(65535,2); return 0; }