美文网首页
【C语言】期末总结C_code

【C语言】期末总结C_code

作者: Roc_J | 来源:发表于2016-07-11 21:48 被阅读0次

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

#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<s2,返回值<0
 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<n;i++)
 for(j=i+1;j<n;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<n-1;i++)
 { k=i;
 for(j=i+1;j<n;j++)
 if(a[j]<a[k]) k=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;
}

相关文章

  • 【C语言】期末总结C_code

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

  • 关于 charsets 的慢查询排查

    下面的 sql 报了慢查询: 其中 c_code、u_code、h_code 分别是 c、u、h 表的一个 uni...

  • C语言指针总结大学霸IT达人

    C语言指针总结大学霸IT达人 C语言的指针是C语言区别其它语言的最主要的特定之一。有了指针,C语言就可以抛开所有束...

  • C语言总结

    学习C语言很久了,感觉总是学习的不够系统,所以就在这总结一下,简单的部分在此仅列出提纲,然后我感觉难的部分就稍微写...

  • C语言总结

    C语言小结,适用于有其他编程语言基础的开发者 ● 所有的 C 语言程序都需要包含 main() 函数。 代码从 m...

  • (精)十天学会C语言(上)

    Day01第01天C语言(00)笔记总结第01天C语言(01):iOS初体验第01天C语言(02):第一个C语言第...

  • (精)十天学会C语言(下)

    Day06 第06天C语言(00)笔记总结第06天C语言(01):进制转换-基本概念第06天C语言(02):进制转...

  • C#语言特性发展史

    C#语言特性发展史 Intro 本文主要总结介绍C# 每个版本带来的不同的语言特性。 C#,读作C Sharp,是...

  • C语言入门笔记

    笔者花了一个晚上自学了C语言,现在将C语言重点总结如下: 初识C语言 C语言一经出现就以其功能丰富、表达能力强、灵...

  • Linux内核分析第四周作业

    实验截图 代码分析 fork.c fork-asm.c 总结 高级编程语言在API中封装了系统调用,比如C语言中就...

网友评论

      本文标题:【C语言】期末总结C_code

      本文链接:https://www.haomeiwen.com/subject/riujjttx.html