第九章函数

#include

#include

#include

#define MAME " GIGGA"

#define ADDEWSS "101"

#define PLACE "Megapolis CA "

#define WIDTH 40

void starbar (void );

void main()

{

    starbar();

    printf("%s \n ", MAME);

    printf("%s \n ",ADDEWSS);

    printf("%s \n",PLACE);

    starbar();

system("pause");

}

void starbar (void )

{

    int count ;

    for (count = 1 ; count <= WIDTH; count++) {

        putchar('*');


    }

    putchar('\n');

}


I


各种复用


#include

#include

#include

#define MAME " GIGGA"

#define ADDEWSS "101"

#define PLACE "Megapolis CA "

#define WIDTH 40

#define SPACE ' '

//void starbar (void );

void show_n_char (char ch , int num);

void main()

{

    int spaces;

show_n_char('*',WIDTH);

show_n_char(SPACE,12);

printf("%s\n",MAME);

spaces = (WIDTH - strlen (ADDEWSS))/2;

  show_n_char(SPACE,spaces);

printf("%s \n" ,ADDEWSS);

show_n_char (SPACE,(WIDTH - strlen (PLACE))/2);

  printf("%s \n" ,PLACE);

  show_n_char('*' , WIDTH);

system("pause");

}

void starbar (void )

{

    int count ;

    for (count = 1 ; count <= WIDTH; count++) {

        putchar('*');


    }

    putchar('\n');

}

void show_n_char (char ch , int num)

{

int count ;

for (int count = 0; count <= num; count++)

{

putchar(ch);

}

}



找出最小数

#include

#include

#include

int imin(int ,int);

void main()

{

int evi11,evi12;

printf("q quit \n");

while (scanf(" %d %d", &evi11,&evi12) == 2)

{

printf("  the lesser of %d and %d is %d .\n" , evi11,evi12,imin(evi11,evi12));

}

printf("\n");

}

int imin(int n ,int m)

{

int min;

if (n < m)

{

min = n;

}

else

min = m;

return min;

}


\


使用函数  原形


#include

int imin(int ,int);//

void main()

{

// printf("  the lesser of %d and %d is %d .\n" , 3,5,imin(3  ));

printf("  the lesser of %d and %d is %d .\n" , 3 ,5,imin(3.0,5.0));

system("pause");

}

int imin(int n ,int m)

{

int min;

if (n < m)

{

min = n;

}

else

min = m;

return min;

}


递归

#include

#include

#include

void up_and_down(int n )

{

printf("lever %d n liocation %p \n" , n ,&n );

if(n < 4)

{

up_and_down(n+1);

}

printf("lever %d n liocation %p \n" , n ,&n );

}

void main()

{

up_and_down(1 );

system("pause");

}





使用循环和递归 计算阶乘


#define _CRT_SECURE_NO_WARNINGS

#include

#include

#include

long face (int );

long rfacet (int );

void  main()

{

int num ;

printf(" this program clculates \n");

printf(" enter a valiue in the rang q ");

while (scanf ("%d",&num) == 1)

{

if (num < 0 )

{

printf(" no  ne");

}

else if (num > 12)

{

printf(" keep \n");

}

else

{

printf("loop %d facetorial = %ld \n" , num,face(num));

printf("recursion%d factorial = %ld \n ",num ,rfacet(num));

}

}

system("pause");

}

long face(int n )

{

long ans ;

for ( ans = 0; n>1; n --)

{

ans *= n;

}

return ans;

}

long rfacet(int n )

{

long ans ;

if(n > 0 )

ans = n * rfacet(n-1);

else

ans = 1;

return ans;

}



递归实现二进制运算

#define _CRT_SECURE_NO_WARNINGS

#include

#include

#include

void to_binary(unsigned long);

int main()

{

unsigned long number;

printf ("enter an integer q\n ");

while (scanf("%ul",&number) == 1 )

{

to_binary(number);

putchar('\n');

}

system ( "pause");

}

void to_binary(unsigned long n)

{

int r ;

r = n %2;

if ( n >=2)

{

to_binary(n/2);

}

putchar('0' + r);

}



#define _CRT_SECURE_NO_WARNINGS

#include

#include "stdafx.h"

int menu(void)

{

int code , status;

printf(" \n %s %s \n",STARS,STARS);

printf("1,fair arms ,2 hotel ,3 ,char ,4 the ,5 quit\n");

printf(" \n %s %s \n",STARS,STARS);

while ((status = scanf("%d", &code)) != 1 || (code < 1 || code> 5) );

{

if (status != 1 )

{

scanf("%*s");

}

printf(" to 5 ");

}

return code;

}

int  getnights (void )

{

int nights;

while (scanf("%d",&nights ) != 1)

{

scanf("%*s");

}

return nights;

}

void showprice ( double rate ,int nights)

{

int n ;

double total = 0.0;

double factor = 1.0;

for (  n = 1; n <= nights; n++, factor *= DISCOUNT)

{

total += rate * factor;

}

printf(" tje tpta; cpst woll be %0.2f\n",total);

}


#include < stdio.h>

#include "stdafx.h"

int main()

{

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;

printf("oooops\n");

break;

}

nights = getnights();

showprice (hotel_rate,nights);

}

printf("hello \n");

}

#include

#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

#include

void mikado (int );

void main()

{

int pooh = 2 ,bah = 5;

printf(" In mikado () , pooh = %d and &pooh = %p \n", pooh,&pooh);

printf(" In mikado () , bah = %d and &pooh = %p \n", bah,&bah);

mikado(pooh);

printf("hello");

system("pause");

}

void mikado (int bah)

{

int pooh = 10 ;

printf(" In mikado () , pooh = %d and &pooh = %p \n", pooh,&pooh);

printf(" In mikado () , bah = %d and &pooh = %p \n", bah,&bah);

}



指针交换两个数值

#include

#include

void interchang  (int *,int *);

void main()

{

int x = 5 , y = 10 ;

printf(" x = %d y = %d ", x,y);

interchang (&x,&y);

printf("\n x = %d y = %d ", x,y);


system("pause");

}

void interchang( int *u ,int *v)

{

int temp ;

temp = *u ;

*u = *v ;

*v = temp;

//printf("\n u = %d v = %d ", u,v);

}

通过

通过指针变量 获取有关zippo的值


#include

#include

#define SIZE 5

void main()

int zippo [4][2] = {{2,4},{6,8},{1,3},{5,7} };

int (*pz)[2];

pz = zippo;

printf("pz = %p  pz + 1 = %p  \n",pz,pz+1 );

printf("pz[0] = %p pz[0]+1 = %p,  \n",pz[0] ,pz[0]+1);

printf("*pz  = %p *pz +1 = %p,  \n",*pz  ,*pz +1);

printf("pz[0][0]  = %d  \n",pz[0][0] );

printf("*pz [0]  = %d  \n",*pz[0] );

  printf("pz[2][1]  = %d  \n",pz[2][1] );

  printf(" pz[2][1]  = %d  \n",  *(*(pz+2)+1) );

system("pause");


你可能感兴趣的:(第九章函数)