C语言的算法

时间:2008-06-12 14:10:48   来源:论坛整理  作者:  编辑:chinaitzhe
望各位02061、2软件编程实验
一、设计要求:
1、对如下文本文件内容
03 04 08 17 18 22 25 29
03 04 17 18 22 25 29 30
03 08 17 18 22 25 29
03 04 08 14 17 18 22 29 30
03 08 14 18 22 25 29 30
03 08 14 17 18 22 25 26 29
03 08 14 18 22 25
03 04 14 17 18 25 29 30
03 04 08 17 22 26 29
03 04 17 18 22 29 30
03 04 08 14 18 22 25
04 08 17 18 22 25 30
03 18 22 25 26 29 30
03 04 08 17 18 22 25
03 04 08 14 17 22 25 26
03 04 08 18 22 30
03 04 08 17 18 22 25 30
03 04 08 14 17 18 22 25
03 08 18 22 25 26 29
03 04 08 17 18 22 25
03 04 18 22 25 26
03 04 08 14 22 25 29
03 08 14 17 18 22 25 29
03 04 08 17 22 29 30
03 08 18 25 29 30
03 08 14 17 18 25
每三组进行组合,假如存在6个以上相同数据,则将该6个数据写入另一个文件保存,如下面三组数据
03 04 08 17 18 22 25 29
03 04 17 18 22 25 29 30
03 08 17 18 22 25 29
中有6个相同的数据 03 17 18 22 25 29,就将该组相同的数据保存在另一文件中,具有相同的只保留一组。
二、设计时间:
11——13周
三、完成提交方式:
1、写出课程设计报告,内容包括需求分析,概要设计,具体设计,调试分析,经验总结。
2、提交实现系统,用数据进行验收。


帮帮忙!不胜感激!
网友回复:好好学习,天天向上!
网友回复:满足你, enjoy~

C/C code





Code highlighting produced by Actipro CodeHighlighter (freeware)

http://www.CodeHighlighter.com/





#include <vector>

#include <fstream>

#include <sstream>

#include <algorithm>

using namespace std;

ifstream in("in.txt");

ofstream out("out.txt");



void process(const vector<int> & v1, const vector<int> & v2, const vector<int> & v3)

{

    if (v1.size() < 6 || v2.size() < 6 || v3.size() < 6)

        return;



    vector<int> result;

    for (vector<int>::const_iterator b = v1.begin(); b != v1.end(); b  )

        if ((find(v2.begin(), v2.end(), *b) != v2.end())

            &&(find(v3.begin(), v3.end(), *b) != v3.end()))

            result.push_back(*b);



    int cnt = result.size();

    if (cnt >= 6 )

    {

        for (int i = 0; i < cnt;   i)

            out << result[i] << " ";

        out << endl;

    }

}



int main()

{

    string line;

    int num;



    while (in) // 出错处这里就不写了。

    {

        vector<int> v[3];

        for (int i = 0; i < 3; i  )

        {

            getline(in, line, '\n');

            stringstream str(line);

            while (str) // 出错处这里就不写了。

                if (str >> num) // 出错处这里就不写了。

                    v[i].push_back(num);

        }



        process(v[0], v[1], v[2]);

    }



    return 0;

}




网友回复:谢谢!不过要求用文件做啊!
我做了一半!但好象没得到预想的结果!大家帮我看#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <math.h>
#include <ctype.h>
#define N1 30
#define N2 10

int main(){
int m=0,n=0,s=0,x=0;
char ch;
int Array[N1][N2];
FILE *fp;
if((fp=fopen("F:\\C3exe\\file.txt","r"))==NULL){
printf("cannot open this file\n");
exit (1);
}
while((ch=fgetc(fp))!=EOF){
char ch;
if(ch=='\n'){
m ;
n=0;
}
else if(ch==' '){
m=0;
n=0;
}
else if('0' <=ch&&ch <='9'){
s =(ch-48)*(int)pow(10,1-m);
x ;
Array[m][n ]=s;
}
if(m==2){
Array[m][n]=s;
n ;
}
fclose(fp);
for(m=0;m <N1;m )
for(n=0;n <N2;n )
printf("\nArray[%d][%d]==%d",m,n,&Array[m][n]);
printf("\n");

return 0;
}
}
数组显现有错误!



网友回复:回复3楼:
首先声明,我在2楼的方法是采用文件的,ifstream和ofstream.
把代码缩进对齐后在贴出来,不然看起来很不方便。
Array最好采用动态内存分配,读取文件的时候最好一次读入一行,而不是一个一个字符读(这样效率很低的!)
从文件读入数据可以采用fscanf函数,这样可以省去很多麻烦,而且效率也比你调用pow等函数高。
你还刚读入数据哦,还有点路要走。。。。加油~~
关键字:语言,算法,

文章评论

共有 0 位网友发表了评论 此处只显示部分留言 点击查看完整评论页面