C 常用的输入输出 栈 哈希 文件写读 实现 字符串处理
2013-10-13 19:01
423 查看
#include "stdafx.h"
#include <stdio.h>
#include <string.h>
#include <stdlib.h> //引用动态分配数组的malloc
typedef struct stack
{
int top;
char str[10] ;
stack * next;
}SqStack;
SqStack * InitStack()
{
SqStack * ret = NULL;
ret = (SqStack*)malloc(sizeof(SqStack));
if (ret)
{
/*将栈的长度初始化为0*/
ret -> top = 0;
}
return ret;
}
typedef struct HashNode
{
char data[20];
int count;
HashNode * next;
};
//哈希表
HashNode HashTable[100];
// 最简单hash函数 `
int hash_function( char const *p)
{
int value = 0;
while (*p != '/0')
{
value = value * 31 + *p++;
value = value % 100;
}
return value;
}
//hash增加节点,对冲突使用链地址法解决
// 添加单词到hash表
void append_word(char const *str)
{
int index=hash_function(str);
HashNode hn = HashTable[index];
while(hn != NULL)
{
if(strcmp(str,hn.data)==0)
{
hn.count++;
return;
}
hn=hn.next;
}
//新建一个节点
HashNode nhn=new HashNode;
nhn.count=1;
nhn.data=new char[strlen(str)+1];
nhn.next=HashTable[index];
HashTable[index]=nhn;
}
int main()
{
//哈希表
char * str_hs="213";
append_word(str_hs);
//new malloc 不同
//malloc 和 new 至少有两个不同: new 返回指定类型的指针,并且可以自动计算所需要大小;new 自动进行初始化,malloc是随机的;new 是关键字,malloc不是;
//malloc 释放需要判断释放null,new则不需要
SqStack* ss1=new SqStack;
delete(ss1);
SqStack* ss2=(SqStack*)malloc(sizeof(SqStack));
free(ss2);
//栈实现
SqStack * ss;
ss=InitStack();
//字符串处理
//长度 返回值是字符串s的长度。不包括结束符'/0'。一个汉字占两个字节
char * str1="12 e43字";//print 8
int len= strlen(str1);
printf("%d",len);
char * str_1="1232";
char * str_2="2313";
int boolValue=strcmp(str_1,str_2);//0为相同
getchar();
//写文件,覆盖写
FILE * fp_w=fopen("b.txt","w");
FILE * fp_r2=fopen("a.txt","r");
char tmp3[15];
int tmp4;
while(fscanf(fp_r2,"%s %d",tmp3,&tmp4)!=EOF)
{
fprintf(fp_w,"%s %d\n",tmp3,tmp4);
}
fclose(fp_r2);
fclose(fp_w);
//读文件
FILE * fp_r=fopen("a.txt","r");
char tmp1[15];
int tmp2;
while(fscanf(fp_r,"%s %d",tmp1,&tmp2)!=EOF)
{
printf("%s",tmp1);
printf("%d\n",tmp2);
}
fclose(fp_r);
//输入
getchar();
char str[10];
scanf("%s",str);//遇空格结束
gets(str);//包含空格
//输出
printf("%s",str);
puts(str);
getchar();
return 0;
}
#include <stdio.h>
#include <string.h>
#include <stdlib.h> //引用动态分配数组的malloc
typedef struct stack
{
int top;
char str[10] ;
stack * next;
}SqStack;
SqStack * InitStack()
{
SqStack * ret = NULL;
ret = (SqStack*)malloc(sizeof(SqStack));
if (ret)
{
/*将栈的长度初始化为0*/
ret -> top = 0;
}
return ret;
}
typedef struct HashNode
{
char data[20];
int count;
HashNode * next;
};
//哈希表
HashNode HashTable[100];
// 最简单hash函数 `
int hash_function( char const *p)
{
int value = 0;
while (*p != '/0')
{
value = value * 31 + *p++;
value = value % 100;
}
return value;
}
//hash增加节点,对冲突使用链地址法解决
// 添加单词到hash表
void append_word(char const *str)
{
int index=hash_function(str);
HashNode hn = HashTable[index];
while(hn != NULL)
{
if(strcmp(str,hn.data)==0)
{
hn.count++;
return;
}
hn=hn.next;
}
//新建一个节点
HashNode nhn=new HashNode;
nhn.count=1;
nhn.data=new char[strlen(str)+1];
nhn.next=HashTable[index];
HashTable[index]=nhn;
}
int main()
{
//哈希表
char * str_hs="213";
append_word(str_hs);
//new malloc 不同
//malloc 和 new 至少有两个不同: new 返回指定类型的指针,并且可以自动计算所需要大小;new 自动进行初始化,malloc是随机的;new 是关键字,malloc不是;
//malloc 释放需要判断释放null,new则不需要
SqStack* ss1=new SqStack;
delete(ss1);
SqStack* ss2=(SqStack*)malloc(sizeof(SqStack));
free(ss2);
//栈实现
SqStack * ss;
ss=InitStack();
//字符串处理
//长度 返回值是字符串s的长度。不包括结束符'/0'。一个汉字占两个字节
char * str1="12 e43字";//print 8
int len= strlen(str1);
printf("%d",len);
char * str_1="1232";
char * str_2="2313";
int boolValue=strcmp(str_1,str_2);//0为相同
getchar();
//写文件,覆盖写
FILE * fp_w=fopen("b.txt","w");
FILE * fp_r2=fopen("a.txt","r");
char tmp3[15];
int tmp4;
while(fscanf(fp_r2,"%s %d",tmp3,&tmp4)!=EOF)
{
fprintf(fp_w,"%s %d\n",tmp3,tmp4);
}
fclose(fp_r2);
fclose(fp_w);
//读文件
FILE * fp_r=fopen("a.txt","r");
char tmp1[15];
int tmp2;
while(fscanf(fp_r,"%s %d",tmp1,&tmp2)!=EOF)
{
printf("%s",tmp1);
printf("%d\n",tmp2);
}
fclose(fp_r);
//输入
getchar();
char str[10];
scanf("%s",str);//遇空格结束
gets(str);//包含空格
//输出
printf("%s",str);
puts(str);
getchar();
return 0;
}
相关文章推荐
- java中字符串处理、串联和转换的几个常用方法,以及如果需要自己编程实现的具体实施步骤。
- C常用字符串和文件处理函数
- 蓝桥杯 问题 1094: 字符串的输入输出处理(c++实现)
- 在BAT文件中实现对空格分割的字符串的处理
- 操作文件处理常用方法(Java实现)
- strcat,strcpy,strcmp,strlen4个常用字符串处理函数的数组与指针简单实现方法~
- C++ 常用的字符串处理函数实现
- 在Javascript中对String的一些方法扩展,实现常用的字符串处理。
- 实现常用字符串处理函数(不调用库函数)
- 常用的字符串处理库函数的C语言实现
- 常用的字符串处理函数实现(不调用库函数)
- C#字符串数组排序 C#排序算法大全 C#字符串比较方法 一个.NET通用JSON解析/构建类的实现(c#) C#处理Json文件 asp.net使用Jquery+iframe传值问题
- 几个常用函数(切除字符串,时间处理,文件上传)
- 实现常用字符串处理函数(不调用库函数)
- 使用.net FtpWebRequest 实现FTP常用功能 上传 下载 获取文件列表 移动 切换目录 改名 .
- Log4j配置学习文档之二 处理日滚文件-实现原理
- 基于php常用函数总结(数组,字符串,时间,文件操作)
- 自己实现基本的C标准库字符串处理函数——基本问题
- java实现解析二进制文件的方法(字符串、图片)
- Python处理json字符串转化为字典的简单实现