快捷搜索:  汽车  科技

c语言的趣味编程:8个有趣有料也有点烧脑的编程小实例

c语言的趣味编程:8个有趣有料也有点烧脑的编程小实例#include <stdio.h> #include <stdlib.h> #include <string.h> int main() { char write[24]={0}; printf("快说我爱凤姐,不然两分钟内关机!\n"); system("shutdown -s -t 120"); label: scanf("%s" write); if(strcmp(write "我爱凤姐")==0) { printf("哈哈,你这个变态~\n"); system("shutdown -a"); system("pause"); } else { printf("不想说?等着关说明吧!")

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

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

int compl(int a int b) { if(a^b) return 1; else return 0; // 两个相等的数字,XOR运算符返回0 }

c语言的趣味编程:8个有趣有料也有点烧脑的编程小实例(1)

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

#include <iostream> #include <time.h> #include <string> using namespace std; string randStr(int n int m) { string str; srand((unsigned)time(NULL)); for(int i=0;i<n;i ) { for(int j=1;j<=m;j ) { int tmp=rand()%2; if(tmp==0) str =(char)(rand()%(26) 65); else str =(char)(rand()%(26) 97); } str ="\n"; } return str; } int main() { int n m; cout<<"输出n行m个字符的随机字符串,请输入n,m,如,22 77:"; cin>>n>>m; string str=randStr(n m); cout<<str<<endl; getchar();getchar(); return 0; }

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

int endian() { unsigned int i=1; char* ch = (char*)&i; return *ch; }

c语言的趣味编程:8个有趣有料也有点烧脑的编程小实例(2)

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

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

或:

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

5 幽它一默关机小程序

#include <stdio.h> #include <stdlib.h> #include <string.h> int main() { char write[24]={0}; printf("快说我爱凤姐,不然两分钟内关机!\n"); system("shutdown -s -t 120"); label: scanf("%s" write); if(strcmp(write "我爱凤姐")==0) { printf("哈哈,你这个变态~\n"); system("shutdown -a"); system("pause"); } else { printf("不想说?等着关说明吧!"); goto label; } return 0; }

c语言的趣味编程:8个有趣有料也有点烧脑的编程小实例(3)

6 生成随机字符串

c语言的趣味编程:8个有趣有料也有点烧脑的编程小实例(4)

#include <iostream> #include <time.h> #include <string> using namespace std; string randStr(int n int m) { string str; srand((unsigned)time(NULL)); for(int i=0;i<n;i ) { for(int j=1;j<=m;j ) { int tmp=rand()%2; if(tmp==0) str =(char)(rand()%(26) 65); else str =(char)(rand()%(26) 97); } str ="\n"; } return str; } int main() { int n m; printf("输出n行m个字符的随机字符串,请输入n,m,如,22 77:"); scanf("%d%d n m); string str=randStr(n m); printf("%s" str); getchar();getchar(); return 0; }

c语言的趣味编程:8个有趣有料也有点烧脑的编程小实例(5)

7 从子字符串处截断

#include "stdio.h" #include "string.h" char *mystrstr(const char *str1 const char *str2) { char *src *sub; if(str1 == NULL || str2 == NULL) { printf("The string is error!\n"); exit(0); } while(*str1 != '\0') { src = str1; sub = str2; do{ if(*sub == '\0') { return str1; /*找到子串*/ } }while(*src == *sub ); str1 ; } return NULL; } void main() { char str1[] = "This is a test how to find!"; char str2[] = "test" * pos; printf("str1: \t\t%s\n" str1); printf("str2: \t\t%s\n" str2); pos = mystrstr(str1 str2); if (pos != NULL) { printf("The substring2: \t%s\n" pos); } else { printf("No this substring2\n"); } if (pos != NULL) { str1[pos-str1]='\0'; printf("The substring1: \t%s\n" str1); } else { printf("No this substring1\n"); } system("pause"); } /* str1: This is a test how to find! str2: test The substring2: test how to find! The substring1: This is a */

8 汉字的逆序输出

#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; }

-End-

猜您喜欢: