C语言复习day2

来源:互联网 发布:怎样写淘宝客招募 编辑:程序博客网 时间:2024/05/17 19:20

数组部分:

1.数组的初始化:int a[10]={1,2,3,4,5}; 对前五个进行赋初值。

Fibonacci数列:

#include<stdio.h>void main(){int i;int f[20]={1,1};for(i=2;i<20;i++){f[i]=f[i-2]+f[i-1];}for(i=0;i<20;i++){if(i%5==0)printf("\n");printf("%12d",f[i]);}}

输入10个数,求出其中最大的数

#include<stdio.h>void main(){int a[10],max,i;for(i=0;i<10;i++){scanf("%d",&a[i]);}    max=a[0];for(i=1;i<10;i++){if(a[i]>max)max=a[i];}printf("The max is: %d\n",max);}

2.二维数组(按行排列的)

一个学习小组有5个人,每个人有3门课的考试成绩,求全组的分科平均成绩和各科总平均成绩。

#include<stdio.h>void main(){   int i,j;   int s=0,average,v[3],a[5][3];   printf("input scores:");   for(j=0;j<3;j++)   {  for(i=0;i<5;i++)  {scanf("%d",&a[i][j]);s=s+a[i][j];  }v[j]=s/5;      注:对一科所有人的成绩循环完之后要使S=0;以便下个科目的循环。s=0;   }         average=(v[0]+v[1]+v[2])/3;  printf("Math:%d,C:%d,Database:%d",v[0],v[1],v[2]);  printf("total:%d",average);}

3.二维数组行和列元素互换,存在另一个二维数组中。

int a[2][3],b[3][2];for(i=0;i<1;i++)  for(j=0;j<2;j++)  { b[j][i]=a[i][j];  }

4.二维数组的最大值,及所在行号和列号。

#include<stdio.h>void main(){  int i,j,row,col,max;  int a[3][4]={{1,2,3,4},{9,8,7,6},{2,10,-5,2}};  max=a[0][0];  for(i=0;i<=2;i++)for(j=0;j<=3;j++){ if(a[i][j]>max)  max=a[i][j];  col=j;  row=i;}  printf("The max num is:%d,row is:%d,col is:%d.\n",max,row,col);}

5.对具有10个整数的数组进行逆置存放。

#include<stdio.h>void main(){  int i,j,t;  int a[10]={1,2,3,4,5,6,7,8,9,10};  for(i=0,j=9;i<4;i++,j--)  { t=a[i]; a[i]=a[j]; a[j]=t;  }  for(i=0;i<10;i++)     printf("%d ",a[i]);}

字符数组和字符串部分:

1.字符数组:
char a[]={'c',' ','p','r','o','g','r','a','m'};
2.字符串:总是以\0 作为结束符。
<strong>char c[]="c program";</strong>
<strong>也可以通过指针来指向字符串,此时不用定义数组。char *s="program"; s指向字符串的第一个字符。还可以拆开写为 char *s;s="program"; 而数组则不可拆开写。</strong>
3.字符串数组(二维的):
char  c[3][5]={"A","bb","CCCC"};

4.字符串的输入和输出:

(1)输出:

char c[]="China";printf("%s",c);
输入:

char str1[5],str2[5,str3[5];scanf("%s%s%s",str1,str2,str3);输入数据How are you? 则三个单词分别赋给3个字符串。char str[13];scanf("%s",str); 若输入前面的字符,只将空格前的How送到str中。
(2)输入:

gets(str);从终端输入一个字符串到字符数组,直到遇到换行符停止。

puts(str);遇到\0结束输出。


函数部分:

1.一个函数中允许有多个return语句,但函数每次执行时只能有一个return语句被执行。
2.函数的递归调用:
#include<stdio.h>long fac(int n){long f;if(n<0)printf("errror!");else if(n==0)f=1;        elsef=fac(n-1)*n;return f;}void main(){ int n;long y;printf("input number:");scanf("%d",&n);    y=fac(n);printf("the result is %d\n",y);}
猴子吃桃问题:(递归)
#include "stdio.h"int fun(int day);main(){int count;count=fun(1);printf("count=%d\n",count);}int fun(int day){if(day==10)  //用if语句来明确结束递归的条件return 1;elsereturn (fun(day+1)+1)*2;}
或者:(非递归)
#include<stdio.h>void main(){ int x=1;int day=10;while(day>1){x=(x+1)*2;day--;}printf("the result is %d\n",x);}
3.数组元素可以作为函数的参数使用。
4.主函数定义的变量只能在主函数中使用。全局变量的作用域是整个源程序。
5在一个函数体内部用static来说明一个变量时,该变量即为静态局部变量。.静态局部变量在静态存储区占据永久性的存储单元,即使退出函数后,下次再调用该函数时,静态存储变量仍然占据着原来的存储单元,不需要再次创建。而auto局部变量每次函数调用时都会重新创建,函数退出时撤销。
6.全局变量又称为外部变量。用static声明的全局变量只能在本文件中使用。

指针部分:

1.定义指针:int *p; 
2.exit(1)表示发生错误后退出程序,exit(0)表示正常退出程序。
3.指针数组:一个数组的元素值为指针。常用于处理字符串。
char *name[]={"Illegal day","Monday","Tuesday","Wednesday","Thursday","Friday","Saturday","Sunday"};//name[0]即指向字符串"Illegal day",name[1]指向"Monday".
4.选择排序:
void sort(char *name,int n){char *pt;int i,j,k;for(i=0;i<n-1;i++){min=i;for(j=i+1;j<n;j++)           if(strcmp(name[min],name[j])>0)   min=j;if(min!=i)   {   pt=name[min];   name[min]=name[i];   name[i]=pt;   }}}

void sort(int a[5])                //属于哪种排序??????是  <strong>比较交换法排序</strong>{</strong>int i,j,pt;for(i=0;i<4;i++){for(j=i+1;j<5;j++)           if(a[i]>a[j])   {   pt=a[i];   a[i]=a[j];   a[j]=pt;   }}}

结构体部分:

1.用typedef说明一种新类型名。
2.结构体:
struct Student{int num;char sex;int age;float score;}student1,student2;//定义结构体变量
struct Student{int num;char sex;int age;float score;}stu[3];//直接定义结构体数组
3.malloc函数:char *p;
              p=(char*)malloc(10);  malloc函数返回值类型是void*,因此要进行强制类型转换。
calloc函数:char *p;
            p=(char*)calloc(n,size);  开辟n个长度为size的连续空间。
free函数: free(p);释放之前动态分配的内存空间。没有返回值。



0 0
原创粉丝点击