[C] 纯文本查看 复制代码
#include<stdio.h>
#include<ctype.h>
#include<math.h>
float min(double *p,double *q);
void chline(char ch,int i,int j);
void chline_2(char ch,int i,int j);
float harmonic_mean(double i,double j);
void larger_of(double *p,double *q);
void three_min(double *p,double *q,double *r);
int read_char(char ch);
int is_a_char(char ch);
int to_base_n(int a,int b);
void my_fibonacci(int a);
double power(double m,double n);
double power_ra(double m,double n);
int to_base_num_n(unsigned long num,int o);
unsigned long Fibonacci(int i);
int main(void)
{
int i,j,k,l,o,f1,f2,f3;
char ch;
double *p,*q,*r,m,n,f;
unsigned long num;
//9.1
printf("请输入两个数\n");
scanf("%lf%lf",&m,&n);
f=min(&m,&n); //输入的两个数的地址传给函数。返回值是较小的数。
printf("%.2f和%.2f中较小的数是%.2f\n",m,n,f);
getchar();
getchar();
//9.2
printf("请输入要打印的字符\n");
scanf("%c",&ch);
printf("请输入要打印的行数和列数\n");
scanf("%d%d",&i,&j);
chline(ch,i,j);
getchar();
getchar();
//9.3
printf("请输入要打印的字符\n");
scanf("%c",&ch);
printf("请输入每行要打印的次数和打印的行数\n");
scanf("%d%d",&i,&j);
chline_2(ch,i,j);
getchar();
getchar();
//9.4
printf("请输入两个整数\n");
scanf("%d%d",&i,&j);
m=harmonic_mean(i,j);
printf("%d和%d的调和平均数是%.2f\n",i,j,m);
getchar();
getchar();
//9.5
printf("请输入两个数\n");
scanf("%lf%lf",&m,&n);
larger_of(&m,&n);
printf("X=%.2fY=%.2f\n",m,n);
getchar();
getchar();
//9.6
printf("请输入三个DOUBLE类型数字\n");
scanf("%lf%lf%lf",&m,&n,&f);
p=&m;
q=&n;
r=&f;
three_min(p,q,r);
printf("您输入的三个数,最小的数是%.2f,中间的数是%.2f,最大的数是%.2f",*p,*q,*r);
getchar();
getchar();
//9.7
printf("\n");
while((ch=getchar())!=EOF)
{
if((i=read_char(ch))!=-1)
printf("字符位置是%d\n",i);
else
printf("不是字符\n");
}
getchar();
getchar();
// 9.8
printf("输入一个数字和冪。\n");
printf("数字将被提高。\n");
printf("按Q退出\n");
while (scanf("%lf%lf",&m, &n) == 2)
{
f=power(m,n);
printf("%.3g 的 %.3g 次幂是 %.5g\n",m,n,f);
printf("输入下一对数字或 q 退出\n");
}
printf("希望你喜欢这次体验--再见!\n");
getchar();
getchar();
// 9.9
printf("输入一个数字和冪2。\n");
printf("数字将被提高。\n");
printf("按Q退出\n");
while (scanf("%lf%lf",&m, &n) == 2)
{
f=power_ra(m,n);
if(n<0)
f=1/f;
printf("%.3g 的 %.3g 次幂是 %.5f\n",m,n,f);
printf("输入下一对数字或 q 退出\n");
}
printf("希望你喜欢这次体验--再见!\n");
getchar();
getchar();
//9.10
printf("请输入一个整数和进制(按“q”键退出)\n");
while(scanf("%lu%lu",&num,&o)==2)
{
if ((o>=2&&o<=10))
{
printf("%lu的%d进制数为:",num,o);
to_base_num_n(num,o);
printf("\n");
printf("请输入一个整数和进制(按“q”键退出)\n");
}
else
printf("进制的数在2和10之间,请重新输入\n");
}
printf("Done!\n");
getchar();
getchar();
//9.11
printf("请输入要打印的斐波纳契的项数:");
scanf("%d",&i);
Fibonacci(i);
printf("\nDone!\n");
getchar();
getchar();
return 0;
}
// 函数部分///////////////////////////////////////////////////////////////////////////////////////////////////
float min(double *p,double *q )
{
double *temp;
temp=(*p>*q)?q:p;
return *temp;
}
void chline(char ch,int i,int j)
{
int k,l;
for(k=i;k>=1;k--)
{
for(l=j;l>=1;l--)
printf("%c",ch);
printf("\n");
}
}
void chline_2(char ch,int i,int j)
{
int k,l;
for(k=j;k>=1;k--)
{
for(l=i;l>=1;l--)
printf("%c",ch);
printf("\n");
}
}
float harmonic_mean(double i,double j)
{
double m,n,l;
m=1/i;
n=1/j;
l=(m+n)/2;
l=1/l;
return l;
}
void larger_of(double *p,double *q)
{
if(*p>*q)
*q=*p;
else
*p=*q;
}
void three_min(double *p,double *q,double *r)
{
double s; //交换值
int i; //循环比较计
for(i=1;i<=3;i++)
{
if (*p>=*q)
{
s=*p;
*p=*q;
*q=s;
}
if (*q>=*r)
{
s=*q;
*q=*r;
*r=s;
}
if(*p>=*r)
{
s=*p;
*p=*r;
*r=s;
}
}
}
int read_char(char ch)
{
int s=0,p;
p=isalpha(ch);
if (p==0)
s=-1;
else if((int)ch>=65 && (int)ch<=90) //强制转换ch为ASCII码
s=ch-65+1;
else if((int)ch>=97 && (int)ch<=122)
s=ch-97+1;
return s;
}
double power (double m,double n)
{
double pow=1;
int j;
if (m==0)
{
if(n==0)
{
printf("0的0次幂未定义,因此把该值处理为1.\n");
pow=1;
}
else
pow=0;
}
if (n==0)
pow=1;
if (n>0)
for (j=1;j<=n;j++)
pow*=m;
else
{
for (j=1;j<=abs(n);j++)
pow*=m;
pow=1/pow;
}
return pow;
}
double power_ra (double m,double n)
{
double pow;
if (n<0)
n=abs(n);
if ((m==0 && n==0))
{
printf("0的0次幂未定义,因此把该值处理为1.\n");
pow=1;
}
else if(n==0)
pow=1;
else if (m==0)
pow=0;
else
{if (n>0)
pow=m*power_ra(m,n-1);
else
pow=1;
}
return pow;
}
int to_base_num_n(unsigned long num,int o)
{
int s;
s=num%o;
if (num>=o)
to_base_num_n(num/o,o);
printf("%d",s);
return 0;
}
unsigned long Fibonacci(int i)
{
int f1=1;
int f2=1;
int f3,j;
printf("%d %d ",f1,f2);
for(j=3;j<=i;j++)
{
f3=f1+f2;
printf("%d ",f3);
f1=f2;
f2=f3;
}
return 0;
}