C语言程序设计-笔记9-函数与程序结构

C语言程序设计-笔记9-函数与程序结构

例10-1    有序表的增删查操作。首先输入一个无重复元素的、从小到大排列的有序表,并在屏幕上显示以下菜单(编号和选项),用户可以反复对该有序表进行插入、删除和查找操作,也可以选择结束。当用户输入编号1-3和相关参数时,将分别对该有序表进行插入、删除和查找操作,输入其他编号,则结束操作。

      [1]  Insert

      [2]  Delete

      [3]  Query

      [Other option]  End

#include<stdio.h>

#define MAXN 100

int Count=0;

void select(int a[],int option,int value);

void input_array(int a[]);

void print_array(int a[]);

void insert(int a[],int value);

void remov(int a[],int value);

void query(int a[],int value);

 

int main(void)

{

      int option,value,a[MAXN];

     

      input_array(a);

      printf("[1] Insert\n");

      printf("[2] Delete\n");

      printf("[3] Query\n");

      printf("[Other option] End\n");

      while(1)

      {

           printf("Input option:");

           scanf("%d",&option);

           if(option<1 || option>3)

           {

                 break;

           }

           printf("Input an element:");

           scanf("%d",&value);

           select(a,option,value);

           printf("\n");

      }

      printf("Thanks.");

      return 0;

}

 

void print_array(int a[])

{

      int i;

      printf("The ordered array a is:");

      for(i=0;i<Count;i++)

      {

           if(i==Count-1)

           {

                 printf("%d",a[i]);

           }

           else

           {

                 printf("%d ",a[i]);

           }

      }

}

 

void delet(int a[ ],int value)

{

      int i,index=-1;

      for(i=0;i<Count;i++)

      {

           if(value==a[i])

           {

                 index=i;

                 break;

           }

      }

      if(index==-1)

      {

           printf("Failed to find the data,deletion failed.");

      }

      else

      {

            for(i=index;i<Count-1;i++)

           {

                 a[i]=a[i+1];

           }

      }

      Count--;

      print_array(a);

}

 

void select(int a[],int option,int value)

{

      switch(option)

      {

           case 1:

                 insert(a,value);

                 break;

           case 2:

                 delet(a,value);

                 break;

           case 3:

                 query(a,value);

                 break;

      }

}

 

void input_array(int a[])

{

      int i;

      printf("Input the number of array elements:");

      scanf("%d",&Count);

      printf("Input an ordered array element:");

      for(i=0;i<Count;i++)

      {

           scanf("%d",&a[i]);

      }

}

 

 

 

void insert(int a[],int value)

{

      int i,j;

      for(i=0;i<Count;i++)

      {

           if(value<a[i])

           {

                 break;

           }

      }

      for(j=Count-1;j>=i;j--)

      {

           a[j+1]=a[j];

      }

      a[i]=value;

      Count++;

      print_array(a);

}

 

 

 

void query(int a[],int value)

{

      int mid,left=0,right=Count-1;

      while(left<=right)

      {

           mid=(left+right)/2;

           if(value==a[mid])

           {

                 printf("The index is:%d",mid);

                 return;

           }

           else if(value<a[mid])

           {

                 right=mid-1;

           }

           else

           {

                 left=mid+1;

           }

      }

      printf("This element does not exist.");

}

 

例10-2  用递归函数实现求n!。

·#include<stdio.h>

double fact(int n);

int main(void)

{

      int n;

     

      scanf("%d",&n);

      printf("%f",fact(n));

     

      return 0;

}

 

double fact(int n)

{

      double result;

     

      if(n==1 || n==0)

      {

           result=1;

      }

      else

      {

           result=n*fact(n-1);

      }

      return result;

}

 

例10-3  定义函数gcd(m,n),用递归法求m和n的最大公约数。

#include<stdio.h>

int gcd(int m,int n)

{

      if(m%n==0)

      {

           return n;

      }

      else

      {

           return gcd(n,m%n);

      }

}

int main(void)

{

      int a,b,gys;

      printf("Enter a,b:");

      scanf("%d%d",&a,&b);

      gys=gcd(a,b);

      printf("%d\n",gys);

     

      return 0;

}

 

例10-4  编写递归函数reverse(int n)实现将整数n逆序输出。

#include<stdio.h>

void reverse(int num)

{

      if(num<=9)

           printf("%d",num);

      else

           {

           printf("%d",num%10);

           reverse(num/10);  

           }   

}

 

int main(void)

{

      int a;

      printf("Enter a:");

      scanf("%d",&a);

      reverse(a);

     

      return 0;

}

 

 

例10-5  汉诺(Hanoi)塔问题。要求用程序模拟盘子搬动过程,并输出搬动步骤。

#include<stdio.h>

void hanoi(int n,char a,char b,char c);

int main(void)

{

      int n;

      printf("input the number of disk:");

      scanf("%d",&n);

      printf("the steps for %d disk are:\n",n);

      hanoi(n,'a','b','c');

     

      return 0;

}

 

void hanoi(int n,char a,char b,char c)

{

      if(n==1)

      {

           printf("%c --> %c\n",a,b);

      }

      else

      {

           hanoi(n-1,a,c,b);

           printf("%c --> %c\n",a,b);

           hanoi(n-1,c,b,a);

      }

}

 

例10-6  分治法求解金块问题:老板有一袋金块(共n块,2),两名最优秀的雇员每人可以得到其中一块,排名第一的得到最重的金块,排名第二的则得到袋子中最轻的金块。输入n及n个整数,用分治法求出最重金块和最轻金块。

/*分治法求a[m]-a[n]中最大值的递归函数*/

int max(int a[],int m,int n)

{

      int k,u,v;

     

      if(m==n)         //数组a中只有1个元素,返回最大值a[m]

      {

           return a[m];

       }

       k=(m+n)/2;         //计算中间元素的下标

       u=max(a,m,k);     //递归调用函数max(),在a[m]-a[k]中找出最大值赋给u

       v=max(a,k+1,n);  //同上

       

       return(u>v)?u:v;

}

 

int min(int b[],int m,int n)

{

      int k,u,v;

     

      if(m==n)

      {

           return b[m];

      }

      k=(m+n)/2;

      u=min(b,m,k);

      v=min(b,k+1,n);

     

      return(u<v)?u:v;

}

 

int main(void)

{

      int n;

      printf("Enter n:");

      scanf("%d",&n);

      int a[n];

      int i;

      for(i=0;i<n;i++)

      {

           scanf("%d",&a[i]);

      }

      int best,better;

      best=max(a,0,n-1);

      better=min(a,0,n-1);

      printf("%d %d\n",best,better);

     

      return 0;

}

 

例10-7  欧美国家长度使用英制单位,如英里、英尺、英寸等,其中1英里=1609米,1英尺=30.48厘米,1英寸=2.54厘米。请编写程序将输入的英里转换成米,英尺和英寸转换成厘米。

#include<stdio.h>

#define Mile_to_meter 1609

#define Foot_to_centimeter 30.48

#define Inch_to_centimeter 2.54

int main(void)

{

      float foot,inch,mile;

     

      printf("Input mile,foot and inch:");

      scanf("%f%f%f",&mile,&foot,&inch);

      printf("%f miles=%f meters\n",mile,mile*Mile_to_meter);

      printf("%f feet=%f centimeters\n",foot,foot*Foot_to_centimeter);

      printf("%f inches=%f centimeters\n",inch,inch*Inch_to_centimeter);

     

      return 0;

}

 

例10-8  简单的带参数的宏定义。

#include<stdio.h>

#define MAX(a,b) a>b?a:b

#define SQR(x) x*x

int main(void)

{

      int x,y;

      scanf("%d%d",&x,&y);

      x=MAX(x,y);

      y=SQR(x);

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

     

      return 0;

}

 

例10-9  将例10-7中长度转换的宏定义成头文件length.h,并写出主函数文件。

#include<stdio.h>

#define Mile_to_meter 1609

#define Foot_to_centimeter 30.48

#define Inch_to_centimeter 2.54

int main(void)

{

      float foot,inch,mile;

     

      printf("Input mile,foot and inch:");

      scanf("%f%f%f",&mile,&foot,&inch);

      printf("%f miles=%f meters\n",mile,mile*Mile_to_meter);

      printf("%f feet=%f centimeters\n",foot,foot*Foot_to_centimeter);

      printf("%f inches=%f centimeters\n",inch,inch*Inch_to_centimeter);

     

      return 0;

}

length.h头文件如下:

#define Mile_to_meter 1609

#define Foot_to_centimeter 30.48

#define Inch_to_centimeter 2.54

 

例10-10  请在例9-1、例9-2和例9-3的基础上,分模块设计一个学生信息库系统。该系统包含学生基本信息的建立和输出、计算学生平均成绩、按照学生的平均成绩排序以及查询、修改学生的成绩等功能。

//主函数程序文件student_system.c

#include "student.h"

#include "input_output.c"   //用文件包含连接各程序文件模块

#include "computing.c"

#include "update.c"

#include "search.c"

 

int Count=0;         //全局变量,记录当前学生总数

int main(void)

{

      struct student students[MaxSize];   //定义学生信息结构数组

      new_student(students);                      //输入学生信息结构数组

      output_student(students);             //显示输入的学生信息结构数组

      average(students);                        //计算每一个学生的平均成绩

      sort(students);                             //按学生的平均成绩排序

      output_student(students);             //显示排序后的结构数组

      modify(students);                         //修改指定输入的学生信息

      output_student(students);             //显示修改后的结构数组

     

      return 0;

}

 

#include <stdio.h>

#include <string.h>

#define MaxSize 50

struct student{

      int num;

      char name[10];

      int computer,english,math;

      double average;

};

 

//输入输出程序文件input_output.c

extern int Count;         //外部变量声明

void new_student(struct student students[])          //新建学生信息的函数

{

      int i,n;

      printf("Input n:");

      scanf("%d",&n);

      printf("Input the student's number,name and course scores\n");

      for(i=1;i<=n;i++)

      {

           printf("No.%d:",i);

           scanf("%d%s%d%d%d",&students[i].num,students[i].name,&students[i].math,\

           &students[i].english,&students[i].computer);

           Count++;

      }

     

}

void output_student(struct student students[]) //输出学生信息的函数

{

      int i;

      printf("output the student's number,name and course scores\n");

      for(i=1;i<=Count;i++)

      {

          

           printf("%d\t %s\t %d\t %d\t %d\n",students[i].num,students[i].name,students[i].math,

           students[i].english,students[i].computer);

      }

}

 

//计算程序文件computing.c

extern int Count;

void average(struct student students[])

{

      int i;

      for(i=0;i<Count;i++)

      {

           students[i].average=(students[i].math+students[i].english+students[i].computer)/3.0;

           printf("%.2f\n",students[i].average);

      }

}

 

//修改程序文件update.c

extern int Count;

void modify(struct student *p)            //修改学生成绩的函数

{

      int i,num,course,score;

      printf("Input the number of the students to be updated:");

      scanf("%d",&num);

      printf("Choice the course:1. math 2. english 3. computer:");

      scanf("%d",&course);

      printf("Input the new score:");

      scanf("%d",&score);

     

      for(i=0;i<Count;i++,p++)

      {

           if(p->num==num)

           {

                 break;

           }

      }

      if(i<Count)

      {

           switch(course)

           {

                 case 1:p->math=score;break;

                 case 2:p->english=score;break;

                 case 3:p->computer=score;break;

           }

      }

     

}

 

void sort(struct student students[])//平均成绩排序的函数

{

      int i,j,index;

      struct student temp;

     

      for(i=0;i<Count-1;++i)

      {

           index=i;

           for(j=i+1;j<Count;j++)

           {

                 if(students[j].average>students[index].average)

                 {

                      index=j;

                 }

           }

           temp=students[index];

           students[index]=students[i];

           students[i]=temp;

      }

}

//查询程序文件search.c

extern int Count;

void search_student(struct student students[],int num)

{

      int i,pos;

      for(i=0;i<Count;i++)

      {

           if(students[i].num==num)

           {

                 break;

           }

      }

      if(i<Count)

      {

           pos=i;

           printf("found the %d student\n"num);

      }

      else

      {

           pos=-1;

           printf("not found the student\n");s

      }

     

 

}

 

上一篇:Python解析JSON对象


下一篇:java 集合三