refactor
This commit is contained in:
@@ -0,0 +1,28 @@
|
||||
#include "stdio.h"
|
||||
|
||||
char char_first_appear_once(const unsigned char *source){
|
||||
int hash[256]={0};
|
||||
char *tmp = source;
|
||||
if (tmp == NULL) return '\0';
|
||||
while(*tmp != '\0'){
|
||||
hash[*tmp]++;
|
||||
tmp++;
|
||||
}
|
||||
|
||||
tmp = source;
|
||||
while(*tmp != '\0'){
|
||||
if (hash[*tmp] == 1) return *tmp;
|
||||
tmp++;
|
||||
}
|
||||
return '\0';
|
||||
}
|
||||
|
||||
|
||||
int main(int argc, char const *argv[])
|
||||
{
|
||||
char *test = "77ah-ba-ccdeff";
|
||||
char c = char_first_appear_once(test);
|
||||
printf("c=%c\n", c);/* c = 'h'*/
|
||||
return 0;
|
||||
}
|
||||
|
||||
@@ -0,0 +1,35 @@
|
||||
#include "stdio.h"
|
||||
|
||||
char *proc(char *str){
|
||||
char *start = str;
|
||||
char *end = str;
|
||||
if (str == NULL) return NULL;
|
||||
while(*end != '\0') end++;
|
||||
end--;
|
||||
|
||||
while(start < end){
|
||||
if (*start >= 'A' && *start <= 'Z'){//大写
|
||||
if (*end >= 'a' && *end <= 'z'){
|
||||
char tmp = *start;
|
||||
*start = *end;
|
||||
*end = tmp;
|
||||
|
||||
start++;
|
||||
}
|
||||
end--;
|
||||
}else{//小写
|
||||
if (*end >= 'A' && *end <= 'Z'){
|
||||
end--;
|
||||
}
|
||||
start++;
|
||||
}
|
||||
}
|
||||
return str;
|
||||
}
|
||||
|
||||
int main(int argc, char const *argv[])
|
||||
{
|
||||
char test[] = "HaJKPnobAACPc";//"char *test" always make "28767 bus error"
|
||||
printf("%s\n", proc(test));
|
||||
return 0;
|
||||
}
|
||||
@@ -0,0 +1,32 @@
|
||||
#include "stdio.h"
|
||||
|
||||
char *replace_blank(char *source){
|
||||
int count = 0;
|
||||
char *tail = source;
|
||||
if (source == NULL) return NULL;
|
||||
while(*tail != '\0'){
|
||||
if (*tail == ' ') count++;
|
||||
tail++;
|
||||
}
|
||||
|
||||
while(count){
|
||||
if(*tail != ' '){
|
||||
*(tail+2*count) = *tail;
|
||||
}else{
|
||||
*(tail+2*count) = '0';
|
||||
*(tail+2*count-1) = '2';
|
||||
*(tail+2*count-2) = '%';
|
||||
count--;
|
||||
}
|
||||
tail--;
|
||||
}
|
||||
|
||||
return source;
|
||||
}
|
||||
|
||||
int main(int argc, char const *argv[])
|
||||
{
|
||||
char str[100]="we are happy";
|
||||
printf("ret=%s\n",replace_blank(str));
|
||||
return 0;
|
||||
}
|
||||
@@ -0,0 +1,57 @@
|
||||
#include "stdio.h"
|
||||
|
||||
void _reverse(char *start,char *end){
|
||||
if ((start == NULL) || (end == NULL)) return;
|
||||
while(start < end){
|
||||
char tmp = *start;
|
||||
*start = *end;
|
||||
*end = tmp;
|
||||
|
||||
start++,
|
||||
end--;
|
||||
}
|
||||
}
|
||||
|
||||
char *_revert_by_word(char *source){
|
||||
char *end = source;
|
||||
char *start = source;
|
||||
while(*start != '\0'){
|
||||
if (*start == ' '){
|
||||
start++;
|
||||
end++;
|
||||
}else if(*end == ' ' || *end == '\0'){
|
||||
_reverse(start,end-1);
|
||||
start = end;
|
||||
}else{
|
||||
end++;
|
||||
}
|
||||
}
|
||||
return source;
|
||||
}
|
||||
|
||||
char *revert_by_word(char *source){
|
||||
char *end = source;
|
||||
char *start = source;
|
||||
if (source == NULL) return NULL;
|
||||
while (*end != '\0') end++;
|
||||
end--;
|
||||
|
||||
/*整个句子逆序*/
|
||||
_reverse(start,end);
|
||||
|
||||
/*按单词逆序.tneduts a ma I */
|
||||
_revert_by_word(source);
|
||||
|
||||
return source;
|
||||
}
|
||||
|
||||
//为啥运行时 bus error?
|
||||
int main(int argc, char const *argv[])
|
||||
{
|
||||
char *test = "how are you ?";
|
||||
printf("%s\n",_revert_by_word(test));
|
||||
|
||||
test = "I am a student.";
|
||||
printf("%s\n",revert_by_word(test));
|
||||
return 0;
|
||||
}
|
||||
@@ -0,0 +1,429 @@
|
||||
|
||||
#include "stdio.h"
|
||||
|
||||
typedef int BOOL;
|
||||
#define YES 1
|
||||
#define NO 0
|
||||
|
||||
|
||||
// 反序
|
||||
void revert(const char *source,char *dest){
|
||||
|
||||
const char *s = source;
|
||||
char *end = dest;
|
||||
while(*s){
|
||||
*end++ = *s++; //先拷贝字符串
|
||||
}
|
||||
*end='\0';
|
||||
end--;// 指向最后一个字符
|
||||
|
||||
while(end>dest){
|
||||
|
||||
char tmp = *end;
|
||||
*end = *dest;
|
||||
*dest = tmp;
|
||||
|
||||
end--;
|
||||
dest++;
|
||||
}
|
||||
|
||||
|
||||
}
|
||||
|
||||
// 按单词逆向 如 this is a sentence 输出 sentence a is this
|
||||
// 先按单词逆序,再对整个句子逆序
|
||||
void revertByWord(const char *src, char *dest){
|
||||
|
||||
// 先拷贝字符串到dest
|
||||
char *tmp = dest;
|
||||
char *dest_end;
|
||||
while(*src){
|
||||
*tmp++ = *src++;
|
||||
}
|
||||
*tmp='\0';
|
||||
dest_end = tmp-1; //这里留作下面用;
|
||||
tmp = dest;
|
||||
|
||||
// 扫描一遍 dest,按单词逆序
|
||||
char *start=dest-1,*end=dest;
|
||||
while(*tmp){
|
||||
|
||||
if (*tmp == ' ')//逆序
|
||||
{
|
||||
start++;
|
||||
|
||||
//对单词逆向
|
||||
while(end>start){
|
||||
*start = *start + *end;
|
||||
*end = *start - *end;
|
||||
*start = *start - *end;
|
||||
|
||||
start++;
|
||||
end--;
|
||||
}
|
||||
|
||||
start=tmp;// 如果句子中又多个连续空格,也会对空格做逆序,浪费cpu
|
||||
|
||||
}
|
||||
tmp++;
|
||||
end=tmp-1;
|
||||
}
|
||||
|
||||
if (!*tmp)//最后一个字符,对最后一个单词逆序
|
||||
{
|
||||
start ++;
|
||||
while(end>start){
|
||||
*start = *start + *end;
|
||||
*end = *start - *end;
|
||||
*start = *start - *end;
|
||||
|
||||
start++;
|
||||
end--;
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
tmp = dest;
|
||||
|
||||
// 逆向整个句子
|
||||
while(dest_end>tmp){
|
||||
|
||||
*tmp = *tmp + *dest_end;
|
||||
*dest_end = *tmp - *dest_end;
|
||||
*tmp = *tmp - *dest_end;
|
||||
|
||||
tmp++;
|
||||
dest_end--;
|
||||
}
|
||||
|
||||
|
||||
}
|
||||
|
||||
|
||||
// 回文,或 对称
|
||||
BOOL symmetrical(const char *scr,int length){
|
||||
|
||||
const char *end = scr+length-1;
|
||||
BOOL is=YES;
|
||||
|
||||
while(end>scr){
|
||||
|
||||
if (*scr++ != *end--)
|
||||
{
|
||||
is=NO;
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
return is;
|
||||
}
|
||||
|
||||
|
||||
//找出第一个只出现一次的字符,找不到就返回‘0’
|
||||
char first_appear_only_once(const char *source){
|
||||
|
||||
// 建立hash表
|
||||
const int length=256;
|
||||
int hash_table[length];
|
||||
for (int i = 0; i < length; ++i)
|
||||
{
|
||||
hash_table[i]=0;
|
||||
}
|
||||
|
||||
//第一次扫描,建立hash表
|
||||
const char *tmp = source;
|
||||
while(*tmp){
|
||||
hash_table[*tmp++]++;
|
||||
}
|
||||
|
||||
// 第二次扫描
|
||||
while(*source){
|
||||
|
||||
if(hash_table[*source]==1){
|
||||
|
||||
return *source;
|
||||
}
|
||||
|
||||
source++;
|
||||
}
|
||||
|
||||
// 没找到
|
||||
if (!*source)
|
||||
{
|
||||
return '0';
|
||||
}
|
||||
|
||||
return '0';
|
||||
}
|
||||
|
||||
|
||||
|
||||
|
||||
// 最大对称子串的长度
|
||||
int max_symmetrical_char_length(const char *scr){
|
||||
|
||||
int max_symmetrical_length=1;
|
||||
|
||||
|
||||
|
||||
return 0;
|
||||
}
|
||||
|
||||
|
||||
// 找最大数字子串
|
||||
int continumax(char *outputstr,char *intputstr){
|
||||
|
||||
|
||||
return 0;
|
||||
}
|
||||
|
||||
// 最大连续递增数字串
|
||||
int continuousnum(const char *input, char *output){
|
||||
|
||||
|
||||
return 0;
|
||||
|
||||
}
|
||||
|
||||
|
||||
|
||||
|
||||
// 2个字符串的最长公共子串
|
||||
int longest_common_subsequence(const char *s1,const char *s2, char *common){
|
||||
|
||||
|
||||
return 0;
|
||||
|
||||
}
|
||||
|
||||
|
||||
|
||||
// 找子串
|
||||
char *strstr(const char *haystack, const char * needle){
|
||||
|
||||
return NULL;
|
||||
|
||||
}
|
||||
|
||||
// 子串个数
|
||||
char *sub_str_count(const char *src, const char *substr, int *count){
|
||||
|
||||
|
||||
return NULL;
|
||||
|
||||
}
|
||||
|
||||
|
||||
|
||||
|
||||
// 删除子串
|
||||
char *delete(char *source,const char *del,BOOL greedy){
|
||||
|
||||
|
||||
return NULL;
|
||||
}
|
||||
|
||||
|
||||
|
||||
|
||||
// 字符串拷贝
|
||||
char *strcpy(const char *src,char *dest){
|
||||
|
||||
char *tmp = dest;
|
||||
while(*src){
|
||||
|
||||
*dest++ = *src++;
|
||||
|
||||
}
|
||||
*dest = '\0';
|
||||
|
||||
return tmp;
|
||||
}
|
||||
|
||||
|
||||
|
||||
// aabcd & bcad , abc&bca
|
||||
BOOL isBrother(const char *first,const char *secd){
|
||||
|
||||
|
||||
return YES;
|
||||
}
|
||||
|
||||
|
||||
|
||||
|
||||
// 字符串就地压缩 如 “aaabcc” “a3bc2”
|
||||
/*
|
||||
注意连续字符大于9的情况
|
||||
*/
|
||||
void compress(char *src){
|
||||
|
||||
if (*src == NULL)
|
||||
{
|
||||
return;
|
||||
}
|
||||
|
||||
int count = 1;
|
||||
char *back=src,*forward=src+1;
|
||||
|
||||
while(*forward){
|
||||
|
||||
count = 1;
|
||||
if (*forward == *back)
|
||||
{
|
||||
count++;
|
||||
forward++;
|
||||
|
||||
}else{ //
|
||||
|
||||
if (count!=1)
|
||||
{
|
||||
back++;
|
||||
*back=count;
|
||||
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
|
||||
}
|
||||
|
||||
*++back = '\0';
|
||||
|
||||
}
|
||||
|
||||
|
||||
|
||||
|
||||
// 删除其中的数字并压缩,2个指针,一前一后
|
||||
void filternum(char *src){
|
||||
|
||||
if (!*src)
|
||||
{
|
||||
return;
|
||||
}
|
||||
|
||||
char *back=src ,*forward=src;
|
||||
while(*forward){
|
||||
if (*forward >= '0' && *forward <= '9')
|
||||
{
|
||||
forward++;
|
||||
}else{
|
||||
*back++ = *forward++;
|
||||
}
|
||||
|
||||
}
|
||||
*back='\0';
|
||||
|
||||
|
||||
// 局部变量释放,不能作为返回值
|
||||
}
|
||||
|
||||
// 删除字符串中的数字,不改变原串,同上
|
||||
char *filternumber(const char*src,char *dest){
|
||||
|
||||
|
||||
return NULL;
|
||||
}
|
||||
|
||||
// 删除指定字符,不开辟空间
|
||||
void delete_character(char *src , char target){
|
||||
|
||||
char *back=src,*forward = src;
|
||||
|
||||
while(*forward){
|
||||
|
||||
if (*forward == target)
|
||||
{
|
||||
forward++;
|
||||
|
||||
}else{
|
||||
|
||||
*back++ = *forward++;
|
||||
|
||||
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
*back = '\0';
|
||||
|
||||
}
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
// 统计文件中单词的出现的个数
|
||||
void statistics(FILE *fd){
|
||||
|
||||
|
||||
}
|
||||
|
||||
|
||||
|
||||
// 字符串中字符的所有排列
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
int main(int argc, char const *argv[])
|
||||
{
|
||||
|
||||
// 反转
|
||||
char *reverse = "nonstriater";
|
||||
char reversed[20];
|
||||
revert(reverse,reversed);
|
||||
printf("%s after reverse : %s\n", reverse,reversed);
|
||||
|
||||
|
||||
// 按单词逆序
|
||||
char *sentence = "this is a sentence!"; // "this is a sentence"
|
||||
char dest[50];
|
||||
revertByWord(sentence,dest);
|
||||
printf("%s revert by word: %s\n",sentence,dest );
|
||||
|
||||
|
||||
|
||||
// 回文
|
||||
char symmetri[] = "dacbcacbcad";
|
||||
printf("%s : %d\n", symmetri, symmetrical(symmetri,sizeof(symmetri)-1) );
|
||||
|
||||
char unsymmetrical[] = "aaacbdaa";
|
||||
printf("%s : %d\n", unsymmetrical, symmetrical(unsymmetrical,sizeof(unsymmetrical)-1) );
|
||||
|
||||
|
||||
// 第一次出现一次的字符
|
||||
printf("%s第一次出现一次的字符是:%c\n",symmetri,first_appear_only_once(symmetri) );// '0'
|
||||
printf("%s第一次出现一次的字符是:%c\n",unsymmetrical,first_appear_only_once(unsymmetrical) ); // 'c'
|
||||
|
||||
// 过滤数字
|
||||
char *charnumber = "this is 58tongcheng12haha!";
|
||||
//filternum(charnumber);
|
||||
printf(" after filter: %s\n", charnumber );
|
||||
|
||||
|
||||
// 过滤指定字符
|
||||
//delete_character(charnumber,'i'); // 居然会出 bus error!!!!!????
|
||||
printf("after filter:%s\n", charnumber);
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
return 0;
|
||||
}
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
@@ -0,0 +1,47 @@
|
||||
|
||||
|
||||
#include "stdio.h"
|
||||
|
||||
void generate(int a,int b,int N ,int * Q){
|
||||
|
||||
int p=1,q=1;
|
||||
int tmpA,tmpB;
|
||||
|
||||
for (int i = 0; i < N; ++i)
|
||||
{
|
||||
tmpA = a*p;
|
||||
tmpB = b*q;
|
||||
|
||||
if (tmpA>tmpB)
|
||||
{
|
||||
Q[i]=tmpB;
|
||||
q++;
|
||||
|
||||
}
|
||||
else if(tmpA<tmpB)
|
||||
{
|
||||
Q[i]=tmpA;
|
||||
p++;
|
||||
|
||||
}
|
||||
else{// ==
|
||||
Q[i]=tmpA;
|
||||
p++;
|
||||
q++;
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
int main(int argc, char const *argv[])
|
||||
{
|
||||
|
||||
int a[10];
|
||||
generate(3,5,10,a);
|
||||
for (int i = 0; i < 10; ++i)
|
||||
{
|
||||
printf(" %d \n", a[i]);
|
||||
}
|
||||
|
||||
return 0;
|
||||
}
|
||||
@@ -0,0 +1,55 @@
|
||||
|
||||
#include "stdio.h"
|
||||
|
||||
|
||||
void reorder(int *data, int length){
|
||||
|
||||
int i=0;
|
||||
int j=length-1;
|
||||
|
||||
while(i<j){
|
||||
|
||||
if (!isEvenNum(data[i]) )
|
||||
{
|
||||
i++;
|
||||
continue;
|
||||
}
|
||||
|
||||
if (isEvenNum(data[j]) )
|
||||
{
|
||||
j--;
|
||||
continue;
|
||||
}
|
||||
|
||||
//交换
|
||||
int tmp ;
|
||||
tmp = data[i];
|
||||
data[i] = data[j];
|
||||
data[j] = tmp;
|
||||
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
int isEvenNum(int n){
|
||||
|
||||
return (n&1) == 0;
|
||||
}
|
||||
|
||||
|
||||
int main(int argc, char const *argv[])
|
||||
{
|
||||
|
||||
int a[]= {5,2,7,8,9,34,21,6};
|
||||
|
||||
reorder(a,8);
|
||||
|
||||
for (int i = 0; i < 8; ++i)
|
||||
{
|
||||
printf(" %d ", a[i]);
|
||||
}
|
||||
|
||||
|
||||
|
||||
return 0;
|
||||
}
|
||||
@@ -0,0 +1,15 @@
|
||||
#include <stdlib.h>
|
||||
#include <stdio.h>
|
||||
|
||||
#define PrintInt(expr) printf("%s : %d\n",#expr,(expr))
|
||||
|
||||
int main()
|
||||
{
|
||||
int y = 100;
|
||||
int *p;
|
||||
p = malloc(sizeof(int));
|
||||
*p = 10;
|
||||
y = y / *p;
|
||||
PrintInt(y);
|
||||
return 0;
|
||||
}
|
||||
@@ -0,0 +1,9 @@
|
||||
#include <stdio.h>
|
||||
int main()
|
||||
{
|
||||
int day,month,year;
|
||||
printf("Enter the date (dd-mm-yyyy) format including -'s:");
|
||||
scanf("%d-%d-%d",&day,&month,&year);
|
||||
printf("The date you have entered is %d-%d-%d\n",day,month,year);
|
||||
return 0;
|
||||
}
|
||||
@@ -0,0 +1,10 @@
|
||||
#include <stdio.h>
|
||||
int main()
|
||||
{
|
||||
int n;
|
||||
printf("Enter a number:\n");
|
||||
scanf("%d",&n); // &
|
||||
|
||||
printf("You entered %d \n",n);
|
||||
return 0;
|
||||
}
|
||||
@@ -0,0 +1,16 @@
|
||||
#include <stdio.h>
|
||||
int main()
|
||||
{
|
||||
int cnt = 5, a;
|
||||
|
||||
do {
|
||||
a /= cnt;
|
||||
} while (cnt --);
|
||||
|
||||
printf ("%d\n", a);
|
||||
return 0;
|
||||
}
|
||||
|
||||
/// cnt==1 时,会发生除0错误
|
||||
|
||||
|
||||
@@ -0,0 +1,11 @@
|
||||
#include <stdio.h>
|
||||
int main()
|
||||
{
|
||||
int i = 6;
|
||||
if( ((++i < 7) && ( i++/6)) || (++i <= 9))
|
||||
;
|
||||
printf("%d\n",i);
|
||||
return 0;
|
||||
}
|
||||
|
||||
// && 优先级大于 ||
|
||||
@@ -0,0 +1,18 @@
|
||||
#include <stdlib.h>
|
||||
#include <stdio.h>
|
||||
#define SIZE 15
|
||||
int main()
|
||||
{
|
||||
int *a, i;
|
||||
|
||||
a = malloc(SIZE*sizeof(int));
|
||||
|
||||
for (i=0; i<SIZE; i++)
|
||||
*(a + i) = i * i;
|
||||
for (i=0; i<SIZE; i++)
|
||||
printf("%d\n", *a++);
|
||||
free(a);
|
||||
return 0;
|
||||
}
|
||||
|
||||
// 指针已经移动,free错误
|
||||
@@ -0,0 +1,17 @@
|
||||
// FROM:http://www.gowrikumar.com/c/
|
||||
|
||||
#include <stdio.h>
|
||||
int main()
|
||||
{
|
||||
char dummy[80];
|
||||
printf("Enter a string:\n");
|
||||
scanf("%[^a]",dummy);
|
||||
printf("%s\n",dummy);
|
||||
return 0;
|
||||
}
|
||||
|
||||
/**
|
||||
|
||||
输入一个串,以字符'a'结尾
|
||||
*/
|
||||
|
||||
@@ -0,0 +1,21 @@
|
||||
#include <stdio.h>
|
||||
int main()
|
||||
{
|
||||
int a=3, b = 5;
|
||||
|
||||
printf(&a["Ya!Hello! how is this? %s\n"], &b["junk/super"]);
|
||||
|
||||
//printf(3["helloworld \n"]);
|
||||
|
||||
printf("%c\n", 0["this"] );
|
||||
|
||||
printf(&a["WHAT%c%c%c %c%c %c !\n"], 1["this"],
|
||||
2["beauty"],0["tool"],0["is"],3["sensitive"],4["CCCCCC"]);
|
||||
return 0;
|
||||
}
|
||||
|
||||
/*
|
||||
|
||||
|
||||
|
||||
*/
|
||||
@@ -0,0 +1,10 @@
|
||||
#include <stdlib.h>
|
||||
#include <stdio.h>
|
||||
int main()
|
||||
{
|
||||
int* ptr1,*ptr2;
|
||||
ptr1 = malloc(sizeof(int));
|
||||
ptr2 = ptr1;
|
||||
*ptr2 = 10;
|
||||
return 0;
|
||||
}
|
||||
@@ -0,0 +1,23 @@
|
||||
#include "stdio.h"
|
||||
|
||||
double Power(double base, int exponent)
|
||||
{
|
||||
if (exponent == 0) return 1;
|
||||
if (exponent == 1) return base;
|
||||
double result = Power(base, exponent >> 1);
|
||||
result *= result;
|
||||
|
||||
if (exponent & 1) result = result*base;
|
||||
|
||||
return result;
|
||||
}
|
||||
|
||||
int main(int argc, char const *argv[])
|
||||
{
|
||||
printf("%f\n", Power(2,4));
|
||||
printf("%f\n", Power(2.1,4));
|
||||
printf("%f\n", Power(0,4));
|
||||
printf("%f\n", Power(2,0));
|
||||
printf("%f\n", Power(2,-3)); //负数就挂了
|
||||
return 0;
|
||||
}
|
||||
@@ -0,0 +1,71 @@
|
||||
|
||||
#include "stdio.h"
|
||||
#include "stdlib.h"
|
||||
|
||||
char *integer_to_bin(long integer){
|
||||
|
||||
int bit_num = sizeof(long)*8;
|
||||
char *buffer= (char *)malloc(bit_num+1);
|
||||
if (buffer==NULL)
|
||||
{
|
||||
return "malloc error";
|
||||
}
|
||||
buffer[bit_num]='\0';
|
||||
|
||||
for (int i = 0; i < bit_num; ++i)
|
||||
{
|
||||
buffer[i]= integer<<i>>(bit_num-1);
|
||||
buffer[i]= buffer[i]==0?'0':'1';
|
||||
|
||||
}
|
||||
return buffer;
|
||||
|
||||
}
|
||||
|
||||
char *integer_to_hex(long integer){
|
||||
|
||||
int bit_num=sizeof(long)*8/4;
|
||||
char *buffer=(char *)malloc(bit_num+3);
|
||||
buffer[0]='0';
|
||||
buffer[1]='x';
|
||||
buffer[bit_num+2]='\0';
|
||||
char *tmp=&buffer[2];
|
||||
|
||||
for (int i = 0; i < bit_num; i++)
|
||||
{
|
||||
tmp[i]= integer<<(i*4)>>(bit_num*4-4);
|
||||
tmp[i]= tmp[i]>=0?tmp[i]:tmp[i]+16;
|
||||
tmp[i]= tmp[i]<10?(tmp[i]+48):(tmp[i]-10+'A');
|
||||
}
|
||||
|
||||
return buffer;
|
||||
}
|
||||
|
||||
|
||||
/*
|
||||
|
||||
A 65
|
||||
0 48
|
||||
|
||||
*/
|
||||
|
||||
|
||||
|
||||
int main(){
|
||||
|
||||
|
||||
printf("b(11) = %s\n", integer_to_bin(11));
|
||||
printf("b(1023) = %s\n",integer_to_bin(1023) );
|
||||
printf("b(23564) = %s\n",integer_to_bin(23564) );
|
||||
|
||||
|
||||
printf("h(11) = %s\n", integer_to_hex(11));
|
||||
printf("h(1023) = %s\n",integer_to_hex(1023) );
|
||||
printf("h(23564) = %s\n",integer_to_hex(23564) );
|
||||
printf("b(11) = %x\n", 11);
|
||||
printf("b(1023) = %x\n",1023 );
|
||||
printf("b(23564) = %x\n",23564 );
|
||||
|
||||
|
||||
return 0;
|
||||
}
|
||||
@@ -0,0 +1,53 @@
|
||||
|
||||
#include "stdio.h"
|
||||
|
||||
int isSquare(unsigned integer){
|
||||
|
||||
if (integer==1 || integer==0)
|
||||
{
|
||||
return integer;
|
||||
}
|
||||
|
||||
int divider=2,result=integer/divider;
|
||||
|
||||
while(result>divider){
|
||||
|
||||
divider++;
|
||||
result = integer/divider;
|
||||
|
||||
}
|
||||
|
||||
if (result!=divider)
|
||||
{
|
||||
return -1;
|
||||
}
|
||||
|
||||
return result;
|
||||
}
|
||||
|
||||
|
||||
int main(int argc, char const *argv[])
|
||||
{
|
||||
// 243,1849(43),289(17), 64(8)
|
||||
|
||||
int a[] = {1849,144,243,289,64,1194877489};
|
||||
for (int i = 0; i < sizeof(a)/sizeof(int); ++i)
|
||||
{
|
||||
int ret = isSquare(a[i]);
|
||||
|
||||
if (ret==-1)
|
||||
{
|
||||
printf("%d 不是某个数的平方\n",a[i]);
|
||||
}else{
|
||||
|
||||
printf("%d 是 %d 的平方\n", a[i], ret);
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
return 0;
|
||||
}
|
||||
|
||||
|
||||
|
||||
|
||||
@@ -0,0 +1,18 @@
|
||||
#include "stdio.h"
|
||||
|
||||
int one_appear_count_by_binary(int num){
|
||||
int count = 0;
|
||||
while(num !=0 ){
|
||||
num &= num-1;
|
||||
count++;
|
||||
}
|
||||
return count;
|
||||
}
|
||||
|
||||
int main(int argc, char const *argv[])
|
||||
{
|
||||
int test = 10;
|
||||
printf("%d\n", one_appear_count_by_binary(test));
|
||||
printf("%d\n", one_appear_count_by_binary(32+1));
|
||||
return 0;
|
||||
}
|
||||
@@ -0,0 +1,37 @@
|
||||
|
||||
#include "stdio.h"
|
||||
#include "string.h"
|
||||
|
||||
int string_to_integer(char *s,int length){
|
||||
|
||||
if (length>1)
|
||||
{
|
||||
return s[0]=='-'?string_to_integer(s,length-1)*10-(s[length-1]-'0'):string_to_integer(s,length-1)*10+s[length-1]-'0';
|
||||
}else{
|
||||
return s[0]=='-'?-1/10:s[0]-'0';
|
||||
|
||||
}
|
||||
}
|
||||
|
||||
/* 问题:
|
||||
1. s是空字符串
|
||||
2. s传参为非数字字符
|
||||
3. 超出整数所能表示范围
|
||||
*/
|
||||
|
||||
|
||||
int main(){
|
||||
|
||||
char *s = "4323";
|
||||
|
||||
printf("integer == %d\n",string_to_integer(s,strlen(s)) );
|
||||
printf("integer == %d\n",string_to_integer("12342",5));
|
||||
printf("integer == %d\n",string_to_integer("-11",3) );
|
||||
printf("integer == %d\n",string_to_integer("-5",2) );
|
||||
printf("integer == %d\n", string_to_integer("3",1));
|
||||
|
||||
printf("integer == %d\n", string_to_integer("abcd",4));
|
||||
printf("integer == %d\n", string_to_integer("a",1));
|
||||
|
||||
return 0;
|
||||
}
|
||||
@@ -0,0 +1,7 @@
|
||||
#include "stdio.h"
|
||||
|
||||
int main(){
|
||||
|
||||
printf("%ld",236432123443*33453098);
|
||||
return 0;
|
||||
}
|
||||
@@ -0,0 +1,22 @@
|
||||
#include "stdio.h"
|
||||
|
||||
char *delete_occurence_character(char *src , char target){
|
||||
char *front = src;
|
||||
char *rear = src;
|
||||
while(*front != '\0'){
|
||||
if (*front != target){
|
||||
*rear = *front;
|
||||
rear++;
|
||||
}
|
||||
front++;
|
||||
}
|
||||
*rear = '\0';
|
||||
return src;
|
||||
}
|
||||
|
||||
int main(int argc, char const *argv[])
|
||||
{
|
||||
char test[] = "abcdeccba";
|
||||
printf("%s\n", delete_occurence_character(test,'c'));
|
||||
return 0;
|
||||
}
|
||||
@@ -0,0 +1,23 @@
|
||||
|
||||
#include "stdio.h"
|
||||
|
||||
|
||||
int factorial(int n){
|
||||
|
||||
int ret=0;
|
||||
(n==0) || (ret=n+factorial(n-1));
|
||||
return ret;
|
||||
}
|
||||
|
||||
|
||||
int main(){
|
||||
|
||||
int n=10;
|
||||
printf("factorial(10)= %d\n",factorial(10));
|
||||
printf("factorial(5)= %d\n",factorial(5));
|
||||
printf("factorial(1)= %d\n",factorial(1));
|
||||
printf("factorial(0)= %d\n",factorial(0));
|
||||
|
||||
return 0;
|
||||
}
|
||||
|
||||
@@ -0,0 +1,57 @@
|
||||
|
||||
#include "stdio.h"
|
||||
#include "time.h"
|
||||
|
||||
|
||||
|
||||
int fibonacci(int n){
|
||||
|
||||
int result[2] = {0,1};
|
||||
if (n<2)
|
||||
{
|
||||
return result[n];
|
||||
}
|
||||
|
||||
return fibonacci(n-1)+fibonacci(n-2);
|
||||
}
|
||||
|
||||
|
||||
// 循环
|
||||
int fibonacci2(int n){
|
||||
|
||||
int result[2] = {0,1};
|
||||
|
||||
if ( n<2 )
|
||||
{
|
||||
return result[n];
|
||||
}
|
||||
|
||||
int fibOne=0;
|
||||
int fibTwo=1;
|
||||
int fibN;
|
||||
for (int i = 2; i < n; ++i)
|
||||
{
|
||||
fibN = fibOne + fibTwo;
|
||||
fibOne = fibTwo;
|
||||
fibTwo = fibN;
|
||||
|
||||
}
|
||||
|
||||
return fibN;
|
||||
}
|
||||
|
||||
|
||||
|
||||
int main(){
|
||||
|
||||
int start,end;
|
||||
long result;
|
||||
|
||||
start = clock();
|
||||
result=fibonacci2(40);
|
||||
end = clock();
|
||||
printf("%ld %.6fs",result,(double)(end-start)/CLOCKS_PER_SEC);
|
||||
|
||||
return 0;
|
||||
}
|
||||
|
||||
@@ -0,0 +1,56 @@
|
||||
#include "stdio.h"
|
||||
|
||||
int longest_continuious_number(const char *input,char *output){
|
||||
int max = 0;
|
||||
char *start= input;
|
||||
char *mid = input;
|
||||
char *end = input;
|
||||
int tmp = 0;
|
||||
if (input == NULL || output == NULL) return 0;
|
||||
|
||||
while (*end != '\0'){
|
||||
if (*end < '0' || *end > '9'){//字母
|
||||
if(tmp > max){
|
||||
max = tmp;
|
||||
start = mid;
|
||||
}
|
||||
tmp = 0;
|
||||
}else{//数字
|
||||
if (tmp == 0){//发现数字
|
||||
mid = end;
|
||||
}
|
||||
tmp++;
|
||||
}
|
||||
end++;
|
||||
}
|
||||
|
||||
//修改已数字结尾的bug
|
||||
if(tmp > max){
|
||||
max = tmp;
|
||||
start = mid;
|
||||
}
|
||||
|
||||
//copy
|
||||
int i=0;
|
||||
while(i<max){
|
||||
*(output+i) = *(start+i);
|
||||
i++;
|
||||
}
|
||||
*(output+i)='\0';
|
||||
|
||||
return max;
|
||||
}
|
||||
|
||||
int main(int argc, char const *argv[])
|
||||
{
|
||||
const char test[] = "abcd12345ed125ss123456789";
|
||||
char output[100] = {};
|
||||
int max = longest_continuious_number(test,output);
|
||||
printf("max=%d,number=%s\n", max,output);
|
||||
|
||||
return 0;
|
||||
}
|
||||
|
||||
|
||||
|
||||
|
||||
@@ -0,0 +1,48 @@
|
||||
|
||||
|
||||
#include "stdio.h"
|
||||
|
||||
//打印和为n的2个值
|
||||
void print_continuous_sequence_sum(int n){// n=1,2
|
||||
|
||||
int small=1;
|
||||
int big=2;
|
||||
int sum = small+big;
|
||||
while(small<big && big<=n/2+1){
|
||||
|
||||
if (sum==n)
|
||||
{
|
||||
printf("%d ,%d\n",small,big);
|
||||
|
||||
}
|
||||
|
||||
while(sum>n){
|
||||
sum-=small;
|
||||
small++;
|
||||
if (sum==n)
|
||||
{
|
||||
printf("%d, %d\n",small,big);
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
big++;
|
||||
sum+=big;
|
||||
|
||||
}
|
||||
|
||||
|
||||
|
||||
}
|
||||
|
||||
|
||||
|
||||
int main(int argc, char const *argv[])
|
||||
{
|
||||
|
||||
|
||||
print_continuous_sequence_sum(115);
|
||||
|
||||
return 0;
|
||||
}
|
||||
|
||||
@@ -0,0 +1,25 @@
|
||||
|
||||
|
||||
#include "stdio.h"
|
||||
|
||||
|
||||
/*
|
||||
|
||||
1 是第一个元素
|
||||
序列中元素被2或3或5整除
|
||||
|
||||
*/
|
||||
unsigned long value_in_sequence(unsigned index){
|
||||
|
||||
|
||||
|
||||
|
||||
}
|
||||
|
||||
|
||||
int main(){
|
||||
|
||||
|
||||
|
||||
return 0;
|
||||
}
|
||||
@@ -0,0 +1,43 @@
|
||||
#include "stdio.h"
|
||||
|
||||
// a[i*rows+j]
|
||||
void print_matrix_incircle(int *matrix,int rows,int cols,int start){
|
||||
int endX = cols-start-1;
|
||||
int endY = rows-start-1;
|
||||
|
||||
//然后依次打印 上,右,下,左
|
||||
for(int i=start;i<=endX;i++){
|
||||
printf("%d ",matrix[start*rows+i]);
|
||||
}
|
||||
|
||||
for(int i=start+1;i<=endY;i++){
|
||||
printf("%d ",matrix[i*rows+endX]);
|
||||
}
|
||||
|
||||
for(int i=endX-1;i>=start;i--){
|
||||
printf("%d ",matrix[endY*rows+i]);
|
||||
}
|
||||
|
||||
for(int i=endY-1;i>start;i--){
|
||||
printf("%d ",matrix[i*rows+start]);
|
||||
}
|
||||
}
|
||||
|
||||
void print_matrix(int *matrix,int rows,int cols){
|
||||
if(matrix==NULL) return ;
|
||||
if(rows<=0 || cols<=0) return;
|
||||
|
||||
int start = 0;
|
||||
while(start*2<cols && start*2<rows){
|
||||
print_matrix_incircle(matrix,rows,cols,start);
|
||||
start++;
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
int main(int argc, char const *argv[])
|
||||
{
|
||||
int matrix[] = {1,2,3,4, 5,6,7,8, 9,10,11,12, 13,14,15,16};
|
||||
print_matrix(matrix,4,4);
|
||||
return 0;
|
||||
}
|
||||
@@ -0,0 +1,70 @@
|
||||
|
||||
|
||||
#include "stdio.h"
|
||||
|
||||
int binary_search_first(int *a,int length,int key){
|
||||
|
||||
int low=0;
|
||||
int high = length-1;
|
||||
int mid = 0;
|
||||
|
||||
while(low<high){
|
||||
|
||||
mid = (low+high)/2;
|
||||
if (a[mid]>=key)//往左找
|
||||
{
|
||||
high = mid;
|
||||
}else{
|
||||
|
||||
low = mid+1;
|
||||
}
|
||||
|
||||
}
|
||||
return high;
|
||||
|
||||
}
|
||||
|
||||
int binary_search_last(int *a,int length,int key){
|
||||
|
||||
int low = 0;
|
||||
int high = length -1;
|
||||
int mid = 0;
|
||||
|
||||
while(low<high){
|
||||
|
||||
mid = (low+high+1)/2;
|
||||
if (a[mid]<=key)// 往右找
|
||||
{
|
||||
low=mid;
|
||||
}else{
|
||||
high = mid-1;
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
return low;
|
||||
}
|
||||
|
||||
|
||||
int element_appear_times(int *a,int length,int key){
|
||||
|
||||
printf("(%d) first appear at index %d\n",key,binary_search_first(a,length,key));
|
||||
printf("(%d) last appear at index %d\n",key,binary_search_last(a,length,key));
|
||||
return binary_search_last(a,length,key)-binary_search_first(a,length,key)+1;
|
||||
|
||||
}
|
||||
|
||||
|
||||
int main(){
|
||||
|
||||
|
||||
int a[] = {2,3,4,4,4,4,4,5,5,7,7,11,11,11,11,11,32,32,54};
|
||||
|
||||
printf("(2)appear times = %d\n", element_appear_times(a,sizeof(a)/sizeof(int),2) );
|
||||
printf("(4)appear times = %d\n", element_appear_times(a,sizeof(a)/sizeof(int),4) );
|
||||
printf("(11)appear times = %d\n", element_appear_times(a,sizeof(a)/sizeof(int),11) );
|
||||
printf("(36)appear times = %d\n", element_appear_times(a,sizeof(a)/sizeof(int),36) );
|
||||
printf("(54)appear times = %d\n", element_appear_times(a,sizeof(a)/sizeof(int),54) );
|
||||
return 0;
|
||||
}
|
||||
|
||||
@@ -0,0 +1,25 @@
|
||||
|
||||
/*
|
||||
二叉查找树变成双向链表
|
||||
*/
|
||||
|
||||
typedef struct BSTreeNode{
|
||||
int m_nValue;
|
||||
struct BSTreeNode *m_pLeft, *m_pRight;
|
||||
}BSTree;
|
||||
|
||||
void convertDoubleLinks(BSTree *root){
|
||||
|
||||
|
||||
}
|
||||
|
||||
|
||||
int main(int argc, char const *argv[])
|
||||
{
|
||||
|
||||
return 0;
|
||||
}
|
||||
|
||||
|
||||
|
||||
|
||||
@@ -0,0 +1,14 @@
|
||||
|
||||
#include "stdio.h"
|
||||
|
||||
int main(){
|
||||
|
||||
int j,k,m;
|
||||
int i= (j=4,k=5,m=8);
|
||||
printf("%d\n", i);
|
||||
|
||||
i= (j==4)?:0;
|
||||
printf("i=%d\n", i);
|
||||
|
||||
return 0;
|
||||
}
|
||||
@@ -0,0 +1,11 @@
|
||||
|
||||
|
||||
#include "stdio.h"
|
||||
|
||||
int main(){
|
||||
|
||||
int a = 257;
|
||||
unsigned char *result = (unsigned char *)&a;
|
||||
printf("%d.%d.%d.%d\n", result[0],result[1],result[2],result[3] );
|
||||
|
||||
}
|
||||
@@ -0,0 +1,23 @@
|
||||
// FROM:http://www.gowrikumar.com/c/
|
||||
|
||||
#include<stdio.h>
|
||||
|
||||
#define TOTAL_ELEMENTS (sizeof(array) / sizeof(array[0]))
|
||||
int array[] = {23,34,12,17,204,99,16};
|
||||
|
||||
int main()
|
||||
{
|
||||
int d;
|
||||
int n = TOTAL_ELEMENTS-2;
|
||||
for(d=-1;d <= (TOTAL_ELEMENTS-2);d++)
|
||||
printf("%d\n",array[d+1]);
|
||||
|
||||
return 0;
|
||||
}
|
||||
|
||||
|
||||
/*
|
||||
|
||||
d 是有符号数,sizeof()计算为无符号数,比较时d会转为无符号数,变成一个大数
|
||||
|
||||
*/
|
||||
@@ -0,0 +1,10 @@
|
||||
#include <stdio.h>
|
||||
int main()
|
||||
{
|
||||
int i=43;
|
||||
printf("%d\n",printf("%d",printf("%d",i)));
|
||||
return 0;
|
||||
}
|
||||
|
||||
|
||||
// printf 返回值是打印数据的长度 4321
|
||||
@@ -0,0 +1,20 @@
|
||||
#include <stdio.h>
|
||||
void foobar1(void)
|
||||
{
|
||||
printf("In foobar1\n");
|
||||
}
|
||||
|
||||
void foobar2()
|
||||
{
|
||||
printf("In foobar2\n");
|
||||
}
|
||||
|
||||
int main()
|
||||
{
|
||||
char ch = 'a';
|
||||
foobar1();
|
||||
foobar2();
|
||||
return 0;
|
||||
}
|
||||
|
||||
// compile error
|
||||
@@ -0,0 +1,18 @@
|
||||
#include <stdio.h>
|
||||
void foobar1(void)
|
||||
{
|
||||
printf("In foobar1\n");
|
||||
}
|
||||
|
||||
void foobar2()
|
||||
{
|
||||
printf("In foobar2\n");
|
||||
}
|
||||
|
||||
int main()
|
||||
{
|
||||
char ch = 'a';
|
||||
foobar1();
|
||||
foobar2(33, ch);
|
||||
return 0;
|
||||
}
|
||||
@@ -0,0 +1,16 @@
|
||||
// FROM:http://www.gowrikumar.com/c/
|
||||
#include <stdio.h>
|
||||
int main()
|
||||
{
|
||||
float a = 12.5;
|
||||
printf("%d\n", a);
|
||||
//printf("%d\n", *(int *)&a);
|
||||
return 0;
|
||||
}
|
||||
|
||||
// 浮点数 转 整形 ,用强制转换(int)a
|
||||
|
||||
/*
|
||||
浮点数的存储
|
||||
|
||||
*/
|
||||
@@ -0,0 +1 @@
|
||||
int arr[80];
|
||||
@@ -0,0 +1,18 @@
|
||||
#include "c13-1.c"
|
||||
|
||||
|
||||
int main()
|
||||
{
|
||||
return 0;
|
||||
}
|
||||
|
||||
|
||||
/**
|
||||
类型并不匹配,所以导致这里的arr并没有指向c13-1.c中声明的arr[80],应该修改为 extern int arr[]
|
||||
|
||||
int *arr 和 int arr[80] 区别?
|
||||
|
||||
|
||||
|
||||
|
||||
*/
|
||||
@@ -0,0 +1,16 @@
|
||||
#include<stdio.h>
|
||||
int main()
|
||||
{
|
||||
int a=1;
|
||||
switch(a)
|
||||
{ int b=20;
|
||||
case 1: printf("b is %d\n",b);
|
||||
break;
|
||||
default:printf("b is %d\n",b);
|
||||
break;
|
||||
}
|
||||
return 0;
|
||||
}
|
||||
|
||||
|
||||
// In my computer ,output:b is 32767 switch中只会从case开始执行,b会随机输出
|
||||
@@ -0,0 +1,17 @@
|
||||
|
||||
#include <stdio.h>
|
||||
#define SIZE 10
|
||||
void size(int arr[SIZE])
|
||||
{
|
||||
printf("size of array is:%d\n",sizeof(arr));
|
||||
}
|
||||
|
||||
int main()
|
||||
{
|
||||
int arr[SIZE];
|
||||
size(arr);
|
||||
return 0;
|
||||
}
|
||||
|
||||
|
||||
// 32位机器 4, 64位机器 8
|
||||
@@ -0,0 +1,38 @@
|
||||
// FROM:http://www.gowrikumar.com/c/
|
||||
|
||||
#include <stdlib.h>
|
||||
#include <stdio.h>
|
||||
void Error(char* s)
|
||||
{
|
||||
printf(s);
|
||||
return;
|
||||
}
|
||||
|
||||
int main()
|
||||
{
|
||||
int *p;
|
||||
p = malloc(sizeof(int));
|
||||
if(p == NULL)
|
||||
{
|
||||
Error("Could not allocate the memory\n");
|
||||
Error("Quitting....\n");
|
||||
exit(1);
|
||||
}
|
||||
else
|
||||
{
|
||||
/*some stuff to use p*/
|
||||
Error("Could not allocate the memory\n");
|
||||
Error("Quitting....\n");
|
||||
Error(5);
|
||||
}
|
||||
return 0;
|
||||
}
|
||||
|
||||
/*
|
||||
|
||||
潜在的问题是:
|
||||
|
||||
Error函数中,如果输入的参数不是字符串,比如传一个整数5,5被转成一个内存地址,printf这时候访问就会出问题了。
|
||||
$1 = 0x5 <error: Cannot access memory at address 0x5>
|
||||
|
||||
*/
|
||||
@@ -0,0 +1,27 @@
|
||||
// FROM:http://www.gowrikumar.com/c/
|
||||
|
||||
#include <stdio.h>
|
||||
int main()
|
||||
{
|
||||
|
||||
char c;
|
||||
scanf("%c",&c);
|
||||
printf("%c\n",c);
|
||||
|
||||
scanf(" %c",&c);
|
||||
printf("%c\n",c);
|
||||
|
||||
return 0;
|
||||
}
|
||||
|
||||
/*
|
||||
|
||||
scanf("%c",&c) 和 scanf(" %c",&c) 区别
|
||||
|
||||
|
||||
使用第二个scanf("%c",&c) 时,系统会将前一个scanf()输入的回车符号读入改变量。
|
||||
这里为什么加一个“空格”就可以?
|
||||
scanf带“空格”后,会从输入缓冲区中skip 空白符(空格、tab,换行符),读取一个字符
|
||||
|
||||
*/
|
||||
|
||||
@@ -0,0 +1,12 @@
|
||||
#include <stdio.h>
|
||||
int main()
|
||||
{
|
||||
int i;
|
||||
i = 10;
|
||||
printf("i : %d\n",i);
|
||||
printf("sizeof(i++) is: %d\n",sizeof(i++));
|
||||
printf("i : %d\n",i);
|
||||
return 0;
|
||||
}
|
||||
|
||||
// sizeof()不会对传入的表达式计算 ,所以后面的输入还是i:10
|
||||
@@ -0,0 +1,23 @@
|
||||
#include <stdio.h>
|
||||
#include <stdlib.h>
|
||||
|
||||
#define SIZEOF(arr) (sizeof(arr)/sizeof(arr[0]))
|
||||
|
||||
#define PrintInt(expr) printf("%s:%d\n",#expr,(expr))
|
||||
int main()
|
||||
{
|
||||
/* The powers of 10 */
|
||||
int pot[] = {
|
||||
0001,
|
||||
0010,
|
||||
0100,
|
||||
1000
|
||||
};
|
||||
int i;
|
||||
|
||||
for(i=0;i<SIZEOF(pot);i++)
|
||||
PrintInt(pot[i]);
|
||||
return 0;
|
||||
}
|
||||
|
||||
// 以0大头的都是8进制数据
|
||||
@@ -0,0 +1,40 @@
|
||||
#include<stdio.h>
|
||||
|
||||
void OS_Solaris_print()
|
||||
{
|
||||
printf("Solaris - Sun Microsystems\n");
|
||||
}
|
||||
|
||||
void OS_Windows_print()
|
||||
{
|
||||
printf("Windows - Microsoft\n");
|
||||
|
||||
}
|
||||
void OS_HP_UX_print()
|
||||
{
|
||||
printf("HP-UX - Hewlett Packard\n");
|
||||
}
|
||||
|
||||
int main()
|
||||
{
|
||||
int num;
|
||||
printf("Enter the number (1-3):\n");
|
||||
scanf("%d",&num);
|
||||
switch(num)
|
||||
{
|
||||
case 1:
|
||||
OS_Solaris_print();
|
||||
break;
|
||||
case 2:
|
||||
OS_Windows_print();
|
||||
break;
|
||||
case 3:
|
||||
OS_HP_UX_print();
|
||||
break;
|
||||
default:
|
||||
printf("Hmm! only 1-3 :-)\n");
|
||||
break;
|
||||
}
|
||||
|
||||
return 0;
|
||||
}
|
||||
@@ -0,0 +1,9 @@
|
||||
int main()
|
||||
{
|
||||
char ch1;
|
||||
char ch2;
|
||||
ch1 = getchar();
|
||||
ch2 = getchar();
|
||||
printf("%d %d", ch1, ch2);
|
||||
return 0;
|
||||
}
|
||||
@@ -0,0 +1,16 @@
|
||||
#include <stdio.h>
|
||||
|
||||
enum {false,true};
|
||||
|
||||
int main()
|
||||
{
|
||||
int i=1;
|
||||
do
|
||||
{
|
||||
printf("%d\n",i);
|
||||
i++;
|
||||
if(i < 15)
|
||||
continue;
|
||||
}while(false);
|
||||
return 0;
|
||||
}
|
||||
@@ -0,0 +1,24 @@
|
||||
#include <stdio.h>
|
||||
#include <unistd.h>
|
||||
int main()
|
||||
{
|
||||
while(1)
|
||||
{
|
||||
fprintf(stdout,"hello-out");
|
||||
fprintf(stderr,"hello-err");
|
||||
sleep(1);
|
||||
}
|
||||
return 0;
|
||||
}
|
||||
|
||||
|
||||
/*
|
||||
|
||||
stdout 会缓冲输出
|
||||
|
||||
\n
|
||||
fflush()
|
||||
|
||||
setbuf(stdout,NULL)
|
||||
|
||||
*/
|
||||
@@ -0,0 +1,15 @@
|
||||
|
||||
// FROM:http://www.gowrikumar.com/c/
|
||||
#include <stdio.h>
|
||||
#define f(a,b) a##b
|
||||
#define g(a) #a
|
||||
#define h(a) g(a)
|
||||
|
||||
int main()
|
||||
{
|
||||
printf("%s\n",h(f(1,2))); //
|
||||
printf("%s\n",g(f(1,2))); // f(1,2)
|
||||
return 0;
|
||||
}
|
||||
|
||||
|
||||
@@ -0,0 +1,20 @@
|
||||
#include<stdio.h>
|
||||
int main()
|
||||
{
|
||||
int a=10;
|
||||
switch(a)
|
||||
{
|
||||
case '1':
|
||||
printf("ONE\n");
|
||||
break;
|
||||
case '2':
|
||||
printf("TWO\n");
|
||||
break;
|
||||
a:
|
||||
printf("NONE\n");
|
||||
}
|
||||
return 0;
|
||||
}
|
||||
|
||||
|
||||
//什么也不输出, “default”这里随便什么都没有编译错误
|
||||
@@ -0,0 +1,8 @@
|
||||
#include <stdlib.h>
|
||||
int main()
|
||||
{
|
||||
int* p;
|
||||
p = (int*)malloc(sizeof(int));
|
||||
*p = 10;
|
||||
return 0;
|
||||
}
|
||||
@@ -0,0 +1,20 @@
|
||||
|
||||
#include <stdio.h>
|
||||
|
||||
int main()
|
||||
{
|
||||
float f=0.0f;
|
||||
int i;
|
||||
|
||||
for(i=0;i<10;i++)
|
||||
f = f + 0.1f;
|
||||
|
||||
if(f == 1.0f)
|
||||
printf("f is 1.0 \n");
|
||||
else
|
||||
printf("f is NOT 1.0\n");
|
||||
|
||||
return 0;
|
||||
}
|
||||
|
||||
// 浮点数判相等
|
||||
@@ -0,0 +1,13 @@
|
||||
|
||||
#include <stdio.h>
|
||||
|
||||
int main()
|
||||
{
|
||||
int a = (1,2);
|
||||
printf("a : %d\n",a);
|
||||
return 0;
|
||||
}
|
||||
|
||||
// ,运算符优先级 最低 ,括号不能少
|
||||
//
|
||||
|
||||
@@ -0,0 +1,125 @@
|
||||
|
||||
#include "stdio.h" // fwrite
|
||||
#include "unistd.h" // write
|
||||
#include "stdlib.h" // itoa
|
||||
#include "string.h"
|
||||
|
||||
#define test_file_path2 "./ip2.txt"
|
||||
#define test_file_path "./ip.txt"
|
||||
#define ip_count 100000000 //随机1亿个IP
|
||||
#define tmp_file_count 32
|
||||
#define mem_count 128*1024*1028 //128MB个IP空间
|
||||
|
||||
int hash(unsigned i){
|
||||
return i>>27;
|
||||
}
|
||||
|
||||
int main(){
|
||||
|
||||
|
||||
// 模拟创建一个文件,产生测试用的IP
|
||||
// FILE *fd;
|
||||
// fd = fopen(tmp_file_path,"a+");
|
||||
// for (int i = 0; i < ip_count;++i)
|
||||
// {
|
||||
// //char c[] = "this is sentence!";
|
||||
// unsigned random_ip = (unsigned)rand();
|
||||
// char tmp[50];
|
||||
// sprintf(tmp,"%d",random_ip);
|
||||
// //printf("random ip = %s\n",tmp);
|
||||
// fwrite(tmp,strlen(tmp),1,fd);
|
||||
// fputc('\n',fd);//加入换行符号
|
||||
// }
|
||||
|
||||
|
||||
//创建临时文件
|
||||
FILE *fd[tmp_file_count];
|
||||
for (int i = 0; i < tmp_file_count; ++i)
|
||||
{
|
||||
char file_path[128];
|
||||
//s[0]=(char)(i+48);
|
||||
//char *file_path = strcat("./ipslice/ipslice",s) ; // 这样写出现 bus error!!!
|
||||
sprintf(file_path,"./ipslice/ipslice%d",i);
|
||||
fd[i] = fopen(file_path ,"a+" );
|
||||
if ( !fd[i])
|
||||
{
|
||||
printf("file %d open fail!! \n" ,i);
|
||||
}
|
||||
}
|
||||
|
||||
//开始读测试数据IP,按IP,映射到32个文件中
|
||||
FILE *testfd = fopen(test_file_path2,"r");
|
||||
if (!testfd)
|
||||
{
|
||||
printf("file open fail !!\n");
|
||||
exit(-1);
|
||||
}
|
||||
unsigned tmp_data;
|
||||
while( getline() ){ //能读到数据 // fread(&tmp_data,sizeof(unsigned),1,testfd) , C语言中没有getline() 这样的函数
|
||||
|
||||
printf("%d\n",(unsigned)tmp_data );
|
||||
int key = hash((unsigned)tmp_data);// key值即为对应文件fd
|
||||
|
||||
int size = fwrite(&tmp_data,sizeof(unsigned),1,fd[key]);
|
||||
printf("写入数据size=%d ,key=%d\n",size, key );
|
||||
|
||||
//跳过换行符
|
||||
fseek(testfd,1,SEEK_CUR);
|
||||
}
|
||||
|
||||
|
||||
// 依次读入每个文件并统计, hash_map 统计每个区间段的最大IP
|
||||
int hash_map[mem_count];
|
||||
int max_ip;
|
||||
int max_times;
|
||||
|
||||
for (int i = 0; i < tmp_file_count; ++i)
|
||||
{
|
||||
|
||||
// hash统计
|
||||
while(){}
|
||||
|
||||
//hash 表里找出最大的,变量一遍
|
||||
for (int i = 0; i < mem_count; ++i)
|
||||
{
|
||||
/* code */
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
|
||||
|
||||
// 释放
|
||||
for (int i = 0; i < tmp_file_count; ++i)
|
||||
{
|
||||
fclose(fd[i]);
|
||||
}
|
||||
|
||||
|
||||
return 0;
|
||||
}
|
||||
|
||||
|
||||
|
||||
/**
|
||||
关于warning
|
||||
implicit declaration of function 'itoa' is invalid in C99
|
||||
|
||||
itoa() 函数并不是一个 standard functions ANSI C standard
|
||||
|
||||
|
||||
inet_ntop() // 整数转换成 .分 ip地址 字符串
|
||||
inet_nton() //
|
||||
|
||||
inet_aton() //
|
||||
|
||||
struct in_addr
|
||||
|
||||
*/
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
Reference in New Issue
Block a user