|
马上注册,结交更多好友,享用更多功能^_^
您需要 登录 才可以下载或查看,没有账号?立即注册
x
#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. **未知字符错误**:`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 小助理,如未能正确解答您的问题,请继续追问。
|
|