展开全部
1. 写一个函数,完成内存之间的拷贝。[考虑问题是否全面]
void* mymemcpy( void *dest, const void *src, size_t count )
{
char* pdest = static_cast<char*>( dest );
const char* psrc = static_cast<const char*>( src );
if( pdest>psrc && pdest<psrc+cout ) 能考虑到这种情况就行了
{
for( size_t i=count-1; i!=-1; --I )
pdest[i] = psrc[i];
}
else
{
for( size_t i=0; i<count; ++I )
pdest[i] = psrc[i];
}
return dest;
}int main( void )
{
char str[] = "0123456789";
mymemcpy( str+1, str+0, 9 );
cout << str << endl; system( "Pause" );
return 0;
}
2、实现一个函数,把一个字符串中的字符从小写转为大写。
#include "stdio.h"
#include "conio.h"
void uppers(char *s,char *us)
{
for(;*s!='\0';s++,us++)
{
if(*s>='a'&&*s<='z')
*us = *s-32;
else
*us = *s;
}
*us = '\0';
}
void main()
{
char *s,*us;
char ss[20];
printf("Please input a string:\n");
scanf("%s",ss);
s = ss;
uppers(s,us);
printf("The result is:\n%s\n",us);
getch();
}
3.如图: 7 8 9 10
6 1 2 11
5 4 3 12
16 15 14 13
设“1”的坐标为(0,0) “7”的坐标为(-1,-1) 编写一个小程序,使程序做到输入坐标(X,Y)之后显示出相应的数字。
#include<stdio.h>
void main(void)
{
int circleX, circleY, circle, x, y, num, numX, numY, i;
scanf("%d %d", &x, &y);
//计算属于第几圈
if(x>=1)
circleX=x;
else if(x<=0)
circleX=1-x;
if(y>=1)
circleY=y;
else if(y<=0)
circleY=1-y;
circle=circleX>circleY?circleX:circleY;
//获得本圈第一个数及其坐标
num = 4*(circle-1)*(circle-1)+1;
numX=circle-1;
numY=1-circle;
//查询输入坐标对应的数
for(i=0;i<2*circle-2;i++)
{
if(numX==x && numY==y)
{
printf("The number is:%d\n",num);
return;
}
numX--;
num++;
}
for(i=0;i<2*circle-1;i++)
{
if(numX==x && numY==y)
{
printf("The number is:%d\n",num);
return;
}
numY++;
num++;
}
for(i=0;i<2*circle-1;i++)
{
if(numX==x && numY==y)
{
printf("The number is:%d\n",num);
return;
}
numX++;
num++;
}
for(i=0;i<2*circle;i++)
{
if(numX==x && numY==y)
{
printf("The number is:%d\n",num);
return;
}
numY--;
num++;
}
}
}
4. 编程实现十进制数转化为十六进制输出,不准用任何已经定义的库函数
#include <stdio.h>
void main()
{
int n,i=0;
char glam[17]="0123456789ABCDEF"; //定义字符表
char hex[20]=""; //用于存储转换之后的十六进制数
printf("please enter n(dec):\n");
scanf("%d",&n);
do{
hex[i++]=glam[n%16];
n=n/16;
}while(n>0);
printf(hex);
}
5.写一段程序,找出数组中第k大小的数,输出数所在的位置。例如{2,4,3,4,7}中,第一大的数是7,位置在4。第二大、第三大的数都是4,位置在1、3随便输出哪一个均可。函数接口为:int find_orderk(const int* narry,const int n,const int k) 要求算法复杂度不能是O(n^2)
#include<iostream>
usingnamespacestd;
int Partition (int*L,intlow,int high)
{
int temp = L[low];
int pt = L[low];
while (low < high)
{
while (low < high && L[high] >= pt)
--high;
L[low] = L[high];
while (low < high && L[low] <= pt)
++low;
L[low] = temp;
}
L[low] = temp;
return low;
}
void QSort (int* L, int low, int high)
{
if (low < high)
{
int pl = Partition (L,low,high);
QSort (L,low,pl - 1);
QSort (L,pl + 1,high);
}
}
int main ()
{
int narry[100],addr[100];
int sum = 1,t;
cout << "Input number:" << endl;
cin >> t;
while (t != -1)
{
narry[sum] = t;
addr[sum - 1] = t;
sum++;
cin >> t;
}
sum -= 1;
QSort (narry,1,sum);
for (int i = 1; i <= sum;i++)
cout << narry[ i] << '\t';
cout << endl;
int k;
cout << "Please input place you want:" << endl;
cin >> k;
int aa = 1;
int kk = 0;
for (;;)
{
if (aa == k)
break;
if (narry[kk] != narry[kk + 1])
{
aa += 1;
kk++;
}
}
cout << "The NO." << k << "number is:" << narry[sum - kk] << endl;
cout << "And it's place is:" ;
for (i = 0;i < sum;i++)
{
if (addr[ i] == narry[sum - kk])
cout << i << '\t';
}
return0;
}
void* mymemcpy( void *dest, const void *src, size_t count )
{
char* pdest = static_cast<char*>( dest );
const char* psrc = static_cast<const char*>( src );
if( pdest>psrc && pdest<psrc+cout ) 能考虑到这种情况就行了
{
for( size_t i=count-1; i!=-1; --I )
pdest[i] = psrc[i];
}
else
{
for( size_t i=0; i<count; ++I )
pdest[i] = psrc[i];
}
return dest;
}int main( void )
{
char str[] = "0123456789";
mymemcpy( str+1, str+0, 9 );
cout << str << endl; system( "Pause" );
return 0;
}
2、实现一个函数,把一个字符串中的字符从小写转为大写。
#include "stdio.h"
#include "conio.h"
void uppers(char *s,char *us)
{
for(;*s!='\0';s++,us++)
{
if(*s>='a'&&*s<='z')
*us = *s-32;
else
*us = *s;
}
*us = '\0';
}
void main()
{
char *s,*us;
char ss[20];
printf("Please input a string:\n");
scanf("%s",ss);
s = ss;
uppers(s,us);
printf("The result is:\n%s\n",us);
getch();
}
3.如图: 7 8 9 10
6 1 2 11
5 4 3 12
16 15 14 13
设“1”的坐标为(0,0) “7”的坐标为(-1,-1) 编写一个小程序,使程序做到输入坐标(X,Y)之后显示出相应的数字。
#include<stdio.h>
void main(void)
{
int circleX, circleY, circle, x, y, num, numX, numY, i;
scanf("%d %d", &x, &y);
//计算属于第几圈
if(x>=1)
circleX=x;
else if(x<=0)
circleX=1-x;
if(y>=1)
circleY=y;
else if(y<=0)
circleY=1-y;
circle=circleX>circleY?circleX:circleY;
//获得本圈第一个数及其坐标
num = 4*(circle-1)*(circle-1)+1;
numX=circle-1;
numY=1-circle;
//查询输入坐标对应的数
for(i=0;i<2*circle-2;i++)
{
if(numX==x && numY==y)
{
printf("The number is:%d\n",num);
return;
}
numX--;
num++;
}
for(i=0;i<2*circle-1;i++)
{
if(numX==x && numY==y)
{
printf("The number is:%d\n",num);
return;
}
numY++;
num++;
}
for(i=0;i<2*circle-1;i++)
{
if(numX==x && numY==y)
{
printf("The number is:%d\n",num);
return;
}
numX++;
num++;
}
for(i=0;i<2*circle;i++)
{
if(numX==x && numY==y)
{
printf("The number is:%d\n",num);
return;
}
numY--;
num++;
}
}
}
4. 编程实现十进制数转化为十六进制输出,不准用任何已经定义的库函数
#include <stdio.h>
void main()
{
int n,i=0;
char glam[17]="0123456789ABCDEF"; //定义字符表
char hex[20]=""; //用于存储转换之后的十六进制数
printf("please enter n(dec):\n");
scanf("%d",&n);
do{
hex[i++]=glam[n%16];
n=n/16;
}while(n>0);
printf(hex);
}
5.写一段程序,找出数组中第k大小的数,输出数所在的位置。例如{2,4,3,4,7}中,第一大的数是7,位置在4。第二大、第三大的数都是4,位置在1、3随便输出哪一个均可。函数接口为:int find_orderk(const int* narry,const int n,const int k) 要求算法复杂度不能是O(n^2)
#include<iostream>
usingnamespacestd;
int Partition (int*L,intlow,int high)
{
int temp = L[low];
int pt = L[low];
while (low < high)
{
while (low < high && L[high] >= pt)
--high;
L[low] = L[high];
while (low < high && L[low] <= pt)
++low;
L[low] = temp;
}
L[low] = temp;
return low;
}
void QSort (int* L, int low, int high)
{
if (low < high)
{
int pl = Partition (L,low,high);
QSort (L,low,pl - 1);
QSort (L,pl + 1,high);
}
}
int main ()
{
int narry[100],addr[100];
int sum = 1,t;
cout << "Input number:" << endl;
cin >> t;
while (t != -1)
{
narry[sum] = t;
addr[sum - 1] = t;
sum++;
cin >> t;
}
sum -= 1;
QSort (narry,1,sum);
for (int i = 1; i <= sum;i++)
cout << narry[ i] << '\t';
cout << endl;
int k;
cout << "Please input place you want:" << endl;
cin >> k;
int aa = 1;
int kk = 0;
for (;;)
{
if (aa == k)
break;
if (narry[kk] != narry[kk + 1])
{
aa += 1;
kk++;
}
}
cout << "The NO." << k << "number is:" << narry[sum - kk] << endl;
cout << "And it's place is:" ;
for (i = 0;i < sum;i++)
{
if (addr[ i] == narry[sum - kk])
cout << i << '\t';
}
return0;
}
推荐律师服务:
若未解决您的问题,请您详细描述您的问题,通过百度律临进行免费专业咨询