C语言数学运算的问题
voidEncrypt(char*str){intascii;srand(time(NULL));intran=rand()%9+1;charcode[]="";char...
void Encrypt(char *str){ int ascii; srand(time(NULL)); int ran=rand()%9+1; char code[]=""; char ch; printf("%d\n",ran); for(int i=0; i<strlen(str); i++) { ch=str[i]; ascii=(int)ch; if(ascii>=33&&ascii<=47){ ascii=(ascii+ran)*2; }else if(ascii>=48&&ascii<=57){ printf("%d\n",ascii);//////////////////////////////输出运算前的ascii ascii=ascii*2+ran; printf("%d\n",ascii);/////////////////////////////输出运算后的ascii,注意数值变化,从第三个开始没有规律了。。。。。。 }else if(ascii>=58&&ascii<=64){ ascii=ascii-ran*2+ascii; }else if(ascii>=65&&ascii<=90){ ascii=ascii-ran*3+ascii; }else if(ascii>=91&&ascii<=126){ ascii=ascii-ran*5; } code[i]=(char)ascii; } printf("code:%s\n",code); getch();}这是一个传入字符然后改变ascii再输出的函数。函数传入“123456”,输出改变后的字符串。
展开
展开全部
#include<stdio.h>
#include<stdlib.h>
#include<string.h>
void Encrypt(char *str)
{
int ascii;
srand(time(NULL));
int i,ran=rand()%9+1;
char *code=(char*)malloc(strlen(str)+1);
char ch;
printf("%d\n\n",ran);
for(i=0;i<strlen(str);i++)
{
ch=str[i];
ascii=(int)ch;
if(ascii>=33&&ascii<=47)
{
ascii=(ascii+ran)*2;
}
else if(ascii>=48&&ascii<=57)
{
printf("%d\n",ascii);
ascii=ascii*2+ran;
printf("%d\n",ascii);
}
else if(ascii>=58&&ascii<=64)
{
ascii=ascii-ran*2+ascii;
}
else if(ascii>=65&&ascii<=90)
{
ascii=ascii-ran*3+ascii;
}
else if(ascii>=91&&ascii<=126)
{
ascii=ascii-ran*5;
}
code[i]=(char)ascii;
}
code[i]='\0';
printf("code:%s\n",code);
free(code);
}
int main()
{
Encrypt("123456");
system("PAUSE");
return 0;
}
#include<stdlib.h>
#include<string.h>
void Encrypt(char *str)
{
int ascii;
srand(time(NULL));
int i,ran=rand()%9+1;
char *code=(char*)malloc(strlen(str)+1);
char ch;
printf("%d\n\n",ran);
for(i=0;i<strlen(str);i++)
{
ch=str[i];
ascii=(int)ch;
if(ascii>=33&&ascii<=47)
{
ascii=(ascii+ran)*2;
}
else if(ascii>=48&&ascii<=57)
{
printf("%d\n",ascii);
ascii=ascii*2+ran;
printf("%d\n",ascii);
}
else if(ascii>=58&&ascii<=64)
{
ascii=ascii-ran*2+ascii;
}
else if(ascii>=65&&ascii<=90)
{
ascii=ascii-ran*3+ascii;
}
else if(ascii>=91&&ascii<=126)
{
ascii=ascii-ran*5;
}
code[i]=(char)ascii;
}
code[i]='\0';
printf("code:%s\n",code);
free(code);
}
int main()
{
Encrypt("123456");
system("PAUSE");
return 0;
}
追问
谢谢(*°∀°)=3,能不能顺便说说free()的用法?
推荐律师服务:
若未解决您的问题,请您详细描述您的问题,通过百度律临进行免费专业咨询