目录
题述:从键盘输入10个学生的学号,姓名和数学语文和英语成绩,自导文本文件f3.txt中,再从中取出数据,计算每个学生的总成绩和平均分,并将显示结果打到屏幕上。
思路:这道题考察对读写文件,我们可以用文本的形式进行读写,也就是用fscanf和fprintf
①、涉及从文件中读和写数据到文件中,所以一定要打开文件两次,一次以写的方式打开,另一次以读的方式打开。不然如果你只是以写的方式打开,可你后续还从文件中读数据了,读出来的就是乱码(原来我就这么错过,半天没找到错误)
②、如果写完之后还要从文件中读数据一定要用到rewind函数,因为你写完数据之后,你的文件位置指针指向数据尾,如果你不用rewind,下次再读就什么数据也没有,用rewind函数可以让文件位置指针指向文件头,以便读取数据。(rewind大概就是使文件位置指针回到头的意思)
- #include
- #include
- #define N 2
- typedef struct student
- {
- int num;
- char name[20];
- double math;
- double chinese;
- double english;
- }student;student stu[N];
- int main()
- {
- FILE* pf = fopen("f3.txt", "w");
- if (pf == NULL)
- {
- perror("fopen"); exit(-1);
- }
- for (int i = 0; i < N; i++)
- {
- scanf("%d %s %lf %lf %lf", &stu[i].num, stu[i].name, &stu[i].math, &stu[i].chinese, &stu[i].english);
- fprintf(pf, "%d %s %lf %lf %lf", stu[i].num, stu[i].name, stu[i].math, stu[i].chinese, stu[i].english);
- fputc('\n', pf);
- }
- rewind(pf);
- pf = fopen("f3.txt", "r");
- if (pf == NULL)
- {
- perror("fopen");
- exit(-1);
- }
- double sum = 0; student b;
- for (int i = 0; i < N; i++)
- {
- sum = 0;
- fscanf(pf, "%d %s %lf %lf %lf", &b.num, b.name, &b.math, &b.chinese, &b.english);
- sum += b.math +b.chinese + b.english;
- printf("每个学生的总成绩和平均分各为%lf,%lf\n", sum, sum / N);
- }
- fclose(pf);
- pf = NULL;
- return 0;
- }
题述:查找星期:定义一个指针数组将下表的星期信息组织起来,输入一个字符串,在表中查找,若存在,输出该字符串在表中的序号,否则输出-1
思路:定义指针数组,再调用strcmp就很好做了
- #include
- #include
-
- #define MAXS 80
-
- int getindex(char* s)
- {
- char* week[7] =
- {
- "Sunday",
- "Monday",
- "Tuesday",
- "Wednesday",
- "Thursday",
- "Friday",
- "Saturday"
- };
- int i = 0;
- for (i = 0; i < 7; i++)
- {
- if (strcmp(s, week[i]) == 0)
- return i;
- }
- return -1;
- }
-
- int main()
- {
- int n;
- char s[MAXS];
-
- scanf("%s", s);
- n = getindex(s);
- if (n == -1) printf("wrong input!\n");
- else printf("%d\n", n);
-
- return 0;
- }
题述:输入一组英文单词(不超过8个),要求按输人顺序取出每个单词的第一个字母并连接在一起形成一个 字符串开输出。
思路:我们可以先定一个n,说明想输入多少个单词。单词总数不超过8个,而每个单词的长度我们还可以假定,所以这里可以用二维数组
- #include
-
- int main()
- {
- char word[8][20];//最多有8个单词,每个单词长度最大为20
- char str[9];//要多出一个空间来存'\0'
- int n = 0;
- scanf("%d", &n);
- for (int i = 0; i < n; i++)
- {
- scanf("%s", word[i]);
- str[i] = word[i][0];
- }
- str[n] = '\0';//字符数组要转为字符串
- printf("藏头词为%s\n", str);
- return 0;
- }
题述:一个IP地址是用四个字节(每个字节8个位)的二进制码组成。输入32位二进制字符进制数),中间用圆点分隔开。试编写相应程序。
了解IP地址结构:
每个IP地址有4组8位二进制组成,8位二进制从左边算起的第一位是2的7次方=64,第二位是2的6次方=64,第三位是2的5次方=32......第八位是2的0次方=1
以1100000000 10101000 01010000 01000110的二进制地址为例
11000000 = 128*1+64*1=192
10101000=128*1+32*1+8*1=168
01010000=64*1+16*1=80
01000110=64*1+4*1+2*1=70
最后得出此IP地址的十进制为192.168.80.70
这正是本题所考察的,中间要有圆点相分隔。
思路:
注意:你输入的二进制位是数字字符,必须转换为对应的数字
1、本质就是考察二进制如何转换为十进制,我们以四组为划分每组每组算一下,每组都是8个二进制位,每组的8个二进制位都要转换为1个十进制数字。
2、数字字符转换为数字的方法:因为'0'的ASC2码为48,这里'0'是字符,48-48就是数字0,比如'1'的ASC2码为49,这里'1'是字符,49-48就是数字1,所以数字字符转换为数字就直接-48即可
- #include
- #include
-
- int main()
- {
- int i = 0, j = 0;
- char s[40];
- gets(s);
- for (i = 0; i < 4; i++)
- {
- int ret = 0;
- for (j = 0; j < 8; j++)
- {//计算每一组对应的十进制数,每一组都有8个二进制位
- ret += (s[8 * i + j] - '0') * pow(2, 8 - j - 1);//下一个就从s[8]开始
- }
- if (i == 0)
- {
- printf("%d", ret);
- }
- else
- {
- printf(".%d", ret);
- }
- }
- return 0;
- }
运行代码如下:
题述:删除单向链表偶数(值域为偶数的)节点:输入若干个正整数(输人-1为结束标志),建立一个单向链表,将其中的值域为偶数的节点删除后输出。
思路:跟我之前写的链表面试题①删除链表中的重复元素的思路大同小异!我敢说你只要看懂我那篇博客,这道题就完全没问题,不懂代码的请看那篇
- #include
- #include
-
- struct ListNode {
- int data;
- struct ListNode* next;
- };
-
- struct ListNode* createlist()
- {
- int x = 0;
- struct ListNode* head = NULL, * tail = NULL;
- while (scanf("%d", &x), x != -1)
- {
- struct ListNode* p = (struct ListNode*)malloc(sizeof(struct ListNode));
- if (p == NULL)
- {
- perror("malloc");
- exit(-1);
- }
- p->data = x;
- p->next = NULL;
- if (head == NULL)
- {
- head = p;
- }
- else
- {
- tail->next = p;
- }
- tail = p;
- }
- return head;
- }
- struct ListNode* deleteeven(struct ListNode* head)
- {
- struct ListNode* prev = NULL, * cur = head;
- while (cur)
- {
- if (cur->data % 2 == 0)
- {
- if (head == cur)
- {
- head = cur->next;
- free(cur);
- cur = head;
- }
- else
- {
- prev->next = cur->next;
- free(cur);
- cur = prev->next;
- }
- }
- else
- {
- prev = cur;
- cur = cur->next;
- }
- }
- return head;
- }
- void printlist(struct ListNode* head)
- {
- struct ListNode* p = head;
- while (p) {
- printf("%d ", p->data);
- p = p->next;
- }
- printf("\n");
- }
-
- int main()
- {
- struct ListNode* head;
-
- head = createlist();
- head = deleteeven(head);
- printlist(head);
-
- return 0;
- }