练习6.10
#include "stdafx.h"
#include "iostream"
#include "stddef.h"
#include<vector>
using namespace std;
//2018_7_26
void exchange(int *p, int *q);
int main()
{
cout << "please enter two numbers" << endl;
int x1, x2;
cin >> x1 >> x2;
exchange(&x1,&x2);
cout <<"x1:"<< x1<<" x2:" << x2 << endl;
system("pause");
return 0;
}
void exchange(int *p, int *q)
{
int temp;
temp = *p;
*p = *q;
*q = temp;
}
6.11
#include "stdafx.h"
#include "iostream"
#include "stddef.h"
#include<vector>
using namespace std;
//2018_7_26
void reset(int &x);
int main()
{
int x;
cin >> x;
reset(x);
cout << x << endl;
system("pause");
return 0;
}
void reset(int &x)
{
x = 9999;
}
6.12
#include "stdafx.h"
#include "iostream"
#include "stddef.h"
#include<vector>
using namespace std;
//2018_7_26
void exchange(int &p, int &q);
int main()
{
cout << "please enter two numbers" << endl;
int x1, x2;
cin >> x1 >> x2;
exchange(x1, x2);
cout << "x1:" << x1 << " x2:" << x2 << endl;
system("pause");
return 0;
}
void exchange(int &p, int &q)
{
int temp;
temp = p;
p = q;
q = temp;
}
6.13
完整的寻找特定字符的程序
#include "stdafx.h"
#include "iostream"
#include "stddef.h"
#include "string"
#include<vector>
using namespace std;
//2018_7_26
int find(const string &s, char theone, int &count);
int main()
{
string s;
cout << "输入字符串" << endl;
cin >> s;
char theone;
cout << "输入要找的字母" << endl;
cin >> theone;
int count;
cout << "第一次出现的位置为:" << find(s, theone, count)+1<< endl;
cout << "出现次数为:" << count << endl;
system("pause");
return 0;
}
int find(const string &s, char theone, int &count)
{
auto ret = s.size();
count = 0;
for (decltype(ret) i = 0;i != s.size();++i)
{
if (s[i] == theone)
{
if (ret == s.size())
ret = i;
++count;
}
}
return ret;
}
6.17
#include "stdafx.h"
#include "iostream"
#include "stddef.h"
#include "string"
#include<vector>
using namespace std;
//2018_7_28 判断是否有大写字母
void judge(const string &s);
int main() {
cout << "请输入要检查的字符串" << endl;
string s;
cin >> s;
judge(s);
system("pause");
return 0;
}
void judge(const string &s)
{
int flag = 0;
for (auto c : s)//用这个for循环对字符串内的每个对象进行操作
{
if (!islower(c))
{
cout << "这个字符串有大写字母" << endl;
flag = 1;
break;
}
}
if (flag ==0)
cout << "这个字符串没大写字母" << endl;
}
6.21
#include "stdafx.h"
#include "iostream"
#include "stddef.h"
#include "string"
#include<vector>
using namespace std;
//2018_7_26
int exchange(int x1, int *p1);
int main() {
cout << "请输入两个数" << endl;
int x1, x2;
int *p1;
cin >> x1 >> x2;
p1 = &x2;//第二个数将由指针所指
cout << "较大值为" << exchange(x1, p1) << endl;;
return 0;
}
int exchange(int x1, int *p1)
{
if (x1 >= *p1)
return x1;
else return *p1;
}