printf("请输入姓名:"); scanf("%s", p->data.name);
时间: 2023-07-18 20:08:52 浏览: 45
这段代码通过 `printf()` 函数输出提示信息,要求用户输入员工的姓名,然后通过 `scanf()` 函数读取用户输入的字符串,将其存储到新节点的 `data` 成员中的 `name` 字符数组中。
该代码段的作用是将员工的姓名存储到新节点的 `data` 成员中的 `name` 字符数组中。
相关问题
node *modification(node *h){//修改订单 node *s,*r; if(h==NULL) { h=malloc(sizeof(node)); h->next=NULL; r=h; } else{ r=h; while(r->next!=NULL) r=r->next; } s=malloc(sizeof(node)); printf("名称:"); scanf("%s",s->data.name); printf("增加时间:"); scanf("%lf",&s->data.time); printf("类型:"); scanf("%d",&s->data.type); r->next=s; r=s; r->next=NULL; return h; } void xiugai(node *s){//修改订单 while(s->next!=NULL){ s=s->next; printf("名称:"); printf("%s\n",&s->data.name); printf("增加时间:"); printf("%s\n",&s->data.time); printf("手机尾号:"); printf("%s\n",&s->data.useful); printf("类型:"); printf("%s\n",&s->data.type); } }这段代码插入上一段中是否会存在问题
这段代码存在一些问题。具体问题如下:
1. 函数`modification`的定义中,应该在参数列表中指明参数的类型,例如`node *modification(node *h)`应该改为`node* modification(node* h)`,这样更符合C语言的函数定义规范。
2. 函数`modification`中,如果链表`h`为空,则应该先为其分配一个头结点,然后将新节点插入到头结点之后,代码中已经实现了这个逻辑。
3. 函数`modification`中,将新节点插入到链表的末尾,代码中已经实现了这个逻辑。
4. 函数`xiugai`中,应该将链表头节点的数据忽略掉,否则第一个节点的数据将没有被输出。
5. 函数`xiugai`中,输出节点数据时,应该使用`s->data.name`,`s->data.time`等成员变量,而不是`&s->data.name`,`&s->data.time`等成员变量的地址。
6. 函数`xiugai`中,输出节点数据的格式应该和输入时一致,例如名称应该用`%s`而不是`%lf`。
7. 函数`xiugai`中,输出节点数据时,应该先输出提示信息,再等待用户输入数据,代码中已经实现了这个逻辑。
综上所述,修改后的代码如下:
```
node* modification(node* h) {
node *s, *r;
if (h == NULL) {
h = malloc(sizeof(node));
h->next = NULL;
r = h;
} else {
r = h;
while (r->next != NULL)
r = r->next;
}
s = malloc(sizeof(node));
printf("名称:");
scanf("%s", s->data.name);
printf("增加时间:");
scanf("%lf", &s->data.time);
printf("类型:");
scanf("%d", &s->data.type);
r->next = s;
r = s;
r->next = NULL;
return h;
}
void xiugai(node* s) {
printf("修改订单:\n");
while (s->next != NULL) {
s = s->next;
printf("名称:%s\n", s->data.name);
printf("增加时间:%lf\n", s->data.time);
printf("类型:%d\n", s->data.type);
}
}
```
请帮我看看这一段代码为什么会卡在输出“新闻标题”然后程序直接结束了?void showNode(Link head) { //查看新闻 Link p = head->next; int choicecc; while (p != nullptr) { if (p == nullptr) { break; } cout << "新闻标题:" << p->data.name << "新闻号:" << p->data.num << " 新闻内容:" << p->data.n << endl; p = p->next; } printf("这是所有的新闻了,输入1返回"); scanf("%d", &choicecc); while(1) if (choicecc == 1) { admin4(); break; } else { printf("重新输入"); } }
这段代码会卡在输出“新闻标题”后面的原因是因为在while循环中,if语句的条件判断不正确,导致程序直接跳出了循环。具体来说,if语句的条件判断为“if (p == nullptr)”,但是在while循环中已经判断了“p != nullptr”,因此这个if语句永远不会成立,导致程序直接跳出循环,执行后面的代码。解决这个问题的方法是将if语句中的条件判断改为“if (p->next == nullptr)”即可。另外,最后一个while循环也存在问题,应该加上一个scanf语句,否则会出现死循环。修改后的代码如下:
void showNode(Link head) {
//查看新闻
Link p = head->next;
int choicecc;
while (p != nullptr)
{
cout << "新闻标题:" << p->data.name << "新闻号:" << p->data.num << " 新闻内容:" << p->data.n << endl;
p = p->next;
}
printf("这是所有的新闻了,输入1返回");
scanf("%d", &choicecc);
while(1) {
if (choicecc == 1) {
admin4();
break;
}
else {
printf("重新输入");
scanf("%d", &choicecc);
}
}
}
相关推荐
![pdf](https://img-home.csdnimg.cn/images/20210720083512.png)
![rar](https://img-home.csdnimg.cn/images/20210720083606.png)
![doc](https://img-home.csdnimg.cn/images/20210720083327.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)