1.编写通常接受一个参数(字符串的地址),并打印该字符串的函数。不过,如果提供了第二个参数(int类型),且该参数不为0,则该函数打印字符串的次数将为该函数被调用的次数(注意,字符串的打印次数不等于第二个参数的值,而等于函数被调用的次数)。是的,这是一个非常可笑的函数,但它让读者能够使用本章介绍的一些技术。在一个简单的程序中使用该函数,以演示该函数是如何工作的。
#include
#include
using namespace std;
void scanf ( string str )
{
cout << str << endl;
}
void scanf ( string str, int n )
{
if ( n != 0 )
{
cout << str << endl;
scanf ( str, --n );
}
}
int main()
{
int n;
string str;
cout << "Please enter a string and number" << endl;
while ( cin >> str >> n )
{
if ( n == 0 )
scanf ( str );
else scanf ( str, n );
cout << "Please enter a string and number" << endl;
}
return 0;
}
2、CandyBar结构饱含3个成员。第一个成员存储candy bar的品牌名称;第二个成员存储candy bar的重量(可能有小数);第三个成员存储candy bar的热量(整数)。请编写一个程序,它使用一个这样的函数,即将CandyBar的引用、char指针、double和int作为参数,并用最后3个值设置相应的结构成员。最后3个参数的默认值分别为"Millennium Munch"、2.85和350。另外,该程序还包含一个以CandyBar的引用为参数,并显示结构内容的函数。请尽可能使用const.
note:如果默认参数char b[]不加const可以运行但会提示
warning: ISO C++ forbids converting a string constant to 'char*'
因为
#include
#include
const int ArSize = 100;
using namespace std;
struct candybar
{
char brand[ArSize];
double weight;
int energy;
};
void cset ( candybar &c, const char b[] = "Millennium Munch", double w = 2.85, int e = 350 )
{
strcpy ( c.brand, b );
c.weight = w;
c.energy = e;
}
void display ( const candybar &c )
{
cout << c.brand << endl << c.weight << endl << c.energy << endl;
}
int main()
{
candybar c;
char b[ArSize];
double w;
int e;
cset ( c );
display ( c );
cout <> w;
cout << endl;
cout << "c.energy = ";
cin >> e;
cout << endl;
cset ( c, b, w, e );
display ( c );
return 0;
}
enter a string (q to quit) :go away
GO AWAY
next string (q to quit) : good grief !
GOOD GRIEF!
next string (q to quit) : q
bye.
这题我没包含cctype,因为我的编译器已自动加进去了,你们用的天一起可能没有,所以要自己加
#include
#include
using namespace std;
void change ( string &s )
{
int i = 0;
while ( s[i] != '\0' )
{
if ( islower ( s[i] ) )
s[i]=toupper ( s[i] );
cout << s[i++];
}
cout << endl;
}
int main()
{
string str;
cout << "Enter a string (q to quit): ";
while ( getline ( cin, str ) && str != "q" )
{
change ( str );
cout << "next string (q to quit): ";
}
cout << "Bye.";
return 0;
}
4.下面是一个程序框架
#include
using namespace std;
#include
//for strlen(),strcpy()
//指向字符串
struct stringy {
char * str; //points to a string
int ct; //length of string (not couting '\0')
};
// prototypes for set(), show(), and show() go here
// set、show、show 三个函数的原型
int main()
{
string beany;
char testing[]="Reality isn't what it used to be.";
set(beany,testing); //first argument is a reference,
//allocates space to hold copy of testing
//sets str member of beany to point to the
//new block, copies testing to new block,
//and sets ct member of beany
请提供其中描述的函数和原型,从而完成该程序。注意,应有两个show()函数,每个都使用默认参数。请尽可能的使用const参数。set() 使用new分配足够的空间来存储制定的字符串。这里使用的技术与设计和实现类使用的相似。(可能害必须修改头文件的名称,删除using编译指令,这取决于所用的编译器。)
#include
using namespace std;
#include
struct stringy
{
char * str;
int ct;
};
void set ( stringy& a, const char *t )
{
a.ct = strlen ( t ) + 1;
a.str = new char[a.ct];
strcpy ( a.str, t );
}
void show ( const stringy& a, int n = 1 )
{
for ( int i = 0; i < n; i++ )
cout << a.str << endl;
}
void show ( const char* t, int n = 1 )
{
for ( int i = 0; i < n; i++ )
{
if ( strcmp ( t, "Done!" ) == 0 )
{
cout << "Done!" << endl;
break;
}
cout << t << endl;
}
}
int main()
{
stringy beany;
char testing[] = "Reality isn't what it used to be.";
set ( beany, testing );
show ( beany );
show ( beany, 2 );
testing[0] = 'D';
testing[1] = 'u';
show ( testing );
show ( testing, 3 );
show ( "Done!" );
return 0;
}
#include
using namespace std;
template
T max(T *a)
{
for(int i=0;i<5;i++)
if(a[i]>a[0]) a[0]=a[i];
return a[0];
}
int main()
{
int a[5]={15,21,3,4,55};
double b[5]={3.5,89.5,45.5,99.5,1.5};
cout<
/*我把template<> const char* maxn( const char **a, const int n ) const去掉
template<> char* maxn( char **a, const int n ) 输出是Fuck me,这样的话maxn ( c, 5 )调用的就是第一个模板,比较地址,所以是fuckme
#include
#include
using namespace std;
template
T maxn ( T *a, const int n )
{
for ( int i = 0; i < n; i++ )
if ( a[i] > a[0] ) a[0] = a[i];
return a[0];
}
template<> const char* maxn( const char **a, const int n )
{
int i,j=0;
for(i=0;istrlen(a[j])) j=i;
return a[j];
}
int main()
{
int a[6] = {5, 7, 3, 6, 8, 0};
double b[4] = {1.1, 2.2, 0.1, 1.01};
const char *c[5] = {"Fuck", "Fuck you", "Fuck you now", "Fuck you and mother", "Fuck me"};
cout << maxn ( a, 6 ) << endl;
cout << maxn ( b, 4 ) << endl;
cout << maxn ( c, 5 ) << endl;
return 0;
}