fshb_ 1370
سه شنبه 05 مهر 1390, 10:48 صبح
سلام
error های این برنامه رو چطور برطرف کنم؟
string2.h
#include<iostream>
using namespace std;
#ifndef STRING2_H_
#define STRING2_H_
class String
{
private:
char *str;
int len;
static int num_strings;
static const int CINLIM=80;
public:
String();
String(const char *s);
String(const String &);
~String();
int length()const{return len;}
String & operator=(const String &s);
String & operator=(const char *);
char & operator[](int i);
const char & operator[](int i)const;
String& stringlow(String &s);
String& stringup(String &s);
int count(char c);
friend bool operator>(const String &s1,const String & s2);
friend bool operator<(const String &s1,const String & s2);
friend bool operator==(const String &s1,const String &s2);
friend String& operator+(const string &s1,const String &s2);
friend ostream & operator<<(ostream &os,const String &s);
friend istream & operator>>(istream &is,String &s);
static int HowMany();
};
#endif
string2.cpp
#include <iostream>
#include <cstring>
#include <cctype>
using namespace std;
#include "string2.h"
int String::num_strings=0;
int String::HowMany()
{
return num_strings;
}
String::String()
{
len=4;
str=new char[1];
str[0]='\ 0';
num_strings++;
}
String::String(const char *s)
{
len=strlen(s);
str=new char[len+1];
strcpy(str,s);
num_strings++;
}
String::String(const String &s)
{
num_strings++;
len=s.len;
str=new char[len+1];
strcpy(str,s.str);
}
String::~String()
{
--num_strings;
delete [] str;
}
String& String::operator =(const char *s)
{
delete [] str;
len=strlen(s);
str=new char[len+1];
strcpy(str,s);
return *this;
}
String& String::operator =(const String &s)
{
if (this==&s)
return *this;
delete [] str;
len=s.len;
str=new char[len+1];
strcpy(str,s.str);
return *this;
}
char& String::operator [](int i)
{
return str[i];
}
const char& String::operator [](int i) const
{
return str[i];
}
String& String::stringlow(String &s)
{
for (int i=0;i<s.len;i++)
tolower(s.str[i]);
return s;
}
String& String::stringup(String &s)
{
for (int i=0;i<s.len;i++)
toupper(s.str[i]);
return s;
}
//
int count(char c)
{
int counter=0;
for (int i=0;i<len;i++)
if(str[i]==c)
counter++;
return counter;
}
bool operator<(const String &s1,const String &s2)
{
return (strcmp(s1.str,s2.str)<0);
}
bool operator>(const String &s1,const String &s2)
{
return s2.str<s1.str;
}
bool operator==(const String &s1,const String &s2)
{
return (strcmp(s1.str,s2.str)==0);
}
//
String& operator+(const String &s1,const String &s2)
{
String s;
s.len=s1.len+s2.len;
s.str=new char[s.len+1];
strcpy(s.str,s1.str);
int count=0;
for(int i=s1.len;i<s.len;i++)
{
s.str[i+1]=s2.str[count];
count++;
}
s.str[s.len]='\0';
return s;
}
ostream & operator<<(ostream &os,const String &s)
{
os<<s.str;
return os;
}
istream & operator>>(istream &is,String &s)
{
char temp[String::CINLIM];
is.get(temp, String::CINLIM);
if (is)
s = temp;
while (is && is.get() != '\ n')
continue;
return is;
}
error های این برنامه رو چطور برطرف کنم؟
string2.h
#include<iostream>
using namespace std;
#ifndef STRING2_H_
#define STRING2_H_
class String
{
private:
char *str;
int len;
static int num_strings;
static const int CINLIM=80;
public:
String();
String(const char *s);
String(const String &);
~String();
int length()const{return len;}
String & operator=(const String &s);
String & operator=(const char *);
char & operator[](int i);
const char & operator[](int i)const;
String& stringlow(String &s);
String& stringup(String &s);
int count(char c);
friend bool operator>(const String &s1,const String & s2);
friend bool operator<(const String &s1,const String & s2);
friend bool operator==(const String &s1,const String &s2);
friend String& operator+(const string &s1,const String &s2);
friend ostream & operator<<(ostream &os,const String &s);
friend istream & operator>>(istream &is,String &s);
static int HowMany();
};
#endif
string2.cpp
#include <iostream>
#include <cstring>
#include <cctype>
using namespace std;
#include "string2.h"
int String::num_strings=0;
int String::HowMany()
{
return num_strings;
}
String::String()
{
len=4;
str=new char[1];
str[0]='\ 0';
num_strings++;
}
String::String(const char *s)
{
len=strlen(s);
str=new char[len+1];
strcpy(str,s);
num_strings++;
}
String::String(const String &s)
{
num_strings++;
len=s.len;
str=new char[len+1];
strcpy(str,s.str);
}
String::~String()
{
--num_strings;
delete [] str;
}
String& String::operator =(const char *s)
{
delete [] str;
len=strlen(s);
str=new char[len+1];
strcpy(str,s);
return *this;
}
String& String::operator =(const String &s)
{
if (this==&s)
return *this;
delete [] str;
len=s.len;
str=new char[len+1];
strcpy(str,s.str);
return *this;
}
char& String::operator [](int i)
{
return str[i];
}
const char& String::operator [](int i) const
{
return str[i];
}
String& String::stringlow(String &s)
{
for (int i=0;i<s.len;i++)
tolower(s.str[i]);
return s;
}
String& String::stringup(String &s)
{
for (int i=0;i<s.len;i++)
toupper(s.str[i]);
return s;
}
//
int count(char c)
{
int counter=0;
for (int i=0;i<len;i++)
if(str[i]==c)
counter++;
return counter;
}
bool operator<(const String &s1,const String &s2)
{
return (strcmp(s1.str,s2.str)<0);
}
bool operator>(const String &s1,const String &s2)
{
return s2.str<s1.str;
}
bool operator==(const String &s1,const String &s2)
{
return (strcmp(s1.str,s2.str)==0);
}
//
String& operator+(const String &s1,const String &s2)
{
String s;
s.len=s1.len+s2.len;
s.str=new char[s.len+1];
strcpy(s.str,s1.str);
int count=0;
for(int i=s1.len;i<s.len;i++)
{
s.str[i+1]=s2.str[count];
count++;
}
s.str[s.len]='\0';
return s;
}
ostream & operator<<(ostream &os,const String &s)
{
os<<s.str;
return os;
}
istream & operator>>(istream &is,String &s)
{
char temp[String::CINLIM];
is.get(temp, String::CINLIM);
if (is)
s = temp;
while (is && is.get() != '\ n')
continue;
return is;
}