打印类型长

时间:2015-11-25 09:55:33

标签: c algorithm

我正在研究我的学校项目表单数据结构,我遇到类型为long的问题。 我想设计一个像大学注册系统这样的小项目。 首先,如果他想注册或退出,那么他就会选择。 如果他想注册,那么他将输入他的名字和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;
    }
}

2 个答案:

答案 0 :(得分:5)

您在程序中使用了错误的转换

printf("ID:%d\t",cur->ID=id++);
// cur->ID is of type unsigned long
// %d is used for values of type int

要打印long类型的值,您需要在"%ld"转化中使用printf()

long longvalue = 42;
printf("%ld\n", lonvgalue);

要打印unsigned long类型的值,您需要在"%lu"转化中使用printf()

unsigned long ulongvalue = 42;
printf("%lu\n", ulongvalue);

答案 1 :(得分:1)

除格式说明符错误外,您在分配ID时遇到问题:s。

每次显示列表时,您的代码都会分配新ID:

创建记录时,我只会为每个学生记录生成一次ID。

请在insertfront中添加:

stu->ID=id++;

并将displaylist中的打印更改为:

printf("ID:%lu\t",cur->ID);

并将id的声明移动到全局范围(在任何函数之外)或作为insertfront内的静态变量:

static unsigned long id=214000000;