TA的每日心情 | 开心 2014-6-18 08:29 |
---|
签到天数: 14 天 [LV.3]偶尔看看II
滴水大师
 
- 积分
- 2345
|
题目
解决代码及点评
- <pre code_snippet_id="91880" snippet_file_name="blog_20131202_1_2646179" class="cpp" name="code"></pre><pre code_snippet_id="91880" snippet_file_name="blog_20131202_1_2646179" class="cpp" name="code"><pre code_snippet_id="91880" snippet_file_name="blog_20131202_1_2646179" class="cpp" name="code"><pre code_snippet_id="91880" snippet_file_name="blog_20131202_1_2646179" class="cpp" name="code">/*
- 有两个磁盘文件,各自存放已排好序的若干个字符(如a1. dat 中放"abort", a2.dat中放 "boy")
- 要求将两个文件合并,合并后仍保持有序,存放在a3.dat文件中。
- 提示:可先将两个文件中的字符存入一个字符型数组中,而后对数组重新排序,再将该数组写入a3.dat文件中。
- 如果不引入一个中间数组进行重新排序,该如何编程?
-
-
- */
-
- #include <stdio.h>
- #include <stdlib.h>
- #include <string.h>
- #include <ctype.h>
-
- #define SWAP(x,y) {char tmp; tmp = (x); (x)=(y); (y)=tmp;}
-
-
- static void str_sort(char *str, int str_size)
- {
- int i,j;
-
- for (i = 0; i < str_size; i++)
- {
- for (j = str_size - 2; j >= i; j--)
- {
- if (str[j] > str[j+1])
- {
- SWAP(str[j],str[j+1]);
- }
- }
- }
- }
-
- int main()
- {
- FILE *fp = NULL;
- char str[128] = "";
- char *p = str;
-
- fopen_s(&fp, "a1.dat", "r");//打开a1.dat
- if (fp == NULL)
- {
- fopen_s(&fp, "a1.dat", "w+");//读写
- fprintf(fp, "%s", "abort");
- rewind(fp);
- }
- fscanf_s(fp, "%s", str);
- fclose(fp);
-
- p = str+strlen(str);//p移动到末尾
-
- fopen_s(&fp, "a2.dat", "r");//打开a2.dat
- if (fp == NULL)
- {
- fopen_s(&fp, "a2.dat", "w+");//读写
- fprintf(fp, "%s", "boy");
- rewind(fp);
- }
- fscanf_s(fp, "%s", p);
- fclose(fp);
-
- str_sort(str, strlen(str));//对str[]进行排序
- printf("%s\n", str);
-
- fopen_s(&fp, "a3.dat", "w");//将str写入a3.dat
- fprintf(fp, "%s", str);
- fclose(fp);
-
-
- return 0;
- }
- </pre><br><br></pre></pre>
|
|