1.
读取INt型变量地址四个字节1&4交换,2与3交换
#include "stdafx.h"
#include
#include
void swap(char **src,char**dst)
{
char *temp = NULL;
temp=*src;
*src=*dst;
*dst=temp;
// src++;dst++;
}
int main(int argc, char* argv[])
{
int i=55;
// char a =*(char *)&i;
char *p = (char *)&i;
char *p1 = (char *)&i+1;
char *p2 = (char *)&i+2;
char *p3 = (char *)&i+3;
swap(&p,&p3);
printf("%s\n",p);
printf("%s\n",p3);
return 0;
}
2.
几种排序算法
#include
#include
using namespace std;
void MaoPaoSort(int arr[],int n);
void QuickSort(int arr[], int first, int end);
void ChoiseSort(int arr[], int n);
void BinSearchSort(int arr[], int n, int nFind);
void BinSearchSort_DiGui(int arr[], int left, int right, int nFind);
void InsertSort(int arr[], int n);
void Swap(int &n1, int &n2);
void PrintData(int arr[],int n);
int main(int, char **, char **)
{
int a[] ={1,3,5,7,8,4,9,2,6,0};
int n = sizeof(a)/sizeof(int);
// MaoPaoSort(a,n);
// InsertSort(a,n);
// ChoiseSort(a,n);
QuickSort(a,0,n-1);
PrintData(a,n);
cout< // BinSearchSort(a, n, 7); // BinSearchSort_DiGui(a,0,n,7); return 0; } // 快速排序,递归思想,和二分法一样 void QuickSort(int arr[], int first, int end) { int i=first,j=end,temp=arr[first]; while(i { while(i j--; swap(arr[i],arr[j]); while(i i++; swap(arr[j],arr[i]); } if(first QuickSort(arr,first,i-1); if(end>i+1) QuickSort(arr,i+1,end); } // 选择排序 每次循环找到一个最大或者最小 和冒泡相似 void ChoiseSort(int arr[], int n) { for(int i=0; i< n-1; i++) { for(int j=i+1; j { if(arr[j] < arr[i]) swap(arr[i],arr[j]); } } } // 二分法查找 void BinSearchSort(int arr[], int n, int nFind) { int Min = 0; int Max = n; while(Min <=Max) { int mid = (Min+Max)/2; if(arr[mid] < nFind) Min =mid +1; else if(arr[mid] >nFind) Max = mid -1; else { cout<<"Elemet At:" < return ; } } } // 二分法递归 void BinSearchSort_DiGui(int arr[], int left, int right, int nFind) { int mid = (left+right)/2; if(arr[mid] BinSearchSort_DiGui(arr, mid+1, right, nFind); else if(arr[mid] >nFind) BinSearchSort_DiGui(arr, left, mid-1, nFind); else cout<<"Elemet At:" < } // 直接插入排序 void InsertSort(int arr[], int n) { for(int i =1; i { for(int j =0; j
{ if(arr[i] < arr[j]) swap(arr[i],arr[j]); } } } // 冒泡排序 void MaoPaoSort(int arr[],int n) { for(int i=0; i { for(int j =0; j { if(arr[j+1] > arr[j]) swap(arr[j+1],arr[j]); } } } // 交换 void Swap(int &n1, int &n2) { int temp; temp = n1; n1 = n2; n2 = temp; } // 打印 void PrintData(int arr[],int n) { for(int i =0; i { cout< } } 3. 字符串颠倒:指出错误 int main() { char* src = "hello,world"; int len = strlen(src); char* dest = (char*)malloc(len+1);//要为\0分配一个空间 char* d = dest; char* s = &src[len-1];//指向最后一个字符 while( len-- != 0 ) *d++=*s--; *d = 0;//尾部要加\0 printf("%s\n",dest); free(dest);// 使用完,应当释放空间,以免造成内存汇泄露 return 0; } 4. 输入一个正整数,要求判断这个数是否是对称数(比如121,123321,1456541)。要求不能调用字符串库函数 #include #include #include int main(int argc, char* argv[]) { while(1) { unsigned int nInt = 0; scanf("%d",&nInt); if(0 == nInt) break; char szDigit[32]; memset(szDigit,0,sizeof(szDigit)); unsigned int nIntT = nInt; int nCount = 0; while(nIntT > 0 && nCount < sizeof(szDigit) - 1) { szDigit[nCount] = nIntT % 10; nIntT /= 10; nCount++; } bool bRel = true; for(int i = 0; i { if(szDigit[i] != szDigit[nCount - i - 1]) bRel = false; } if(bRel) printf("%d is Y\n",nInt); else printf("%d is N\n",nInt); } system("pause"); return 0; } 5. atoi函数实现 int atoi(const char *s) { char *p = s; char c; int i = 0; while(c=*p++) { if(c>='0' && c<='9') { i = i*10 + (c-'0'); } else return -1; //Invalid string } return i; } 6. itoa函数实现 int main(int, char **, char **) { char temp[20]={0}; int n =12345; int i =0; do { temp[i] = n%10+'0'; n = n/10; i++; } while (n>0); cout< int n1 = strlen(temp); for(i=n1-1; i>=0; i--) { cout< } cout< return 0; } 7. strcpy函数实现 char *strcpy(char *strDest, const char *strSrc) { assert((strDest!=NULL) && (strSrc !=NULL)); // 2分 if(strDest ==strSrc) return strDest; char *address = strDest; // 2分 while( (*strDest++ = * strSrc++) != ‘\0’ ) // 2分 NULL ; return address ; // 2分 } 8. strcmp函数实现 int strcmp (char *str1, char *str2) { while ( *str1 && *str2 && *str1++ == *str2++ ) ; if (*str1 > *str2) return 1; /* string1 > string2 */ else if (*str1 == *str2) return 0; /* string1 == string2 */ else return -1; /* string1 < string 2 */ } 9. strcat函数实现 char * strcat(char * dest, const char * src) { char *tmp = dest; while (*dest) dest++; while ((*dest++ = *src++) != '\0') ; return tmp; }