練習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;
}