帮朋友的朋友做的额。。<assignment5>

来源:互联网 发布:月度数据分析报告模板 编辑:程序博客网 时间:2024/04/30 15:59
Homework  Assignment  5 
Objectives: 
1. Define C Struct data type
2. Work with C pointers extensively
3. Use dynamic memory allocation
4. Work with file operations.
5. Implement sorted linked list data structure
6. Check function return code and handle error condition appropriately
7. Work with command line argument
Description: 
You are about to handle some bank account data. Each account has account number,
name and balance. Account numbers are integers, name has no space and up to 20
characters, and balance is double type. All the account information is stored in
a txt file, with each line represent an account. See below for the sample test
file balance.txt.
Your job is to write sort.c to sort the data from filename.txt by balance and
saved the data to a new file named sorted_filename.txt. See the sample
sorted_balance.txt below.
bash-3.2$ more balance.txt
1 Alice 100.05
2 Bob 50
3 Mike 200
4 Marry 1000
5 Keven 250
bash-3.2$ sort balance.txt
bash-3.2$ more sorted_balance.txt
4 Marry 1000.00
5 Keven 250.00
3 Mike 200.00
1 Alice 100.05
2 Bob 50.00
Requirements: 
1. Your  program  accepts  the  input  filename  as  argument  and  creates  the  output  file  using 
the  name  sorted_filename.  See  the  above  example,  the  input  filename  is  balance.txt,  the 
output  file  name  is  sorted_balance.txt. 
2. You  must  use  linked  list  data  structure  to  store  data  in  memory.  Static  array  is  not 
allowed.  
3. You  need  to  define  a  struct  type  to  store  each  bank  account  data,  i.e  name  and  balance.   
4. You  have  to  handle  error  checking  for  following  function: 
a. fopen() 
b. fscanf() 
Tips 
1. Start to work on it right away!!!
2. Review the related material in text book and slides first!!
3. If you need help, come to the instructor’s office hour, contact TA, or
stop by learning center.
How  to  write  README 
Create  a  text  file  named  README.  In  the  file,  include  
1. A  short  description  of  your  program 
2. The  instructions  of  how  to  compile  your  code  
3. Sample  output  of  running  your  program.  
Grading: 
!! You will get 0 point if your code does not compile regardless how well you
documented your code!!
1. (75 points) The grading of this homework will be very tough! You either
passed all test cases and get full points or get 0 point. There is no
partial credit.
Even your program passed the test, you may still loose point for the
following case:
a. (-5 points) if no struct type used for bank data.
b. (-30 points) if you use static array instead of linked list.
2. (10 points) You should check and handle all error conditions error
appropriately. In this homework, you need to at least check the following.
Otherwise you will loose points.
a. check for the right number of command arguments
b. check for the return code of fopen
c. check for the return code of fwrite
3. (10 points) Good programming style (variable names, space, indentation,
comments)
4. (5 points) README file
Submit: 
1. Submit to canvas the source code sort.c

2. Submit README


上面是原题,下面是代码额。用到链表的知识。。并且有单链表的排序额。。

#include <stdio.h>#include <stdlib.h>#include <string.h>#include <errno.h>extern int errno;// 下面是打印错误信息的宏#define PRN_ERRMSG(errmsg) \fprintf(stderr, "Say->%s : File->%s : Line->%d : Fun->%s : Cause->%s\n\r", \errmsg,__FILE__,__LINE__,__func__,strerror(errno))struct balance_t {int    num;           // 编号char   name[20];      // 名字double balance;       // 钱struct balance_t *next; // 下一节点指针}* balance_head = NULL; // 声明头节点指针/***  单链表的快速排序**  传入参数为要排序的链表头和尾的指针**  此处的头是没有实际数据的,如果要排序整个链表**  时tail为NULL*/void QuickSort( struct balance_t *head, struct balance_t *tail ){if ( head->next == tail || head->next->next == tail )return;struct balance_t * mid = head->next;struct balance_t * p = head;struct balance_t * q = mid;double pivot = mid->balance;struct balance_t * t = mid->next;while ( t != tail ) {if ( t->balance > pivot )p = p->next = t;elseq = q->next = t;t = t->next;}p->next = mid;q->next = tail;QuickSort( head, mid );QuickSort( mid, tail );}int main(int argc,char *argv[]){    FILE *fp;    if (2 != argc) { // 命令行参数个数必须正确printf("use <exe> <balance.txt>\n");return 0;}if (NULL == (fp = fopen(argv[1], "r"))) { // 读的方式打开文件PRN_ERRMSG("fopen");return 0;}if (NULL == (balance_head = (struct balance_t*)malloc(sizeof(struct balance_t)))) {PRN_ERRMSG("malloc"); // 申请头结点指针的内存空间return 0;}memset(balance_head, 0, sizeof(struct balance_t));// 因为头结点不保存实际数据,所以使其值无意义struct balance_t *p = balance_head, *q; // 创造链表时不要改变头结点的值while ( !feof(fp) ) { // 检测文件是否读完,读完则退出循环if (NULL == (q = (struct balance_t*)malloc(sizeof(struct balance_t)))) {PRN_ERRMSG("malloc");return 0;}p->next = q;q->next = NULL;p = q; // 上面三句话是让链表连接起来    if (0 > fscanf(fp, "%d %s %lf", &p->num, p->name, &p->balance)) {        PRN_ERRMSG("fscanf");; // fscanf 错误处理        return 0;    }}fclose(fp); // 文件读取完毕要关闭    QuickSort(balance_head, NULL); // 进入快速排序char outFileName[50];sprintf(outFileName, "sorted_%s", argv[1]); // 拼出输出文件的名称if (NULL == (fp = fopen(outFileName, "w"))) {PRN_ERRMSG("fopen");return 0;}p = balance_head->next; // 下面将链表内排好序的内容保存进文件while (NULL != p) {        fprintf(fp, "%d\t%s\t\t%.2lf\n", p->num, p->name, p->balance);        p = p->next;}fclose(fp);p = balance_head;while (NULL != p) { // 释放分配的内存    q = p;    p = p->next;    free(q);}return 0;}



0 0