|
|
|
[讨论]DEV C++中初学者编程 共同学习者顶起来
有较长一段时间了,这儿显得冷清清的, 偶尔过来看一看。 回过头来, 看看自己走过的路, 虽然都是一些再基础得不过的东东, 但还是逐渐感觉,脚下的路一步步都在变得更加的坚实。 现在随意用一些高级语言,编写一点实用的东西, 动起手来,感觉轻松许多, 再也没有过去那种陷入死胡同出不来的困惑了。。 希望明天的路更好 |
|
[讨论]DEV C++中初学者编程 共同学习者顶起来
例题中,请注意加红的语句。通常情况下,if应与下两句交换一下位置,为什么现在这样也是正常运行??? #include "stdio.h" int main() { int * p1, * p2, * p ,a,b; printf("please enter two integer numbers:"); scanf("%d,%d",&a,&b); if(a<b) p1=&a; p2=&b; {p=p1;p1=p2;p2=p;} printf("a=%d,b=%d\n",a,b); printf("max=%d,mix=%d\n",*p1,*p2); fflush(stdin); getchar(); } |
|
[讨论]DEV C++中初学者编程 共同学习者顶起来
//写一个在主函数输入十六进制数,调用子函数转换成十进制数后,返回主函数输出。 #include "stdio.h" #include "math.h" main() { int six_ten(int x); int x; int s=0; printf("请输入一个十六进制数:\n"); scanf("%x",&x); s=six_ten(x); printf("十六进制数:%x转化为十进制数为:%d",x,s); fflush(stdin); getchar(); } //十六进制化十进制子函数 int six_ten(int x) { int s=0,i; /* s为权位法求和的总和 */ int n[9]={}; /* 从元素n[0]向n[9]存放想当于十进制的个位 十位 百位... */ for(i=0;i<9;i++) /* 分别半从键盘输入的十六进制各位数值放入数组元素 */ { n[i]=int(x/pow(16,double(i)))%16; /* 计算式,注意函数的数据类型 */ printf("%d\n",n[i]); /* 用于检测各位数值是否正确 */ } for(i=0;i<9;i++) /* 权位法求和 */ s=s+n[i]*pow(16,double(i)); return(s); } |
|
[讨论]DEV C++中初学者编程 共同学习者顶起来
#include "stdio.h" int i=1; int main() { void hanoi(int n,char one,char two, char three); int m; printf("input the number of diskes:"); scanf("%d",&m); printf("The step to move %d diskes:\n",m); hanoi(m,'A','B','C'); fflush(stdin); getchar(); } void hanoi(int n,char one,char two,char three) { void move(char x,char y); if(n==1) move(one,three); else { hanoi(n-1,one,three,two); move(one,three); hanoi(n-1,two,one,three); } } void move(char x,char y) { printf("%d %c-->%c\n",i,x,y);i++; } |
|
[讨论]DEV C++中初学者编程 共同学习者顶起来
//P168—4 有一个已排好序的数组,要求输入一个数后,按照原来的规律将它插入数组中云。 //算法: //1、准备数据排列好后依次放入数组A[]中。 //2、输入需要插入的数到变量M中 //3、判断原有数的排序方式:从小到大还是从大到小 //4、将M与数组A[]中的元素依次比较,并记录M的前一数与后一数 //5、依序插入M到A[]中,后面的元素向后移一位。 //6、输出新的排列结果。 # include "stdio.h" main() { int A[10]={1,2,3,5,7,9},M; int i,j,k; scanf("%d",&M); if(A[0]>A[1]) k=1; else k=0; printf("%d",k); if(k==0) { for(i=0;i<10;i++) {if(M<A[i]) j=i,i=10;} } else { for(i=0;i<10;i++) {if(M>A[i]) j=i,i=10;} } printf("\n%d",j); for(i=9;i>j;i--) { A[i]=A[i-1]; } A[j]=M; for(i=0;i<10;i++) printf("\n%d",A[i]); fflush(stdin); getchar(); } |
|
[讨论]DEV C++中初学者编程 共同学习者顶起来
//题目:1、从键盘输入一个英文单词字符串到字符数组,判断这个单词有几个字符; // 2、修改前题为从键盘输入一句英文日常用语,判断这句话有几个单词; // 3、修改前一题,增加字字符串的输出,要求用2种以上的方法输出; # include "stdio.h" # include "string.h" main() { char string[81]={}; /* 字符型数组*/ int i=0,word=0,num=0; char c; printf("请输入一个英文单词\n"); gets(string); for(i=0;(c=string[i])!='\0';i++) { if(c==' ') word=0; else if(word==0) { word=1;num++; } } printf("there are %d words in this line.\n",num); |
|
[讨论]DEV C++中初学者编程 共同学习者顶起来
//题目:将一个二维数组元素行列互换: // 1 4 1 2 3 // a=[ 2 5 ] b= [ ] // 3 6 4 5 6 // 准备数组和变量 // 显示数组a的元素阵列 // 按列读取数组a 放入b数组行 // 显示数组b的元素阵列 #include "stdio.h" main() { //准备 int a[3][2]={1,4,2,5,3,6}; int b[2][3]={}; int i,j,m,n; int c[6]; //显示a数组阵列 for(i=0;i<3;i++) { for(j=0;j<2;j++) printf("%5d",a[i][j]); printf("\n"); } //按列读取数组a到数组c m=0; for(j=0;j<2;j++) { for(i=0;i<3;i++) { c[m]=a[i][j];m++; } } //显示数组c是否合要求 for(m=0;m<6;m++)printf("%d",c[m]); printf("\n"); //确定数组b坐标分别存入对应数组c的数据 i=0; for(m=0;m<2;m++) for(n=0;n<3;n++) {b[m][n]=c[i];i++;} //显示b数组结果 for(m=0;m<2;m++) { for(n=0;n<3;n++) printf("%5d",b[m][n]); printf("\n"); } //屏幕暂停 getchar(); } |
|
[讨论]DEV C++中初学者编程 共同学习者顶起来
//题目:从键盘输入一组自然数,将它们从小到大排序后,在屏幕上输出 //流程:准备数据及数组, // 随意输入自然数到数组元素,检查输入结果, // 将保存在数组元素中的数排序后再保存到数组中, // 输出每个数组元素。 # include "stdio.h" main() { int a[20]; /* 字符型数组*/ int i,m,z; for(i=0;i<5;i++) { scanf("%d",&a[i]); } //比较排序 for(m=5;m>1;m--) /*去尾*/ { for(i=0;i<m;i++) { if(a[i]>a[i+1]) {z=a[i];a[i]=a[i+1];a[i+1]=z;} /*交换*/ else {} /*不变*/ } } //输出排序结果 for(i=0;i<5;i++) { printf("%d",a[i]); } fflush(stdin); //清除输入设备缓存数据 getchar(); } |
|
[讨论]DEV C++中初学者编程 共同学习者顶起来
//题目:从键盘输入一个小写英文单词,将每个字母换成大写字母后输出 //流程:准备数据及数组, // 输入小写英文字母到数组元素,检查输入结果, // 将保存在数组元素中的小写字母变成大写字母, // 输出每个数组元素。 # include "stdio.h" main() { char a[20]; /* 字符型数组*/ int i; for(i=0;i<20;i++) { scanf("%c",&a[i]); if(a[i]==10) i=20; } for(i=0;i<20;i++) { if(a[i]==10) i=20; a[i]=a[i]-32; } for(i=0;i<20;i++) { printf("%c",a[i]); if(a[i]==10) i=20; } fflush(stdin); //清除输入设备缓存数据 getchar(); } |
|
[讨论]DEV C++中初学者编程 共同学习者顶起来
//题目:从键盘输入一个英文单词,判断这个这个单词中是否有字母a, //若有输出这个单词,否则输出:请换一个。 /* 在用c语言编写输入语句的时候常用到scanf函数,初学者在刚用scanf函数输入时, 经常会遇到各种各样的输入错误,最重要的是一定要记住scanf函数的输入格式,scanf 函数里包含了哪些东西,输入的时候就必须有哪些东西,比如:scanf("%c%c%c"),那 么输入的时候就必须连续输入3个字符,中间不能有任何空格逗号或者回车符等分开, 而如果是scanf("%c,%c,%c"),那么输入3个字符的时候中间就必须用一个逗号分隔。 scanf函数最常用的两种输入方式%d整数输入方式,以及%c字符输入方式。 用%d方式输入整数时,输入一串数字然后按回车符,输入就完成了。而用%c方式输入 字符时,就稍微复杂一点。一般来说用%c输入字符时,当输完以后,都习惯按一下回车, 这时回车符就成了一个问题。 【例1】: // scanf("%c",&a); /*此时输入一个字符,并按下回车,那么变量a将会正确接收输入的字符*/ // scanf("%c",&b); /*但是变量b将会接收到回车符,其值为10,或者说ASCII码为10*/ # include "stdio.h" main() { char a[20]; /* 字符型数组*/ int i,j,k=0; rem: for(i=0;i<20;i++) { scanf("%c",&a[i]); if(a[i]==10) i=20; } for(j=0;j<20;j++) { if(a[j]=='a') k=1; } if(k==1) for(i=0;i<20;i++) { printf("%c",a[i]); if(a[i]==10) i=20; } else {printf("请重输一个 \n"); goto rem;} fflush(stdin); //清除输入设备缓存数据 getchar(); } |
|
[讨论]DEV C++中初学者编程 共同学习者顶起来
#include "stdio.h" #include "math.h" main() { int i,j=0,k,sum=0; int n=4; for(k=1;k<=n;k++) { j=0; for(i=0;i<k;i++) { j=j+2*float(pow(10.0,float(i))); //此行改为: j=j+pow(10,i);就会让结果成为110,而正确为111. } printf("%d\n",j); sum=sum+int(j); } printf("%d",sum); getchar(); } |
|
[讨论]DEV C++中初学者编程 共同学习者顶起来
//pow(x,y) 所使用的数据类型为float 或 double 所以下面程序中不进行数据类型转换就会出现0数据溢出错误。 #include "stdio.h" #include "math.h" main() { int i,j=0,k; for(i=0;i<3;i++) { j=j+ int(1*float(pow(10,i))); //此行改为: j=j+pow(10,i);就会让结果成为110,而正确为111. } printf("%d",j); getchar(); } |
|
[讨论]DEV C++中初学者编程 共同学习者顶起来
小YY同学,你的程序中使用了scanf 由于它会从键盘读入数据,但没有清其缓冲区, 所以getchar 直接就从缓冲区获取了数据,导致不能停住屏幕。 250楼使用的方法对你有用 加了: fflush(stdin); //清空输入设备缓存 getchar(); 加上这两句就一切OK |
|
[讨论]DEV C++中初学者编程 共同学习者顶起来
if else 后面很多同学喜欢加个分号, 请注意不要画蛇添足了。 |
|
[讨论]DEV C++中初学者编程 共同学习者顶起来
实例 C语言Printf格式 使用printf打印时发现,如果数据类型和打印使用的类型不一致,结果就是混乱的。 这是因为printf本身并不进行数据的类型转换,他只是把输入按照给定的数据格式输出,如果二者不匹配,那么由于不同数据类型的二进制存储方式不一样,读出的数据就是错误的。 所以,为求稳妥,在使用printf,fprintf,scanf,fscanf在类型不一致时还是手动转化吧! printf("%f\n",1.3); printf("%d %f\n",1.3,1.3); 打印结果: 1.300000 -858993459 -92559653408872784000000000000000000000000000000000000000000000.000000 前一行打印正确,但是后一行1.3与%d不匹配打印错误,不仅如此还影响到后一个也打印错误。 读入fscanf 用%f 读入赋给float型,要读入赋给double型,用%lf C语言Printf格式 C中格式字符串的一般形式为: %[标志][输出最小宽度][.精度][长度]类型, 其中方括号[]中的项为可选项。各项的意义介绍如下: 1.类型类型字符用以表示输出数据的类型,其格式符和意义下表所示: 表示输出类型的格式字符 格式字符意义 a 浮点数、十六进制数字和p-计数法(C99) A 浮点数、十六进制数字和p-计数法(C99) c 输出单个字符 d 以十进制形式输出带符号整数(正数不输出符号) e 以指数形式输出单、双精度实数 E 以指数形式输出单、双精度实数 f 以小数形式输出单、双精度实数 g 以%f%e中较短的输出宽度输出单、双精度实数,%e格式在指数小于-4或者大 于等于精度时使用 G 以%f%e中较短的输出宽度输出单、双精度实数,%e格式在指数小于-4或者大于等于精度时使用 i 有符号十进制整数(与%d相同) o 以八进制形式输出无符号整数(不输出前缀O) p 指针 s 输出字符串 x 以十六进制形式输出无符号整数(不输出前缀OX) X 以十六进制形式输出无符号整数(不输出前缀OX) u 以十进制形式输出无符号整数 /* 测试常见的输出类型 */ #include "stdio.h" #include "conio.h" main() { printf("The program test print style!\n"); /* 以十进制形式输出带符号整数(正数不输出符号) */ printf("%d\n" , 223); printf("%d\n" , -232); printf("\n"); /* 以八进制形式输出无符号整数(不输出前缀O) */ printf("%o\n" , 223); printf("%o\n" , -232); printf("\n"); /* 以十六进制形式输出无符号整数(不输出前缀OX) */ printf("%x\n" , 223); printf("%x\n" , -232); printf("\n"); /* 以十进制形式输出无符号整数 */ printf("%u\n" , 223); printf("%u\n" , -232); printf("\n"); /* 以小数形式输出单、双精度实数 */ printf("%f\n" , 223.11); printf("%f\n" , 232.11111111); printf("%f\n" , -223.11); printf("%f\n" , -232.11111111); printf("\n"); /* 以指数形式输出单、双精度实数 */ printf("%e\n" , 223.11); printf("%e\n" , 232.11111111); printf("%e\n" , -223.11); printf("%e\n" , -232.11111111); printf("\n"); /* 以%f%e中较短的输出宽度输出单、双精度实数 */ printf("%g\n" , 223.11); printf("%g\n" , 232.111111111111); printf("%g\n" , -223.11); printf("%g\n" , -232.111111111111); printf("\n"); /* 输出单个字符 */ printf("%c\n" , 'a'); printf("%c\n" , 97); printf("\n"); /* 输出单个字符 */ printf("%s\n" , "this is a test!"); printf("%s\n" , "2342o34uo23u"); printf("\n"); getch(); } 2.标志 标志字符为-、+、#、空格和0五种,其意义下表所示: 标志格式字符 标 志 意 义 - 结果左对齐,右边填空格 + 输出符号(正号或负号) 空格 输出值为正时冠以空格,为负时冠以负号 # 对c,s,d,u类无影响;对o类,在输出时加前缀0;对x类, 在输出时加前缀0x或者0X;对g,G 类防止尾随0被删除; 对于所有的浮点形式,#保证了即使不跟任何数字,也打印一个小数点字符 0 对于所有的数字格式,用前导0填充字段宽度,若出现-标志或者指定了精度(对于整数),忽略 3.输出最小宽度 用十进制整数来表示输出的最少位数。若实际位数多于定义的宽度,则按实际位数输出,若实际位数少于定义的宽度则补以空格或0。 /* 测试标志字符为-、+、#、空格四种 */ #include "stdio.h" #include "conio.h" main() { /* 以十进制形式输出带符号整数(正数不输出符号) */ printf("*%-10d*\n", 223); printf("*%+10d*\n" , -232); printf("*%2d*\n" , 223); printf("*%#d*\n" , -232); printf("\n"); getch(); /* 以八进制形式输出无符号整数(不输出前缀O) */ printf("*%-10o*\n" , 223); printf("*%+10o*\n" , -232); printf("*%o*\n" , 223); printf("*%#o*\n" , -232); printf("\n"); getch(); /* 以十六进制形式输出无符号整数(不输出前缀OX) */ printf("$%-10x$\n" , 223); printf("$%010x$\n" , -232); printf("$% x$\n" , 223); printf("$%#x$\n" , -232); printf("\n"); /* 以十进制形式输出无符号整数 */ printf("%-10u\n" , 223); printf("%+10u\n" , -232); printf("% u\n" , 223); printf("%#u\n" , -232); printf("\n"); getch(); /* 以小数形式输出单、双精度实数 */ printf("%-10f\n" , 223.11); printf("%+10f\n" , 232.11111111); printf("% f\n" , -223.11); printf("%#f\n" , -232.11111111); printf("\n"); getch(); /* 以指数形式输出单、双精度实数 */ printf("%-10e\n" , 223.11); printf("%+10e\n" , 232.11111111); printf("% e\n" , -223.11); printf("%#e\n" , -232.11111111); printf("\n"); getch(); /* 以%f%e中较短的输出宽度输出单、双精度实数 */ printf("%-10g\n" , 223.11); printf("%+10g\n" , 232.111111111111); printf("% g\n" , -223.11); printf("%#g\n" , -232.111111111111); printf("\n"); getch(); /* 输出单个字符 */ printf("%-10c\n" , 'a'); printf("%+10c\n" , 97); printf("% c\n" , 'a'); printf("%#c\n" , 97); printf("\n"); getch(); /* 输出单个字符 */ printf("%-20s\n" , "this is a test!"); printf("%+20s\n" , "2342o34uo23u"); printf("% 20s\n" , "this is a test!");/* 不足补空格 */ printf("%#s\n" , "2342o34uo23u"); printf("\n"); getch(); } 4.精度 精度格式符以“.”开头,后跟十进制整数。本项的意义是:如果输出数字,则表示小数的位数;如果输出的是字符,则表示输出字符的个数;若实际位数大于所定义的精度数,则截去超过的部分。 /* 测试精度 */ #include "stdio.h" #include "conio.h" main() { printf("%.3d\n" , 5555); getch(); printf("%.3f\n" , 0.88888); getch(); printf("%.3f\n" , 0.9999); getch(); printf("%.4s\n" , "this is a test!"); getch(); } 5.长度 长度格式符为h,l两种,h表示按短整型量输出,l表示按长整型量输出。 h和整数转换说明符一起使用,表示一个short int 或者unsigned short int类型的数值 ,示例: %hu,%hx,%6.4hd hh和整数转换说明符一起使用,表示一个short int 或者unsigned short类型的数值 ,示例: %hhu,%hhx,%6.4hhd j和整数转换说明符一起使用,表示一个intmax_t或者uintmax_t类型的数值 ,示例: %jd,%8jx l和整数转换说明符一起使用,表示一个long int 或者unsigned long int类型的数值 ,示例: %ld,%8lu ll和整数转换说明符一起使用,表示一个long int 或者unsigned long int类型的数值 (C99),示例: %lld,%8llu L和浮点转换说明符一起使用,表示一个long double的值,示例:%Lf,%10.4Le t和整数转换说明符一起使用,表示一个ptrdiff_t值(两个指针之间的差相对应的类型)(C99),示例: %td,%12ti z和整数转换说明符一起使用,表示一个size_t值(sizeof返回的类型)(C99),示例:%zd,%12zx main(){ int a=15; float b=138.3576278; double c=35648256.3645687; char d='p'; printf("a=%d,%5d,%o,%x\n",a,a,a,a); printf("b=%f,%lf,%5.4lf,%e\n",b,b,b,b); printf("c=%lf,%f,%8.4lf\n",c,c,c); printf("d=%c,%8c\n",d,d); } a<--15 b<--138.3576278 c<--35648256.3645687 d<--'p' main() { int a=29; float b=1243.2341; double c=24212345.24232; char d='h'; printf("a=%d,%5d,%o,%x\n",a,a,a,a); /* 其中“%f”和“%lf ”格式的输出相同,说明“l”符对“f”类型无影响 * “%5.4lf”指定输出宽度为5,精度为4,由于实际长度超过5故应该按实际位数输出,小数位数超过4位 * 部分被截去 */ printf("b=%f,%lf,%5.4lf,%e\n",b,b,b,b); /* 输出双精度实数,“%8.4lf ”由于指定精度为4位故截去了超过4位的部分 */ printf("c=%lf,%f,%8.4lf\n",c,c,c); /* 输出字符量d,其中“%8c ”指定输出宽度为8故在输出字符p之前补加7个空格 */ printf("d=%c,%8c\n",d,d); getch(); } 使用printf函数时还要注意一个问题,那就是输出表列中的求值顺序。不同的编译系统不一定相同,可以从左到右,也可从右到左。Turbo C是按从右到左进行的 main(){ int i=8; printf("%d\n%d\n%d\n%d\n%d\n",++i,--i,i--,i++,-i--); } 6.特殊用法 对于m.n的格式还可以用如下方法表示(例) char ch[20]; printf("%*.*s\n",m,n,ch); 前边的*定义的是总的宽度,后边的定义的是输出的个数。分别对应外面的参数m和n 。我想这种方法的好处是可以在语句之外对参数m和n赋值,从而控制输出格式。 今天(06.6.9)又看到一种输出格式 %n 可以将所输出字符串的长度值赋绐一个变量, 见下例: int slen; printf("hello world%n", &slen); 执行后变量被赋值为11。 又查了一下, 看到一篇文章(查看)说这种格式输出已经确认为一个安全隐患,并且已禁用。再搜搜果然这种用法都被用来搞什么溢出、漏洞之类的,随便找了一个:格式化字符串攻击笔记 特别注意下%*.*s这种用法 |
|
[讨论]DEV C++中初学者编程 共同学习者顶起来
//红版谭C第3章习题3。1 //求利率 # include "stdio.h" # include "math.h" main() { float p,n,r; r=0.09; n=10; printf("%f\n",r); r=1+r; printf("%f\n",r); p=pow(r,n); printf("The p=%f ",p); fflush(stdin); //清除输入设备缓存数据 getchar(); } |
|
[讨论]DEV C++中初学者编程 共同学习者顶起来
//红版谭C第3章例3。3 /* 给定一个大写字母,要求用小写字母输出。 */ # include "stdio.h" main() { char a1,b1; // a1 表示输入的大写字母,a2表示输出的小写字母 printf("Please input the a1 : "); scanf("%c",&a1); b1=a1+32; /* 公式计算b1的值 */ printf("The a1=%c the b1=%c",a1,b1); //输出 fflush(stdin); //清除输入设备缓存数据 getchar(); } # include "stdio.h" main() { char a1; a1='A'; dd: printf("%c ",a1); //输出 a1++; if(a1>90) goto gg; goto dd; gg: fflush(stdin); //清除输入设备缓存数据 getchar(); } |
|
[讨论]DEV C++中初学者编程 共同学习者顶起来
//红版谭C第3章例3。1 /* 算法: 输入f的值 c=5/9(f-32) 输出C的值 */ # include "stdio.h" main() { float c,f; // c 表示摄氏度 f 表示华氏度 printf("Please input the f : "); scanf("%f",&f); c=(5/9)*(f-32); /* 公式计算C的值 */ printf("The f=%f the C=%f",f,c); fflush(stdin); //清除输入设备缓存数据 getchar(); } 楼上程序中计算式改成如上,运行结果就不对了,谁知道原因?????? |
|
[讨论]DEV C++中初学者编程 共同学习者顶起来
又开始新的一学年了 又带零基础学员了 今天做谭红版顺序结构题3-1 //红版谭C第3章例3。1 /* 算法: 输入f的值 c=5/9(f-32) 输出C的值 */ # include "stdio.h" main() { float c,f; // c 表示摄氏度 f 表示华氏度 printf("Please input the f : "); scanf("%f",&f); c=(f-32)*5/9; /* 公式计算C的值 */ printf("The f=%f the C=%f",f,c); fflush(stdin); //清除输入设备缓存数据 getchar(); } |
操作理由
RANk
{{ user_info.golds == '' ? 0 : user_info.golds }}
雪币
{{ experience }}
课程经验
{{ score }}
学习收益
{{study_duration_fmt}}
学习时长
基本信息
荣誉称号:
{{ honorary_title }}
能力排名:
No.{{ rank_num }}
等 级:
LV{{ rank_lv-100 }}
活跃值:
在线值:
浏览人数:{{ visits }}
最近活跃:{{ last_active_time }}
注册时间:{{ user_info.create_date_jsonfmt }}
勋章
兑换勋章
证书
证书查询 >
能力值