C|8个有趣有料也有点烧脑的编程小实例

编程也可以是一件很有趣的事情,虽然有时有点烧脑。

1 不使用算术运算符或比较运算符的情况下检查两个数字是否相等

<code>int compl(int a, int b)
{
\tif(a^b)
\t\treturn 1;
\telse
\t\treturn 0; // 两个相等的数字,XOR运算符返回0
}/<code>
C|8个有趣有料也有点烧脑的编程小实例

2 不使用循环,打印1-10

<code>#include <iostream>
#include <time.h>
#include <string>
using namespace std;
string randStr(int n, int m)
{
\tstring str;
\tsrand((unsigned)time(NULL));
\tfor(int i=0;i\t{
\t\tfor(int j=1;j<=m;j++)
\t\t{
\t\t\tint tmp=rand()%2;
\t\t\tif(tmp==0)
\t\t\t\tstr+=(char)(rand()%(26)+65);
\t\t\telse
\t\t\t\tstr+=(char)(rand()%(26)+97);
\t\t}
\t\tstr+="\\n";
\t}
\treturn str;
}

int main()
{
\tint n,m;
\tcout<\tcin>>n>>m;
\tstring str=randStr(n,m);
\tcout< getchar();getchar();
\treturn 0;
}
/<string>/<time.h>/<iostream>/<code>

3 不使用共用体简单判断大小端

<code>int endian()
{
\tunsigned int i=1;
\tchar* ch = (char*)&i;
\treturn *ch;
}



/<code>


C|8个有趣有料也有点烧脑的编程小实例

4 不考虑ASCII编码的简单的大小写转换

<code>char toLower(char ch)
{
\treturn (ch^32); // 异或表示不考虑进位(每位)的加法,因没有进位,+32
}
char toUpper(char ch)
{
\treturn (ch^32); // 因有进位,不考虑,-32
}/<code>

或:

return (ch^('a'-'A'));

5 幽它一默关机小程序

<code>#include <stdio.h>
#include <stdlib.h>
#include <string.h>
int main()
{
\tchar write[24]={0};
\tprintf("快说我爱凤姐,不然两分钟内关机!\\n");
\tsystem("shutdown -s -t 120");
label:
\tscanf("%s",write);
\tif(strcmp(write,"我爱凤姐")==0)
\t{
\t\tprintf("哈哈,你这个变态~\\n");
\t\tsystem("shutdown -a");
\t\tsystem("pause");
\t}
\telse
\t{
\t\tprintf("不想说?等着关说明吧!");
\t\tgoto label;
\t}
\treturn 0;

}/<string.h>/<stdlib.h>/<stdio.h>/<code>
C|8个有趣有料也有点烧脑的编程小实例

6 生成随机字符串

C|8个有趣有料也有点烧脑的编程小实例

<code>#include <iostream>
#include <time.h>
#include <string>
using namespace std;
string randStr(int n, int m)
{
\tstring str;
\tsrand((unsigned)time(NULL));
\tfor(int i=0;i\t{
\t\tfor(int j=1;j<=m;j++)
\t\t{
\t\t\tint tmp=rand()%2;
\t\t\tif(tmp==0)
\t\t\t\tstr+=(char)(rand()%(26)+65);
\t\t\telse
\t\t\t\tstr+=(char)(rand()%(26)+97);
\t\t}
\t\tstr+="\\n";
\t}
\treturn str;
}

int main()
{
\tint n,m;
\tprintf("输出n行m个字符的随机字符串,请输入n,m,如,22 77:");
\tscanf("%d%d,n,m);
\tstring str=randStr(n,m);
\tprintf("%s",str);
getchar();getchar();
\treturn 0;
}
/<string>/<time.h>/<iostream>/<code>


C|8个有趣有料也有点烧脑的编程小实例

7 从子字符串处截断

<code>#include "stdio.h"
#include "string.h"

char *mystrstr(const char *str1, const char *str2)
{
\tchar *src,*sub;
\t
\tif(str1 == NULL || str2 == NULL)
\t{
\t\tprintf("The string is error!\\n");
\t\texit(0);
\t}
\twhile(*str1 != '\\0')
\t{
\t\tsrc = str1;
\t\tsub = str2;
\t\tdo{
\t\t\tif(*sub == '\\0')
\t\t\t{
\t\t\t\treturn str1; \t\t/*找到子串*/
\t\t\t}
\t\t}while(*src++ == *sub++);
\t\tstr1++;
\t}
\treturn NULL;
}

void main() {
\tchar str1[] = "This is a test,how to find!";
\tchar str2[] = "test", * pos;
\tprintf("str1: \\t\\t%s\\n",str1);
\tprintf("str2: \\t\\t%s\\n",str2);
\tpos = mystrstr(str1,str2);

\tif (pos != NULL) {
\t\tprintf("The substring2: \\t%s\\n",pos);
\t} else {
\t\tprintf("No this substring2\\n");
\t}

\tif (pos != NULL) {
\t\tstr1[pos-str1]='\\0';
\t\tprintf("The substring1: \\t%s\\n",str1);
\t} else {
\t\tprintf("No this substring1\\n");

\t}

\tsystem("pause");
}
/*
str1: This is a test,how to find!
str2: test
The substring2: test,how to find!
The substring1: This is a
*//<code>

8 汉字的逆序输出

<code>#include <stdio.h> 
#include <stdlib.h>
#include <string.h>
int main()
{
char str[20] = "我爰你!";
for (int i = strlen(str)-1; i >=0;)
{
if (str[i] >= 0 && str[i] <= 127) // ASCII 0-127,一个字节存储一个ASCII字符
{
printf("%c", str[i]);
i--;
}
else // 两个字节存储一个汉字字符
{
i--;
printf("%c%c", str[i], str[i + 1]);
i--;
}
}
printf("\\n");
system("pause");
return 0;

}/<string.h>/<stdlib.h>/<stdio.h>/<code>

-End-


分享到:


相關文章: