为什么我的程序在保存为.CPP能运行,保存为.C不能运行?? 谢谢大家啦·

/*为什么我的程序在保存为.CPP能运行,保存为.C不能运行??*/#include"stdio.h"#include"stdlib.h"#include"ctype.h... /*为什么我的程序在保存为.CPP能运行,保存为.C不能运行?? */
#include"stdio.h"
#include"stdlib.h"
#include"ctype.h"
#include"string.h"
typedef struct Student
{
char num[10];
char name[20];
double score;
struct Student *next;
struct Student *prior;
}StudentList;

StudentList *head= (StudentList*)malloc(sizeof(StudentList));
StudentList *tail= (StudentList*)malloc(sizeof(StudentList));
void Display(StudentList **head,int n) /*显示所有记录*/
{
printf("-----------------------------------\n"); /*格式头*/
printf("学号 姓名 成绩\n");
printf("-----------------------------------\n");
StudentList * current=*head;
for(;current!=NULL&¤t->score>0;current=current->next) /*循环输入*/
{
printf("%-16s%-15s%lf\n",current->num,current->name,current->score);
}
printf("");
}
int AddtoList(StudentList **head,StudentList **tail,StudentList* newNode)
{
StudentList *current=*tail;
if(current->score<0||current==NULL)
{
*head=*tail=newNode;
}
else
{
newNode->prior=current;
current->next=newNode;
*tail=newNode;
}
(*tail)->next=NULL;
return 1;
}
int Insert_a_record(int n)
{
int i=0;
char sign,x[10];

while(sign!='n'&&sign!='N')
{
StudentList* stu = (StudentList*)malloc(sizeof(StudentList));
printf("学生学号:");
scanf("%s",stu->num);
printf("学生姓名:");
scanf("%s",stu->name);
printf("学生成绩:");
scanf("%lf",&stu->score);
AddtoList(&head,&tail,stu);
gets(x);
printf("是否增加更多?(Y/N)");
scanf("%c",&sign);
i++;
}
return(n+i);
}
void main()
{
int n=0;
int opt=0;
for(;;)
{
int c=0;
printf("\n\n");
printf("1. 增加一条记录\n");
printf("2. 显示\n");
printf("0. 退出\n");
printf("请选择):");
scanf("%d",&c);
switch(c)
{
case 1:
printf("增加新记录\n");
n=Insert_a_record(n);
break;
case 2:
printf("增加新记录\n");
Display(&head, n) ;
break;
case 0:
exit(0);
}
}
}

程序保留为.cpp就好使,但是保留为.c 时就出现如下错误
: error C2099: initializer is not a constant
: error C2099: initializer is not a constant
: error C2275: 'StudentList' : illegal use of this type as an expression
: see declaration of 'StudentList'
: error C2065: 'current' : undeclared identifier
:warning C4047: '!=' : 'int ' differs in levels of indirection from 'void *'
: error C2223: left of '->score' must point to struct/union
: error C2223: left of '->next' must point to struct/union
: error C2223: left of '->num' must point to struct/union
: error C2223: left of '->name' must point to struct/union
: error C2223: left of '->score' must point to struct/union
展开
 我来答
logiciel
推荐于2016-03-07 · TA获得超过140个赞
知道小有建树答主
回答量:223
采纳率:0%
帮助的人:377万
展开全部
C不能象C++那样自由地声明变量。修改如下:

#include"stdio.h"
#include"stdlib.h"
#include"ctype.h"
#include"string.h"
typedef struct Student
{
char num[10];
char name[20];
double score;
struct Student *next;
struct Student *prior;
}StudentList;

/*以下移到main中。
StudentList *head= (StudentList*)malloc(sizeof(StudentList));
StudentList *tail= (StudentList*)malloc(sizeof(StudentList));
*/
void Display(StudentList **head,int n) /*显示所有记录*/
{
StudentList * current=*head;
printf("-----------------------------------\n"); /*格式头*/
printf("学号 姓名 成绩\n");
printf("-----------------------------------\n");
//移上 StudentList * current=*head;
for(;current!=NULL&¤t->score>0;current=current->next) /*循环输入*/
{
printf("%-16s%-15s%lf\n",current->num,current->name,current->score);
}
printf("");
}
int AddtoList(StudentList **head,StudentList **tail,StudentList* newNode)
{
StudentList *current=*tail;
if(current->score<0||current==NULL)
{
*head=*tail=newNode;
}
else
{
newNode->prior=current;
current->next=newNode;
*tail=newNode;
}
(*tail)->next=NULL;
return 1;
}

int Insert_a_record(StudentList **head,StudentList **tail, int n) //int Insert_a_record(int n)
{
int i=0;
char sign,x[10];

while(sign!='n'&&sign!='N')
{
StudentList* stu = (StudentList*)malloc(sizeof(StudentList));
printf("学生学号:");
scanf("%s",stu->num);
printf("学生姓名:");
scanf("%s",stu->name);
printf("学生成绩:");
scanf("%lf",&stu->score);
AddtoList(head,tail,stu); //AddtoList(&head,&tail,stu);
gets(x);
printf("是否增加更多?(Y/N)");
scanf("%c",&sign);
i++;
}
return(n+i);
}
void main()
{
int n=0;
int opt=0;

StudentList *head= (StudentList*)malloc(sizeof(StudentList));
StudentList *tail= (StudentList*)malloc(sizeof(StudentList));

for(;;)
{
int c=0;
printf("\n\n");
printf("1. 增加一条记录\n");
printf("2. 显示\n");
printf("0. 退出\n");
printf("请选择):");
scanf("%d",&c);
switch(c)
{
case 1:
printf("增加新记录\n");
n=Insert_a_record(&head,&tail,n); //n=Insert_a_record(n);
break;
case 2:
printf("增加新记录\n");
Display(&head, n) ;
break;
case 0:
exit(0);
}
}
}
本回答被提问者采纳
已赞过 已踩过<
你对这个回答的评价是?
评论 收起
推荐律师服务: 若未解决您的问题,请您详细描述您的问题,通过百度律临进行免费专业咨询

为你推荐:

下载百度知道APP,抢鲜体验
使用百度知道APP,立即抢鲜体验。你的手机镜头里或许有别人想知道的答案。
扫描二维码下载
×

类别

我们会通过消息、邮箱等方式尽快将举报结果通知您。

说明

0/200

提交
取消

辅 助

模 式