c语言table和code,【C语言】期末总结C_code

又到期末了,啪啪啪写起了代码。。。

#include "stdio.h"

#include "math.h"

#include "ctype.h"

#include "string.h"

#include "stdlib.h"

#include "time.h"

/*************************

符号常量

**************************/

#define PI 3.1415926

const double PI_=3.1415926; //注意格式区别

/*************************

枚举类型变量

**************************/

enum week {mon, tue, wed, thu, fri, sat, sun}; //mon=0,tue=1,...

//enum week {mon=1, tue, wed, thu, fri, sat, sun};

/*************************

结构体类型变量

**************************/

struct Point

{

double x,y;

};

typedef Point pit;

typedef struct{ double x,y; } pit_;

//可以嵌套

int max_thr(int a, int b, int c);

int is_leap(int x);

int is_prime1(int x);

int is_prime2(int x);

int fac(int n);

int com_factor(int m, int n);

void swap_a_b(int *p,int *q);

void mp_sort(int *a,int n);

void slt_sort(int *a,int n);

double dist(pit a, pit b);

int main()

{

/*****************************************

基本输入输出(注意输出结果区别)

******************************************/

float a1; double a2;

//scanf("%f %lf",a1,a2);

int b1=2016;

printf("%8d\n",b1); //右对齐,左补空格

printf("%-8d\n",b1); //左对齐

printf("%08d\n",b1); //右对齐,左补0

printf("%.2f\n",PI);

printf("%1.2f\n",PI);

printf("%8.2f\n",PI);

printf("%-8.2f\n",PI);

printf("%d\n",8/5); //整数/整数=整数

printf("%.2f\n",8/5);

printf("%.2f\n",(float)8/5);//强类型转换

printf("%.2f\n",8.0/5); //浮点数/整数=浮点数

printf("%.2f\n",8.0/5); //浮点数/浮点数=浮点数

printf("\\\t\"\n");

/************************************************************

比较float和double类型数据,

因为float/double精度问题,

比如 1.000000001 可能和1.0000000000001相等

比较 > 、< 可以直接进行比较

比较 == 、!= 最好用两个数做差取绝对值跟指定的精度进行比较

*************************************************************/

float c1=0.0000000001,c2=0.0000000001;

if(fabs(c1-c2) < 1e-10) printf("c1 == c2\n");

/*************************************

交换变量

t = a; a = b; b = t; //使用范围广

a = a + b;

b = a - b;

a = a - b;

swap( &a, &b);

**************************************/

/*************************

switch语句

**************************/

int d=3;

switch(d)

{

case 1: d=0;break;

case 2: d=1;break;

default:d=2;break;

}

/*************************

产生随机数( 1 ~ 100 )

**************************/

int rand_num;

srand(time( NULL));

rand_num = rand() % 100 +1;

/*************************

数组清零

#include "string.h"

memset(a, 0, sizeof(a));

**************************/

/*************************

字符函数

getchar( ch);

putchar( ch);

**************************/

char ch1 = 'a';

if( isalpha( ch1) ) printf("%c is a char\n", ch1);

if( isdigit( ch1) ) ;

/*************************

字符串函数

gets( str);

puts( str);

**************************/

char str[100],s1[100]={'a'},s2[100]="asdfgh";

sprintf(str,"%s","asd2016");

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

int len = strlen( str);

strcpy(s1, s2);

strcmp(s1, s2); //s1>s2,返回值>0;s1==s2,返回值=0;s1

strcat(s1, s2);

char *ptr_str = strchr(str, 'd'); //查找字符串str中首次出现字符d的位置

/*************************

输出数据以空格隔开

int first=1;

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

{

if(first) first = 0;

else printf(" ");

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

}

**************************/

return 0;

}

/*************************

三个数中最大数

**************************/

int max_thr(int a, int b, int c)

{

return (a>b?a:b) > c ? (a>b?a:b) : c;

}

/*************************

判断闰年

**************************/

int is_leap(int x)

{

if((x%4==0 && x%100!=0) || x%400==0)

return 1;

else

return 0;

}

/*************************

判断素数 1

**************************/

int is_prime1(int x)

{

int i;

if(i <= 1) return 0;

for(i = 2; i*i<=x; i++)

if(x % i == 0) return 0;

return 1;

}

/*************************

判断素数 2

#include "math.h"

**************************/

int is_prime2(int x)

{

int i,m;

if(x <= 1) return 0;

m = floor(sqrt(x) + 0.5);

for(i = 2; i <= m; i++)

if(x % i == 0) return 0;

return 1;

}

/*************************

计算最大公约数

**************************/

int com_factor(int m, int n)

{

int r,t;

if(m < n) {t=m; m=n; n=t;}

r = m % n;

while(r != 0)

{

m = n;

n = r;

r = m % n;

}

return n;

}

/*************************

交换变量

**************************/

void swap_a_b(int *p,int *q)

{

int t;

t = *p; *p = *q; *q = t;

//下面是错误方法

//int *t;

//t = p; p = q; q = t;

}

/*************************

计算两点间距离

#include "math.h"

**************************/

double dist(pit a, pit b)

{

return hypot(a.x-b.x, a.y-b.y);

}

/*************************

冒泡排序

**************************/

void mp_sort(int *a,int n)

{

int i,j,t;

for(i=0;i

for(j=i+1;j

if(a[i]>a[j])

{ t=a[i];a[i]=a[j];a[j]=t;}

}

/*************************

选择排序

**************************/

void slt_sort(int *a,int n)

{

int i,j,k,t;

for(i=0;i

{ k=i;

for(j=i+1;j

if(a[j]

if(k!=i)

{ t=a[k];a[k]=a[i];a[i]=t;}

}

}

/*************************

计算 n!

**************************/

int fac(int n)

{

return n==0 ? 1 : fac(n-1)*n;

}

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值