谭浩强C程序设计第四版第九章课后答案

您所在的位置:网站首页 c程序设计基础课后答案第四章 谭浩强C程序设计第四版第九章课后答案

谭浩强C程序设计第四版第九章课后答案

2024-07-06 04:39| 来源: 网络整理| 查看: 265

1.计算天数

#include typedef struct date{ int year; int month; int day; }date; int main() { date day1; printf("输入年月日:"); scanf("%d%d%d",&day1.year,&day1.month,&day1.day); int count = 0; int month2 = (day1.year % 4 && !(day1.year % 100) || day1.year % 400) ? 29 : 28; switch(day1.month) { case 1: count = day1.day; break; case 2: count = day1.day + 31; break; case 3: count = day1.day + 31 + month2; break; case 4: count = day1.day + 31 + month2 + 31; break; case 5: count = day1.day + 31 + month2 + 31 + 30; break; case 6: count = day1.day + 31 + month2 + 31 + 30 + 31; break; case 7: count = day1.day + 31 + month2 + 31 + 30 + 31 + 30; break; case 8: count = day1.day + 31 + month2 + 31 + 30 + 31 + 30 + 31; break; case 9: count = day1.day + 31 + month2 + 31 + 30 + 31 + 30 + 31 + 31; break; case 10: count = day1.day + 31 + month2 + 31 + 30 + 31 + 30 + 31 + 31 + 30 ; break; case 11: count = day1.day + 31 + month2 + 31 + 30 + 31 + 30 + 31 + 31 + 30 + 31; break; case 12: count = day1.day + 31 + month2 + 31 + 30 + 31 + 30 + 31 + 31 + 30 + 31 + 30; break; } printf(" %d 月 %d 日是 %d 年的第 %d 天。\n",day1.month,day1.day,day1.year,count); return 0; }

2.函数实现上题

#include typedef struct date{ int year; int month; int day; }date; void fuction(date day1) { int count = 0; int month2 = (day1.year % 4 && !(day1.year % 100) || day1.year % 400) ? 29 : 28; switch(day1.month) { case 1: count = day1.day; break; case 2: count = day1.day + 31; break; case 3: count = day1.day + 31 + month2; break; case 4: count = day1.day + 31 + month2 + 31; break; case 5: count = day1.day + 31 + month2 + 31 + 30; break; case 6: count = day1.day + 31 + month2 + 31 + 30 + 31; break; case 7: count = day1.day + 31 + month2 + 31 + 30 + 31 + 30; break; case 8: count = day1.day + 31 + month2 + 31 + 30 + 31 + 30 + 31; break; case 9: count = day1.day + 31 + month2 + 31 + 30 + 31 + 30 + 31 + 31; break; case 10: count = day1.day + 31 + month2 + 31 + 30 + 31 + 30 + 31 + 31 + 30 ; break; case 11: count = day1.day + 31 + month2 + 31 + 30 + 31 + 30 + 31 + 31 + 30 + 31; break; case 12: count = day1.day + 31 + month2 + 31 + 30 + 31 + 30 + 31 + 31 + 30 + 31 + 30; break; } printf(" %d 月 %d 日是 %d 年的第 %d 天。\n",day1.month,day1.day,day1.year,count); } int main() { date day1; printf("输入年月日:"); scanf("%d%d%d",&day1.year,&day1.month,&day1.day); fuction(day1); return 0; }

3.打印函数

#include struct student{ int num; char name[3]; float score; }stu[3]; int main() { printf("input the students' num name score:"); for ( int i = 0;i next->next; n--; i = 0; } i++; pt = pt->next; } printf("最后剩下%d\n",pt->num); }

7.链表删除

#include #include #define LEN sizeof(struct student) typedef struct student{ int num; float score; struct student *next; }stu; int n; struct student *creat() { struct student *head; struct student *p1,*p2; n = 0; p1 = p2 = (struct student *)malloc( LEN ); scanf("%ld,%f",&p1->num,&p1->score); head = NULL; while( p1->num != 0) { n = n + 1; if ( n == 1) { head = p1; } else p2->next = p1; p2 = p1; p1 = (struct student *)malloc( LEN ); scanf("%ld,%f",&p1->num,&p1->score); } p2->next = NULL; return head; } stu *del(struct student *head,int num1) { stu *p = head;int flag = 0; stu *p1; while(p->num !=num1 && p->next!=NULL)//循环查找要删除的节点 { p1 = p; p = p->next; } if ( p == head ) { printf("删除成功!\n:"); head = head->next; free(p); } else { p1->next = p->next; free(p); } return head; } void print(struct student *head) { struct student *p; printf("\nNow ,These %d recordss are:\n",n); p = head; while( p != NULL) { printf("%ld %5.1f\n",p->num,p->score); p = p->next; } } int main() { struct student *p1; p1 = creat(); p1 = del(p1,1001); print(p1); return 0; }

8.链表插入操作

#include #include #define LEN sizeof(struct student) typedef struct student{ int num; float score; struct student *next; }stu; int n; struct student *creat() { struct student *head; struct student *p1,*p2; n = 0; p1 = p2 = (struct student *)malloc( LEN ); scanf("%ld,%f",&p1->num,&p1->score); head = NULL; while( p1->num != 0) { n = n + 1; if ( n == 1) { head = p1; } else p2->next = p1; p2 = p1; p1 = (struct student *)malloc( LEN ); scanf("%ld,%f",&p1->num,&p1->score); } p2->next = NULL; return head; } stu *del(struct student *head,int num1) { stu *p = head;int flag = 0; stu *p1; while(p->num !=num1 && p->next!=NULL)//循环查找要删除的节点 { p1 = p; p = p->next; } if ( p == head ) { printf("删除成功!\n:"); head = head->next; free(p); } else { p1->next = p->next; free(p); } return head; } stu *insert(stu *head,int m,stu *p) { stu *q = head; if (m == 0) { p->next = head; head = p; } else { for (int i = 0;i next); p->next = q->next; q->next = p; } return head; } void print(struct student *head) { struct student *p; printf("\nNow ,These %d recordss are:\n",n); p = head; while( p != NULL) { printf("%ld %5.1f\n",p->num,p->score); p = p->next; } } int main() { struct student *p1; p1 = creat(); p1 = del(p1,1001); print(p1); int m; printf("输入需要插入的学生信息"); stu p; scanf("%ld,%f",&p.num,&p.score); printf("输入需要插入的位置"); scanf("%d",&m); p1 = insert(p1,m,&p); print(p1); return 0; }

9.链表,建立、插入、删除

#include #include typedef struct Node { int data; struct Node *next; }NODE; NODE *head = NULL; NODE *tail = NULL; void init_node() { int data; printf("输入10个数据:\n"); for (int i = 0;i data); head->next = tail; tail = head; } else { NODE *p = (NODE *)malloc(sizeof(NODE)); scanf("%d",&p->data); tail->next = p; tail = p; } tail->next = NULL; } } void del() { NODE *p = head; NODE *p1; printf("输入需要删除的数据:"); int n; scanf("%d",&n); while(p->data != n && p -> next != NULL) { p1 = p; p = p->next; } if (p->data == n) { if ( p == head) { head = head->next; free(p); } else { p1->next = p->next; free(p); } } else { printf("没有找到这个值.\n"); } } void insert() { int n,m; NODE *p = head; NODE *node = (NODE *)malloc(sizeof(NODE)); printf("输入需要插入的数据和位置:"); scanf("%d%d",&n,&m); node->data = n; if ( m == 0) { node->next = head; head = node; } else { for (int i = 0;i next); node->next = p->next; p->next = node; } } void traverse() { NODE *p = head; printf("遍历链表如下:\n"); while( p != NULL) { printf("%d\t",p->data); p = p->next; } printf("\n"); } int main() { init_node(); traverse(); del(); traverse(); insert(); traverse(); }

10.链表合并

#include #include typedef struct Node { int num; float score; struct Node *next; }NODE; NODE *creat() { NODE *head; NODE *tail; int n = rand()%10 + 5; printf("请输入输入%d个学生学号与成绩:",n); for (int i = 0;i num); scanf_s("%f",&head->score); head->next = tail; tail = head; } else { NODE *p = (NODE *)malloc(sizeof(NODE)); scanf_s("%d",&p -> num); scanf_s("%f",&p -> score); tail->next = p; tail = p; } tail->next = NULL; } return head; } NODE *add(NODE *ahead,NODE *bhead) { NODE *p = ahead; while( p->next != NULL) { p = p->next; } p->next = bhead; return ahead; } void sort(NODE *head) { NODE *p1 = head; NODE *p2; int temp1; float temp2; for ( p1 = head;p1 -> next != NULL;p1 = p1->next) { for ( p2 = p1->next;p2 != NULL;p2 = p2->next) { if (p1->num > p2->num) { temp1 = p1->num; temp2 = p1->score; p1->num = p2->num; p2->num = temp1; p1->score = p2->score; p2->score = temp2; } } } } void traverse(NODE *head) { NODE *p = head; printf("遍历链表如下:\n"); while( p != NULL) { printf("学号:%d\t%成绩:%5.2f\n",p->num,p->score); p = p->next; } printf("\n"); } int main() { NODE *p1; p1 =creat(); traverse(p1); NODE *p2; p2 = creat(); traverse(p2); printf("相加结果如下:\n"); p1 = add(p1,p2); traverse(p1); printf("按学号大小排序:\n"); sort(p1); traverse(p1); return 0; }

11.链表删除相同项

#include #include typedef struct Node { int num; char name[10]; struct Node *next; }NODE; NODE *creat() { NODE *head; NODE *tail; int n = rand()%10 + 5; printf("需要输入 %d 组数据:\n",n); for (int i = 0;i num); scanf("%s",&head->name); head->next = tail; tail = head; } else { NODE *p = (NODE *)malloc(sizeof(NODE)); scanf("%d",&p -> num); scanf("%s",&p -> name); tail->next = p; tail = p; } tail->next = NULL; } return head; } void del(NODE *head,int num) { NODE *p = head; NODE *p1; while ( p->num != num && p->next != NULL) { p1 = p; p = p->next; } if ( p->num == num) { if ( p == head) { head = head->next; free(p); } else { p1->next = p->next; free(p); } } } void fun(NODE *ahead,NODE *bhead) { NODE *p2 = bhead; while ( p2 != NULL) { del(ahead,p2->num); p2 = p2->next; } } void traverse(NODE *head) { NODE *p = head; printf("遍历链表如下:\n"); while( p != NULL) { printf("学号:%d\t%姓名:%s\n",p->num,p->name); p = p->next; } printf("\n"); } int main() { NODE *p1,*p2; printf("建立a链表:\n"); p1 = creat(); printf("遍历a链表: \n"); traverse(p1); printf("建立b链表:\n"); p2 = creat(); printf("遍历b链表: \n"); traverse(p2); fun(p1,p2); printf("删除之后:\n"); traverse(p1); return 0; return 0; }

12.节点的删除

#include #include typedef struct Node { int num; char name[10]; char m; int age; struct Node *next; }NODE; NODE *creat() { NODE *head; NODE *tail; int n = rand()%10 + 5; printf("需要输入一共%d组信息\n",n); for (int i = 0;i num); scanf("%s",&head->name); scanf(" %c",&head->m); scanf("%d",&head->age); head->next = tail; tail = head; } else { NODE *p = (NODE *)malloc(sizeof(NODE)); scanf("%d",&p->num); scanf("%s",&p->name); scanf(" %c",&p->m); scanf("%d",&p->age); tail->next = p; tail = p; } tail->next = NULL; } return head; } NODE *del(NODE *head,int age) { NODE *p = head; NODE *p1; while ( p->age != age && p->next != NULL) { p1 = p; p = p->next; } if ( p->age == age) { if ( p == head) { head = head->next; free(p); } else { p1->next = p->next; free(p); } } else printf("没有这个节点。\n"); return head; } void traverse(NODE *head) { NODE *p = head; printf("遍历链表如下:\n"); while( p != NULL) { printf("学号:%d\t%姓名:%s\t性别:%c\t年龄:%d\n",p->num,p->name,p->m,p->age); p = p->next; } printf("\n"); } int main() { NODE *p1; p1 =creat(); traverse(p1); int age; printf("输入需要删除的年龄的学生:"); scanf("%d",&age); p1 = del(p1,age); traverse(p1); return 0; }


【本文地址】


今日新闻


推荐新闻


CopyRight 2018-2019 办公设备维修网 版权所有 豫ICP备15022753号-3