本文主要是介绍成信大2021自动化专业-平时自主学习-C语言改错题解题参考-整理第08页,希望对大家解决编程问题提供一定的参考价值,需要的开发者们随着小编来一起学习吧!
文章目录
- 第08页
- 题面如下:
- 题解如下:
- D1054.c
- D1055.c
- D1056.c
- D1057.c
- D1058.c
- D1059.c
- D1060.c
- D1061.c
- D1062.c
- D1063.c
第08页
题面如下:
题解如下:
D1054.c
原文件
#include <stdio.h>int main(void)
{ unsigned long int number, num1, num2, mask;int i;char str[33];printf("Please input num1 and num2 : ");scanf("%lu %lu", &num1, &num2);/*********Found************/mask = __________________;number = num1 ^ num2;for(i=0; i<32; i++){ /*********Found************/str[i] = __________________;mask >>= 1;}str[32] = '\0';printf("%lu ^ %lu = %s\n", num1, num2, str);return 0;
}
改后文件
#include <stdio.h>int main(void)
{ unsigned long int number, num1, num2, mask;int i;char str[33];printf("Please input num1 and num2 : ");scanf("%lu %lu", &num1, &num2);/*********Found************/mask = 1<<31;number = num1 ^ num2;for(i=0; i<32; i++){ /*********Found************/str[i] = number&mask ?'1':'0';mask >>= 1;}str[32] = '\0';printf("%lu ^ %lu = %s\n", num1, num2, str);return 0;
}
考查要点:
- 位运算,移位,要注意编译器给出的相应的数据类型的内存长度
- 从高位到低位,依次用与的方式取出该位上的1或是0,然后存入数组中
- 最后将字符数组做成字符串,即补结束符号
D1055.c
原文件
#include<stdio.h>
#include<malloc.h> #define N 13
#define LEN sizeof(struct person) int main(void)
{ int i, count;struct person{int number;/*********Found************/___________________________;} *head, *p1, *p2;head = p2 = NULL;for (i=1; i<=N; i++){ p1 = (struct person *)malloc(LEN);p1->number = i;if (NULL == head){/*********Found************/___________________________;}else{p2->next = p1; }p2 = p1;}p2->next = head;printf("the sequence out of the circle is:\n");for (count=1; count<N; count++){i = 1;while (i != 3){p1 = head;/*********Found************/___________________________;i++;}p2 = head;printf("%3d ", p2->number);p1->next = head = p2->next;/*********Found************/___________________________;}printf("\nThe betrayer of them is:%3d\n", head->number);return 0;
}
改后文件
#include<stdio.h>
#include<malloc.h> #define N 13
#define LEN sizeof(struct person) int main(void)
{ int i, count;struct person{int number;/*********Found************/struct person * next;} *head, *p1, *p2;head = p2 = NULL;for (i=1; i<=N; i++){ p1 = (struct person *)malloc(LEN);p1->number = i;if (NULL == head){/*********Found************/head = p1;}else{p2->next = p1; }p2 = p1;}p2->next = head;printf("the sequence out of the circle is:\n");for (count=1; count<N; count++){i = 1;while (i != 3){p1 = head;/*********Found************/head = head->next;i++;}p2 = head;printf("%3d ", p2->number);p1->next = head = p2->next;/*********Found************/free(p2);}printf("\nThe betrayer of them is:%3d\n", head->number);return 0;
}
考查要点:
- 约瑟夫环问题
- 节点定义中,需要有指针域
- 首节点,用头指针来指
- 用head指针后移的方式完成出圈
- 出圈后,要记得归还内存
D1056.c
原文件
#include <stdio.h>
#include <stdlib.h>
#include <string.h>int main(int argc, char *argv[])
{FILE *fp;char str[100], substr[100];int count = 0;if (argc != 2){printf("error");exit(0);}/*********Found************/fp = fopen(__________, "r");if (NULL == fp){printf("file open error\n"); exit(0);}printf("Please input the string : ");gets(substr);while (!feof(fp)){count++;str[0] = '\0';/*********Found************/___________________________;if (strstr(str, substr)){printf("Line%d : ", count);puts(str);}}/*********Found************/___________________________;return 0;
}
改后文件
#include <stdio.h>
#include <stdlib.h>
#include <string.h>int main(int argc, char *argv[])
{FILE *fp;char str[100], substr[100];int count = 0;if (argc != 2){printf("error");exit(0);}/*********Found************/fp = fopen(argv[1], "r");if (NULL == fp){printf("file open error\n"); exit(0);}printf("Please input the string : ");gets(substr);while (!feof(fp)){count++;str[0] = '\0';/*********Found************/fgets(str,100,fp);if (strstr(str, substr)){printf("Line%d : ", count);puts(str);}}/*********Found************/fclose(fp);return 0;
}
考查要点:
- 文件打开后要记得关闭
- fgets的三个参数要清楚:写内存缓区冲指针,大小,文件指针
D1057.c
原文件
#include <stdio.h>int main(void)
{float numA, numB, *ptr;numA = 4.5;numB = 6.8f;/*********Found************/ptr = numB;/*********Found************/printf("numA=%f, numB=%f\n", numA, ptr);return 0;
}
改后文件
#include <stdio.h>int main(void)
{float numA, numB, *ptr;numA = 4.5;numB = 6.8f;/*********Found************/ptr = &numB;/*********Found************/printf("numA=%f, numB=%f\n", numA, *ptr);return 0;
}
考查要点:
- 指针和变量的关系
- 取值和取址
D1058.c
原文件
#include <stdio.h>int main(void)
{ struct student{int num;char name[10];float score[3];} stu1 = {2012, "WuHua", {75.4f, 80, 92}}; struct student *ptr;/*********Found************/ptr = stu1;/*********Found************/printf("%s\n", ptr.name);return 0;
}
改后文件
#include <stdio.h>int main(void)
{ struct student{int num;char name[10];float score[3];} stu1 = {2012, "WuHua", {75.4f, 80, 92}}; struct student *ptr;/*********Found************/ptr = &stu1;/*********Found************/printf("%s\n", ptr->name);return 0;
}
考查要点:
- 指针和结构
- 用指针的方式访问结构成员:指向
D1059.c
原文件
#include <stdio.h>int main(void)
{ int *ptr, i, arrA[10];/*********Found************/___________________________;for (i=0; i<10; i++){scanf("%d", ptr++);}printf("\n");/*********Found************/___________________________;for(i=0; i<10; i++, ptr++){printf("%d ",*ptr);}printf("\n");return 0;
}
改后文件
#include <stdio.h>int main(void)
{ int *ptr, i, arrA[10];/*********Found************/ptr = arrA;for (i=0; i<10; i++){scanf("%d", ptr++);}printf("\n");/*********Found************/ptr = arrA;for(i=0; i<10; i++, ptr++){printf("%d ",*ptr);}printf("\n");return 0;
}
考查要点:
- 指针和数组的关系
- 用指针移动和遍历数组元素
D1060.c
原文件
#include <stdio.h>int main(void)
{ struct student{int num;char name[10];} stu[3], *ptr;int i;for (i=0; i<3; i++){scanf("%d,%s", &stu[i].num, stu[i].name);}/*********Found************/for (ptr=stu; __________; _________){printf("%d, %s\n", ptr->num, ptr->name);}return 0;
}
改后文件
#include <stdio.h>int main(void)
{ struct student{int num;char name[10];} stu[3], *ptr;int i;for (i=0; i<3; i++){scanf("%d,%s", &stu[i].num, stu[i].name);}/*********Found************/for (ptr=stu; ptr<stu+3; ptr++){printf("%d, %s\n", ptr->num, ptr->name);}return 0;
}
考查要点:
- 结构体基础知识
- 指针基础知识
- 数组基础知识
- 使用指针来遍历结构体数组
D1061.c
原文件
#include <stdlib.h>
#include <stdio.h>#define LEN sizeof(struct student)struct student
{int num;char name[20];struct student *next;
};struct student *creat(void);
void display(struct student *head);int main(void)
{struct student * head;head = creat();display(head);return 0;
}struct student *creat(void)
{struct student *p1, *p2, *head=NULL;int num;printf("请输入学号及姓名(学号输入为0时表示停止):\n");while (1){scanf("%d", &num);if (0 == num) {break;}p1 = (struct student *)malloc(LEN);p1->num = num;scanf("%s", p1->name);if (NULL == head){head = p1;}else{p2->next = p1;}/*********Found************/___________________________;}if (head != NULL){p2->next = NULL;}/*********Found************/___________________________;
}void display(struct student *head)
{struct student *p1;p1 = head;while (p1 != NULL){printf("%d, %s ", p1->num, p1->name);p1 = p1->next;}
}
改后文件
#include <stdlib.h>
#include <stdio.h>#define LEN sizeof(struct student)struct student
{int num;char name[20];struct student *next;
};struct student *creat(void);
void display(struct student *head);int main(void)
{struct student * head;head = creat();display(head);return 0;
}struct student *creat(void)
{struct student *p1, *p2, *head=NULL;int num;printf("请输入学号及姓名(学号输入为0时表示停止):\n");while (1){scanf("%d", &num);if (0 == num) {break;}p1 = (struct student *)malloc(LEN);p1->num = num;scanf("%s", p1->name);if (NULL == head){head = p1;}else{p2->next = p1;}/*********Found************/p2 = p1;}if (head != NULL){p2->next = NULL;}/*********Found************/return head;
}void display(struct student *head)
{struct student *p1;p1 = head;while (p1 != NULL){printf("%d, %s ", p1->num, p1->name);p1 = p1->next;}
}
考查要点:
- 结构
- 链表
- 遍历链表节点
D1062.c
原文件
#include <stdio.h>void findmax(int (*pArr)[4], int *pmax, int m, int n);int main(void)
{ int arr[3][4], *pa, max[3], i;printf("input 12 elements : \n");pa = arr[0];for (i=0; i<12; i++){scanf("%d", pa++);}findmax(arr, max, 3, 4);for (i=0; i<3; i++){printf("line %d's max = %d\n", i, max[i]);}return 0;
}void findmax(int (*pArr)[4], int *pmax, int m, int n)
{int i, j;for (i=0; i<m; i++, pmax++){/*********Found************/*pmax = ___________________;for (j=1; j<n; j++){/*********Found************/if (_______________ > *pmax){/*********Found************/*pmax = ___________________;}}}
}
改后文件
#include <stdio.h>void findmax(int (*pArr)[4], int *pmax, int m, int n);int main(void)
{ int arr[3][4], *pa, max[3], i;printf("input 12 elements : \n");pa = arr[0];for (i=0; i<12; i++){scanf("%d", pa++);}findmax(arr, max, 3, 4);for (i=0; i<3; i++){printf("line %d's max = %d\n", i, max[i]);}return 0;
}void findmax(int (*pArr)[4], int *pmax, int m, int n)
{int i, j;for (i=0; i<m; i++, pmax++){/*********Found************/*pmax = *(*(pArr+i));for (j=1; j<n; j++){/*********Found************/if (*(*(pArr+i)+j) > *pmax){/*********Found************/*pmax = *(*(pArr+i)+j);}}}
}
考查要点:
- 函数
- 指针
- 二维数组
- 行指针指向二级数组
D1063.c
原文件
#include <stdio.h>void change(int *p1, int *p2);
/*********Found************/
______sort(int *pArr, int n);int main( )
{int arr[20], *pa, i, n;printf("Please input the number n = ");scanf("%d", &n);printf("Please input the array elements : ");for (i=0, pa=arr; i<n; i++){scanf("%d", pa++); }/*********Found************/___________________________;printf("\n output : \n");for (i=0; i<n; i++){printf("%d ", *(pa + i));}printf("\n");return 0;
}void change(int *p1, int *p2)
{int tmp;tmp = *p1;*p1 = *p2;*p2 = tmp;
}/*********Found************/
______sort(int *pArr, int n)
{int i, *pa;for (i=n-1; i>0; i--){for (pa=pArr; pa<pArr+i; pa++){if (*pa > *(pa+1)){change(pa, pa+1);}}}/*********Found************/___________________________;
}
改后文件
#include <stdio.h>void change(int *p1, int *p2);
/*********Found************/
int * sort(int *pArr, int n);int main( )
{int arr[20], *pa, i, n;printf("Please input the number n = ");scanf("%d", &n);printf("Please input the array elements : ");for (i=0, pa=arr; i<n; i++){scanf("%d", pa++); }/*********Found************/pa = sort(arr, n);printf("\n output : \n");for (i=0; i<n; i++){printf("%d ", *(pa + i));}printf("\n");return 0;
}void change(int *p1, int *p2)
{int tmp;tmp = *p1;*p1 = *p2;*p2 = tmp;
}/*********Found************/
int * sort(int *pArr, int n)
{int i, *pa;for (i=n-1; i>0; i--){for (pa=pArr; pa<pArr+i; pa++){if (*pa > *(pa+1)){change(pa, pa+1);}}}/*********Found************/return pArr ;
}
考查要点:
- 函数
- 返回地址的情况,即指针函数
这篇关于成信大2021自动化专业-平时自主学习-C语言改错题解题参考-整理第08页的文章就介绍到这儿,希望我们推荐的文章对编程师们有所帮助!