C语言
carolAIF
这个作者很懒,什么都没留下…
展开
-
C程序设计语言(第二版) 1-14
编写一个程序,打印输入中各个字符出现频率的直方图 #include<stdio.h>void main(){ int letters[26]; for(int i=0;i<26;i++) letters[i]=0; int c; //a c=getchar(); int charA=65;//A的ascii码为65,a的ascii为97...原创 2010-07-05 22:29:34 · 194 阅读 · 0 评论 -
C程序 关于字符串的问题
#include<stdio.h>void strcpy(char *s,char *t);int main(){ char *s = new char[100]; s = "hello"; //char s[100] = "hello"; char *d = "world"; strcpy(s,d); printf("%s\...原创 2010-07-23 15:47:24 · 203 阅读 · 0 评论 -
C程序设计语言(第二版)5-2
#include <ctype.h>#include <stdio.h>#define BUFSIZE 100char buf[BUFSIZE]; /* buffer for ungetch */int bufp = 0; /* next free position in buf */int getch(void);void...原创 2010-07-22 17:13:44 · 259 阅读 · 0 评论 -
C程序设计语言(第二版) 4-14
定义宏swap(t, x, y)以交换t类型的两个参数 #include<stdio.h>#define swap(t,x,y) do{t z=x;x=y;y=z;}while(0) int main(void) { int ix, iy; double dx, dy; char *px, *py; ix = 42; iy = 6...原创 2010-07-22 11:47:59 · 265 阅读 · 0 评论 -
C程序设计语言(第二版) 4-13 递归版本reverse()将字符串倒置输出
编写一个递归版本的reverse()函数,将字符串倒置 #include<stdio.h>void reverse(char *s);int main(){ char *s ="hello"; reverse(s); return 0;}void reverse(char *s){ char line =*s; if(line){...原创 2010-07-22 11:18:24 · 459 阅读 · 1 评论 -
C程序设计语言(第二版) 4-12
运用printd函数的设计思想编写一个递归版本的itoa函数,即通过递归调用把整数转换为字符串 #include <stdlib.h> #include <stdio.h> /*思路:先调用itoa,在itoa中判断int型value是正还是负,无论正负,将value转换成unsigned型,然后调用utoa*/char *utoa(unsigned...原创 2010-07-22 10:29:06 · 242 阅读 · 0 评论 -
C程序 递归小例子 快速排序
从执行速度来讲,下列版本的快速排序可能不是最快的,但是它是最简单的算法之一。在每次划分子集时,该算法总是选取各个子数组的中间元素,(作为划分子集的基准)。 /* qsort: 以递增顺序对v[left]...v[right]进行排序 */ void qsort(int v[], int left, int right) { int i, last; ...原创 2010-07-22 09:26:54 · 170 阅读 · 0 评论 -
C程序 递归实现将数字作为字符串输出
将一个数字作为字符串打印的情况,数字是反序生成的:低位数字先于高位数字生成,但他们必须以与此相反的次序打印。解决该问题有两种方法,一种是将生成的各个数字依次存储在字符数组中,然后再以相反的次序打印,这种方式与3.6中的itoa函数的处理方式相似。 另一种方法则是使用递归:#include<stdio.h>void printd(int n){ if(n&...原创 2010-07-21 15:47:13 · 381 阅读 · 0 评论 -
C语言学习之变量存储
http://zhidao.baidu.com/question/42163882.html?fr=ala0 C语言中对变量的说明包括两方面的内容:变量类型以及变量的存储类型。变量类型如:int(整形),char(字符型)是用来说明变量所占用的内存空间的大小。变量存储类型用来说明变量的作用范围。C语言的变量存储类有:自动类、寄存器类、静态类和外部类。关键字auto加在变量名及其类型前,用来...原创 2010-07-21 15:14:52 · 213 阅读 · 0 评论 -
C语言外部变量的使用以及extern的用法
http://hi.baidu.com/%C3%E2%B7%D1%B4%F3%BB%B0%D0%F2%C1%D0%BA%C5/blog/item/e3b2e4cb4f2be1f453664f06.html 对外部变量的说明和定义不是一回事.对外部变量的说明,只是声明该变量是在外部定义过的一个全局变量..在这里引用.而对外部变量的定义,则是要分配存储单元.一个全局变量只能定义一次,却可...原创 2010-07-21 15:11:11 · 1417 阅读 · 0 评论 -
C语言 全局变量和局部变量
http://hi.baidu.com/%C3%E2%B7%D1%B4%F3%BB%B0%D0%F2%C1%D0%BA%C5/blog/item/2d559ddc8f07e2a8cd11661d.html 变量可以在程序中三个地方说明: 函数内部、函数的参数定义中或所有的函数外部。根据所定义位置的不同, 变量可分为局部变量、形式参数和全程变量。从空间角度来看,变量可以分为全局变量和局部变量...原创 2010-07-21 15:07:41 · 155 阅读 · 0 评论 -
C语言 extern
在头文件中声明函数时,前面的extern可有可无,只要这些函数曾在某个.c文件中实现就行了,不过一些函数没有在所包含的头文件中给出声明,同时函数的定义位于其它文件中,这时候使用该函数的.c文件必须用extern标记该函数为外部函数。举个例子: /* c.h */extern int max(int a, int b);extern int min(int a, int b);...原创 2010-07-21 11:20:49 · 143 阅读 · 0 评论 -
C程序设计语言(第二版) 4-8 最多只压回 一个字符
#include <stdio.h> int buf = EOF; /* buffer for ungetch */ int getch(void) /* get a (possibly pushed back) character */ { int temp; if (buf != EOF) { temp = buf; buf...原创 2010-07-20 18:47:58 · 220 阅读 · 0 评论 -
C程序设计语言(第二版) 5-3 编写strcat()
#include<stdio.h>void strcat(char *s, char *t);void strcpy(char *s,char *t);int main(){ char s[100] = "hello"; char *d = "world"; strcat(s,d); printf("%s\n",s); retu...原创 2010-07-23 16:02:56 · 205 阅读 · 0 评论 -
C程序设计语言(第二版) 5-4 strend(s,t)
strend(s,t) 如果字符串t出现在字符串s的尾部,该函数返回0,否则返回1 #include<stdio.h>int strend(char *s,char *t);int main(){ char *s="hewawowa"; char *t="wo"; int cmp=strend(s,t); printf("%d\n...原创 2010-07-23 16:29:42 · 357 阅读 · 0 评论 -
C程序设计语言(第二版) 1-13
编写一个程序,打印输入中单词长度的直方图。水平方向要容易些,垂直方向要难些 水平方向: #include<stdio.h>//#define aaa 10;void main(){ //int charArray[aaa];//记录每个单词中字符的个数的数组 int charArray[10]; int j; for(j=0;j<10;...原创 2010-07-05 21:56:40 · 203 阅读 · 0 评论 -
让人迷糊的getchar() putchar()
http://topic.csdn.net/u/20090120/14/cb567344-199e-4b54-a480-8246b7cd4bf1.html原创 2010-07-04 21:43:11 · 80 阅读 · 0 评论 -
C程序设计语言(第二版) 1-10
//编写一个将输入复制到输出的程序,并将其中的制表符替换为\t,把回退符替换为\b,把反斜杠替换为\\。#include<stdio.h>void main(){ int c; while((c=getchar())!=EOF){ if(c=='\t'){ putchar('\\'); putchar('t'); } ...原创 2010-07-04 21:37:23 · 134 阅读 · 0 评论 -
C程序设计语言(第二版) 1-9
//编写一个将输入复制到输出的程序,并将其中连续的多个空格用一个空格代替#include<stdio.h>void main(){ int c; int flag=0; while((c=getchar())!=EOF){ if(c!=' '){ flag=0; putchar(c); } if(c==' '){ i...原创 2010-07-04 21:15:47 · 388 阅读 · 1 评论 -
C程序设计语言(第二版) 1-8
//1-8输出所输入字符中空格,制表符,换行符的个数#include<stdio.h>void main(){ int c,space,table,enter; space=0; table=0; enter=0; while((c=getchar())!=EOF){ if(c==' ')//空格的ASCII码为32 space++;...原创 2010-07-04 16:54:13 · 124 阅读 · 0 评论 -
C语言学习笔记 之EOF
#include<stdio.h>void main(){ int c; c=getchar(); while(c!=EOF){ putchar(c); c=getchar(); }} 连续输入多个字符,并保持在缓存中,输入回车后,输出所输入的字符。Ctrl+Z不会结束输入,会结束输出EOF即Ctrl+Z...原创 2010-07-03 20:37:49 · 145 阅读 · 0 评论 -
C程序 声明与定义的区别
http://zhidao.baidu.com/question/144340465.html?fr=ala0 如果是指变量的声明和定义:从编译原理上来说,声明是仅仅告诉编译器,有个某类型的变量会被使用,但是编译器并不会为它分配任何内存。而定义就是分配了内存。对于下面的两句代码:void Func(){int a;int b=0;a=0;}对于第一行代码,编译器不会做任何事,它不会为它在栈...原创 2010-07-30 15:34:36 · 291 阅读 · 0 评论 -
C语言 函数指针 与 指针函数
指针函数是返回指针的函数,即本质是一个函数。函数返回类型是某一类型的指针例如:int *f(x,y);函数指针是指向函数的指针变量,即本质是一个指针变量。例如: int (*f) (int x); /* 声明一个函数指针 */ f=func; /* 将func函数的首地址赋给指针f */ 分析复杂的声明: char (*a[3])(int); ...原创 2010-07-30 10:27:57 · 129 阅读 · 0 评论 -
C程序 命令行参数
echo.cpp #include <stdio.h>/* echo command-line arguments; 1st version */main(int argc, char *argv[]){ int i; for (i = 1; i < argc; i++) printf("%s%s", argv[i], (i < argc...原创 2010-07-30 09:34:05 · 160 阅读 · 0 评论 -
C程序 整型数按位取反 输出结果
#include <stdio.h>int main(){ int i = 2; int j=~i; printf("%d\n",j); return 0;}// 输出结果为-3 在计算机里数据以补码的形式存放,2的源码是 0010,其补码是0010,取反后为1101,输出时,计算机认为1101是一个负数,因此,既是一个求补码的原码的过程。一个数的补码是...原创 2010-07-29 15:16:10 · 813 阅读 · 0 评论 -
C程序 对文本行进行排序
#include <stdio.h>#include <string.h>#include <stdlib.h> #define MAXLINES 5000 /* max #lines to be sorted */char *lineptr[MAXLINES]; /* pointers to text lines */in...原创 2010-07-26 09:30:30 · 316 阅读 · 0 评论 -
C语言:malloc()函数与alloc()函数
C语言跟内存分配方式 (1) 从静态存储区域分配。内存在程序编译的时候就已经分配好,这块内存在程序的整个运行期间都存在。例如全局变量,static变量。 (2) 在栈上创建。在执行函数时,函数内局部变量的存储单元都可以在栈上创建,函数执行结束时这些存储单元自动被释放。栈内存分配运算内置于处理器的指令集中,效率很高,但是分配的内存容量有限。 (3)从堆上分配,亦称动态内存分配。程序...原创 2010-07-24 10:02:14 · 184 阅读 · 0 评论 -
C程序设计语言(第二版) 4-7 编写一个函数ungets(),将整个字符串压回到输入中...
#include<stdio.h>#include<string.h> #define BUFSIZE 100 char buf[BUFSIZE]; int bufp = 0; int getch(void); void unGetch(int);/* Getch: get a ( possibly pushed back) ...原创 2010-07-20 18:38:16 · 502 阅读 · 0 评论 -
C程序设计语言(第二版) 4-6
#include<stdlib.h> #include<stdio.h> #include<ctype.h> #include<math.h> #include<string.h> #define MAXOP 100 #define TRUE 1 #define FALSE 0 #define ...原创 2010-07-20 18:15:13 · 209 阅读 · 0 评论 -
C程序设计语言(第二版) 2-7 ☆
2-7 编写一个函数invert(x,p,n),该函数返回对x执行下列操作后的结果值,将x中从第p位开始的n个(二进制)位求反(即,1变成0,0变成1),x的其余各位保持不变。 #include<stdio.h>unsigned invert(unsigned x,int p,int n);int main(){ unsigned x=5; int ...原创 2010-07-15 10:55:28 · 229 阅读 · 1 评论 -
C程序设计语言(第二版) 2-6
2-6 编写一个函数setbits(x,p,n,y),该函数返回对x执行下列操作后的结果值:将x中从第p位开始的n个(二进制)位设置为y中最右边n位的值,x的其余各位保持不变 #include<stdio.h>int setbits(int x,int p,int n,int y);int main(){ int x=10; int y=7; int y...原创 2010-07-14 21:55:23 · 308 阅读 · 1 评论 -
C程序设计语言(第二版) 2-5
2-5 编写函数any(s1,s2),将字符串s2中的任意字符在字符串s2中第一次出现的位置作为结果返回,如果s1中有不包含在s2中的字符,返回-1#include<stdio.h>#define max 100void rlString(char* s1,char* s2);int main(){ char left[100]="shizhendema"...原创 2010-07-14 19:59:34 · 172 阅读 · 0 评论 -
C程序设计语言(第二版)2-4
2-4编写函数squeeze(s1,s2),将字符串s1中与字符串s2中字符匹配的字符都删掉 #include<stdio.h>#define max 100void squeeze(char* leftStrig,char* rightString);int main(){ char left[100]="shizhendema"; char ri...原创 2010-07-14 19:55:52 · 153 阅读 · 0 评论 -
C程序设计语言(第二版) 2-3
2-3 编写函数htoi(s),把由十六进制数字组成的字符串(包含可选的前缀0x或0X)转换为与之等价的整型值。字符串中允许包含的数字包括:0~9,a~f,A~Z #include <stdio.h>#include <math.h>#define MAXLENGTH 10int getline(char s[], int max);int ...原创 2010-07-14 11:45:09 · 176 阅读 · 0 评论 -
很乱很混乱
#include<stdio.h>int main(){ int num=23451; int array[10]; for(int i=0;i<10;i++) array[i]=-1; int each; int j=0; int prev; int store=0; each=num/10; store=num-each...原创 2010-07-13 21:57:20 · 115 阅读 · 0 评论 -
C语言小程序 将一串数字转换为相应的数值
/*atoi(char s[])函数将一串数字转换为相应的整型数*/#include <stdio.h> int atoi(char s[]){ int i,n; n=0; for(i=0;s[i]>='0'&&s[i]<='9';++i) { n=10*n + (s[i]-'0'); } return n;...原创 2010-07-13 21:18:26 · 414 阅读 · 0 评论 -
转义字符
所有的ASCII码都可以用“\”加数字(一般是8进制数字)来表示。而C中定义了一些字母前加"\"来表示常见的那些不能显示的ASCII字符,如\0,\t,\n等,就称为转义字符,因为后面的字符,都不是它本来的ASCII字符意思了。 转义字符 意义 ASCII码值(十进制) \a 响铃(BEL) 007 \b 退格(BS) 008 \f ...原创 2010-07-13 16:01:25 · 112 阅读 · 0 评论 -
C程序设计语言(第二版) 2-1
2-1 编写一个程序以确定分别由signed和unsigned限定的char、short、int以及long 类型变量的取值范围。 采用打印标准头文件中的相应值:#include <stdio.h>#include <limits.h>int main(){ printf("Size of char %d\n",CHAR_BIT); prin...原创 2010-07-13 10:58:58 · 209 阅读 · 0 评论 -
C语言小程序 从键盘输入一个字符串
int getline(char s[], int lim) { int c, i; for(i = 0; i < lim - 1 && (c = getchar()) != EOF && c != '\n'; ++i) { s[i] = c; } if(c == '\n') { s[i++] = c;...原创 2010-07-12 21:22:23 · 1021 阅读 · 0 评论 -
C程序设计语言(第二版) 1-19
1-19 编写函数reserse(s),将字符串s中的顺序颠倒过来。使用该函数编写一个程序,每次颠倒一个输入行中的字符顺序。 #include<stdio.h>#define LENGTH 5void getline(char s[]) { int c, i; for(i = 0; ((c = getchar()) != EOF &a...原创 2010-07-12 21:02:37 · 142 阅读 · 0 评论