如何转换c.在乐高头脑风暴中奔跑 [英] How to convert in c. to run in lego mindstorm

查看:83
本文介绍了如何转换c.在乐高头脑风暴中奔跑的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

#include <iostream.h>   
#include <math.h>   
#include <fstream.h>   
#define  NATTRS 5 //number of attributes   
#define  MAXSZ  1700 //max size of training set   
#define  MAXVALUE  10000.0 //the biggest attribute's value is below 10000(int)   
#define  K  5      
struct vector {   
    double attributes[NATTRS];   
    double classlabel;   
};   
struct item {   
    double distance;   
    double classlabel;   
};   
struct vector trSet[MAXSZ];//global variable,the training set   
struct item knn[K];//global variable,the k-neareast-neighbour set   
int curTSize = 0; //current size of the training set   
int AddtoTSet(struct vector v)   
{   
    if(curTSize&gt;=MAXSZ) {   
        cout&lt;&lt;endl&lt;&lt;"The training set has "&lt;&lt;MAXSZ&lt;&lt;" examples!"&lt;&lt;endl&lt;&lt;endl;    
        return 0;   
    }   
    trSet[curTSize] = v;   
    curTSize++;   
    return 1;   
}   
double Distance(struct vector v1,struct vector v2)   
{   
    double d = 0.0;   
    double tem = 0.0;   
    for(int i = 0;i  NATTRS;i++)   
        tem += (v1.attributes[i]-v2.attributes[i])*(v1.attributes[i]-v2.attributes[i]);   
    d = sqrt(tem);   
    return d;   
}   
int max(struct item knn[]) //return the no. of the item which has biggest distance(   
                           //should be replaced)    
{   
    int maxNo = 0;   
    if(K &gt; 1)   
    for(int i = 1;i  K;i++)   
        if(knn[i].distance&gt;knn[maxNo].distance)   
            maxNo = i;   
    return maxNo;   
}   
   
double Classify(struct vector v)//decide which class label will be assigned to   
                             //a given input vetor with the knn method   
{   
    double dd = 0;   
    int maxn = 0;   
    int freq[K];   
    double mfreqC = 0;//the class label appears most frequently    
    int i;   
    for(i = 0;i  K;i++)   
        knn[i].distance = MAXVALUE;   
    for(i = 0;i  curTSize;i++)   
    {   
        dd = Distance(trSet[i],v);   
        maxn = max(knn);//for every new state of the training set should update maxn   
            if(dd  knn[maxn].distance) {   
                knn[maxn].distance = dd;   
                knn[maxn].classlabel = trSet[i].classlabel;   
            }   
    }   
    for(i = 0;i  K;i++)//freq[i] represents knn[i].classlabel appears how many times    
        freq[i] = 1;   
    for(i = 0;i  K;i++)     
        for(int j = 0;j  K;j++)   
            if((i!=j)&&(knn[i].classlabel == knn[j].classlabel))   
                freq[i]+=1;   
    int mfreq = 1;   
    mfreqC = knn[0].classlabel;   
    for(i = 0;i  K;i++)   
        if(freq[i] &gt; mfreq)  {   
            mfreq = freq[i];//mfreq represents the most frepuences   
            mfreqC = knn[i].classlabel; //mfreqNo is the item no. with the most frequent   
                                         //classlabel   
        }   
    return mfreqC;   
}   
void main()   
{      
   
    double classlabel;   
    double c;    
    double n;   
    struct vector trExmp;      
    int i;   
    ifstream filein("G:\\data\\for knn\\data.txt");   
    if(filein.fail()){cout&lt;&lt;"Can't open data.txt"&lt;&lt;endl; return;}   
    while(!filein.eof()) {   
        filein&gt;&gt;c;   
        trExmp.classlabel = c;   
        cout&lt;&lt;trExmp.classlabel&lt;&lt;" ";   
   
        for(int i = 0;i  NATTRS;i++) {   
        filein&gt;&gt;n;   
        trExmp.attributes[i] = n;   
        cout&lt;&lt;trExmp.attributes[i]&lt;&lt;" ";   
        }   
   
        cout&lt;&lt;endl;   
     if(!AddtoTSet(trExmp))   
        break;   
    }   
   
    filein.close();   
   
    struct vector testv={{142,188,11,1159,0.5513196},17};   
    classlabel = Classify(testv);   
    cout&lt;&lt;"The classlable of the testv is:    ";   
    cout&lt;&lt;classlabel&lt;&lt;endl;   
    for(i = 0;i  K;i++)   
        cout&lt;&lt;knn[i].distance&lt;&lt;"\t"&lt;&lt;knn[i].classlabel&lt;&lt;endl;   
    //cout&lt;&lt;max(knn);   
}



[edit]添加了代码块-OriginalGriff [/edit]



[edit]Code block added - OriginalGriff[/edit]

推荐答案

只是让您入门,因为除了C ++的io类之外,我什么都看不到:

将包含内容删除到iostream fstream,而是包含stdio.h.

如果您不知道如何使用它们,请在Internet上查找FILE及其相关功能.相同于printf,getline()等.
Just to get you started, since I don''t see anything but io classes from C++:

Remove the includes to iostream and fstream, instead include stdio.h.

Look up FILE and related functions on the internet if you don''t know how to use them. Same for printf, getline(), and the like.


这篇关于如何转换c.在乐高头脑风暴中奔跑的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆