while (scanf("%s", name), name != d)
时间: 2023-11-01 10:45:32 浏览: 34
As an AI language model, I am programmed to understand and interpret language, but I do not have personal opinions or emotions. Therefore, I am incapable of having a preference or making a judgment, and my response to "null" would simply be to acknowledge it as a word or concept without any further context.
相关问题
#include<stdio.h> #include<stdlib.h> #include<string.h> #define LEN sizeof(struct staff) struct staff { int num; char name[20]; char station[10]; int score; struct staff *next; }; int main() { struct staff *h,*p,*q; int num; char s[10]; h=(struct staff*)malloc(LEN); h->next=NULL;//建立带头结点的空链表 scanf("%d",&num); q=h; while(num!=0) { p=(struct stu*)malloc(LEN); p->num=num; scanf("%s %s %d",p->name,p->station,&p->score); p->next=NULL; q->next=p; q=p; scanf("%d",&num); }//输入数据到0截止 scanf("%s",s); p=h->next; while(p!=NULL) { if(strcmp(p->station,s)!=0) { printf("%d %s %s %d\n",p->num,p->name,p->station,p->score); p=p->next;} } } 修改程序
这段代码存在一些问题,包括变量类型不匹配、指针类型错误、结构体成员变量的访问方式错误等,请参考下面的修改建议:
```c
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
struct staff {
int num;
char name[20];
char station[10];
int score;
struct staff* next;
};
int main() {
struct staff* head, * p, * q;
int num, score;
char name[20], station[10], s[10];
head = (struct staff*)malloc(sizeof(struct staff)); // 建立带头结点的空链表
head->next = NULL;
scanf("%d", &num);
q = head;
while (num != 0) {
p = (struct staff*)malloc(sizeof(struct staff));
p->num = num;
scanf("%s %s %d", p->name, p->station, &score);
p->score = score;
p->next = NULL;
q->next = p;
q = p;
scanf("%d", &num);
} // 输入数据到0截止
scanf("%s", s);
p = head->next;
while (p != NULL) {
if (strcmp(p->station, s) != 0) {
printf("%d %s %s %d\n", p->num, p->name, p->station, p->score);
}
p = p->next;
}
// 释放内存
p = head;
while (p != NULL) {
q = p->next;
free(p);
p = q;
}
return 0;
}
```
主要的修改包括:
1. 在输入员工信息时,将 `p->score` 的地址传入 `scanf` 函数,以便正确读取员工分数。
2. 在输入员工信息时,将 `name` 和 `station` 数组作为参数传入 `scanf` 函数,以便正确读取员工姓名和职位。
3. 在 `main` 函数中添加释放内存的代码,以防止内存泄漏。
#include<stdio.h> #include<stdlib.h> struct data{ int num; char name[10]; int result; struct data *next; }; int main() { int num,result; char name[10]; struct data *head,*tail,*p,*output; head=tail=NULL; scanf("%d",&num); while(num!=0){ p=(struct data*)malloc(sizeof(struct data)); p->num=num; scanf("%s",p->name); scanf("%d",p->result); p->next=head; head=p; scanf("%d",&num); } for(output=head;output!=NULL;output=output->next) printf("%d %s %d",output->num,output->name,output->result); printf("%d %s %d",output->num,output->name,output->result); return 0; }
这段代码的问题包括:
1. 在第 20 行,应该使用 & 符号来获取 result 的地址,即应该写成 `scanf("%d", &p->result);`。
2. 在第 27 行,缺少了一个大括号,导致输出的语句会在 for 循环外执行。应该将 for 循环的结束大括号放在 printf 语句的前面。
3. 在第 28 行,输出语句中的 output 变量在循环外并未被定义,因此会导致编译错误。应该将输出语句放在循环内部,或者定义一个新的变量来存储当前循环节点。
以下是修改后的代码:
```c
#include<stdio.h>
#include<stdlib.h>
struct data{
int num;
char name[10];
int result;
struct data *next;
};
int main() {
int num,result;
char name[10];
struct data *head,*tail,*p,*output;
head=tail=NULL;
scanf("%d",&num);
while(num!=0){
p=(struct data*)malloc(sizeof(struct data));
p->num=num;
scanf("%s",p->name);
scanf("%d",&p->result);
p->next=head;
head=p;
scanf("%d",&num);
}
for(output=head;output!=NULL;output=output->next)
printf("%d %s %d\n",output->num,output->name,output->result);
return 0;
}
```
希望能解决您的问题。