C++第五章编程
1.字符串中插入字符串(有待用函数方法改进)
#include <iostream>
#include <string.h>
using namespace std;
char wordInsert(char str1[], char str2[], int n);
int main(){
char str1[] = {"hello"};
char str2[] = {"world"};
int n;
cout<<"请输入在主字符串第几个字符后插入子字符串"<<endl;
cin>>n;
cout<<"得到的字符串是:";
wordInsert(str1,str2,n);
return 0;
}
char wordInsert(char str1[], char str2[] , int n){
char str[10];
for (int i=0;i<n;i++){
str[i]=str1[i];
}
for (int i=0;i<5;i++){
str[n+i]=str2[i];
}
for (int i=n;i<n+(5-n);i++){
str[5+i] = str1[i];
}
for (int i=0;i<10;i++){
cout<<str[i];
}
return 0;
}
2.字符串单词、字母统计
#include <iostream>
#include <string>
using namespace std;
int main(){
int alphabetNumber=0,wordNum=0;
string str1;
cout<<"请输入字符串:";
getline(cin,str1);
unsigned long n = str1.length();
cout<<"字符串长度为:"<<n<<endl;
for (int i=0; i <int(n); i++ ){
if ( (str1[i]>=(0x41))&&(str1[i]<=(0x41+26)) || ( (str1[i]>=(0x61))&& (str1[i]<=(0x61+26))) ) {
alphabetNumber++;
}
if (str1[i]==0x20){
wordNum++;
}
}
if (alphabetNumber!=0){
wordNum++;
}
cout<<"字母个数是:"<<alphabetNumber<<endl;
cout<<"单词的个数是:"<<wordNum<<endl;
return 0;
}
3.动态数组
#include <iostream>
using namespace std;
int main(){
int n;
double sum=0,x0=0,sum1=0,delta=0,s ;
cout<<"请输入需要数字数目:";
cin>>n;
double *a = new double[n];
for (int i=0;i<n;i++){
double m;
cout<<"请输入第"<<i+1<<"个数"<<endl;
cin>>m;
a[i]=m;
sum = sum+a[i];
}
x0 = sum/n;
for (int i=0;i<n;i++){
delta = delta + (a[i]-x0)*(a[i]-x0);
}
s=delta;
cout<<"方差s为:"<<s<<endl;
delete []a;
return 0;
}
4.判断对称阵
#include <iostream>
using namespace std;
int main(){
bool flag = 0;
double a[5][5];
cout<<"判断输入的5*5矩阵是否为对称阵"<<endl;
for (int i=0;i<5;i++){
for (int j=0;j<5;j++){
cout<<"请输入a["<<i+1<<"]["<<j+1<<"]:"<<endl;
cin>>a[i][j];
}
}
cout<<"输入的矩阵是:"<<endl;
for (int i=0;i<5;i++){
for (int j=0;j<5;j++){
cout<<a[i][j]<<"\t";
}
cout<<endl;
}
for (int i=0;i<5;i++){
for (int j=0;j<5;j++){
if (a[i][j]==a[j][i]){
flag = true;
}else{
flag = false;
cout<<"no!"<<endl;break;
}
}
if(!flag){
break;
}
}
if(flag) {
cout << "yes" << endl;
}
return 0;
}
5.求随机矩阵非对角元素的和
#include <iostream>
using namespace std;
int main(){
bool flag = 0;
double a[5][5];
cout<<"判断输入的5*5矩阵是否为对称阵"<<endl;
for (int i=0;i<5;i++){
for (int j=0;j<5;j++){
cout<<"请输入a["<<i+1<<"]["<<j+1<<"]:"<<endl;
cin>>a[i][j];
}
}
cout<<"输入的矩阵是:"<<endl;
for (int i=0;i<5;i++){
for (int j=0;j<5;j++){
cout<<a[i][j]<<"\t";
}
cout<<endl;
}
for (int i=0;i<5;i++){
for (int j=0;j<5;j++){
if (a[i][j]==a[j][i]){
flag = true;
}else{
flag = false;
cout<<"no!"<<endl;break;
}
}
if(!flag){
break;
}
}
if(flag) {
cout << "yes" << endl;
}
return 0;
}
6.求鞍点
#include <iostream>
using namespace std;
double findMin(double a[][5],int i){
double minRow=a[i][0];
int record=0;
for (int k=0;k<5;k++){
if (a[i][k]<minRow) { //找每行的最小
minRow = a[i][k];
record= k;
}
}
return record;
}
int main(){
double a[5][5];
srand(time(0));
for (int i=0;i<5;i++){
for (int j=0;j<5;j++){
a[i][j]=rand()%1000;
}
}
cout<<"所求的5*5随机矩阵为"<<endl;
for(int i=0;i<5;i++){
for(int j =0;j<5;j++){
cout<<a[i][j]<<"\t";
}
cout<<endl;
}
for (int i=0;i<5;i++){
int p = findMin(a,i);
double maxList = a[i][p];
int record1=i;
for(int num=0;num<5;num++){
if (a[num][p]>maxList){
maxList = a[num][p];
record1 = num;
}
}
if ( (record1==i)||(a[i][p]==a[record1][p]) ){
cout<<"第"<<i+1<<"行的鞍点是a["<<i+1<<"]["<<p+1<<"]="<<maxList<<endl; //视图符合从1开始
}else{
cout<<"第"<<i+1<<"行不存在鞍点"<<endl;
}
}
return 0;
}
7.