指针真的难,地址的地址都要给我绕懵了……
这几天顺便把第十章看完了,回过头来做完了第九章编程练习题。
进度真的慢了……
9.11.1
设计一个函数min(x, y),返回两个double类型值的较小值。在一个简单的驱动程序中测试该函数。
#include<stdio.h>
double min(double x,double y);
int main(void)
{
double a,b;
scanf("%lf %lf",&a,&b);
printf("%lf\n",min(a,b));
return 0;
}
double min(double x,double y)
{
return (x<y)?x:y;
}
9.11.2
设计一个函数chline(ch, j, i),打印指定的字符j行i列。在一个简单的驱动程序中测试该函数。
题目有误?请移步这位大神的博客
就当作j行i列好了……
/*这个代码是我理解错误是写出来的
*所以大家要好好看题……
当时我以为要输出指定的字符……*/
void chline(,int j,int i)
{
char x;
int a=1;
while(x=getchar()&&a<j){
if(x=='\n')
a++;
}//丢掉之前的j-1行
for(a=1;a<i;a++)
getchar();//丢掉j行的i-1个字符
getchar(putchar());
}
上面的代码是我瞎写的……
写的时候还在纳闷怎么把文本传入函数
/*现场直编……*/
#include <stdio.h>
void chline(char ch, int row, int col);
int main(void)
{
char ch;
int j,i;
scanf("%c%d%d",&ch,&j,&i);
chline(ch,j,i);
return 0;
}
void chline(char ch,int row,int col)
{
for (int i=1;i<=row;i++)
{
for (int j=1;j<=col;j++)
putchar(ch);
putchar('\n');
}
return;
}
9.11.3
编写一个函数,接受三个参数:一个字符和两个整数。
字符参数是待打印的字符,第1个整数指定一行中打印字符的次数,
第2个整数指定打印指定字符的行数。编写一个调用该程序的函数。
#include <stdio.h>
void func(char z,int x,int y);
int main(void)
{
char ch;
int j,i;//j为列数,i为行数
scanf("%c%d%d",&ch,&j,&i);
func(ch,j,i);
return 0;
}
void func(char z,int x,int y)
{
int i,j;
for(i=0;i<y;i++){
for(j=0;j<x;j++)
putchar(z);
putchar('\n');
}
}
9.11.4
两数的调和平均数这样计算:先得到两数的倒数,然后计算这两个倒数的平均值,最后取计算结果的倒数。编写一个函数,接受两个double类型的参数,返回这两个参数的调和平均数。
double func(double x,double y)
{
double a,b,c;
a=1/x;
b=a/y;
c=(a+b)/2;
return 1/c;
}
总感觉自己好像又理解错了……
9.11.5
编写并测试一个函数large_of(),该函数把两个double类型变量的值替换为较大的值。例如large_of(x, y)会把x,y中较大的值重新赋给两个变量。
void large_of(double *x,dounble *y)
{
if(*x>*y)
*y=*x;
else
*x=*y;
}
这题终于用上指针了……
还没测试过
2.11.6
编写并测试一个函数,该函数以3个double变量的地址作为参数,把最小值放入第1个变量,中间值放入第2个变量,最大值放入第3个变量。
void func(double * x,double * y,double * z)
{
double temp;
temp=*y;
if(*x>*y){
temp=*y;
*y=*x;
*x=temp;
}
if(*y>*z){
temp=*z;
*z=*y;
*y=temp;
}
if(*x>*y){
temp=*y;
*y=*x;
*x=temp;
}
}
指针牌冒泡排序?
9.11.7
编写一个函数,从标准输入中读取字符,直到遇到文件结尾。程序要报告每个字符是否是字母。如果是,还要报告该字母在字母表中的位置。例如,c和C在字母表中的位置都是3。合并一个函数,以一个字符作为参数,如果该字符是一个字母则返回一个数值位置,否则返回-1。
#include<stdio.h>
int func(char ch);
int main(void)
{
char ch,x;
while((ch=getchar())!=EOF){
func(ch);
}
return 0;
}
int func(char ch)
{
int x=0;
if(ch!=EOF){
if('A'<=ch&&ch<='Z'){
printf("%c在字母表的位置%d\n",ch,x=ch-64);
}
else if('a'<=ch&&ch<='z'){
printf("%c在字母表的位置%d\n",ch,x=ch-96);
}
else{
printf("%c is not a letter.\n",ch);
x=-1;
}
}
return x;
}
9.11.8
第6章的程序清单6.20中,power()函数返回一个double类型数的正整数次幂。改进该函数,使其能正确计算负幂。另外,函数要处理0的任何次幂都为0,任何数的0次幂都为1(函数应报告0的0次幂未定义,因此把该值处理为1)。要使用一个循环,并在程序中测试该函数。
double power(double n,int p)
{
double pow=1;
int i;
if(p>0||n!=0){
for(i=1;i<=p;i++)
pow*=n;
}
else if(p<0||n!=0){
p=-p;
for(i=1;i<=p;i++)
pow*=1/n;
}
else if(p=0)
pow=1;
else
pow=0;
return pow;
}
9.11.9
使用递归函数重写编程练习8.
double power(double n,int p)
{
double pow;
int i;
if(p>0||n!=0){
pow=n*power(n,p-1);
}
else if(p<0||n!=0){
pow=n*power(n,p+1);
}
else if(p=0)
pow=1;
else
pow=0;
return pow;
}
9.11.10
为了让程序清单9.8中的to_binary()函数更通用,编写一个to_base_n()函数,接受两个在2~10范围内的参数,然后以第2个参数中指定的进制打印第1个参数的数值。例如to_base_n(129, 8)显示的结果为201,也就是129的8进制数。在一个完整的程序中测试该函数。
#include<stdio.h>
void to_base_n(unsigned long x,int n);
int main(void)
{
unsigned long x;
int n;
printf("请输入一个正整数和转换进制。\n");
scanf("%lu %d",&x,&n);
to_base_n(x,n);
return 0;
}
void to_base_n(unsigned long x,int n)
{
int r;
r=x%n;
if(x>=n)
to_base_n(x/n,n);
printf("%d",r);
return;
}
9.11.11
编写并测试Fibonacci()函数,该函数用循环代替递归计算斐波那契数。
#include<stdio.h>
unsigned long Fibonacci(unsigned n);
int main(void)
{
unsigned n;
scanf("%d",&n);
printf("%lu\n",Fibonacci(n));
return 0;
}
unsigned long Fibonacci(unsigned n)
{
unsigned long sum0=1,sum1=1,sum2=1;
int i;
if(n>2)
for(i=3;i<=n;i++){
sum2=sum1+sum0;//sum2等于前两项之和
sum0=sum1;//
sum1=sum2;
}
return sum2;
}
这个斐波那契数差点给我绕迷糊……
代码没有之前两章长了……
但内容还是挺难的……
真头疼
2019年2月6日02点05分