c primer plus 9编程练习

复习题 9

#include <stdio.h>

void show_menu(void);

int getchoice(int low, int high);

int main(void)
{
    int res;
    show_menu();
    
    while((res = getchoice(1,4)) != 4)
    {
        printf("I would like to choose %d. \n\n", res);
        show_menu();
    }
    
}

void show_menu(void)
{
    printf("Please choose one of the following: \n");
    printf("1) copy files            2) move files\n");
    printf("3) remove files          4) quit.\n");
    printf("Enter the number of your choice: ");
}

int getchoice(int low, int high)
{
    int choice;
    int status;
    
    while((status = scanf("%d", &choice)) == 1 && ( choice < low || choice > high ))
    {
        printf("\n You can only intput %d - %d.\n", low, high);
        show_menu();
    }
    
    if(status != 1)
        choice = 4;
        
    return choice;
}

 

 

1、

#include <stdio.h>

double smaller(double x, double y);

int main(void)
{
    double a = 3.0, b = 1.0;
    
    printf("the smaller is: %.2f.\n", smaller(a, b));
    
    return 0;
}

double smaller(double x, double y)
{
    double smaller;
    
    if (x < y)
        smaller = x;
    else
        smaller = y;
        
    return smaller;
}

 

 

#include <stdio.h>

double smaller(double x, double y);

int main(void)
{
    double a = 3.0, b = 1.0;
    
    printf("the smaller is: %.2f.\n", smaller(a, b));
    
    return 0;
}

double smaller(double x, double y)
{
    return x < y ? x : y;
}

 

 

2、

#include <stdio.h>

void chline(char ch, int i, int j);

int main(void)
{
    char ch = '!';
    int a = 5, b = 3;
    
    chline(ch, a, b);
    
    return 0;
}

void chline(char ch, int i, int j)
{
    int x, y;
    
    for(x = 1; x <= j; x++)
    {
        for(y = 1; y <= i; y++)
            putchar(ch);
        putchar('\n');
    }
}

 

 

3、

#include <stdio.h>

void putstr(char ch, int x, int y);

int main(void)
{
    char ch;
    int rows, cols;
    printf("please input the char you want to put: ");
    scanf("%c", &ch);
    printf("please input the cols: ");
    scanf("%d", &cols);
    printf("please input the rows: ");
    scanf("%d", &rows);
    
    putstr(ch, cols, rows);
    
    return 0;
}

void putstr(char ch, int x, int y)
{
    int i, j;
    
    for(i = 1; i <= y; i++)
    {
        for(j = 1; j <= x; j++)
        {
            putchar(ch);
        }
        putchar('\n');
    }
}

 

 

4、

 

#include <stdio.h>

double harmean(double x, double y);

int main(void)
{
    double a, b;
    printf("please input two double num.\n");
    printf("a = ");
    scanf("%lf", &a);
    printf("b = ");
    scanf("%lf", &b);
    
    printf("the harmean is : %.2f.\n", harmean(a, b));
    
    return 0; 
}

double harmean(double x, double y)
{
    double temp1;
    
    temp1 = 1/((1/x + 1/y)/2);
    
    return temp1;
}

 

 

#include <stdio.h>

double harmean(double x, double y);

int main(void)
{
    double a, b;
    printf("please input two double num.\n");
    printf("a = ");
    scanf("%lf", &a);
    printf("b = ");
    scanf("%lf", &b);
    
    printf("the harmean is : %.2f.\n", harmean(a, b));
    
    return 0; 
}

double harmean(double x, double y)
{
    double temp1;
    
    temp1 = 2/(1/x + 1/y);
    
    return temp1;
}

 

5、

#include <stdio.h>

void larger(double * x, double * y);

int main(void)
{
    double a, b;
    printf("please input double a: ");
    scanf("%lf", &a);
    printf("please input double b: ");
    scanf("%lf", &b);
    
    larger(&a, &b);
    
    printf("a: %.2f.\n", a);
    printf("b: %.2f.\n", b);
    
    return 0;
}

void larger(double * x, double * y)
{
    if(*x > *y)
        *y = *x;
    else
        *x = *y;
}

 

6、

#include <stdio.h>

void sort(double * x, double * y, double * z);

int main(void)
{
    double a, b, c;
    printf("please input double a: "); scanf("%lf", &a);
    printf("please input double b: "); scanf("%lf", &b);
    printf("please input double c: "); scanf("%lf", &c);
    
    sort(&a, &b, &c);
    
    printf("a: %.2f.\n", a);
    printf("b: %.2f.\n", b);
    printf("c: %.2f.\n", c);
    
    return 0;
}

void sort(double * x, double * y, double * z)
{
    double min, med, max;
    
    min = *x;
    min = min < *y ? min : *y;
    min = min < *z ? min : *z;
    
    max = *x;
    max = max > *y ? max : *y;
    max = max > *z ? max : *z;
    
    if(*x > min && *x < max)
        med = *x;
    if(*y > min && *y < max)
        med = *y;
    if(*z > min && *z < max)
        med = *z;
    
    *x = min;
    *y = med;
    *z = max;
    
}

 

#include <stdio.h>

void sort(double * x, double * y, double * z);

int main(void)
{
    double a, b, c;
    printf("please input double a: "); scanf("%lf", &a);
    printf("please input double b: "); scanf("%lf", &b);
    printf("please input double c: "); scanf("%lf", &c);
    
    sort(&a, &b, &c);
    
    printf("a: %.2f.\n", a);
    printf("b: %.2f.\n", b);
    printf("c: %.2f.\n", c);
    
    return 0;
}

void sort(double * x, double * y, double * z)
{
    double temp;
    if(*x > *y)
    {
        temp = *x;
        *x = *y;
        *y = temp;
    }
    if(*x > *z)
    {
        temp = *x;
        *x = *z;
        *z = temp;
    }
    if(*y > *z)
    {
        temp = *y;
        *y = *z;
        *z = temp;
    }
    
}

 

7、

#include <stdio.h>
#include <ctype.h>

void report(void);

int main(void)
{
    printf("please input some char: ");
    report(); 
    
    return 0;
}

void report(void)
{
    char ch;
    
    while((ch = getchar()) != EOF)
    {
        if(isalpha(ch))
        {
            if(isupper(ch))
            {
                putchar(ch);
                putchar(' ');
                printf("%d\n", 26 - ('Z' - ch));
            }
            else
            {
                putchar(ch);
                putchar(' ');
                printf("%d\n", 26 - ('z' - ch));
            }
        }
        else
            {
                putchar(ch);
                putchar(' ');
                printf("%d\n", -1);
            }
            
    }
}

 

#include <stdio.h>

void get_char_pos(void);

int position(char ch);

int main(void)
{
    get_char_pos();
}

void get_char_pos(void)
{
    char ch;
    printf("Enter the chars(ended by EOF, now enter):" );
    while((ch = getchar()) != EOF)
    {    
    if(ch == '\n')
        continue;
    if(position(ch) != -1)
    {
        printf("The char %c's position in alphabet is %d.\n", ch, position(ch));
    }
    else
        printf("%c is not a alphabet.\n", ch);
    }
}

int position(char ch)
{
    if(ch >= 'A' && ch <= 'Z')
        return ch - 'A' + 1;
    if(ch >= 'a' && ch <= 'z')
        return ch - 'a' + 1;
    return -1;
}

 

#include <stdio.h>

void getch(void);

int show(char ch);

int main(void)
{
    getch();
    
    return 0;
} 

void getch(void)
{
    char ch;
    
    printf("please input str: \n");
    
    while((ch = getchar())  != EOF)
    {
        if(ch == '\n')
            continue;
        if(show(ch) != -1)
        {
            printf("%c is %d.\n", ch, show(ch));
        }
        else
        {
            printf("%c is not alpha.\n");
        }
    }
}

int show(char ch)
{
    if(ch >= 'A' && ch <= 'Z')
        return ch - 'A' + 1;
    if(ch >= 'a' && ch <= 'z')
        return ch - 'a' + 1;
    return -1;
}

 

8、

#include <stdio.h>

double power(double x, int y);

int main(void)
{
    double a;
    int b;
    double result;
    printf("please input a pair number, q to quit: \n");
    
    while((scanf("%lf %d", &a, &b)) == 2)
    {
        result = power(a, b);
        printf("the result is: %.2f.\n", result);
        printf("please input another pair number: \n"); 
            
    }
    printf("hope you enjoy the power trip -- bye.\n");
    
    return 0;
} 

double power(double x, int y)
{
    int i;
    double power = 1.0;
    
    if(x == 0)
    {
        if(y != 0)
            power = 0;
        else
            {
                printf("0 power 0 not defined, regard as 1.\n");
                power = 1;
            }
    }
    
    else
    {
        if(y > 0)
        {
            for(i = 1; i <= y; i++)
            {
                power *= x;
            }
        }
        else if(y < 0)
        {
            for(i = 1; i <= -y; i++)
            {
                power *= x;
            }
            power = 1/power;
        }
        else
        {
            power = 1;
        }
    }
    return power;
}

 

9、

#include <stdio.h>

double power(double n, int p);

int main(void)
{
    double x, xpow;
    int exp;
    
    printf("Enter a number and the integer power:(q to quit).\n");
    
    while(scanf("%lf %d", &x, &exp) == 2)
    {
        xpow = power(x, exp);
        printf("%.3g to the power %d is %.5g\n", x, exp, xpow);
        printf("Enter the next pair of number or q to quit.\n");
    }
    printf("Hope you enjoy this power trip -- bye!\n");
    
    return 0;
}

double power(double n, int p)
{
    double pow = 1;
    int i;
    
    if(n == 0 && p == 0)
    {
        printf("The %g to the power %c is not define, return 1!\n", n, p);
        return 1;
    }
    if(n == 0)
        return 0;
    if(p == 0)
        return 1;
    if(p > 0)
    {
        return n * power(n, p-1);
    }
    else
    {
        return power(n, p+1)/n;
    }
}

 

 

 

 

10、

#include <stdio.h>

unsigned long get_long(void);

int get_unit(void);

void to_binary(unsigned long x, int y);

int main(void)
{
    unsigned long a;
    int b;
    char ch;
    
    do
    {    
        
        a = get_long();
        
        
        b = get_unit();
        
        to_binary(a, b);
        putchar('\n');
        
        while(getchar() != '\n')
            continue;
        
        printf("choose to continue or exit q to quit other to continue: ");
        while((ch = getchar()) == 'q')
            break;
        
    }
    while(ch != 'q');
    
    
    return 0;
} 

void to_binary(unsigned long x, int y)
{
    int temp;
    
    temp = x % y;
    if(x >= y)
        to_binary(x/y, y);
    printf("%d",temp);
}

unsigned long get_long(void)
{
    unsigned temp;
    
    printf("please input an unsigned long: ");
    
    while(scanf("%lu", &temp) != 1)
    {
        scanf("%*s");
        printf("please input an unsigned long num: ");
    }
    
    return temp;
}

int get_unit(void)
{
    int temp;
    int status;
    
    printf("please input an integer: ");
    
    while((status = scanf("%d", &temp)) != 1 || (temp < 2 || temp > 10))
    {
        if(status != 1)
        {
            scanf("%*s");
            printf("please input num: ");
        }    
        else
        {
            printf("please input 2-10 number: ");
        }
    }
    return temp;
}

 

#include <stdio.h>

void to_base_n(unsigned long n, unsigned short int t);

int main(void)
{
    unsigned long number;
    unsigned short int target;
    
    printf("please enter the integer and N for natation(q to quit): ");
    
    while(scanf("%lu %hu", &number, &target) == 2)
    {
        if(target < 2 || target > 10)
        {
            printf("please input N between 2 and 10!\n");
            printf("Enter the integer and N for notation(q to quit): ");
            continue;
        }
        printf("conber %lu to %hu notation is: ", number, target);
        to_base_n(number, target);
        putchar('\n');
        printf("Enter the integer and N for notation(q to quit): ");
    }
    return 0;
}

void to_base_n(unsigned long n, unsigned short t)
{
    int r;
    r = n % t;
    if(n >= t)
        to_base_n(n/t, t);
    printf("%d", r);
}

 

11、

#include <stdio.h>

void fibonacci(int x);

int main(void)
{
    int end;
    
    printf("please input the end: ");
    scanf("%d", &end);
    
    fibonacci(end);
        
} 


void fibonacci(int x)
{
    int i;
    int temp1 = 1;
    int temp2 = 1;
    int temp3 = 0;
    
    printf("%d ", 1);
    printf("%d ", 1);
    for(i = 1; 1 ;i++)
    {
        temp3 = temp1 + temp2;
        if(temp3 > x)
            break;
        printf("%d ", temp3);
        temp1 = temp2;
        temp2 = temp3; 
        
    }
}

 

 

#include <stdio.h>

void fibonacci(int x);

int main(void)
{
    int end;
    int status;
    printf("please input the end (q to quit): ");
    
    while(scanf("%d", &end) == 1)
    {
        fibonacci(end);
        printf("please input the end (q to quit): ");
    }
    
    return 0;    
} 

void fibonacci(int x)
{
    int i;
    int temp1 = 1;
    int temp2 = 1;
    int temp3 = 0;
    
    printf("%d ", 1);
    printf("%d ", 1);
    for(i = 1; 1 ;i++)
    {
        temp3 = temp1 + temp2;
        if(temp3 > x)
            break;
        printf("%d ", temp3);
        temp1 = temp2;
        temp2 = temp3; 
        
    }
    putchar('\n');
}

 

#include <stdio.h>

void fibonacci(int x);

int main(void)
{
    int n;
    
    printf("please input an unsigned short int(q to quit): ");
    while(scanf("%uh",&n) == 1)
    {
        if(n < 1)
        {
            printf("please input positive num: ");
            continue;
        }
        fibonacci(n);
        printf("please input an unsigned short int(q to quit): ");
    }    
  return 0; }
void fibonacci(int x) { int f1 = 1, f2 = 1; int i, temp; for(i = 1; i <= x; i++) { printf("%d ", f1); temp = f1 + f2; f1 = f2; f2 = temp; } putchar('\n'); }

 

版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 dio@foxmail.com 举报,一经查实,本站将立刻删除。

相关推荐


一.C语言中的static关键字 在C语言中,static可以用来修饰局部变量,全局变量以及函数。在不同的情况下static的作用不尽相同。 (1)修饰局部变量 一般情况下,对于局部变量是存放在栈区的,并且局部变量的生命周期在该语句块执行结束时便结束了。但是如果用static进行修饰的话,该变量便存
浅谈C/C++中的指针和数组(二) 前面已经讨论了指针和数组的一些区别,然而在某些情况下,指针和数组是等同的,下面讨论一下什么时候指针和数组是相同的。C语言标准对此作了说明:规则1:表达式中的数组名被编译器当做一个指向该数组第一个元素的指针; 注:下面几种情况例外 1)数组名作为sizeof的操作数
浅谈C/C++中的指针和数组(一)指针是C/C++的精华,而指针和数组又是一对欢喜冤家,很多时候我们并不能很好的区分指针和数组,对于刚毕业的计算机系的本科生很少有人能够熟练掌握指针以及数组的用法和区别。造成这种原因可能跟现在大学教学以及现在市面上流行的很多C或者C++教程有关,这些教程虽然通俗易懂,
从两个例子分析C语言的声明 在读《C专家编程》一书的第三章时,书中谈到C语言的声明问题,《C专家编程》这本书只有两百多页,却花了一章的内容去阐述这个问题,足以看出这个问题的重要性,要想透彻理解C语言的声明问题仅仅看书是远远不够的,需要平时多实践并大量阅读别人写的代码。下面借鉴《C专家编程》书中的两个
C语言文件操作解析(一)在讨论C语言文件操作之前,先了解一下与文件相关的东西。一.文本文件和二进制文件 文本文件的定义:由若干行字符构成的计算机文件,存在于计算机系统中。文本文件只能存储文件中的有效字符信息,不能存储图像、声音等信息。狭义上的二进制文件则指除开文本文件之外的文件,如图片、DOC文档。
C语言文件操作解析(三) 在前面已经讨论了文件打开操作,下面说一下文件的读写操作。文件的读写操作主要有4种,字符读写、字符串读写、块读写以及格式化读写。一.字符读写 字符读写主要使用两个函数fputc和fgetc,两个函数的原型是: int fputc(int ch,FILE *fp);若写入成功则
浅谈C语言中的位段 位段(bit-field)是以位为单位来定义结构体(或联合体)中的成员变量所占的空间。含有位段的结构体(联合体)称为位段结构。采用位段结构既能够节省空间,又方便于操作。 位段的定义格式为: type [var]:digits 其中type只能为int,unsigned int,s
C语言文件操作解析(五)之EOF解析 在C语言中,有个符号大家都应该很熟悉,那就是EOF(End of File),即文件结束符。但是很多时候对这个理解并不是很清楚,导致在写代码的时候经常出错,特别是在判断文件是否到达文件末尾时,常常出错。1.EOF是什么? 在VC中查看EOF的定义可知: #def
关于VC+ʶ.0中getline函数的一个bug 最近在调试程序时,发现getline函数在VC+ʶ.0和其他编译器上运行结果不一样,比如有如下这段程序:#include &lt;iostream&gt;#include &lt;string&gt;using namespace std;int
C/C++浮点数在内存中的存储方式 任何数据在内存中都是以二进制的形式存储的,例如一个short型数据1156,其二进制表示形式为00000100 10000100。则在Intel CPU架构的系统中,存放方式为 10000100(低地址单元) 00000100(高地址单元),因为Intel CPU
浅析C/C++中的switch/case陷阱 先看下面一段代码: 文件main.cpp#includeusing namespace std;int main(int argc, char *argv[]){ int a =0; switch(a) { case ...
浅谈C/C++中的typedef和#define 在C/C++中,我们平时写程序可能经常会用到typedef关键字和#define宏定义命令,在某些情况下使用它们会达到相同的效果,但是它们是有实质性的区别,一个是C/C++的关键字,一个是C/C++的宏定义命令,typedef用来为一个已有的数据类型
看下面一道面试题:#include&lt;stdio.h&gt;#include&lt;stdlib.h&gt;int main(void) { int a[5]={1,2,3,4,5}; int *ptr=(int *)(&amp;aʱ); printf(&quot;%d,%d&quot;,*(
联合体union 当多个数据需要共享内存或者多个数据每次只取其一时,可以利用联合体(union)。在C Programming Language 一书中对于联合体是这么描述的: 1)联合体是一个结构; 2)它的所有成员相对于基地址的偏移量都为0; 3)此结构空间要大到足够容纳最&quot;宽&quo
从一个程序的Bug解析C语言的类型转换 先看下面一段程序,这段程序摘自《C 专家编程》:#include&lt;stdio.h&gt;int array[]={23,34,12,17,204,99,16};#define TOTAL_ELEMENTS (sizeof(array)/sizeof(ar
大端和小端 嵌入式开发者应该对大端和小端很熟悉。在内存单元中数据是以字节为存储单位的,对于多字节数据,在小端模式中,低字节数据存放在低地址单元,而在大端模式中,低字节数据存放在高地址单元。比如一个定义一个short型的变量a,赋值为1,由于short型数据占2字节。在小端模式中,其存放方式为0X40
位运算和sizeof运算符 C语言中提供了一些运算符可以直接操作整数的位,称为位运算,因此位运算中的操作数都必须是整型的。位运算的效率是比较高的,而且位运算运用好的话会达到意想不到的效果。位运算主要有6种:与(&amp;),或(|),取反(~),异或(^),左移(&gt;)。1.位运算中的类型转换位
C语言文件操作解析(四)在文件操作中除了打开操作以及读写操作,还有几种比较常见的操作。下面介绍一下这些操作中涉及到的函数。一.移动位置指针的函数 rewind函数和fseek函数,这两个函数的原型是:void rewind(FILE *fp); 将位置指针移动到文件首 int fseek(FILE
结构体字节对齐 在用sizeof运算符求算某结构体所占空间时,并不是简单地将结构体中所有元素各自占的空间相加,这里涉及到内存字节对齐的问题。从理论上讲,对于任何变量的访问都可以从任何地址开始访问,但是事实上不是如此,实际上访问特定类型的变量只能在特定的地址访问,这就需要各个变量在空间上按一定的规则排
C语言文件操作解析(二)C语言中对文件进行操作必须首先打开文件,打开文件主要涉及到fopen函数。fopen函数的原型为 FILE* fopen(const char *path,const char *mode) 其中path为文件路径,mode为打开方式 1)对于文件路径,只需注意若未明确给出绝