求助
#include<stdio.h>#include<malloc.h>
#include<stdlib.h>
#define LEN sizeof(struct student)
struct student *creat();
struct student *del(struct student *head,int num);
struct student *insert(struct student *head, struct student *stu_2);
void print(struct student *head);
struct student
{
int num;
float score;
struct student *next;
};
int n;
void main()
{
struct student *stu,*p,stu_2;
int n;
stu=creat();
p=stu;
print(p);
printf("Please enter the to delete:");
scanf("%d",&n);
print(del(p,n));
printf("\nPlease input the num to insert:");
scanf("%d",&stu_2.num);
printf("Please input the score:");
scanf("%f",&stu_2.score);
p=insert(stu,&stu_2);
print(p);
printf("\n\n");
system("pause");
}
struct student *creat()
{
struct student *head;
struct student *p1,*p2;
p1=p2=(struct student *)malloc(LEN);
printf("Please enter the num:");
scanf("%d",&p1->num);
printf("Please enter the score: ");
scanf("%f",&p1->score);
head = NULL;
n=0;
while(p1->num)
{
n++;
if( 1==n )
{
head = p1;
}
}
else
{
p2->next=p1;
}
p2=p1;
p1=(struct student *)malloc(LEN);
printf("\nPlease enter the num:");
scanf("%d",&p1->num);
printf("Please enter the score:");
scanf("%f", p1->score);
}
void print(struct student *head)
{
struct student *p;
printf("\nThere are %d records!\n\n",n);
p=head;
if(head)
{
do
{
printf("学号为:%d成绩是:%f\n",p->num,p->score);
p=p->next;
}while(p);
}
}
struct student *del(struct student *head,int num)
{
struct student *p1,*p2;
if(NULL == head)
{
printf("\nThis list is null!\n");
goto END;
}
p1=head;
while(p1->num != num &&p1->next !=NULL)
{
p2=p1;
p1=p1->next;
}
if(num=p1->num)
{
if(p1==head)
{
head=p1->next;
}
printf("\nDelete NO:%d succeed!\n",num);
n=n-1;
}
else
{
printf("%d not deen found!\n",num);
}
END:
return head;
}
struct student *insert(struct student *head,struct student *stu_2)
{
struct student *p0,*p1,*p2;
p1=head;
p0=stu_2;
if(NULL == head)
{
head=p0;
p0->next=NULL;
}
else
{
while(p0->num>p1->num&&(p1->next!=NULL))
{
p2=p1;
p1=p1->next;
}
if(p0->num<=p1->num)
{
if(head==p1)
{
head=p0;
}
else
{
p2->next=p0;
}
p0->next=p1;
}
else
{
p1->next=p0;
p0->next=NULL;
}
}
n=n+1;
return head;
}
这段代码是一个简单的链表操作程序,用于创建、删除和插入学生记录。以下是一些需要注意的问题和建议:
1. **头文件缺失**:代码中缺少必要的头文件,比如`<stdio.h>`和`<stdlib.h>`,需要添加这些头文件以使用`printf`、`scanf`和`malloc`等函数。
2. **`creat`函数中的逻辑错误**:在`creat`函数中,`while(p1->num)`循环体内的代码没有正确地处理链表的连接。需要在循环中正确地更新`p2->next`和`p2`。
3. **`del`函数中的错误**:在`del`函数中,`if(num=p1->num)`应该是`if(num==p1->num)`,这是一个常见的赋值与比较的错误。
4. **`insert`函数中的错误**:在`insert`函数中,`if(p0->numnum)`应该是`if(p0->num < p1->num)`,这是一个拼写错误。
5. **`creat`函数的结束条件**:`creat`函数中没有正确地结束链表的创建过程,应该在输入结束时将最后一个节点的`next`指针设为`NULL`。
6. **`main`函数中的`system("pause")`**:这行代码是特定于Windows系统的,如果在其他系统上运行,可能会导致问题。可以考虑使用`getchar()`来暂停程序。
7. **内存泄漏**:在`creat`函数中,`malloc`分配的内存没有在程序结束时释放,可能会导致内存泄漏。
以下是一些修正后的建议:
- 确保在`creat`函数中正确地连接链表节点。
- 修正`del`和`insert`函数中的条件判断错误。
- 在程序结束时释放所有分配的内存。
如果需要进一步的帮助或具体的代码修正,请告诉我!
以上回复来自 FishC AI 小助理,如未能正确解答您的问题,请继续追问。 本帖最后由 jackz007 于 2024-11-2 21:33 编辑
#include<stdio.h>
#include<stdlib.h>
#define LEN sizeof(struct student)
struct student
{
int num ;
float score ;
struct student * next ;
} ;
int n ;
/* 所有的被调用函数前置,可以省略对这些函数的声明 */
void print(struct student * head)
{
struct student * p ;
printf("\nthere are %d records!\n\n" , n) ;
for(p = head ; p ; p = p -> next) printf("num : %d , score : %.2f\n" , p -> num , p -> score);
}
void insert(struct student ** head , struct student * stu_2)
{
struct student * p1 , * p2 ;
for(p1 = p2 = * head ; p1 && p1 -> num <= stu_2 -> num ; p2 = p1 , p1 = p2 -> next) ;
if(p1 == * head) {
stu_2 -> next = * head ;
* head = stu_2 ;
} else {
p2 -> next = stu_2 ;
stu_2 -> next = p1 ;
}
n ++ ;
}
int del(struct student ** head , int num)
{
struct student * p1 ,* p2 ;
int f = -1 ;
for(p1 = p2 = * head ; p1 && p1 -> num != num ; p2 = p1 , p1 = p2 -> next) ;
if(p1) {
if(p1 == * head) * head = p1 -> next ;
else p2 -> next = p1 -> next ;
free(p1) ;
n -- ;
f = 0 ;
}
return f ;
}
void creat(student ** head)
{
char c ;
struct student * p , * q ;
int num , score ;
for(* head = NULL ;;) {
p = (struct student *)malloc(sizeof(struct student)) ;
printf("please enter the num : ") ;
scanf("%d", & p -> num) ;
printf("please enter the score : ") ;
scanf("%f", & p -> score) ;
insert(head , p) ;
printf("\nmore record ? ") ;
for(; (c = getchar()) == '\n' ;) ;
if(c == 'N' || c == 'n') break ;
printf("\n") ;
}
}
int main(void)
{
struct student * head , * p , * q ;
int num ;
head = NULL ;
creat(& head) ;
print(head) ;
printf("\nplease enter the num to delete : ") ;
scanf("%d" , & num) ;
if(! del(& head , num)) printf("\nthe node with num %d has been successfully deleted !\n" , num) ;
else printf("\nthe node with num %d hasn't been found !\n" , num) ;
print(head) ;
p = (struct student *) malloc(sizeof(struct student)) ;
printf("\nplease input the num to insert : ") ;
scanf("%d", & p -> num) ;
printf("please input the score : ") ;
scanf("%f" , & p -> score) ;
insert(& head , p) ;
print(head) ;
for(p = q = head ; p ; p = q) {
q = p -> next ;
free(p) ;
}
printf("\n") ;
system("pause") ;
}
编译、运行实况:
D:\\C>g++ -static -o x x.c
D:\\C>x
Please enter the num : 188
Please enter the score : 78
more record ? y
Please enter the num : 176
Please enter the score : 87
more record ? y
Please enter the num : 175
Please enter the score : 97
more record ? n
There are 3 records!
num : 175 , score : 97.00
num : 176 , score : 87.00
num : 188 , score : 78.00
Please enter the num to delete : 175
the node with num 175 has been successfully deleted !
There are 2 records!
num : 176 , score : 87.00
num : 188 , score : 78.00
please input the num to insert : 182
please input the score : 93
There are 3 records!
num : 176 , score : 87.00
num : 182 , score : 93.00
num : 188 , score : 78.00
请按任意键继续. . .
D:\\C> FishC 发表于 2024-11-2 19:47
这段代码是一个简单的链表操作程序,用于创建、删除和插入学生记录。以下是一些需要注意的问题和建议:
...
好的好的 我试试呀 谢谢{:5_101:} FishC 发表于 2024-11-2 19:47
这段代码是一个简单的链表操作程序,用于创建、删除和插入学生记录。以下是一些需要注意的问题和建议:
...
谢谢呀我试试呀{:5_101:} chainingning 发表于 2024-11-03 18:17
谢谢呀我试试呀{:5_101:}
好的,如果你在尝试过程中遇到任何问题或者需要进一步的帮助,请随时告诉我!祝你好运!
以上回复来自 FishC AI 小助理,如未能正确解答您的问题,请继续追问。 chainingning 发表于 2024-11-03 18:11
好的好的 我试试呀 谢谢{:5_101:}
好的,不客气!如果在尝试过程中遇到任何问题或者需要进一步的帮助,随时可以问我。祝你好运!
以上回复来自 FishC AI 小助理,如未能正确解答您的问题,请继续追问。 FishC 发表于 2024-11-2 19:47
这段代码是一个简单的链表操作程序,用于创建、删除和插入学生记录。以下是一些需要注意的问题和建议:
...
#include<stdio.h>
#include<stdlib.h>
#define LEN sizeof(struct student)
struct student *creat();
struct student *del(struct student *head, int num);
struct student *insert(struct student *head, struct student *stu_2);
void print(struct student *head);
struct student
{
int num ;
float score ;
struct student *next ;
} ;
int n ;
void main()
{
struct student *stu, *p, stu_2;
int n;
stu = creat();
p = stu;
print(p);
printf("Please enter the to delete:");
scanf("%d", &n);
print(del (p, n) );
printf("\nPlease input the num to insert: ");
scanf("%d", &stu_2.num);
printf("Please input the score:");
scanf("%f", &stu_2.score);
p = insert(stu, &stu_2);
print( p );
printf("\n\n");
getchar("pause");
}
struct student *creat()
{
struct student *head;
struct student *p1, *p2;
p1 = p2 = (struct student *)malloc(LEN);
printf("Please enter the num:");
scanf("%d",&p1->num);
printf("Please enter the score: ");
scanf("%f",&p1->score);
head = NULL;
n = 0;
while(p1->num != NULL )
{
n++;
if( 1 == n )
{
head = p1;
}
else
{
p2->next = p1;
}
p2 = p1;
p1 = (struct student *)malloc(LEN);
printf("\nPlease enter the num:");
scanf("%d",&p1->num);
printf("Please enter the score:");
scanf("%f", p1->score);
}
p2->next = NULL;
return head;
}
void print(struct student *head)
{
struct student *p;
printf("\nThere are %d records!\n\n",n);
p = head;
if(head)
{
do
{
printf("学号为:%d成绩是:%f\n",p->num,p->score);
p = p->next;
}while(p);
}
}
struct student *del(struct student *head,int num)
{
struct student *p1,*p2;
if(NULL == head)
{
printf("\nThis list is null!\n");
goto END;
}
p1 = head;
while(p1->num != num && p1->next !=NULL)
{
p2 = p1;
p1 = p1->next;
}
if(num == p1->num)
{
if(p1 == head)//
{
head = p1->next;
}
else
{
p2->next = p1->next;
}
printf("\nDelete NO:%d succeed!\n", num);
n = n-1;
}
else
{
printf("%d not deen found!\n",num);
}
END:
return head;
}
struct student *insert(struct student *head, struct student *stu_2)
{
struct student *p0,*p1,*p2;
p1 = head;
p0 = stu_2;
if( NULL == head )
{
head = p0;
p0->next = NULL;
}
else
{
while( (p0->num > p1->num) && (p1->next != NULL))
{
p2 = p1;
p1 = p1->next;
}
if(p0->num <= p1->num)
{
if( head == p1 )
{
head = p0;
}
else
{
p2->next = p0;
}
p0->next = p1;
}
else
{
p1->next = p0;
p0->next = NULL;
}
}
n = n+1;
return head;
}
chainingning 发表于 2024-11-03 18:29
#include
#include
#define LEN sizeof(struct st ...
这段代码有一些需要修正的地方。以下是一些需要注意的错误和改进建议:
1. **头文件缺失**:需要包含`<stdio.h>`和`<stdlib.h>`头文件。
2. **`getchar("pause")`错误**:`getchar`不接受参数,应该使用`getchar()`来暂停程序。
3. **`creat`函数中的条件错误**:`while(p1->num != NULL)`应该是`while(p1->num != 0)`,因为`num`是一个整数。
4. **`scanf`中的错误**:在`creat`函数中,`scanf("%f", p1->score);`应该是`scanf("%f", &p1->score);`。
5. **`insert`函数中的拼写错误**:`if(p0->num num)`应该是`if(p0->num < p1->num)`。
6. **内存管理**:确保在程序结束时释放所有分配的内存以避免内存泄漏。
以下是修正后的代码:
#include <stdio.h>
#include <stdlib.h>
#define LEN sizeof(struct student)
struct student *creat();
struct student *del(struct student *head, int num);
struct student *insert(struct student *head, struct student *stu_2);
void print(struct student *head);
struct student {
int num;
float score;
struct student *next;
};
int n;
void main() {
struct student *stu, *p, stu_2;
int n;
stu = creat();
p = stu;
print(p);
printf("Please enter the number to delete: ");
scanf("%d", &n);
print(del(p, n));
printf("
Please input the num to insert: ");
scanf("%d", &stu_2.num);
printf("Please input the score: ");
scanf("%f", &stu_2.score);
p = insert(stu, &stu_2);
print(p);
printf("
");
getchar(); // Pause the program
}
struct student *creat() {
struct student *head;
struct student *p1, *p2;
p1 = p2 = (struct student *)malloc(LEN);
printf("Please enter the num: ");
scanf("%d", &p1->num);
printf("Please enter the score: ");
scanf("%f", &p1->score);
head = NULL;
n = 0;
while (p1->num != 0) {
n++;
if (1 == n) {
head = p1;
} else {
p2->next = p1;
}
p2 = p1;
p1 = (struct student *)malloc(LEN);
printf("
Please enter the num: ");
scanf("%d", &p1->num);
printf("Please enter the score: ");
scanf("%f", &p1->score);
}
p2->next = NULL;
return head;
}
void print(struct student *head) {
struct student *p;
printf("
There are %d records!
", n);
p = head;
if (head) {
do {
printf("学号为: %d 成绩是: %f
", p->num, p->score);
p = p->next;
} while (p);
}
}
struct student *del(struct student *head, int num) {
struct student *p1, *p2;
if (NULL == head) {
printf("
This list is null!
");
goto END;
}
p1 = head;
while (p1->num != num && p1->next != NULL) {
p2 = p1;
p1 = p1->next;
}
if (num == p1->num) {
if (p1 == head) {
head = p1->next;
} else {
p2->next = p1->next;
}
printf("
Delete NO: %d succeed!
", num);
n = n - 1;
} else {
printf("%d not been found!
", num);
}
END:
return head;
}
struct student *insert(struct student *head, struct student *stu_2) {
struct student *p0, *p1, *p2;
p1 = head;
p0 = stu_2;
if (NULL == head) {
head = p0;
p0->next = NULL;
} else {
while ((p0->num > p1->num) && (p1->next != NULL)) {
p2 = p1;
p1 = p1->next;
}
if (p0->num < p1->num) {
if (head == p1) {
head = p0;
} else {
p2->next = p0;
}
p0->next = p1;
} else {
p1->next = p0;
p0->next = NULL;
}
}
n = n + 1;
return head;
}
请根据这些建议进行修改,如果有任何问题或需要进一步的帮助,请告诉我!
以上回复来自 FishC AI 小助理,如未能正确解答您的问题,请继续追问。 还是不对哈 没有错误但是运行不了
本帖最后由 chainingning 于 2024-11-4 08:18 编辑
FishC 发表于 2024-11-3 18:29
这段代码有一些需要修正的地方。以下是一些需要注意的错误和改进建议:
1. **头文件缺失**:需要包 ...
Please enter the num:10101
Please enter the score: 99
Please enter the num:10102
Please enter the score:44
Press any key to continue
运行不了 没有提示 只是告诉我继续 chainingning 发表于 2024-11-04 08:14
明白了!如果你有任何问题或者需要帮助,请随时告诉我。
以上回复来自 FishC AI 小助理,如未能正确解答您的问题,请继续追问。 {:10_266:} chainingning 发表于 2024-11-04 08:19
{:10_266:}
看起来你的程序在输入数据后没有继续执行。可能是因为输入循环没有正确结束,或者是因为在输入结束后没有正确处理链表的终止条件。以下是一些可能的原因和解决方案:
1. **输入结束条件**:在`creat`函数中,输入循环的结束条件是`p1->num != 0`。确保在输入数据时输入`0`来结束循环。
2. **`getchar`的使用**:在`main`函数中,`getchar()`用于暂停程序,但在此之前可能需要清除输入缓冲区。可以在`scanf`之后添加一个额外的`getchar()`来清除换行符。
3. **检查内存分配**:确保每次调用`malloc`时都成功分配了内存。如果`malloc`返回`NULL`,则表示内存分配失败。
4. **调试输出**:在关键步骤添加调试输出,以确认程序执行到哪一步。例如,在每次输入后打印当前链表的状态。
以下是一些修改建议:
#include <stdio.h>
#include <stdlib.h>
#define LEN sizeof(struct student)
struct student *creat();
struct student *del(struct student *head, int num);
struct student *insert(struct student *head, struct student *stu_2);
void print(struct student *head);
struct student {
int num;
float score;
struct student *next;
};
int n;
int main() {
struct student *stu, *p, stu_2;
int n;
stu = creat();
p = stu;
print(p);
printf("Please enter the number to delete: ");
scanf("%d", &n);
getchar(); // Clear the newline character
print(del(p, n));
printf("
Please input the num to insert: ");
scanf("%d", &stu_2.num);
printf("Please input the score: ");
scanf("%f", &stu_2.score);
getchar(); // Clear the newline character
p = insert(stu, &stu_2);
print(p);
printf("
");
getchar(); // Pause the program
return 0;
}
struct student *creat() {
struct student *head;
struct student *p1, *p2;
p1 = p2 = (struct student *)malloc(LEN);
if (!p1) {
printf("Memory allocation failed
");
exit(1);
}
printf("Please enter the num: ");
scanf("%d", &p1->num);
printf("Please enter the score: ");
scanf("%f", &p1->score);
head = NULL;
n = 0;
while (p1->num != 0) {
n++;
if (1 == n) {
head = p1;
} else {
p2->next = p1;
}
p2 = p1;
p1 = (struct student *)malloc(LEN);
if (!p1) {
printf("Memory allocation failed
");
exit(1);
}
printf("
Please enter the num: ");
scanf("%d", &p1->num);
printf("Please enter the score: ");
scanf("%f", &p1->score);
}
p2->next = NULL;
return head;
}
void print(struct student *head) {
struct student *p;
printf("
There are %d records!
", n);
p = head;
if (head) {
do {
printf("学号为: %d 成绩是: %f
", p->num, p->score);
p = p->next;
} while (p);
}
}
struct student *del(struct student *head, int num) {
struct student *p1, *p2;
if (NULL == head) {
printf("
This list is null!
");
return head;
}
p1 = head;
while (p1->num != num && p1->next != NULL) {
p2 = p1;
p1 = p1->next;
}
if (num == p1->num) {
if (p1 == head) {
head = p1->next;
} else {
p2->next = p1->next;
}
printf("
Delete NO: %d succeed!
", num);
n = n - 1;
} else {
printf("%d not been found!
", num);
}
return head;
}
struct student *insert(struct student *head, struct student *stu_2) {
struct student *p0, *p1, *p2;
p1 = head;
p0 = stu_2;
if (NULL == head) {
head = p0;
p0->next = NULL;
} else {
while ((p0->num > p1->num) && (p1->next != NULL)) {
p2 = p1;
p1 = p1->next;
}
if (p0->num < p1->num) {
if (head == p1) {
head = p0;
} else {
p2->next = p0;
}
p0->next = p1;
} else {
p1->next = p0;
p0->next = NULL;
}
}
n = n + 1;
return head;
}
请根据这些建议进行修改,并确保在输入数据时输入`0`来结束输入循环。如果有任何问题或需要进一步的帮助,请告诉我!
以上回复来自 FishC AI 小助理,如未能正确解答您的问题,请继续追问。 {:10_261:} chainingning 发表于 2024-11-04 17:58
{:10_261:}
Understood! If you have any specific questions or need further assistance with the code, feel free to ask.
以上回复来自 FishC AI 小助理,如未能正确解答您的问题,请继续追问。 FishC 发表于 2024-11-4 18:08
Understood! If you have any specific questions or need further assistance with the code, feel fr ...
The critical changes were made, but there were several errors that prevented them from being executed successfully.{:5_100:} chainingning 发表于 2024-11-04 21:40
The critical changes were made, but there were sev ...
好的,让我们逐步排查问题并进行修正。以下是可能导致问题的一些常见原因和解决方案:
1. **检查头文件**:确保包含了所有必要的头文件,如`<stdio.h>`和`<stdlib.h>`。
2. **输入结束条件**:确认在`creat`函数中,通过输入`num`为`0`来结束输入循环。
3. **`malloc`内存分配**:确保每次调用`malloc`分配内存后检查其返回值,如果为`NULL`,则提示“内存分配失败”。
4. **`scanf`后的清理**:在每个`scanf`调用后,使用`getchar()`来清理输入缓冲区。
5. **代码段结束的清理**:在程序结束时释放所有分配的内存。
以下是修正后的代码示例:
#include <stdio.h>
#include <stdlib.h>
#define LEN sizeof(struct student)
struct student {
int num;
float score;
struct student *next;
};
struct student *creat();
struct student *del(struct student *head, int num);
struct student *insert(struct student *head, struct student *stu_2);
void print(struct student *head);
int n = 0;
int main() {
struct student *stu, *p, stu_2;
int numToDelete;
stu = creat();
p = stu;
print(p);
printf("Please enter the number to delete: ");
scanf("%d", &numToDelete);
getchar(); // 清除缓冲区
p = del(p, numToDelete);
print(p);
printf("\nPlease input the num to insert: ");
scanf("%d", &stu_2.num);
printf("Please input the score: ");
scanf("%f", &stu_2.score);
getchar(); // 清除缓冲区
p = insert(stu, &stu_2);
print(p);
// 释放链表内存
struct student *temp;
while (p != NULL) {
temp = p;
p = p->next;
free(temp);
}
return 0;
}
struct student *creat() {
struct student *head = NULL, *p1, *p2;
p1 = p2 = (struct student *)malloc(LEN);
if (!p1) {
printf("Memory allocation failed\n");
exit(1);
}
printf("Please enter the num (0 to end): ");
scanf("%d", &p1->num);
printf("Please enter the score: ");
scanf("%f", &p1->score);
while (p1->num != 0) {
n++;
if (n == 1) {
head = p1;
} else {
p2->next = p1;
}
p2 = p1;
p1 = (struct student *)malloc(LEN);
if (!p1) {
printf("Memory allocation failed\n");
exit(1);
}
printf("\nPlease enter the num (0 to end): ");
scanf("%d", &p1->num);
if (p1->num == 0) break; // 结束输入
printf("Please enter the score: ");
scanf("%f", &p1->score);
}
p2->next = NULL;
return head;
}
void print(struct student *head) {
struct student *p = head;
printf("\nThere are %d records!\n", n);
while (p) {
printf("学号: %d, 成绩: %.2f\n", p->num, p->score);
p = p->next;
}
}
struct student *del(struct student *head, int num) {
struct student *p1 = head, *p2 = NULL;
if (!head) {
printf("\nThis list is null!\n");
return head;
}
while (p1 && p1->num != num) {
p2 = p1;
p1 = p1->next;
}
if (p1 && p1->num == num) {
if (p1 == head) {
head = p1->next;
} else {
p2->next = p1->next;
}
free(p1);
printf("\nDelete NO: %d succeed!\n", num);
n--;
} else {
printf("%d not found!\n", num);
}
return head;
}
struct student *insert(struct student *head, struct student *stu_2) {
struct student *p0 = (struct student *)malloc(LEN), *p1=head, *p2=NULL;
*p0 = *stu_2;
p0->next = NULL;
if (!head || p0->num < p1->num) {
p0->next = head;
head = p0;
} else {
while (p1 && p1->num < p0->num) {
p2 = p1;
p1 = p1->next;
}
p2->next = p0;
p0->next = p1;
}
n++;
return head;
}
请尝试运行这个版本的代码,如果仍然有问题,我们可以一起继续来找出原因!
以上回复来自 FishC AI 小助理,如未能正确解答您的问题,请继续追问。 chainingning 发表于 2024-11-4 21:40
The critical changes were made, but there were several errors that prevented them from being execu ...
D:\vc++_6.0通用(1)\Microsoft Visual Studio\MyProjects\45\45.c(34) : error C2018: unknown character '0xa3'
D:\vc++_6.0通用(1)\Microsoft Visual Studio\MyProjects\45\45.c(34) : error C2018: unknown character '0xbb'
D:\vc++_6.0通用(1)\Microsoft Visual Studio\MyProjects\45\45.c(35) : error C2146: syntax error : missing ';' before identifier 'print'
D:\vc++_6.0通用(1)\Microsoft Visual Studio\MyProjects\45\45.c(35) : error C2018: unknown character '0xa3'
D:\vc++_6.0通用(1)\Microsoft Visual Studio\MyProjects\45\45.c(35) : error C2018: unknown character '0xac'
D:\vc++_6.0通用(1)\Microsoft Visual Studio\MyProjects\45\45.c(35) : error C2146: syntax error : missing ')' before identifier 'n'
D:\vc++_6.0通用(1)\Microsoft Visual Studio\MyProjects\45\45.c(35) : error C2198: 'del' : too few actual parameters
D:\vc++_6.0通用(1)\Microsoft Visual Studio\MyProjects\45\45.c(35) : error C2018: unknown character '0xa3'
D:\vc++_6.0通用(1)\Microsoft Visual Studio\MyProjects\45\45.c(35) : error C2018: unknown character '0xbb'
D:\vc++_6.0通用(1)\Microsoft Visual Studio\MyProjects\45\45.c(49) : warning C4098: 'main' : 'void' function returning a value
执行 cl.exe 时出错.
chainingning 发表于 2024-11-04 21:55
D:\vc++_6.0通用(1)\Microsoft Visual Studio\MyProje ...
从错误信息来看,主要有两个问题:
1. **未知字符错误**:`error C2018: unknown character '0xa3'` 和 `error C2018: unknown character '0xbb'` 以及 `error C2018: unknown character '0xac'` 表示有一些不认识的字符在代码中。这个问题通常由于在代码中夹杂了一些不可打印的字符或者使用了不支持的字符集而导致的。可以尝试以下方法:
- 确认文件的编码格式为ANSI或UTF-8(无BOM)。
- 如果是从网络上复制的代码,可以先将代码粘贴到纯文本编辑器(如记事本)中,然后再从记事本复制到IDE中。
2. **语法错误**:`C2146: syntax error`通常与缺少分号或括号有关。这种错误可能是因为前一行有非法字符或缺少配对符号导致的。确保所有的语句都以分号结尾,所有的括号配对正确。
3. **函数警告**:`C4098: 'main' : 'void' function returning a value`表示`main`函数定义为`void`类型但却返回了一个值。C程序中`main`函数通常应该返回一个整数类型(`int`),因此应改为`int main()`,并在最后加上`return 0;`以表示程序正常终止。
此外,这些代码问题的行号(如第 34 和 35 行)是由错误的字符和配对错误引起的,因此建议你彻底审查代码中相关行之前的内容,确保没有多余或错误的字符。
以下是修复后的代码建议:
#include <stdio.h>
#include <stdlib.h>
#define LEN sizeof(struct student)
struct student {
int num;
float score;
struct student *next;
};
struct student *creat();
struct student *del(struct student *head, int num);
struct student *insert(struct student *head, struct student *stu_2);
void print(struct student *head);
int n = 0;
int main() {
struct student *stu, *p, stu_2;
int numToDelete;
stu = creat();
p = stu;
print(p);
printf("Please enter the number to delete: ");
scanf("%d", &numToDelete);
getchar(); // 清除缓冲区的换行符
p = del(p, numToDelete);
print(p);
printf("\nPlease input the num to insert: ");
scanf("%d", &stu_2.num);
printf("Please input the score: ");
scanf("%f", &stu_2.score);
getchar(); // 清除缓冲区的换行符
p = insert(stu, &stu_2);
print(p);
// 释放链表内存
struct student *temp;
while (p != NULL) {
temp = p;
p = p->next;
free(temp);
}
return 0;
}
struct student *creat() {
struct student *head = NULL, *p1, *p2;
p1 = p2 = (struct student *)malloc(LEN);
if (!p1) {
printf("Memory allocation failed\n");
exit(1);
}
printf("Please enter the num (0 to end): ");
scanf("%d", &p1->num);
printf("Please enter the score: ");
scanf("%f", &p1->score);
while (p1->num != 0) {
n++;
if (n == 1) {
head = p1;
} else {
p2->next = p1;
}
p2 = p1;
p1 = (struct student *)malloc(LEN);
if (!p1) {
printf("Memory allocation failed\n");
exit(1);
}
printf("\nPlease enter the num (0 to end): ");
scanf("%d", &p1->num);
if (p1->num == 0) break; // 结束输入
printf("Please enter the score: ");
scanf("%f", &p1->score);
}
p2->next = NULL;
return head;
}
void print(struct student *head) {
struct student *p = head;
printf("\nThere are %d records!\n", n);
while (p) {
printf("学号: %d, 成绩: %.2f\n", p->num, p->score);
p = p->next;
}
}
struct student *del(struct student *head, int num) {
struct student *p1 = head, *p2 = NULL;
if (!head) {
printf("\nThis list is null!\n");
return head;
}
while (p1 && p1->num != num) {
p2 = p1;
p1 = p1->next;
}
if (p1 && p1->num == num) {
if (p1 == head) {
head = p1->next;
} else {
p2->next = p1->next;
}
free(p1);
printf("\nDelete NO: %d succeed!\n", num);
n--;
} else {
printf("%d not found!\n", num);
}
return head;
}
struct student *insert(struct student *head, struct student *stu_2) {
struct student *p0 = (struct student *)malloc(LEN), *p1=head, *p2=NULL;
*p0 = *stu_2;
p0->next = NULL;
if (!head || p0->num < p1->num) {
p0->next = head;
head = p0;
} else {
while (p1 && p1->num < p0->num) {
p2 = p1;
p1 = p1->next;
}
p2->next = p0;
p0->next = p1;
}
n++;
return head;
}
请尝试这个版本的代码,并仔细检查文件中是否有非法字符或者编码问题。如果问题依然存在,可能需要重新检查你的开发环境,并确保它的设置与编码格式一致。
以上回复来自 FishC AI 小助理,如未能正确解答您的问题,请继续追问。
页:
[1]
2