一套 编程题 编程1-10

本文涵盖了C/C++编程中的链表操作,包括链表节点的反转、删除所有节点、字符搜索、字符串比较以及查找最大公共子串。同时,通过示例展示了异常处理在文件写入中的应用,以及如何使用C++内置函数实现字符串逆序。此外,还提供了不使用库函数的内存复制实现和交换两个变量值的方法。
摘要由CSDN通过智能技术生成
1. 写出程序把一个链表中的接点顺序倒排
typedef struct linknode
{
    int data;
    struct linknode *next;
}node;
//将一个链表逆置
node *reverse(node *head)
{
    node *p,*q,*r;
    p=head;
    q=p->next;
    while(q!=NULL)
    {
        r=q->next;
        q->next=p;
        p=q;
        q=r;
     }
head->next=NULL;
head=p;
return head;
}

2. 写出程序删除链表中的所有接点
void del_all(node *head)
{
    node *p;
    while(head!=NULL)
   {
      p=head->next;
      free(head);
      head=p;
   }
cout<<"释放空间成功!"<<endl;
}

3. 请编写一个 C 函数,该函数在给定的内存区域搜索给定的字符,并返回该字符所在位置索引值。
int search(char *cpSource, int n, char ch)
{
 	int i;
   for(i=0; i<n && *(cpSource+i) != ch; ++i);
   return i;
}

4. 写一个函数比较两个字符串str1和str2的大小,若相等返回0,若str1大于
str2返回1,若str1小于str2返回-1
int strcmp ( const char * src,const char * dst)
{
        int ret = 0 ;
        while( ! (ret = *(unsigned char *)src - *(unsigned char *)dst) && *dst)
{
                ++src;
++dst;
}
        if ( ret < 0 )
                ret = -1 ;
        else if ( ret > 0 )
                ret = 1 ;
        return( ret );
}

5. 编程实现:找出两个字符串中最大公共子字符串,如"abccade","dgcadde"的最大子串为"cad" 
int GetCommon(char *s1, char *s2, char **r1, char **r2)
{
    int len1 = strlen(s1);
    int len2 = strlen(s2);
    int maxlen = 0;
for(int i = 0; i < len1; i++)
{
    for(int j = 0; j < len2; j++)
    {
        if(s1[i] == s2[j])
       {
           int as = i, bs = j, count = 1;
           while(as + 1 < len1 && bs + 1 < len2 && s1[++as] == s2[++bs])
           count++;
if(count > maxlen)
{
    maxlen = count;
    *r1 = s1 + i;
    *r2 = s2 + j;
}
}
}
}
	}

6. 请写一个函数重载的简单例子
	int add(int a,int b)
    {
    		return (a + b);
     }

    float add(float a,float b)
    {
        return (a + b);
    }
   函数重载:// 函数重载是指函数名相同,但参数类型或返回值可能不同的函数。

7.请把下述代码加上异常处理。
int MyWriteFile(CString strFileName, CString strText)
{
		int nRet = 0;
	
		CFile myFile;
		myFile.Open(strFileName, CFile::modeWrite|CFile::shareExclusive|CFile::modeCreate, 
NULL);	
	
		int nLen = strText.GetLength();
		myFile.Write((char*)(LPCSTR)strText, nLen);
	
		myFile.Close();

		return nRet;
}

int MyWriteFile(CString strFileName, CString strText)
    {
       int nRet = 0;
       CFile myFile;
       __try
      {
        nRet = myFile.Open(strFileName,\
               CFile::modeWrite|CFile::shareExclusive|CFile::modeCreate, NULL);
        if(!nRet)
        {
            // 输出异常信息
            __leave;// 结束try块,跳到__finaly代码块
        }
        int nLen = strText.GetLength();
        nRet = myFile.Write((char*)(LPCSTR)strText, nLen);
        if(!nRet)
        {
            //输出异常信息
            __leave;// 结束try块,跳到__finaly代码块
        }
      }
      __finaly
     {
         myFile.Close();
     }
     return nRet;
}

8、输入一个字符串,将其逆序后输出。(使用C++,不建议用伪码)
#include <iostream>
using namespace std;
void main(){
  		char a[50];
memset(a,0,sizeof(a));
 		int i=0,j;
char t;
cin.getline(a,50,'\n');
 		for(i=0,j=strlen(a)-1;i<strlen(a)/2;i++,j--)
 		{
  			t=a[i];
a[i]=a[j];
 			a[j]=t;
 		}
cout<<a<<endl; 
}

拓展:直接用STL里现成的算法:
#include <iostream>
#include <string>
#include <algorithm>
using namespace std;
int main()
{
     string str;
   	getline(cin,str);
 		reverse(str.begin(), str.end());
 		cout<<"str Reversed:"<<str<<endl;
 		system("pause");
 		return 0;
}


9、在不用第三方参数的情况下,交换两个参数的值
#include <stdio.h>
void main()
{
    int i=60;
  int j=50;
  i=i+j;
  j=i-j;
  i=i-j;
  printf("i=%d\n",i);
  printf("j=%d\n",j);
}

方法二:
i^=j;
j^=i;
i^=j;


10. 编写my_memcpy函数,实现与库函数memcpy类似的功能,不能使用任何库函数;
void* mymemcpy(void* pvTo, const char* pvFrom, size_t size)
{ 
assert((dest != NULL) && (src != NULL));
byte* psTo = (byte*)pvTo;
byte* psFrom = (byte*)pvFrom;
while (size– > 0)
{*psTo++ = *psFrom++;}
return pvTo;
}

 

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值