帮朋友的朋友做的额。。<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
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
- 帮朋友的朋友做的额。。<assignment5>
- 帮朋友的朋友做的作业题额。。就当练习吧。。<assignment4>
- 帮朋友发的
- PSobj [11]帮朋友做的建筑作品集封面
- 做我的朋友
- 做朋友的原则
- 帮朋友写的一个VBA
- 帮我顶顶,朋友的,谢谢
- 帮朋友收集世界杯数据的感触
- 学会做自己的朋友
- 微波炉模拟应用程序(帮朋友的同学做的一个java选修课大作业)
- 想找个会CGI的朋友在北京兼职帮我做些事情
- 想找个会CGI的朋友在北京兼职帮我做些事情 c315@qq.com
- 挑战未知 | 帮朋友做小插件
- toj-2469-朋友的朋友是朋友
- 帮朋友Every写的消费者的例子
- 帮朋友物色 Web前端设计高手,有兴趣的朋友请联系我!
- 箭牌卫浴(帮朋友发的广告贴)
- oracle忘记密码的步骤
- “八项规定”哪类违纪处理最多?
- ubuntu下安装和卸载软件的方法
- 第十六周项目四 为动态数组扩容
- nginx用php上传大文件的配置
- 帮朋友的朋友做的额。。<assignment5>
- c++第一天笔记(上)
- 引用NDK自带例子
- 【hackerrank】字符串题目
- 黑马程序员--c语言:字符串、指针
- 计算机书籍推荐
- spring四种依赖注入方式
- hibernate反向工程报错:hibernate getting database metadata
- 程序员之路