您的位置:首页 > 编程语言 > C语言/C++

C语言温习

2016-03-30 15:04 429 查看
学习网站 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;
}
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签:  c语言 代码