移动不消失的小球
#include<stdio.h>
#include<conio.h>//监听键盘输入
#include<windows.h>//提供许多系统自带函数
void gotoxy(int x, int y);
void moveCircle();
struct Circle {
int x;
int y;
}circle;
int direct = '0';
int main()
{
circle.x = 20;
circle.y = 10;
gotoxy(circle.x, circle.y);
printf("o");
gotoxy(80, 0);
while (1) {
moveCircle();
}
return 0;
}
void gotoxy(int x, int y)//辅助函数,系统自带
{
COORD pos = { x,y };
HANDLE hOut = GetStdHandle(STD_OUTPUT_HANDLE);//获取标准输出设备句柄
SetConsoleCursorPosition(hOut, pos);
}
void moveCircle()
{
if (_kbhit()) {
fflush(stdin);
direct = _getch();
switch (direct) {
case 'w':
case'W':
circle.y--;
break;
case 's':
case'S':
circle.y++;
break;
case 'a':
case'A':
circle.x -= 2;
break;
case 'd':
case'D':
circle.x += 2;
break;
}
gotoxy(circle.x, circle.y);
printf("o");
gotoxy(80, 0);
}
}
移动的小球
#include<stdio.h>
#include<conio.h>//监听键盘输入
#include<windows.h>//提供许多系统自带函数
void gotoxy(int x, int y);
void moveCircle();
struct Circle {
int x;
int y;
}circle;
int direct = '0';
int main()
{
circle.x = 20;
circle.y = 10;
gotoxy(circle.x, circle.y);
printf("o");
gotoxy(80, 0);
while (1) {
moveCircle();
}
return 0;
}
void gotoxy(int x, int y)//辅助函数,系统自带
{
COORD pos = { x,y };
HANDLE hOut = GetStdHandle(STD_OUTPUT_HANDLE);//获取标准输出设备句柄
SetConsoleCursorPosition(hOut, pos);
}
void moveCircle()
{
if (_kbhit()) {
fflush(stdin);
direct = _getch();
//清除上一次位置
gotoxy(circle.x, circle.y);
printf(" ");
switch (direct) {
case 'w':
case'W':
circle.y--;
break;
case 's':
case'S':
circle.y++;
break;
case 'a':
case'A':
circle.x -= 2;
break;
case 'd':
case'D':
circle.x += 2;
break;
}
gotoxy(circle.x, circle.y);
printf("o");
gotoxy(80, 0);
}
}
链表学习
#include<stdio.h>
#include<malloc.h>//动态存储分配函数头文件,当对内存区进行操作时,调用相关函数
void createHeadNode();
void createNewNode(struct node* phead);
void showList(struct node* phead);
struct node {
int data;//数据域
struct node* pnext;//指针域,指向下一个节点的指针
};
struct node* phead = NULL;//全局变量,方便我们调用
int main()
{
createHeadNode();
createNewNode(phead);
showList(phead);
return 0;
}
void createHeadNode() {
phead = (struct node*)malloc(sizeof(struct node));
if (phead == NULL)
{
printf("头节点创建失败\n");
return;
}
else {
phead->pnext == NULL;
}
}
//刚开始phead和pfind都指向头节点
void createNewNode(struct node* phead) {
struct node* pnew = NULL;
struct node* pfind = phead;
for (int i = 0; i < 20; i++) {
pnew = (struct node*)malloc(sizeof(struct node));
pnew->data = i;
pnew->pnext = NULL;
pfind->pnext = pnew;
//pfind = pfind->pnext;
pfind = pnew;
}
}
void showList(struct node* phead) {
struct node* pfind = phead->pnext;
while (pfind != NULL) {
printf("%d ", pfind->data);
pfind = pfind->pnext;
}
}
通讯录管理系统V1.0
#include<stdio.h>
#include<stdlib.h>//exit(0),包含<malloc.h>
#include<string.h>//strcpy
#define _CRT_SECURE_NO_WARNINGS 1
void createFace();
void createHeadNode();
void addNewNode(struct node* phead);
void deleteNodeByNo(struct node* phead);
void showAllNode(struct node* phead);
void info_output(struct node* pfind);
void searchNodeByNo(struct node* phead);
void updateNodeByNo(struct node* phead);
struct Person {
int no;
char name[20];
int age;
char tel[15];
char address[30];
};
struct node {
Person data;
struct node* pnext;
};
struct node* phead = NULL;
int main()
{
createHeadNode();
createFace();
return 0;
}
void createFace() {
int a = 0;
while (1) {
printf("\t\t\t欢迎使用通讯录管理系统\n");
printf("\t\t\t1:添加一条联系人信息\n");
printf("\t\t\t2:删除一条联系人信息\n");
printf("\t\t\t3:打印所有联系人信息\n");
printf("\t\t\t4:查询一条联系人信息\n");
printf("\t\t\t5:修改一条联系人信息\n");
printf("\t\t\t6:退出系统\n");
scanf("%d", &a);
switch (a) {
case 1:addNewNode(phead); break;
case 2:deleteNodeByNo(phead); break;
case 3:showAllNode(phead); break;
case 4:searchNodeByNo(phead); break;
case 5:updateNodeByNo(phead); break;
case 6:exit(0);
}
}
}
//创建链表的头节点
void createHeadNode() {
phead = (struct node*)malloc(sizeof(struct node));
if (!phead) {
printf("头节点分配失败\n");
return;
}
else {
phead->pnext = NULL;
}
}
//添加一个新节点
void addNewNode(struct node* phead) {
struct node* pfind = phead;
struct node* pnew = NULL;
while (pfind->pnext != NULL) {
pfind = pfind->pnext;
}
pnew = (struct node*)malloc(sizeof(struct node));
printf("请输入编号\n");
scanf("%d", &pnew->data.no);
printf("请输入姓名\n");
scanf("%s", pnew->data.name);
printf("请输入年龄\n");
scanf("%d", &pnew->data.age);
printf("请输入电话\n");
scanf("%s", pnew->data.tel);
printf("请输入地址\n");
scanf("%s", pnew->data.address);
pnew->pnext = NULL;//尾节点的指针为空
pfind->pnext = pnew;//前一个指针的后一个指向新节点,即可连接起来
printf("%s的信息录入成功\n", pnew->data.name);
}
//删除一个指定的节点
void deleteNodeByNo(struct node* phead) {
int no = 0;
struct node* ptemp = NULL;
struct node* pfind1 = phead;//前指针
struct node* pfind2 = phead->pnext;//后指针
printf("请输入需要删除的联系人的编号\n");
scanf("%d", &no);
while (pfind2 != NULL) {
if (pfind2->data.no == no) {
break;
}
pfind1 = pfind1->pnext;
pfind2 = pfind2->pnext;
}
if (pfind2 == NULL) {
printf("对不起,查无此记录");
}
else {
ptemp = pfind2->pnext;
free(pfind2);
pfind2 = NULL;
pfind1->pnext = ptemp;
printf("删除成功\n");
}
}
//遍历链表,输出数据
void showAllNode(struct node * phead) {
struct node* pfind = phead->pnext;
while (pfind != NULL) {
info_output(pfind);
pfind = pfind->pnext;
}
}
//封装函数,用于输出,目的是为了简化代码
void info_output(struct node * pfind) {
printf("++++++++++++++++++++++++++++++++++++++++++++++\n");
printf("编号:%d\t姓名:%s\t年龄:%d\t电话:%s\t地址:%s\n", pfind->data.no, pfind->data.name, pfind->data.age, pfind->data.tel, pfind->data.address);
printf("++++++++++++++++++++++++++++++++++++++++++++++\n");
}
//查询一条指定节点
void searchNodeByNo(struct node* phead) {
int no = 0;
struct node* pfind = phead;
printf("请输入你想查找联系人的编号\n");
scanf("%d", &no);
while (pfind != NULL) {
if (pfind->data.no == no) {
break;
}
pfind = pfind->pnext;
}
if (pfind == NULL) {
printf("对不起,查无此记录\n");
}
else {
info_output(pfind);
}
}
//修改一条指定节点中的数据
void updateNodeByNo(struct node* phead) {
int no = 0;
struct node* pfind = phead;
printf("请输入你想修改联系人的编号\n");
scanf("%d", &no);
while (pfind != NULL) {
if (pfind->data.no == no) {
break;
}
pfind = pfind->pnext;
}
if (pfind == NULL) {
printf("对不起,查无此记录\n");
}
else {
info_output(pfind);
printf("请输入编号\n");
scanf("%d", &pfind->data.no);
printf("请输入姓名\n");
scanf("%s", pfind->data.name);
printf("请输入年龄\n");
scanf("%d", &pfind->data.age);
printf("请输入电话\n");
scanf("%s", pfind->data.tel);
printf("请输入地址\n");
scanf("%s", pfind->data.address);
printf("%s的信息修改成功\n", pfind->data.name);
info_output(pfind);
}
}