主题:#include语句是放在stdafx.h 还是可放在search1.cpp 中
vc2008建了一个Win32 控制台应用程序,要像在vc6.0那样编译一个文件是不是这样做?
另外
#include "isInt.h"
#include "freqcount.h"
#include <iostream> 语句是放在stdafx.h 还是可放在search1.cpp 中
头文件目录有stdafx.h targetver.h 我添加了freqcount.h isInt.h(用于判断输入是否为数字)
源文件目录有stdafx.cpp 我添加了search1.cpp
//search1.cpp
#include "stdafx.h"
#include "isInt.h"
#include "freqcount.h"
#include <iostream>
using namespace std;
int _tmain(int argc, _TCHAR* argv[])
{
Freqlist<int> list(50);
char str[20];
int a;
cout<<"请输入记录,以-1结束:";
cin>>str;
while(atoi(str)!=-1)
{
while(!isInt(str))
{
cout<<"请输入数字:";
cin>>str;
}
a = atoi(str);
list.Freqcount(a);
cin>>str;
}
list.print();
return 0;
}
//freqcount.h
template <class Elem>
class Freqlist
{
private:
const int CAPACITY;
Elem *record;
int *count;
int used;
public:
Freqlist(int n):CAPACITY(n)
{
// CAPACITY = n;
record = new Elem[CAPACITY];
count = new int[CAPACITY];
used = 0;
}
~Freqlist()
{
delete []record;
delete []count;
}
bool Freqcount(Elem& record);
void print();
};
template<class Elem>
bool Freqlist<Elem>::Freqcount(Elem& recordval)
{
for(int i = 0; i<used;i++)
{
if(record[i] == recordval)
{
count[i] ++;
for(int j = i-1;j>= 0&&(count[i]>count[j]);j--)
{
if(j!= i-1)
{
Elem temp = record[i];
int counttemp= count[i];
for( int k=i;k>j;k--)
{
record[i] = record[i-1];
count[i] = count[i-1];
}
record[j+1] = temp;
count[j+1] = counttemp;
}
}
return true;
}
}
record[used] = recordval;
count[used] =1;
used++;
return false;
}
template<class Elem>
void Freqlist<Elem>::print()
{
for(int i=0;i<used;i++)
{
cout<<record[i]<<"("<<count[i]<<")"<<" ";
}
cout<<endl;
}
另外
#include "isInt.h"
#include "freqcount.h"
#include <iostream> 语句是放在stdafx.h 还是可放在search1.cpp 中
头文件目录有stdafx.h targetver.h 我添加了freqcount.h isInt.h(用于判断输入是否为数字)
源文件目录有stdafx.cpp 我添加了search1.cpp
//search1.cpp
#include "stdafx.h"
#include "isInt.h"
#include "freqcount.h"
#include <iostream>
using namespace std;
int _tmain(int argc, _TCHAR* argv[])
{
Freqlist<int> list(50);
char str[20];
int a;
cout<<"请输入记录,以-1结束:";
cin>>str;
while(atoi(str)!=-1)
{
while(!isInt(str))
{
cout<<"请输入数字:";
cin>>str;
}
a = atoi(str);
list.Freqcount(a);
cin>>str;
}
list.print();
return 0;
}
//freqcount.h
template <class Elem>
class Freqlist
{
private:
const int CAPACITY;
Elem *record;
int *count;
int used;
public:
Freqlist(int n):CAPACITY(n)
{
// CAPACITY = n;
record = new Elem[CAPACITY];
count = new int[CAPACITY];
used = 0;
}
~Freqlist()
{
delete []record;
delete []count;
}
bool Freqcount(Elem& record);
void print();
};
template<class Elem>
bool Freqlist<Elem>::Freqcount(Elem& recordval)
{
for(int i = 0; i<used;i++)
{
if(record[i] == recordval)
{
count[i] ++;
for(int j = i-1;j>= 0&&(count[i]>count[j]);j--)
{
if(j!= i-1)
{
Elem temp = record[i];
int counttemp= count[i];
for( int k=i;k>j;k--)
{
record[i] = record[i-1];
count[i] = count[i-1];
}
record[j+1] = temp;
count[j+1] = counttemp;
}
}
return true;
}
}
record[used] = recordval;
count[used] =1;
used++;
return false;
}
template<class Elem>
void Freqlist<Elem>::print()
{
for(int i=0;i<used;i++)
{
cout<<record[i]<<"("<<count[i]<<")"<<" ";
}
cout<<endl;
}