我正在研究我的学校项目表格数据结构,我遇到类型长的问题.我想设计一个像大学注册系统这样的小项目.首先,如果他想注册或退出,那么他就会选择.如果他想要注册,那么他将输入他的名字和gpa,根据他的gpa选择可用的专业,系统将自动生成一个id.
问题是id没有正确递增.
附加信息:我正在使用双向链表结构
#include<stdio.h>
#include<stdlib.h>
#include<conio.h>
#include<string.h>
struct student{
char name[30];
int GPA;
unsigned long ID;
char colloege[100];
student *next, *prev;
};
student *stu, *cur, *head, *tail;
void insertfront();
void displaylist(){
unsigned long id=214000000;
cur=head;
printf(" ");
if(cur==NULL)
printf(" ");
else
while(cur!=NULL){
printf("Name:%s\t",cur->name);
printf("ID:%d\t",cur->ID=id++);
printf("GPA:%d\t",cur->GPA);
printf("Colloege of:%s\t",cur->colloege);
printf("\n");
cur=cur->next;
}
}
void main(){
clrscr();
int x;
printf("\n\t\t\t\t******* King Faisal University *********\n");
while(x!=2){
printf("\n press 1 to insert your information ,press 2 to exit\n");
scanf("%d",&x);
insertfront();
displaylist();
}
stu=NULL;
cur=NULL;
head=NULL;
tail=NULL;
getch();
}
void insertfront(){
int x,c;
stu=(student*)malloc(sizeof(student));
fflush(stdin);
printf("Enter your name:\n");
scanf("%[^\n]%*c",stu->name);
printf("Enter your GPA:\n");
scanf("%ul",&stu->GPA);
printf("\n Available Colloeges\n");
if(stu->GPA>=85)
{
printf("1.Colloege of Medicine\n 2.Colloege of Engenering\n 3.Colloege of Computer Science\n 4.Colloege of Business\n 5.Colloege of Art\n");
printf("Enter the colloege number \n");
scanf("%d",&c);
switch(c){
case 1: strcpy(stu->colloege,"Medicien");break;
case 2: strcpy(stu->colloege,"Engenering");break;
case 3: strcpy(stu->colloege,"Computer Science");break;
case 4: strcpy(stu->colloege,"Business");break;
case 5: strcpy(stu->colloege,"Art");break;
}
}
else if(stu->GPA>=75)
{
printf("1.Colloege of Computer Science\n 2.Colloege of Business\n 3.Colloege of Art\n");
printf("Enter the colloege number\n");
scanf("%d",&c);
switch(c){
case 1: strcpy(stu->colloege,"Computer Science");break;
case 2: strcpy(stu->colloege,"Business");break;
case 3: strcpy(stu->colloege,"Art");break;
}
}
else
strcpy(stu->colloege,"Art");
if(head==NULL)
{
stu->next=NULL;
stu->prev=NULL;
head=stu;
tail=stu;
}
else
{
stu->next=head;
stu->prev=NULL;
head->prev=stu;
head=stu;
}
}
Run Code Online (Sandbox Code Playgroud)
您在程序中使用了错误的转换
printf("ID:%d\t",cur->ID=id++);
// cur->ID is of type unsigned long
// %d is used for values of type int
Run Code Online (Sandbox Code Playgroud)
要打印long您需要"%ld"在printf()转换中使用的类型值
long longvalue = 42;
printf("%ld\n", lonvgalue);
Run Code Online (Sandbox Code Playgroud)
要打印unsigned long您需要"%lu"在printf()转换中使用的类型值
unsigned long ulongvalue = 42;
printf("%lu\n", ulongvalue);
Run Code Online (Sandbox Code Playgroud)