第8章函数探幽
编程题
#include <iostream>
using namespace std;
void loop_print(const char *str , int n = 0);
int main(int argc, char const *argv[])
{
loop_print("Hello World!");
loop_print("Hello World!");
loop_print("Hello World!",1);
return 0;
}
void loop_print(const char * str, int n ){
static int count = 0; // 使用静态变量存储函数的调用次数
count ++ ;
if(!n){
cout << str << endl;
}
else{
for(int i = 0;i < count; i++){
cout << "Print No." << i+1 << "." << endl;
cout << str << endl;
}
}
}
#include <iostream>
using namespace std;
#include <string>
struct CandyBar{
string brand;
double weight;
int colories;
};
void createCandy(CandyBar &, string, double , int);
void show(const CandyBar &);
int main(int argc, char const *argv[])
{
CandyBar cb;
createCandy(cb, "Millennium Munch",2.85,3);
show(cb);
return 0;
}
void createCandy(CandyBar & candy, string brand, double weight, int colories){
candy.brand = brand;
candy.weight = weight;
candy.colories = colories;
}
void show(const CandyBar & cb){
cout << "Candy info:" << endl;
cout << "Candy Brand:" << cb.brand << endl;
cout << "Candy Weight: " << cb.weight << endl;
cout << "Candy Colories: " << cb.colories << endl;
}
#include <iostream>
using namespace std;
#include <string>
#include <cctype>
void stringToUpper(string &);
int main(int argc, char const *argv[])
{
// string str = "Hello World";
// cout << "Before translate :" << str << endl;
// stringToUpper(str);
// cout << "After translate :" << str << endl;
string st;
cout << "Enter a string (q to quit): " ;
getline(cin , st);
while(st != "q"){
stringToUpper(st);
cout << st << endl;
cout << "Next string (q to quit) :" ;
getline(cin, st);
}
cout << "Bye." << endl;
return 0;
}
void stringToUpper(string & str){
int size = str.size();
// for(string::iterator begin = str.begin(); begin != str.end(); begin ++ ){
// }
for(int i = 0; i < size; i++){
if(islower(str[i])){
str[i] = toupper(str[i]);
}
}
}
#include <iostream>
using namespace std;
#include <cstring> // 为了使用strcpy() strlen()
struct stringy
{
char *str;
int ct;
};
void show(const string & , int n =0);
void show(const stringy &, int n = 0);
void set(stringy &, char *);
int main(int argc, char const *argv[])
{
stringy beany;
char testing[] = "Reality isn't what it used to be." ;
set(beany , testing);
show(beany);
show(beany,2);
testing[0] = 'P';
testing[1] = 'A';
show(testing);
show(testing,3);
show("Done!");
/*回收堆内存*/
delete beany.str;
return 0;
}
void show(const string & st , int n ){
if(n == 0 ) n++;
for(int i = 0; i < n ;i++){
cout << st << endl;
}
}
void show(const stringy & sty, int n ){
if (n == 0){
n ++ ;
}
for(int i = 0; i < n;i++){
cout << sty.str << endl;
}
}
/*输出 stringy 类型对象的信息*/
void set(stringy & sty, char *st){
sty.ct = strlen(st);
sty.str = new char[sty.ct];
/*通过new 创建动态存储,此处不考虑回收*/
strcpy(sty.str,st);
}
#include <iostream>
using namespace std;
template <class T>
T max5(T [] , int n=5);
int main(int argc, char const *argv[])
{
int x[] = {1,2,3,4,5};
int res = max5(x);
cout << "max value of x array : " << res << endl;
double y[] = {4.8,3.4,9.8,10,1.2};
cout << "max value of y array:" << max5(y) << endl;
return 0;
}
template <class T>
T max5(T x[] , int n ){
int max = x[0];
for(int i = 0; i < n ; i++){
max = max < x[i] ? x[i]: max;
}
return max;
}
#include <iostream>
using namespace std;
#include <cstring>
template <typename T>
T maxn(T[],int n);
template <> char* maxn(char * str[], int n );
int main(int argc, char const *argv[])
{
int arr[5] = {1,2,3,4,5};
char *str [5] = {"wqewfw","dfg","fweshif","revdser","Hellofwe"};
cout << "Max value int arr " << maxn(arr, 5) << endl;
cout << "Max length string int str " << maxn(str, 5) << endl;
return 0;
}
template <typename T>
T maxn(T a[],int n){
T max = a[0];
for(int i = 0; i< n; i++){
max = max > a[i] ? max : a[i];
}
return max;
}
template <> char* maxn<char*>(char * str[], int n ){
cout << "Hello " << endl;
int pos = 0;
for(int i =0 ;i < n; i++){
if(strlen(str[pos]) < strlen(str[i])){
pos = i;
}
}
return str[pos];
}
标签:const,函数,show,int,char,探幽,include,string
From: https://www.cnblogs.com/lofly/p/16619338.html