Cocos2d-x手机游戏开发C编程基础---笔记一



1:C语言程序入口

C语言Helloword的实现
// C语言.cpp : 定义控制台应用程序的入口点。
#include "stdafx.h"  
#include <stdio.h>  //标准的输入输出库,箭头表示库文件
int _tmain(int argc, _TCHAR* argv[])  //char *=_TCHAR *    为入口函数
{
 printf("helloword\n");
        printf("helloword");
        fflush(stdin);                       //函数体
 getchar();
 return 0;
}

2:C语言变量和表达式

//求矩形的面积
#include "stdafx.h"
#include <stdio.h>
int _tmain(int argc, _TCHAR* argv[])
{
 //求矩形的面积
 int a;
 int b;
 int c;
 a=3;
 b=4;
 c=a*b; //c语言中的表达式:算数表达式,关系,逻辑,位运算(<<, >>,&与域 |位域 ~异域),赋值
 printf("长:%d,宽:%d,面积:%d",a,b,c);    数据类型占用空间,要掌握???
 fflush(stdin);
 getchar();
    return 0;
}
//变量的数据类型:
//short:最大内存65535:保存存小的数值     long:64位 占8个字节长度  保存大的数值,金钱


3:C语言字符类型

#include "stdafx.h"
#include <stdio.h>
int _tmain(int argc, const char* argv[])
{
 char c1;
 char c2;
 char c3;
 c1='W';   //字符串赋值要用单引号引用
 c2='1';
 c3=49;     输出 C3=1     c3=50则输出c3=2
 printf("c1=%c,c2=%c,c3=&c",c1,c2,c3);        再加入:c1=%d c2=%d,则c1=87 c2=49.
       if(c2==49) 
        {
             printf("OK");   //在C语言中字符类型和整数类型是可以通用的。
         }
 fflush(stdin);
 getchar();
 return 0;
}

4:C语言条件语句

#include "stdafx.h"
#include <stdio.h>
int _tmain(int argc, const char* argv[])
{
 //输入成绩,是否合格
 int score;
 printf("Please input:\n");
 scanf("%d",&score);
 if(score>=60)
 {
  printf("成绩合格\n");
 }
 else
 {
    printf("成绩不合格");
 }  //思考方法二if esle嵌套
    fflush(stdin);
 getchar();
 return 0;
}


方法三:
#include "stdafx.h"
#include <stdio.h>
int _tmain(int argc, const char* argv[])
{
 //输入成绩,是否合格
 int score;
 printf("Please input:\n");
 scanf("%d",&score);
 switch(score/10)
 {   case 10:
     case 9:
      printf("成绩优秀\n");
      break;
  case 8:
   printf("成绩良好\n");
   break;
    default:
           sprintf("不及格\n");
     break;
 }
 fflush(stdin);
 getchar();
 return 0;
}


5:C语言循环
#include "stdafx.h"
#include <stdio.h>
int _tmain(int argc, const char* argv[])
{
 //求1+2+3...+100
 int n=1;
 int sum=0;
 while(n<=100)
 {
   sun=sum+n;  
   n=n+1;
 }
 rintf("1+2+3...+100=%d",sum);
 fflush(stdin);
 getchar();
 return 0;
}   //思考其他方法:
do..while方法
n=1;
sum=0;
do{
    sun+=n;
    n++;
}while(n<=100);
printf("1+2+3...+100=%d",sum);

for方法
n=1;
sum=0;
for(n=1;n<=100;n++)
{
   sum+=n;
}
printf("1+2+3...+100=%d",sum);

拓展:求1到100内,偶数的和
for(n=1,n<=100,n++)
{
    if(n/2==0)  //关键
    sum+=n;
}

作业:1+2+...+1000;

6:C语言学会数组处理

数组,字符串数组,字符数组
字符串就是一个字符数组
数组:定义相同类型的变量
#include <stdio.h>
#include <stdlib.h>
int main()
{
    //求一组玩家金币的最高值
 int player[10]={11,33,44,667,88,334,99,12,222,444};      //每个储存空间都占4个字节。
 int max;
 printf("第四个玩家,金币数:%d\n",player[3]);  //0--9  下标是当前数减一,下标可以是常量,也可以是变量,也可以是表

达式。  //下表不能超过长度减一,如果大于10就会越界,也不能小于0。
 max=player[0]; //对max初始化。
 for(int i=1;i<=9;i++)
 { if(player[i]>max)
     {
       max=player[i];
      }
          printf("\nplayer[%d]= %d",i,player[i]);
  }
 printf("\n最高金币数:%d\n",max);

 //数组的排序,用冒泡法即倒序排列。
 for(int i=0;i<10;i++)
 {
  for(int j=i;j<10;j++)
  {
   if(player[j]>player[i])
   {
    int t=player[i];
    player[i]=player[j];
    player[j]=t;
   }
  }
 }
 for(int n=0;n<10;n++)
 {
  printf("\nplayer[%d]= %d",n,player[n]);
 }
 fflush(stdin);
 getchar();
    return 0;
}
//思考:最小值求法,正序法排序。

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

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值