 |
#include <iostream> using namespace std;
int T(int a[4][5]) { int z=0; for (int i=0;i<=3;i++) { for (int y=0;y<=4;y++) z=z+a[i][y]; } return z; }
int Y(int a[4][5]) { int D=0; for (int b=0;b<=4;b++) for (int e=0;e<=3;e++) D=D+a[b][e]; return D; }
int main() { int f[4][5]={{1,2,3,4,5},{6,7,8,9,10},{11,12,13,14,15},{16,17,18,19,20}}; int x,w; x=T(f); w=Y(f); cout<<"各列之和="<<x<<endl; cout<<"各行之和="<<w<<endl; return 0; }
|
 |
#include<iostream> #include<stdlib.h> using namespace std; //函数指针 void (*fp) (int a[],int n); //两个数交换大小 void swap(int *a, int *b){ int x; x = *a; *a = *b; *b = x; } //冒泡排序实现代码 void bubble_sort(int A[],int n){ int t; for(int i=0;i<n-1;i++) { for(int j=0;j<n-i-1;j++) //注意在内层循环中j的结束值是 n-i-1 { if(A[j+1]<A[j]) swap(A[j+1],A[j]); } } } //选择排序实现代码 void selection_sort(int a[],int n){ int temp,min; for(int i=0;i<n;i++) { min=i;//先假设最小下标为i for(int j=i+1;j<n;j++) if(a[j]<a[min]) min=j;//对i之后的数进行扫描将最小的数赋予min if(min!=i) swap(a[min],a[i]); } } //插入排序实现代码 void insert_sort(int a[],int n){ int i,j,t; for ( i=1; i<n; i++) //i表示插入次数,共进行n-1次插入 { t=a[i]; //把待排序元素赋给t j=i-1; while ((j>=0)&& (t<a[j])) { a[j+1]=a[j]; j--; } // 顺序比较和移动 a[j+1]=t; } } //希尔排序实现代码 void insertion_sort(int data[], int n, int increment){ int i, j; for(i = increment; i < n; i += increment) for(j = i; j >= increment && data[j] < data[j - increment]; j -= increment) swap(&data[j], &data[j - increment]); } void shell_sort(int data[], int n){ int i, j; for(i = n / 2; i > 2; i /= 2) for(j = 0; j < i; j++) insertion_sort(data + j, n - j, i); insertion_sort(data, n, 1); } int main(){ const int n=10; int i; int a[n]={5,-2,8,7,6,14,0,-9,91,-32}; char key; cout<<"排序前:"<<endl; for(i=0;i<n;i++) cout<<a[i]<<" "; cout<<endl; cout<<"*******请输入你想用什么算法排序(0,退出)*********"<<endl; cout<<"*1、冒泡排序,2、选择排序,3、插入排序,4、希尔排序*"<<endl; cin>>key; while(key!='0') { switch(key) { case '1':{fp=&bubble_sort;fp(a,n);break;} case '2': {fp=&selection_sort;fp(a,n);break;} case '3': {fp=&insert_sort;fp(a,n);break;} case '4': {fp=&shell_sort;fp(a,n);break;} default:break; } cout<<"排序后:"<<endl; for(i=0;i<n;i++) cout<<a[i]<<" "; cout<<endl; cout<<"请输入你的选择:\t"; cin>>key; } }
|
 |
#include <iostream> using namespace std; class TestClass { public: TestClass(int i,int j):m_Num2(j) { m_Num1=i; m_Num3+=i; } void Print() { cout<<"m_Num1="<<m_Num1<<endl; cout<<"m_Num2="<<m_Num2<<endl; cout<<"m_Num3="<<m_Num3<<endl; } void Add(int i) { m_Num3+=i; }
private: int m_Num1; const int m_Num2; static int m_Num3; friend void fun(); };
int TestClass::m_Num3=0; void fun() { TestClass Num(1,2); Num.m_Num1=4; Num.Add(5); Num.Print(); } int main() { TestClass Num(1,2); Num.Print(); fun(); return 0; }
|
 |
#include "A.h"
int main() { A* p=new A[10]; p[0].get(); A* p1[10]; p1[0]=new A(); p1[0]->get(); delete p; return 0; }
class A { public: void get(); };
#include <iostream> #include "A.h" using namespace std;
void A::get() { cout<<"大神告诉我答案\n"<<endl; }
|