| 网站首页 | 业界新闻 | 小组 | 威客 | 人才 | 下载频道 | 博客 | 代码贴 | 在线编程 | 编程论坛
欢迎加入我们,一同切磋技术
用户名:   
 
密 码:  
共有 1651 人关注过本帖
标题:[讨论]c语言提高练习题⒉
只看楼主 加入收藏
忆楠
Rank: 1
等 级:新手上路
帖 子:721
专家分:0
注 册:2004-7-5
收藏
 问题点数:0 回复次数:4 
[讨论]c语言提高练习题⒉
编程实现将用户从键盘上随机输入的30和学生的学号、姓名、数学成绩、计算机成绩及总分写入数据文件score中,假设30个学生的学号从1~30连续。输入时不必按学号顺序进行,程序自动按学号顺序将输入的数据写入文件。
搜索更多相关主题的帖子: c语言 练习题 数学 计算机 学号 
2004-07-24 09:06
忆楠
Rank: 1
等 级:新手上路
帖 子:721
专家分:0
注 册:2004-7-5
收藏
得分:0 
大家一起来做啊 !!!

点 鼠 标 , 救 饥 民 http://www./
2004-07-24 16:46
jellen
Rank: 1
等 级:新手上路
威 望:1
帖 子:107
专家分:0
注 册:2004-5-3
收藏
得分:0 

呵呵,终于通过运行了:) 说来惭愧,我学习C语言已经有两年了(断断续续,同时在看C++,Java),自己感觉学得还可以,所以认为这个题目应该很简单。结果昨天花了一晚上也没有搞定(因为很长时间没有使用C了,所以在指针理解上出了问题,一个链表总是不能正确连接,看来要真正掌握C语言的指针确实要花功夫。),今天早上再看了看才把程序做好: 我的程序由三个文件组成:list.h, list.c, main.c(在VC, Dev-C++中建立一个工程然后把文件加进去就可以运行了)

还有一个问题是在结构体中的学号(id),经过保存,读取这个过程之后结果会变不正确。(估计是个X86计算机的体系结构有关系,不知哪位大虾能解释一下。)

下面是源程序:

程序代码:
/* list.c */
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include \"list.h\"

Student *head = NULL; /* the head of the list */

void createList(void) { /* The first node doesn't contain useful data */ head = (Student *)malloc(sizeof(Student));

if(head) head->next = NULL; else { printf(\"Memory Access Failed!\n\"); exit(1); } }

void addNode(void) { Student *ptr, *pre; Student *temp = (Student *)malloc(sizeof(Student));

if(temp) { printf(\"\nStudent ID: \"); scanf(\"%d\", &temp->id); printf(\"Name: \"); scanf(\"%s\", temp->name); printf(\"Math. Score: \"); scanf(\"%d\", &temp->math); printf(\"Computer Score: \"); scanf(\"%d\", &temp->computer); temp->next = NULL; } else { printf(\"Memory Access Failed!\n\"); exit(1); }

pre = head; ptr = head->next; while(ptr) { /* find the right place to insert */ if(temp->id > ptr->id) { pre = ptr; ptr = ptr->next; } else break; }

if(ptr == NULL) /* insert at last */ pre->next = temp; else { /*insert in the middle */ pre->next = temp; temp->next = ptr; } }

void saveList(void) { FILE *fp; Student *ptr = head->next;

fp = fopen(\"score\", \"wb\"); if(fp == NULL) { printf(\"Can not open file!\n\"); exit(1); }

while(ptr) { if(fwrite(ptr, sizeof(Student), 1, fp) == 1) ptr = ptr->next; else { printf(\"File write error!\n\"); exit(1); } } fclose(fp); }

void readList(void) { FILE *fp; Student *ptr, *pre;

fp = fopen(\"score\", \"rb\"); if(fp == NULL) { printf(\"Can not open file!\n\"); exit(1); }

head = (Student *)malloc(sizeof(Student)); if(head == NULL) { printf(\"Memory Access failed!\n\"); exit(1); } else head->next = NULL;

pre = head; ptr = (Student *)malloc(sizeof(Student)); if(ptr == NULL) { printf(\"Memory Access Failed\n\"); exit(1); } else ptr->next = NULL;

while(fread(ptr, sizeof(Student), 1, fp) == 1) { pre->next = ptr; pre = ptr; ptr = (Student *)malloc(sizeof(Student)); if(ptr == NULL) { printf(\"Memory Access Failed!\n\"); exit(1); } else ptr->next = NULL; }

fclose(fp); }

void displayList(void) { Student *ptr = head->next;

printf(\"\n=================================\n\"); printf(\"==== The Students' Message ====\n\"); printf(\"=================================\n\"); while(ptr) { printf(\"\nStudnet ID: %d\n\", ptr->id); printf(\"Name: %s\n\", ptr->name); printf(\"Math. Score: %d\n\", ptr->math); printf(\"Computer Score: %d\n\", ptr->computer); ptr = ptr->next; } }

void clearList(void) { Student *pre = head; Student *ptr;

while(pre) { ptr = pre->next; free(pre); pre = ptr; } }

int isEmpty(void) { return (head == NULL) ? 1 : 0; }

[此贴子已经被作者于2004-07-25 12:13:13编辑过]


再见,理想!
2004-07-25 12:11
jellen
Rank: 1
等 级:新手上路
威 望:1
帖 子:107
专家分:0
注 册:2004-5-3
收藏
得分:0 

还有两个文件:

程序代码:
/* list.h */

/* This file contains the basic data structures and the fuction declarations that will be used in other files. */

/* The struct represents a student */ typedef struct node { int id; char name[20]; int math; int computer; struct node *next; } Student;

/* create the list */ void createList(void);

/* add a node to the list */ void addNode(void);

/* save the data to hard disk */ void saveList(void);

/* clear the list */ void clearList(void);

/* read the data from disk */ void readList(void);

/* display the list */ void displayList(void);

/* whether the list is empty */ int isEmpty(void);

程序代码:

/* main.c */ #include <stdio.h> #include \"list.h\"

int menu(void) { int choice;

printf(\"\n-----------MENU----------\n\"); printf(\"1 - Add a student's data\n\"); printf(\"2 - Save the data\n\"); printf(\"3 - Display the data\n\"); printf(\"4 - Quit\n\"); printf(\"Enter your choice: \"); scanf(\"%d\", &choice);

return choice; }

int main(void) { int choice;

createList(); choice = menu();

while(choice != 4) { switch(choice) { case 1: addNode(); break; case 2: saveList(); clearList(); break; case 3: if(isEmpty()) { readList(); displayList(); } else displayList(); break; case 4: break; default: printf(\"Wrong input!\n\"); break; } choice = menu(); }

if(!isEmpty()) clearList();

return 0; }

[此贴子已经被作者于2004-07-25 12:15:33编辑过]


再见,理想!
2004-07-25 12:14
忆楠
Rank: 1
等 级:新手上路
帖 子:721
专家分:0
注 册:2004-7-5
收藏
得分:0 

帮我看看这个程序有没有错误的地方 谢谢各位 !!!

#include <stdio.h>

main()

{ FILE *fp;

struct st

{ int number;

char name[20];

float math;

float computer;

float total;

} student;

int i,j;

if((fp=fopen("score","wb+"))==NULL)

{ printf("file open error\n");

exit(0);

}

for(i=0;i<30;i++)

{ scanf("%d,%20s,%f,%f",

&student.number,student.name,&student.math,&student.computer);

student.total=student.math+student.computer;

j=student.number-1;

fseek(fp,(long)j*sizeof(struct st),SEEK_SET);

if(fwrite(&student,sizeof(student),1,fp)!=1) printf("write file error\n");

}

fclose(fp);

}

[此贴子已经被作者于2004-07-26 17:36:19编辑过]


点 鼠 标 , 救 饥 民 http://www./
2004-07-26 17:28
快速回复:[讨论]c语言提高练习题⒉
数据加载中...
 
   



关于我们 | 广告合作 | 编程中国 | 清除Cookies | TOP | 手机版

编程中国 版权所有,并保留所有权利。
Powered by Discuz, Processed in 0.017428 second(s), 8 queries.
Copyright©2004-2024, BCCN.NET, All Rights Reserved