我想检查重复添加相同姓名的情况,可这个代码运行有问题,可以帮忙看看问题出在哪里了吗
#include <stdio.h>
#include <string.h>
#include <stdlib.h>
typedef struct student
{
char name[20];//名字
char wm[20];//性别
char work[100];//工作单位
char stel[20];//手机
struct student *next;
}stu;
stu *head;
void input()
{
int ans;
stu *p1,p2;
p1=(stu)malloc(sizeof(stu));
if(p1!=NULL)
{
printf("========输入数据========\n");
head=p1;
while(1)
{
printf("名字:");
scanf("%s",&p1->name);
stu* tmp = head;
while (tmp != NULL) {
if (strcmp(tmp->name, p1->name) == 0) {
printf("该名字已经存在,请重新输入!\n");
continue;
}
tmp = tmp->next;
}
printf("性别:");
scanf("%s",&p1->wm);
printf("工作单位:");
scanf("%s",&p1->work);
printf("手机:");
scanf("%s",&p1->stel);
printf("===================================\n");
p2=p1;
p1=(stu*)malloc(sizeof(stu));
if(p1!=NULL)
p2->next=p1;
printf("请选择是否继续输入:1.继续 2.退出\n请选择:");
scanf("%d",&ans);
if(ans==1)
continue;
else//退出
{
printf("========输入完毕========\n");
p2->next=NULL;
free(p1);
break;
}
}
}
修改如下,改动处见注释,供参考:
#include <stdio.h>
#include <string.h>
#include <stdlib.h>
typedef struct student
{
char name[20];//名字
char wm[20];//性别
char work[100];//工作单位
char stel[20];//手机
struct student *next;
}stu;
stu *head = NULL;
void input()
{
int ans;
stu *p1, *p2, *tmp; //stu *p1,p2; 修改
printf("========输入数据========\n");
while(1)
{
p1=(stu*)malloc(sizeof(stu)); // 修改
p1->next = NULL; // 修改
printf("名字:");
scanf("%s",p1->name); // scanf("%s",&p1->name); 修改
getchar();
if (head) {
tmp = head;
while (tmp != NULL) {
if (strcmp(tmp->name, p1->name) == 0) {
printf("该名字已经存在,请重新输入!\n");
free(p1); // 修改
break; //continue; 修改
}
tmp = tmp->next;
}
if (tmp) continue; // 修改
}
printf("性别:");
scanf("%s",p1->wm); //scanf("%s",&p1->wm); 修改
getchar();
printf("工作单位:");
scanf("%s",p1->work); //scanf("%s",&p1->work); 修改
getchar();
printf("手机:");
scanf("%s",p1->stel); //scanf("%s",&p1->stel); 修改
getchar();
printf("===================================\n");
if (!head) //if(p1!=NULL) 修改
head = p1;
else
p2->next = p1;
p2 = p1;
do { // 修改
printf("请选择是否继续输入:1.继续 2.退出\n请选择:");
scanf("%d",&ans);
}while (ans < 1 || ans > 2);// 修改
//continue; 修改
if(ans == 2) //else//退出 修改
{
printf("========输入完毕========\n");
//p2->next=NULL; 修改
//free(p1); 修改
break;
}
}
}
void print(stu *P)
{
while (P){
printf("%s %s %s %s\n",P->name,P->wm,P->work,P->stel);
P = P->next;
}
}
int main()
{
input();
print(head);
return 0;
}
p2的类型是 stu
p1的类型是 stu*
效果如下:
击杀Single敌机版(C代码实现)
详细代码如下:
#include<stdio.h>
#include<stdlib.h>
#include<conio.h>
#include<windows.h>
#define high 20 //游戏画面尺寸
#define width 36 //如果不把这两个变量设为符号常量而设为全局变量,编译器就会报错
//至于为什么大家可以在评论区下讨论,我想看看大家对这个的想法
//问题:符号常量与全局变量有什么区别?
//最后我会公布答案
int x, y; //飞机坐标
int enemy_x, enemy_y; //敌机坐标
int score; //得分
int life; //飞机生命值
int canvas[high][width + 17] = {0}; //二维数组存储游戏画布中对应的元素
//1为飞机,2为子弹,3为敌机
//width + 17 是为了保证飞机右翼不会超出右线
//而之后的enemy_y = rand() * 100 % width + 9;是
//为了保证敌机在左边线以右9个单位的距离
//这是间接的控制飞机左翼不超过左边界的方法
int digit(int s)
{
int ne, t, bi;
ne = s;
int num = 0;
while(ne)
{
t = ne % 10;
ne /= 10;
num++;
}
return num;
}
void startup()
{
x = high / 2;
y = width / 2;
canvas[x][y] = 1;
enemy_x = 0;
enemy_y = y;
life = 3;
canvas[enemy_x][enemy_y] = 3;
score = 0;
}
void gotoxy(int x, int y) //将光标移动到(x,y)位置
{
HANDLE handle = GetStdHandle(STD_OUTPUT_HANDLE);
COORD pos;
pos.X = x;
pos.Y = y;
SetConsoleCursorPosition(handle, pos);
}
void HideCursor()
{
CONSOLE_CURSOR_INFO cursor_info = {1, 0};
SetConsoleCursorInfo(GetStdHandle(STD_OUTPUT_HANDLE), &cursor_info);
}
void show()
{
gotoxy(0, 0);
HideCursor();
int i, j;
for(i = 0; i < high; i++)
{
for(j = 0; j < width + 17; j++)
{
if((i == x) && (j == y))
{
printf("*");
}
else if((i == x + 1) && (j == y - 2))
{
printf(" * *");
j += 3;
}
else if((i == x + 2) && (j == y - 2))
{
printf("* * *");
j += 4;
}
else if((i == x + 3) && (j == y - 6))
{
printf("* * * * * * *");
j += 12;
}
else if((i == x + 4) && (j == y - 8))
{
printf("* * * * * * * * *");
j += 16;
}
else if((i == x + 5) && (j == y - 1))
{
printf("* * ");
j += 3;
}
else if(canvas[i][j] == 3)
printf("@");
else if(canvas[i][j] == 2)
printf("|");
else
printf(" ");
}
printf("#\n");
}
printf("生命值:%d", life);
int space = digit(life);
if(space == 1)
{
for(i = 0; i < width + 8; i++)
printf(" ");
}
else
{
for(i = 0; i < width + 8 - (space - 1); i++)
printf(" ");
}
printf("#\n");
printf("得分:%d", score);
int space2 = digit(score);
if(space2 == 1)
{
for(i = 0; i < width + 9; i++)
printf(" ");
}
else
{
for(i = 0; i < width + 9 - (space2 - 1); i++)
printf(" ");
}
printf("#\n");
for(i = 0; i < width + 17; i++)
printf("#");
}
void updateWithoutInput() //与用户输入无关的更新
{
int i, j;
for(i = 0; i < high; i++)
{
for(j = 0; j < width + 17; j++)
{
if(canvas[i][j] == 2)
{
if((i - 1 == enemy_x) && (j == enemy_y))
{
score++; //得分
canvas[enemy_x][enemy_y] = 0; //敌机已被消灭
enemy_x = 0;
enemy_y = rand() * 100 % width + 9;
canvas[enemy_x][enemy_y] = 3; //产生敌机
canvas[i][j] = 0; //子弹消耗掉
}
canvas[i][j] = 0;
if(i > 0)
canvas[i - 1][j] = 2; //子弹向上移动
}
}
}
if(enemy_x > high) //防线被突破
{
canvas[enemy_x][enemy_y] = 0;
enemy_x = 0;
enemy_y = rand() * 100 % width + 9;
canvas[enemy_x][enemy_y] = 3;
score--; //减分
life--;
}
if(enemy_x >= x)
{
life--;
score--;
if(life == 0)
{
printf("\n任务失败!!!\n");
system("pause");
exit(0);
}
canvas[enemy_x][enemy_y] = 0;
enemy_x = 0;
enemy_y = rand() * 100 % width + 9;
canvas[enemy_x][enemy_y] = 3;
}
static int speed = 0; //控制敌机下落速度
if(speed < 10)
speed++;
if(speed == 10)
{
canvas[enemy_x][enemy_y] = 0;
enemy_x++;
speed = 0;
canvas[enemy_x][enemy_y] = 3;
}
}
void updateWithInput() //与用户输入有关的更新
{
char input;
if(kbhit())
{
input = getch();
if(input == 'a')
{
canvas[x][y] = 0;
y--;
canvas[x][y] = 1;
}
if(input == 'd')
{
canvas[x][y] = 0;
y++;
canvas[x][y] = 1;
}
if(input == 'w')
{
canvas[x][y] = 0;
x--;
canvas[x][y] = 1;
}
if(input == 's')
{
canvas[x][y] = 0;
x++;
canvas[x][y] = 1;
}
if(input == ' ')
{
// bullet_x = x - 1;
// bullet_y = y;
canvas[x - 1][y] = 2;
}
}
}
int main()
{
startup();
while(1)
{
show();
updateWithoutInput();
updateWithInput();
}
return 0;
}
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
// 定义节点结构体
typedef struct _Node {
char name[20];
struct _Node* next;
} Node;
// 定义链表结构体
typedef struct _List {
Node* head;
Node* tail;
} List;
// 初始化链表
void initList(List *pList) {
pList->head = NULL;
pList->tail = NULL;
}
// 添加节点到链表
void addNode(List *pList, char *name) {
Node *pNode = (Node*) malloc(sizeof(Node));
strcpy(pNode->name, name);
pNode->next = NULL;
if (pList->head == NULL) {
pList->head = pNode;
pList->tail = pNode;
} else {
pList->tail->next = pNode;
pList->tail = pNode;
}
}
// 检查链表中是否重复添加了相同姓名的节点
int checkDuplicate(List *pList, char *name) {
Node *pNode = pList->head;
while (pNode != NULL) {
if (strcmp(pNode->name, name) == 0) {
return 1;
}
pNode = pNode->next;
}
return 0;
}
int main() {
List list;
initList(&list);
addNode(&list, "Tom");
addNode(&list, "Jerry");
addNode(&list, "Tom"); // 添加相同姓名的节点
if (checkDuplicate(&list, "Tom")) {
printf("Duplicate found!\n");
} else {
printf("No duplicate found!\n");
}
return 0;
}
在上述代码中,定义了一个链表结构体(List),包含了链表的头指针和尾指针;定义了一个节点结构体(Node),包含了以字符串形式存储的姓名和指向下一个节点的指针。除此之外,还定义了三个函数:初始化链表(initList)、向链表中添加节点(addNode)和检查链表中是否重复添加了相同姓名的节点(checkDuplicate)。
代码中checkDuplicate函数的实现是通过遍历链表来检查是否已经有相同姓名的节点被添加了。如果检查到重复的姓名,则返回1;否则返回0。在上述代码中,向链表中添加了相同姓名的两个节点“Tom”,因此checkDuplicate函数应该返回1。