1. 程式人生 > >慕課-程式設計與演算法(大學先修課)-郭煒-第八九周練習題

慕課-程式設計與演算法(大學先修課)-郭煒-第八九周練習題

1.下面程式片段的輸出結果是 Hello ,請填空

#include <iostream>
using namespace std;
int main() {
    char s[] = "Hello";  
    char * p;
    for(p=s;*p!=NULL;p++)
        cout << * p ;
    return 0;
}

2.下面程式輸出結果是 Tesla Tes 請填空

#include <iostream>
using namespace std;
void Print(const char * p1, const
char * p2) { for(;p1<p2;p1++) cout << * p1; } int main() { const char * s = "Tesla123"; Print(s,s+5); cout << endl; Print(s,s+3); cout << endl; return 0; }

3.程式填空,使得輸出結果為:
1,4,9,16,25,
h,e,l,l,o,!,

#include <iostream>
using
namespace std; void ForEach(void * a, int width, int num, void f(void *) ) { for(int i = 0;i < num; ++i) f((char*)a+width*i); } void PrintSquare(void * p) { int * q = (int*)p; int n = *q; cout << n * n << ","; } void PrintChar(void * p) { char * q = (char
*)p; cout << *q << ","; } int main() { int a[5] = {1,2,3,4,5}; char s[] = "hello!"; ForEach(a,sizeof(int),5,PrintSquare); cout << endl; ForEach(s,sizeof(char),6,PrintChar); return 0; }

4.Memcpy之一
輸入:
第一行是整數t
接下來是t個整數
再接下來是t個不帶空格的字串,長度不超過20
輸出:
按原樣輸出t個整數和t個字串

#include <iostream>
using namespace std;
void Memcpy(char * src,char * dest,int n)
{
    for(int i=0;i<n;i++)
    {
        dest[i]=src[i];
    }
}
int Strlen(char * s)
{   
    int i;
    for( i = 0; s[i]; ++i);
    return i;
}
int main()  
{
    int a;
    char s1[30];
    char s2[30];
    int t;
    cin >> t;
    for(int i = 0;i < t; ++i) {
        cin >> a;
        int b = 99999999;
        Memcpy((char*)&a,(char *) &b,sizeof(int));
        cout << b << endl;
    }
    for(int i = 0;i < t; ++i) {
        cin >> s1;
        Memcpy(s1,s2,Strlen(s1)+1);
        cout << s2 << endl;
    }
    return 0;
}

5.程式填空,使其輸出結果是: 1,2,3,4, 10,12,14,16, 18,20,11,12,

#include <iostream>
using namespace std;

void Double(int * p, int n)
{
    for(int i = 0;i < n; ++i)
        p[i] *= 2;
}


int main()
{
    int a[3][4] = { { 1,2,3,4},{5,6,7,8},
                    { 9,10,11,12 } };

    Double(
a[1],6
);
    for(int i = 0;i < 3; ++i) {
        for(int j = 0; j < 4; ++j)
            cout << a[i][j] << ",";
        cout << endl; 
    }

    return 0;
}

6.Memcpy之二
輸入:
第一行是整數n (1<=n<=10)
第二行是 n個整數
輸出:
先原序輸出輸入資料中的n個整數
然後再輸出:
1,2,3,4,5,1,2,3,4,5,
123434567
167896789

#include <iostream>
using namespace std;
void Memcpy( void * src, void * dest, int size)
{
char * dest1 = (char *)dest;
    char * src1 = (char *)src;
    for (int i = size-1; i >= 0; i--)
        dest1[i] = src1[i];
}

void Print(int * p,int size)
{
    for(int i = 0;i < size; ++i)
        cout << p[i] << ",";
    cout << endl;
}

int main()
{
    int a[10];
    int n;
    cin >> n;
    for(int i = 0;i < n; ++i)
        cin >> a[i];
    int b[10] = {0};
    Memcpy(a,b,sizeof(a));
    Print(b,n);

    int c[10] = {1,2,3,4,5,6,7,8,9,10};
    Memcpy(c,c+5,5*sizeof(int)); //將c的前一半拷貝到後一半 
    Print(c,10);

    char s[10] = "123456789";
    Memcpy(s+2,s+4,5); //將s[2]開始的5個字元拷貝到s[4]開始的地方 
    cout << s << endl;

    char s1[10] = "123456789";
    Memcpy(s1+5,s1+1,4); //將s1[5]開始的4個字元拷貝到s1[1]開始的地方 
    cout << s1 << endl;


    return 0;
}

7.編寫一個 MyMax函式,可以用來求任何陣列中的最大值 使得程式按要求輸出

#include <iostream>
using namespace std;
void *MyMax(void *a, unsigned int b, int n, int(*fun)(void *, void *))
{
    int judge;
    int *max = (int *)a;
    for (int i = 1; i<n ; ++i)
    {
        judge = fun(max, (int *)a+i);
        if (judge < 0)
            max = (int *)a + i;
    }
    return max;
}
int Compare1(void * n1,void * n2)
{
    int * p1 = (int * )n1;
    int * p2 = (int * )n2;
    return ((*p1)%10) - ((*p2)%10);
}
int Compare2(void * n1,void * n2)
{
    int * p1 = (int * )n1;
    int * p2 = (int * )n2;
    return *p1 - *p2;
}
#define eps 1e-6
int Compare3(void * n1,void * n2)
{
    float * p1 = (float * )n1;
    float * p2 = (float * )n2;
    if( * p1 - * p2 > eps)
        return 1;
    else if(* p2 - * p1 > eps)
        return -1;
    else
        return 0; 
}

int main()
{
    int t;
    int a[10];
    float d[10];
    cin >> t;
    while(t--) {
        int n;
        cin >> n;
        for(int i = 0;i < n; ++i)
            cin >> a[i];
        for(int i = 0;i < n; ++i)
            cin >> d[i];
        int * p = (int *) MyMax(a,sizeof(int),n,Compare1);
        cout << * p << endl;
        p = (int *) MyMax(a,sizeof(int),n,Compare2);
        cout << * p << endl;
        float * pd = (float * )MyMax(d,sizeof(float),n,Compare3);
        cout << * pd << endl;
    }
    return 0;
}

8.指向指標的指標,程式填空使得輸出指定結果

#include <iostream>
using namespace std;
int main()
{
    int x,y,z;
    x = 10;
    y = 20;
    z = 30;

    int * a[3]  = { &x, &y,&z};
    for(int *(*p)=a; p < a + 3; ++p) 
            cout<< * (*p) << endl;
    return 0;

}

9.填寫記憶體交換函式 SwapMemory,使得程式輸出指定結果

#include <iostream>
using namespace std;
void SwapMemory(void * m1,void * m2, int size)
{
if (size > 5)
    {
        int *m11 = (int *)m1;
        int *m22 = (int *)m2;
        int temp;
        for (int i = 0; i < 5; i++)
        {
            temp = *(m11 + i);
            *(m11 + i) = *(m22 + i);
            *(m22 + i) = temp;
        }
    }
    else
    {
        char *m11 = (char *)m1;
        char *m22 = (char *)m2;
        char temp;
        for (int i = 0; i < size; i++)
        {
            temp = *(m11 + i);
            *(m11 + i) = *(m22 + i);
            *(m22 + i) = temp;
        }
    }

}

void PrintIntArray(int * a,int n)
{
    for(int i = 0;i < n; ++i)
        cout << a[i] << ",";
    cout << endl;
}

int main()
{
    int a[5] = {1,2,3,4,5};
    int b[5] = {10,20,30,40,50};
    SwapMemory(a,b,5 * sizeof(int));
    PrintIntArray(a,5);
    PrintIntArray(b,5);
    char s1[] = "12345";
    char s2[] = "abcde";
    SwapMemory(s1,s2,5);
    cout << s1 << endl;
    cout << s2 << endl;
    return 0;
}