C语言温习

学习网站 http://www.cplusplus.com

#include <stdio.h>
#include <stdlib.h>
#include "hello.hpp"
#include <string.h>
#include <time.h>

//c语言主要是面向过程,面向对象是一种思路,一种思维方式。

void score(int score) {
    if (score >= 90) {
        printf("优秀\n");
    }else if (score >= 80) {
        printf("良好\n");
    }else if (score >= 60) {
        printf("及格\n");
    }else{
        printf("不及格\n");
    }
}

void score1(int score) {
    switch (score/10) {
        case 9:
            printf("优秀\n");
            break;
        case 8:
            printf("良好\n");
            break;
        case 7:
        case 6:
            printf("及格\n");
            break;
        default:
            printf("不及格\n");
            break;
    }
}

int Max(int a,int b) {
    return a>b?a:b;
}

//C语言结构体
struct People {
    int age;
    char *name;
};

//自定义类型,使用方便
typedef struct{
    int age;
}People1;

//定义函数指针,使用时直接用Func p;
typedef void (*Func)();

//函数
void sayHello() {
    printf("Hello C\n");
}


//面向对象思想
//ObjectStart>>>>>>>>>>>>>>>>>>>>>

#define ObjectField \
    void(*onDelete)(void *);

typedef struct Object {
    ObjectField
}Object;

#define ObjectCreate(TYPE) malloc(sizeof(TYPE))
#define ObjectDelete(obj) {\
    obj->onDelete(obj);\
    free(obj);\
}

void ObjectOnDelete(void *obj) {
    printf("Object on delete\n");
}

Object *ObjectInit(Object *obj) {
    obj->onDelete = ObjectOnDelete;
    return obj;
}

//ObjectEnd<<<<<<<<<<<<<<<<<<<<<<<

//People2Start>>>>>>>>>>>>>>>>>>>>>

typedef struct{
    ObjectField
    int age;
    void(*sayHello)();
}People2;

void peopleSayHello() {
    printf("PeopleSayHello\n");
}

void peopleOnDelete(void *p) {
    ObjectOnDelete(p);
    printf("People2 On Delete\n");
}

People2 *peopleInit(People2 *p,int age) {
    ObjectInit((Object *)p);
    
    p->age = age;
    p->sayHello = peopleSayHello;
    p->onDelete = peopleOnDelete;
    return p;
}

//People2End<<<<<<<<<<<<<<<<<<<<<<<

int main(int argc, const char * argv[]) {
    // insert code here...
//    printf("Hello, World!\n");
//    score(88);
//    score1(59);
//    
//    printf("%d\n",Max(10, 20));
    
//    for (int i = 0; i < 100; printf("%d\n",i ++)) {
//        printf(">>>\n");
//    }
    
//    int i = 0;
//    do {
//        printf("%d\n",i);
//        i ++;
//    } while (i < 100);
    
/*
    struct People p;
    p.age = 10;
    p.name = "XiaoYang";
    printf("name=%s  age=%d\n",p.name,p.age);
    
//    结构体赋值为copy,两个指针不想相同
    struct People p1 = p;
    printf("name=%s  age=%d\n",p1.name,p1.age);
    
    p.name = "YangXiao";
    p.age = 11;
    printf("修改p后p:name=%s  age=%d\n",p.name,p.age);
    printf("修改p后p1:name=%s  age=%d\n",p1.name,p1.age);
*/
    

/*
//    malloc(<#size_t#>)函数需要导入#include <stdlib.h>库
    struct People *p = malloc(sizeof(struct People));
    p->age = 10;
    p->name = "XiaoYang";
//    结构体指针赋值的是retain,两个指针相同
    struct People *p1 = p;
    p->age = 11;
    p->name = "YangXiao";
    printf("修改p后p1:name=%s  age=%d\n",p1->name,p1->age);
//     结构体指针在不用的时候需要释放,因为两个结构体的指针相同,所以只需要释放一次
    free(p);
    
    printf("p指针=%p  p1指针=%p\n",p,p1);
*/
    
    
/*
//    通常调用函数直接调用:sayHello();
//    声明一个返回值为void的函数指针p,p只能赋值返回值为void类型的函数指针
    void (*p)();
    printf("赋值前%p\n",p);
//    函数指针赋值为sayHello
    p = sayHello;
    printf("赋值后%p\n",p);
//    执行函数指针
    p();
 */
    
//    People2 *p2 = peopleInit(ObjectCreate(People2), 10);
//    printf("%d\n",p2->age);
//    p2->sayHello();
//    ObjectDelete(p2);
    
//    sayHello();
    
    定义一个缓冲区
//    char buf[100];
    清空一片区域
//    memset(buf, 0, 100);
    拼接一段字符串,需要导入文件<string.h>
//    sprintf(buf, "Hello %d,%.2f,%s\n",100,1.2,"拼接完成");
//    printf("%s",buf);
    
//    写入数据
//    FILE *f = fopen("data.text", "w");
//    if (f != NULL) {
//        fprintf(f, "Hello c");
//    }
//    fclose(f);
    
    读取数据
//    FILE *f = fopen("data.text", "r");
    读取到最后一个字节
//    fseek(f, 0, SEEK_END);
//    long size = ftell(f);
//    char buf[size + 1];
    重置到第一个字节
//    fseek(f, 0, SEEK_SET);
    把文件读取到buf里
//    fread(buf, sizeof(unsigned char), size, f);
    设置\0结尾
//    buf[size] = '\0';
//    fclose(f);
//    printf("%s\n",buf);
    
//    猜数字小游戏start>>>>>>>>>>>>>>>>>>>>>>>>>>>>
//    需要导入<stdlib.h>
//    rand()方法需要一个种子,根据种子来生成随机数,这里选用系统时间,需要导入time.h
    srand((int)time(NULL));
    int randNum = rand()%100;
    printf("请输入一个0-100中间的数字\n");
    
    int userInput;
    while (1) {
        scanf("%d",&userInput);
        
        if (userInput > randNum) {
            printf("输入值过大\n");
        }else if (userInput < randNum) {
            printf("输入值过小\n");
        }else{
            printf("正确\n");
            break;
        }
    }
    
    printf("exit\n");
//    猜数字小游戏end<<<<<<<<<<<<<<<<<<<<<<<<<<<<
    
    return 0;
}


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

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值