C语言_lesson_6 内存结构、字符串

来源:互联网 发布:冰川网络的游戏有名吗 编辑:程序博客网 时间:2024/06/05 15:15
一、知识点

1、按地址传递


////////////////////////////////////////////////////////////////////////////////按址传递//void fun(int *a)//{//printf("\nin fun:%d,%x",*a,a);//*a = 10; //让参数a等于10//}////void main()//{//int a = 0;  //初始化a值为0//fun(&a);      //调用函数fun//printf("\nint main:%d,%x",a,&a);  //输出a的结果//}////////////////////////////////////////////////////////////////////////////void fun(int *pSZ)//按地址传递,用指针,推荐写法//{//p+=2;//*p=100;//}////////////////////////////////////////////////////////////////////////////void fun(int p[])//按地址传递//{//p[2]=100;//}////void main()//{//int num[5]={1,2,3,4,5};//fun(num);//将数组首地址传过去//printf("%d",num[2]);//}////////////////////////////////////////////////////////////////////////////传引用//void fun(int &a)//{//printf("%x\n",&a);//a = 10; //让参数a等于10//}////void main()//{//int a = 0;  //初始化a值为0//fun(a);      //调用函数fun//printf("%d,%x\n",a,&a);  //输出a的结果//}//////////////////////////////////////////////////////////////////////////////引用类型////int num=500;//int &ref=num; //void main() //{//num=600; ////int a=100,b;//int &ref=a; //声明a的引用//a=500;////ref=600;//printf("%d,%x,%x\n",ref,&a,&ref);//}



2、函数的递归,使用递归实现回文功能


//////////////////////////////////////////////////////////////////////////////函数的递归//void fun(int num,int n)//{//printf("%d,%x\n",n,&n);////static int nIndex=1;//if (num>0)//{////printf("%d,",num%10);//fun(num/10,n++);//printf("%d,",n);////printf("%d,",num);//}//}//////void main()//{////fun(12345,1);//}


3、实现c语言字符串中的库函数


////////////////////////////////////////////////////////////////////////////字符串void main(){//char str[]="12345";////str[100]='a';//错误写法,越界了//printf("%d",sizeof(str));//char str[20]={0};//边界检查要靠自己检查////不能接收空格//scanf("%s",&str);//printf("%s",str);//可以接收空格//gets(str);//puts(str);///////////////////////////////////////////////////////////////////////////*char *pStr="c plus plus so easy";//常量区printf("%s\n",pStr);pStr=pStr+12;printf("%s\n",pStr);pStr="no  no";printf("%s",pStr);pStr[0]='y';pStr[1]='e';pStr[2]='s';printf("%s",pStr);*/////栈上面//char szStr[]="c plus plus so easy";//szStr[0]='y';//szStr[1]='e';//szStr[2]='s';//puts(szStr);//int GetNumberCount(char p[]);////字符串练习,用户输入一串字符串,判断其中数字的个数//char szStr[250]={0};//gets(szStr);//int n=GetNumberCount(szStr);//printf("%d",n);//////////////////////////////////////////////////////////////////////////////库函数//char str[500]={0};//gets(str);//int nLen=strlen(str);//strlen不包含尾部\0//printf("%d",nLen);//char str2[500];//strcpy(str2,str);//printf("\n");////str[4]='4';//puts(str);//if(strcmp(str2,str)==0)//相等为0//printf("str2==str\n");//else//printf("str2!=str\n");//不相等 》1 or 《0//str2[0]='|';//strcat(str,str2);//printf("str:%s\nstr2:%s\n",str,str2);//char *names [] = {"Zhangsan", "Lisi", "Wangwu", "Zhaoliu"};//printf("%s,%x\n",names[0],&names[0]);//printf("%s,%x\n",names[1],&names[1]);//printf("%s,%x\n",names[2],&names[2]);//printf("%s,%x\n",names[3],&names[3]);//names[0][0]='0';//错误的,常量不可修改////////////////////////////////////////////////////////////////////////////字符串练习//////////////////////////////////////////////////////////////////////////int  myStrlen(char *p);void myStrcpy(char *pTage,char *pSource);bool myStrcmp(char *pTage,char *pSource);void myStrcat(char *pTage,char *pSource);//!!!!边界检查一定要注意char szInfo[]="hello~ ";char szName[]="jack";//int n=myStrlen(szInfo);//printf("%d",n);//myStrcpy(szInfo,szName);//puts(szInfo);//if (myStrcmp(szInfo,szName))//printf("相等\n");//else//printf("不相等\n");myStrcat(szInfo,szName);printf("%s",szInfo);}int  myStrlen(char *p){int nCount=0;while(*p!='\0'){p++; nCount++;}return nCount;}void myStrcpy(char *pTage,char *pSource){while((*pTage=*pSource)!='\0'){pTage++;pSource++;}}bool myStrcmp(char *pTage,char *pSource){bool bRet=true;if(myStrlen(pTage)==myStrlen(pSource)){while(*pTage!='\0' && *pSource!='\0'){if(*pTage!=*pSource){bRet=false;break;}pTage++;pSource++;}}elsebRet=false;returnbRet;//bool bRet=false;////长度//if(myStrlen(pTage)==myStrlen(pSource))//{//bool bExist=true;//while(*pTage!='\0' && *pSource!='\0')//{//if(*pTage!=*pSource)//{//bExist=false;//break;//}//pTage++;//pSource++;//}//////if (true==bExist)//bRet=true;//}//return  bRet;}void myStrcat(char *pTage,char *pSource){int nLen=myStrlen(pTage);pTage+=nLen;myStrcpy(pTage,pSource);}////int GetNumberCount(int *p)//{//int nSum=0;//while(*p!='\0')//{//if (*p>='0' && *p<='9')//nSum++;//p++;//}////return nSum;//}


二、课后题

1.写个函数,在函数内通过位运算判断传入的数是奇数还是偶数。


2.实现一个函数,用户输入一个整数,输出该数二进制表示中1的个数。
例如用户输入:9表示成二进制是1001,有2位是1。
因此如果输入9,该函数输出2
(将解题步骤写出来、二进制位的变化)
12345 加密 asdfasd  12345
3.写一个字符串“加密”函数,可对用户输入的一串字符串进行加密,然后写个“解密”函数可对加密的字符串进行解密(越不容易被破解越好)

//#include <stdio.h>//#include <Windows.h>////void sha1(int key, char* value[])//{////}//int main(void)//{//sha1(int key, char* value[]);//return 0;//}#include <stdio.h>#include <windows.h>#include <tchar.h>void EncodeString(char lpszText[], char lpszKey[]){int nTextLen = strlen(lpszText);int nKeyLen = strlen(lpszKey);int i = 0;int k = 0;for(; i < nTextLen; i++){lpszText[i] = lpszText[i] ^ lpszKey[k] + i;k++;if(k >= nKeyLen)k = 0;}}int main(int argc, char* argv[]){char strText[] = "hell0 world!";puts(strText);puts("\n");//cout<<strText<<endl;//cout<<"========================================"<<endl;puts("============================================");puts("\n");EncodeString(strText, "yao");   // 加密//cout<<strText<<endl;//cout<<"========================================"<<endl;puts(strText);puts("\n");puts("========================================");puts("\n");EncodeString(strText, "yao");   // 解密//cout<<strText<<endl;puts(strText);puts("\n");return 0;}


4.自己写函数实现,将“1234”转化为1234(字符串转成int)

int str2int(char *str){            int temp = 0;    while (0 != *str) {        temp = temp * 10 + (*str - '0');        str++;            }    return temp;}int main(void){    /* 第四题 *///    int n = 0;//    char p[] = "1234";//    //    n = str2int(p);//    //    //    printf("整数:%d\n", n);return 0}


5.实现int find(char *pSource,char *pTag)函数,实现能够从pSouce中找到首次出现pTag的下标并返回
abcdef   de

char *find(char *pSource, char *pTag){    char *ps;    char *pt;            while (*pSource) {        ps = pSource;        pt = pTag;                do        {            if (!*pt) {                return pSource;            }        }while (*pt++ == *ps++);        pSource++;    }    return 0;}int main(void){    /* 第五题 * /         //char p[] = "abcdef";    //char q[] = "de";       // char *r = find(p,q);    //printf("r: %s\n", r);    return 0;}


1.编写一个函数,接受用户传入的两个数,用大的数减去小的数,并将结果在main函数中打印出来。

int minus(int big, int little){    int result = 0;    result = big - little;    return result;}int main(void){    /*第一题*/    //    int big = 0;//    int little = 0;//    //    scanf("%d%d", &big,&little);//    printf("结果为:%d",minus(big,little));    return 0;}



2.编写一个函数,用于交换两个数组的内容。


3.主函数中有个数组保存一组乱序的数字,写一个函数将这个数组中的最大数与最小数的位置互换,然后再主函数中打印数组。


4.编写一个函数其生成20个随机数,并返回到主函数中,主函数调用打印函数将其打印出出来,然后主函数中调用另外一个函数对这20个随机数进行排序,最后再次在主函数调用打印函数将排序后的数组打印出来。


5.通过函数递归求出某个数的阶乘结果

  5    5*4*3*2*1


double fac(int n){    double f; if(n==0||n==1)        f=1;    else        f=fac(n-1)*n;        //printf("fac(n-1) = %f%f%d\n", fac(n-1),n,f);    return f;}int main(void){    int n;    double result;    printf("请输入要递归的整数N=");    scanf("%d",&n);    result=fac(n);    printf("%d的阶乘是:%f\n",n,result);    return 0;}



0 0
原创粉丝点击