此笔记由个人整理
尚观C语言
#include "stdio.h"
#include "stdlib.h"
#define M 3
int main()
{
int arr[M];//定义,初始化
printf("%d\n",sizeof(arr));
printf("arr = %p",arr);
for(i = 0;i < M;i++)
{
scanf("%d",&arr[i]);
}
for(i = 0;i < M;i++)
{
printf("%p --> %d\n",&arr[i],arr[i]);//元素引用
}
exit(0);
}
int arr[3];
int arr[3] = {1,2,3};
int arr[3] = {1};
static int arr[3];
#include "stdio.h"
#include "stdlib.h"
void fibonacci(void)
{
int fib[10] = {1,1};
int i,j,tmp;
for(i = 2;i < sizeof(fib)/sizeof(fib[0]);i++)
fib[i] = fib[i-1] + fib[i-2];
for(i = 0;i < sizeof(fib)/sizeof(fib[0]);i++)
printf("%d\t",fib[i]);
printf("\n");
i = 0;
j = sizeof(fib)/sizeof(fib[0])-1;
while(i<j)
{
tmp = fib[i];
fib[i] = fib[j];
fib[j] = tmp;
i++;
j--;
}
for(i = 0;i < sizeof(fib)/sizeof(fib[0]);i++)
printf("%d\t",fib[i]);
printf("\n");
return;
}
int main()
{
fibonacci();
}
//冒泡排序
//让第一个数与后一个数相比,如果相比结果为小则不动,让后一个数与再后一个数相比;如果相比结果为大则后移,让后移的数与再后一个数相比。目的是将其中最大的数后移到最后一位。
#include "stdio.h"
#include "stdlib.h"
#define N 10
static void sort1()
{
int a[N] = {12,8,45,30,98,67,2,68,11};
int tmp;
int i,j;
for(i = 0;i < N ;i++)
printf("%d ",a[i]);
printf("\n");
for(i = 0;i < (N-1);i++)
{
for(j = 0;j < (N-1-i);j++)
{
if(a[j] > a[j+1])
{
tmp = a[i];
a[i] = a[j+1];
a[j+1] =tmp;
}
}
}
for(i = 0;i < N ;i++)
printf("%d ",a[i]);
printf("\n");
}
int main()
{
sort1();
exit(0);
}
//选择法排序
//找到其中最小的值,将其移位到最前面,被替换的值放到最小值之前所在的位置,剩下的内容不变;在找出了第一位的最小值,将其放到第二位,被替换的值放到最小值之前所在的位置,剩下的不变;依次移位,最终得到正确的排序。
#include "stdio.h"
#include "stdlib.h"
#define N 10
static void sort2()
{
int a[N] = {23,45,90,76,13,55,76,45,3,8};
int i,j,k,tmp;
for(i = 0;i < sizeof(a)/sizeof(a[i]);i++)
printf("%d",a[i]);
printf("\n");
for(i = 0;i < (N-1);i++)
{
k = i;
for(j = i+1;j < N;j++)
{
if(a[j] < a[k])
k=j;
}
if(i != k)
{
tmp = a[i];
a[i] = a[k];
a[k] =tmp;
}
}
for(i = 0;i < sizeof(a)/sizeof(a[i]);i++)
printf("%d",a[i]);
printf("\n");
}
int main()
{
sort2();
exit(0);
}
//gjl:求逆序数,要用户自己输入数组
#include "stdio.h"
#include "stdlib.h"
#define N 10
static void nixu()
{
int arr[N];
int i,j,count=0;
printf("Please enter 10 number:\n");
for(i = 0;i < 10;i++)
scanf("%d",&arr[i]);
for(i = 0;i < (N-1);i++)
{
for(j = i;j < (N-1);j++)
{
if(arr[i] > arr[j])
{
count++;
printf("%d >%d\n",arr[i],arr[j]);
}
}
}
printf("逆序数=%d",count);
}
int main()
{
nixu();
exit(0);
}
//gjl:求逆序数,要用户自己输入数组
#include "stdio.h"
#include "stdlib.h"
#define N 10
static void base_convert(void)
{
int num,base;
int n[128];
int i = 0;
printf("please enter the coverted num:");
scanf("%d",&num);
printf("please enter the base:");
scanf("%d",&base);
do
{
n[i] = num % base;
num = num / base;
i++;
}while(num != 0);
for(i --;i >= 0;i--)
{
if(n[i]>=10)
printf("%c",n[i]-10+'A');
else
printf("%d",n[i]);
}
printf("\n");
}
int main()
{
base_convert();
exit(0);
}
//gjl:求逆序数,要用户自己输入数组
#include "stdio.h"
#include "stdlib.h"
#define N 1001
static void primer(void)
{
char primer[N] = {0};
int i,j;
for(i = 2;i < 1001;i++)
{
if(primer[i] == 0)
{
for(j = i*2;j < 1001;j += i)
primer[j] = -1;
}
}
for(i = 2;i < 1001; i++)
if(primer[i] == 0)
printf("%d ",i);
printf("\n");
}
int main()
{
primer();
exit(0);
}
#include "stdio.h"
#include "stdlib.h"
#define M 2
#define N 3
int main()
{
int a[M][N];
int i,j;
#if 0
printf("please enter 6 num:\n");
for(i = 0;i < M;i++)
for(j = 0;j < N;j++)
scanf("%d",&a[i][j]);
#endif
for(i = 0;i < M;i++)
{
for(j = 0;j < N;j++)
{
printf("%d ",a[i][j]);
}
printf("\n");
}
exit(0);
}
int a[2][3];
int a[2][3] = {{1,3,2},{4,5,6}};
int a[2][3] = {{1,2},{3}};
int a[M];
//a+1为a[1]的存储位置-->a[0]的地址基础上增加一个int存储字节
//移动一个元素
int a[M][N];
//a+1为a[1][0]的存储位置-->a[0][0]的地址基础上增加N个int存储字节
//移动一行元素
#include "stdio.h"
#include "stdlib.h"
#define M 2
#define N 3
static void change(void)
{
int a[M][N] = {1,2,3,4,5,6},b[N][M];
int i,j;
for(i = 0;i < M;i++)
{
for(j = 0;j < N;j++)
{
printf("%d ",a[i][j]);
b[j][i] = a[i][j];
}
printf("\n");
}
printf("\n");
for(i = 0;i < N;i++)
{
for(j = 0;j < M;j++)
printf("%d ",b[i][j]);
printf("\n");
}
}
int main()
{
change();
exit(0);
}
#include "stdio.h"
#include "stdlib.h"
#define M 2
#define N 3
static void maxfunc(void)
{
int a[M][N] = {1,2,3,4,5,6};
int i,j;
int max = a[0][0],row = 0,colum = 0;
for(i = 0;i < M;i++)
{
for(j = 0;j < N;j++)
{
if(max < a[i][j])
{
max = a[i][j];
row = i;
colum = j;
}
}
}
printf("max=a[%d][%d]=%d",row,colum,max);
}
int main()
{
maxfunc();
exit(0);
}
#include "stdio.h"
#include "stdlib.h"
static void sumfunc(void)
{
int a[5][4] = {{1,2,3},{4,5,6},{7,8,9},{10,11,12}};
int i,j;
for(i = 0;i < 4;i++)
{
for(j = 0;j < 3;j++)
{
a[4][3] += a[i][j];
a[4][j] += a[i][j];
a[i][j] += a[i][j];
}
}
for(i = 0;i < 5;i++)
{
for(j = 0;j < 4;j++)
printf("%4d ",a[i][j]);
printf("\n");
}
}
int main()
{
sumfunc();
exit(0);
}
#include "stdio.h"
#include "stdlib.h"
#define M 2
#define N 3
#define K 2
static void func(void)
{
int a[M][N] = {1,2,3,4,5,6};
int b[N][K] = {1,3,4,1,2,7};
int c[M][K];
int i,j,k;
for(i = 0;i < M;i++)
{
for(j = 0;j < K;j++)
{
for(k = 0;k < N;k++)
c[i][j] = a[i][k] * b[k][j];
}
}
for(i = 0;i < M;i++)
{
for(j = 0;j < K;j++)
printf("%4d",c[i][j]);
printf("\n");
}
}
int main()
{
func();
exit(0);
}
#include "stdio.h"
#include "stdlib.h"
#define N 3
int main()
{
int i;
char str[N] = {'a','b','c'};
for(i = 0;i < N;i++)
printf("%c ",str[i]);
printf("\n");
}
char str[3] = {'a','b','c'};
char str[3] = "ab";
#include "stdio.h"
#include "stdlib.h"
#define N 32
int main()
{
char str[N],str1[N],str2[N];
scanf("%s%s%s",str,str1,str2);
printf("%s\n%s\n%s\n",str,str1,str2);
}
#include "stdio.h"
#include "stdlib.h"
#include "string.h"
int main()
{
char str[] = "hello";
printf("strlen(str) = %d\n",strlen(str));
printf("sizeof(str) = %d\n",sizeof(str));
}
#include "stdio.h"
#include "stdlib.h"
#include "string.h"
#define STRSIZE 32
int main()
{
char str1[32] = "hello";
char str2[STRSIZE];
strcpy(str1,"abcde");
strncpy(str2,"fghigklm",STRSIZE);
printf("strcpy-->%s\n",str1);
printf("strncpy-->%s\n",str2);
}
#include "stdio.h"
#include "stdlib.h"
#include "string.h"
#define STRSIZE 32
int main()
{
char str1[STRSIZE] = "hello";
char str2[STRSIZE] = "hello";
strcat(str1," ");
strcat(str1,"c");
strncat(str2," ",STRSIZE);
strncat(str2,"world",STRSIZE);
puts(str1);
puts(str2);
}
#include "stdio.h"
#include "stdlib.h"
#include "string.h"
#define STRSIZE 32
int main()
{
char str1[STRSIZE] = "hello";
char str2[STRSIZE] = "helloa";
printf("strcmp-->%d\n",strcmp(str1,str2));
printf("strncmp-->%d\n",strncmp(str1,str2,5));
}
#include "stdio.h"
#include "stdlib.h"
int main()
{
char str[128];
int count = 0;
int flag = 0;
int i;
gets(str);
for(i = 0;str[i] != '\0';i++)
{
if(str[i] == ' ')
flag = 0;
else//str[i] is a char
if(flag == 0)
{
count++;
flag = 1;
}
}
printf("count = %d\n",count);
exit(0);
}
//暂未支持tab键