#include <stdio.h>
int main(int argc, const char * argv[])
{
printf("hello world\n");
return 0;
}
变量与表达式
#include <stdio.h>
int main(int argc, const char * argv[])
{
//求矩形面积
int l,w,area; //定义变量
l = 3;//变量赋值
w = 4;
area = l*w;//表达式
printf("%d\n", area);
return 0;
}
字符类型
#include <stdio.h>
int main(int argc, const char * argv[])
{
//字符类型
char c1,c2;
c1 = 'a';
c2 = 98;
printf("%c %d\n", c1, c1);
printf("%c %d\n", c2, c2);
return 0;
}
条件语句
#include <stdio.h>
int main(int argc, const char * argv[])
{
//条件语句
int score;
printf("please input score \n");
scanf("%d", &score);
if(score >= 60){
printf("ok\n");
}else{
printf("sorry\n");
}
return 0;
}
输入三角形的三边长,求三角形面积。
#include <stdio.h>
#include <math.h>
int main()
{
float a,b,c,s,area;
scanf("%f %f %f", &a, &b, &c);
s = 1.0/2*(a+b+c);
area = sqrt(s*(s-a)*(s-b)*(s-c));
printf("%7.2f\n",area);
return 0;
}
条件分支
#include <stdio.h>
int main(int argc, const char * argv[])
{
//条件语句
int score;
printf("please input score \n");
scanf("%d", &score);
switch(score/10){
case 10:
case 9: printf("best\n"); break;
case 8:
case 7: printf("better\n");break;
case 6: printf("good\n");break;
default:printf("week\n"); break;
}
return 0;
}
循环语句
#include <stdio.h>
int main(int argc, const char * argv[])
{
//循环语句
int n=1,sum=0;
while(n<=100){
sum = sum+n;
n=n+1;
}
printf("%d\n",sum);
return 0;
}
#include <stdio.h>
int main(int argc, const char * argv[])
{
//循环语句
int n=1,sum=0;
do{
sum = sum+n;
n++;
}while(n<=100);
printf("%d\n",sum);
return 0;
}
#include <stdio.h>
int main(int argc, const char * argv[])
{
//循环语句
int n=1,sum=0;
for(n=1; n<=100; n++){
if(n%2==0){
sum += n;
}
}
printf("%d\n",sum);
return 0;
}
数组
#include <stdio.h>
int main(int argc, const char * argv[])
{
//求一组玩家金币的最高值
int player[10] = {1,2,3,4,2,5,10};
int max = player[0];
int length = sizeof(player)/sizeof(int);
for(int i=0; i<length; i++){
if(player[i]>max){
max = player[i];
}
}
printf("%i\n",max);
return 0;
}
数组排序:冒泡排序
#include <stdio.h>
int main(int argc, const char * argv[])
{
//数组排序
int player[10] = {1,2,3,4,2,5,10};
int length = sizeof(player)/sizeof(int);
int tmp = player[0];
for(int i=0; i<length; i++){
for(int j=0; j<length; j++){
if(player[i] < player[j]){
int tmp = player[i];
player[i] = player[j];
player[j] = tmp;
}
}
}
for(int n=0; n<length; n++){
printf("%i\n", player[n]);
}
return 0;
}
字符串(字符数组)
#include <stdio.h>
int main(int argc, const char * argv[])
{
//字符串(字符数组)
char name[20] = {'a','l','i','c','e','\0'};//\0是字符数组结束的标识
printf("%s\n", name);
return 0;
}
字符串处理库
#include <stdio.h>
#include <string.h>
int main(int argc, const char * argv[])
{
//字符串(字符数组)
char name[20] = {'a','l','i','c','e','\0'};//\0是字符数组结束的标识
char tmp[20] = {'o','k'};
tmp[0] = 'k';
strcpy(tmp,name);//将name中的字符拷贝给tmp
printf("%s\n%s\n", name, tmp);
strcat(name,tmp);//将tmp连接到name上
printf("%s\n%s\n",name,tmp);
return 0;
}
函数定义
#include <stdio.h>
//函数定义
int max(int a, int b)
{
if(a>b){
return a;
}else{
return b;
}
}
//入口函数
int main(int argc, const char * argv[])
{
int result = max(11,10);
printf("%d\n", result);
return 0;
}
求最大值
$ vim util.h
int max(int a, int b)
{
return a>b ? a : b;
}
$ vim program.c
#include <stdio.h>
#include "util.h" //加载自定义库
//入口函数
int main(int argc, const char * argv[])
{
int result = max(11,10);
printf("%d\n", result);
return 0;
}
指针
$ vim program.c
#include <stdio.h>
int main()
{
int i = 10;
int *p;
*p = &i;// 指向变量的指针,指针保存着变量在内存中的地址
printf("%d\n%d\n", i, *p);
return 0;
}
$ gcc program.c -o program
$ program.c
10
10
数组指针:指向数组的指针
$ vim program.c -o program
#include <stdio.h>
int main()
{
int iarr[10] = {1,2,3,4,5,6,7,8,9};
int *p;
p = iarr;// *p = &iarr[0];
printf("%d\n%d\n", *p, *(p+2));
return 0;
}
$ gcc program.c -o program
$ program.exe
1
3
函数指针
$ vim program.c
#include <stdio.h>
int max(int x, int y)
{
return x>y ? x : y;
}
int main()
{
int (* p)(int a, int b);//定义函数指针
p = max;
printf("%d\n", p(1, 10));
return 0;
}
$ gcc program.c -o program
$ program.exe
10
结构体
$ vim program.c
#include <stdio.h>
#include <string.h>
#定义结构体
struct User
{
char username[20];
int diamond;
}
#定义自定义类型
typedef struct _Admin
{
char account[20];
int status;
} Admin;
int main()
{
sturct User u;//定义结构体变量
strcpy(u.username, "junchow");
u.diamond = 100;
printf("%s\n%d\n", u.username, u.diamond);
Admin admin;//定义自定义类型变量
strcpy(admin.account, "junchow");
admin.status = 1;
printf("%s\n%d", admin.account, admin.status);
Admin * root;
root = &admin;
printf("%s\n%d\n", root->account, root->status);
return 0;
}
$ gcc program.c -o program
$ program.exe
junchow
100
junchow
1
junchow
1
结构体数组
$ vim program.c
#include <stdio.h>
// 定义自定义结构体类型
typedef struct _Player
{
char name[20];
int life;
int job;
// 定义联合体
union {
int classID;
char officeAddress[20];
}jobinfo;
} Player;
int main
{
Player p;//定义自定义类型变量
p.job = 1;
if(p.job==0){
p.jobinfo.classID=1;
printf("%d\n", sizeof(p));
}else{
printf("please input job address\n");
scanf("%s", p.jobinfo.officeAddress);
}
return 0;
}
结构体和联合体
$ vim program.c
#include <stdio.h>
typedef struct _Player{
char name[20];
int life;
int job;
union{
int classID;
char officeAddress[20];
}jobinfo;
} Player;
int main()
{
int length = 3;
Player p[length];
for(int i=0; i<length; i++){
printf("please input job type\n");
scanf("%d", &p[i].job);
if(p[i].job == 0){
printf("please input class id\n");
scanf("%d", &p[i].job.classID);
}else{
printf("please input office address\n");
scanf("%s", &p[i].job.officeAddress);
}
}
return 0;
}
$ gcc program.c -o program
$ program.exe
NPC管理系统(bug)
#include <stdio.h>
#include <stdlib.h>
//定义结构体
typedef struct _NPC{
int nid;
char name[20];
int nx,ny;
}NPC;
//定义节点对象
typedef struct _NDOE{
NPC * data;
struct _NPC * next;
}NODE;
//定义链表的头节点
NODE * head;
//添加节点
void addNode();
//查找节点
void searchNode(int nid);
//显示节点
void showNode();
//插入节点
void insertNodeAt(NODE *node, int index);
//定义枚举类型
enum SYSMENU{
ADD,//1显示
SEARCH,//2查询
SHOW,//3显示
QUIT//4
};
int main()
{
enum SYSMENU input;
head = NULL;
//main menu
while(1){
printf("NPC Manage system\n0 add npc\n1 search npc\n2 show npc\n3 quit\n");
scanf("%d", &input);
switch(input){
case ADD:
printf("choose add npc\n");
addNode();
break;
case SEARCH:
printf("search npc\n");
break;
case SHOW:
printf("show npc\n");
showNode();
break;
case QUIT:
printf("quit\n");
exit(0);
break;
default:
break;
}
printf("---------------------------------------------------\n");
}
return 0;
}
//添加节点
void addNode()
{
//新节点
NODE * inode = (NODE *)malloc(sizeof(NODE));//动态分配
//链表数据区
inode->data = (NPC *)malloc(sizeof(NPC));
//节点赋值
printf("please input npc id and name and position\n");
scanf("%d %s %d %d", &inode->data->nid, &inode->data->name, &inode->data->nx, &inode->data->ny);
if(head==NULL){
//直接添加一个新节点
head = inode;
}else{
//找到最后一个基点后添加
NODE * pnode;
pnode = head;
while(pnode->next != NULL){
pnode = pnode->next;
}
pnode->next = inode;
}
}
void showNode()
{
NODE * pnode;
pnode = head;
while(pnode != NULL){
printf("%d %s %d %d", pnode->data->nid, pnode->data->name, pnode->data->nx, pnode->data->ny);
pnode = pnode->next;
}
}