鱼C论坛

 找回密码
 立即注册
查看: 4868|回复: 57

[技术交流] 小白学习C语言日记

[复制链接]
发表于 2019-4-30 23:05:18 | 显示全部楼层 |阅读模式

马上注册,结交更多好友,享用更多功能^_^

您需要 登录 才可以下载或查看,没有账号?立即注册

x
本帖最后由 bin554385863 于 2019-5-5 16:23 编辑

2019.4.30 23:03
1-1
  1. include <stdio.h>
  2. //include是文件包含命令。作用是把系统目录下的stdio.h包含到本程序中。stdio.h是系统提供的头文件或者称为首部文件。

  3. int main()
  4. //main()是一个主函数名,表示这是一个主函数。一个可执行的C语言程序只允许有一个main函数
  5. //main()函数中的内容必放在一对{}花括号中
  6. {
  7.     printf("Hello World\n");

  8.     return 0;
  9. }
复制代码


1-2
  1. #include<stdio.h>
  2. #include<math.h>
  3. main()//计算角度的余弦值
  4. {
  5.     double x,s; //声明两个双精度实数x和s

  6.     printf("please input value of x:\n");
  7.     //\n表示换行
  8.     scanf("%lf", &x);
  9.     //%lf是格式字符对应双精度浮点型double,%f对应单精度浮点型float。
  10.     //float类型可以使用%lf格式,但不会有任何好处。
  11.     //double类型如果使用了%f格式可能会导致输出错误。
  12.     //float类型使用double类型不仅会导致输入错误,还可能引起程序崩溃。所以在输入输出时,一定要区分好double和float,而使用对应的格式符号。
  13.     s=cos(x); //math中的余弦函数
  14.     printf("cos(%lf)=%lf\n",x,s);

  15.    
  16. }
复制代码


1-3-0

  1. /*计算两个数的和*/
  2. #include<stdio.h>
  3. int add(int x, int y);//函数原型声明
  4. main()
  5. {
  6.     int a, b, c;
  7.     scanf("%d %d", &a , &b);
  8.     printf("please input value of a and b:\n");//%d实证性对应的格式字符作用等同1-2的%lf
  9.     c=add(a , b);//add()函数的使用
  10.     printf("max=%d\n",c);

  11. }
  12. int add(int x, int y)//add()函数的定义
  13. {
  14. return(x+y);
  15. }
  16. //把函数声明和函数的定义放到一起也是可以的如1-3-1所示
复制代码


1-3-1
  1. /*计算两个数的和*/
  2. #include<stdio.h>
  3. int add(int x, int y)////函数原型声明+函数定义,注意后面没有分号
  4. {
  5. return(x+y);
  6. };
  7. main()
  8. {
  9.     int a, b, c;
  10.     scanf("%d %d", &a , &b);
  11.     printf("please input value of a and b:\n");//%d是整形对应的格式字符作用等同1-2的%lf
  12.     c=add(a , b);//add()函数的使用
  13.     printf("max=%d\n",c);

  14. }
复制代码
想知道小甲鱼最近在做啥?请访问 -> ilovefishc.com
回复

使用道具 举报

 楼主| 发表于 2019-5-1 16:37:59 | 显示全部楼层
本帖最后由 bin554385863 于 2019-5-5 16:26 编辑

2019.05.01 17:39
2-1

  1. /*验证数据类型的字节长度*/
  2. #include<stdio.h>
  3. void main()
  4. {
  5.     char a1;
  6.     short b1;
  7.     int c1;
  8.     long d1;
  9.     float e1;
  10.     double f1;
  11.     printf("size of (char)=%d\n", sizeof(a1));
  12.     printf("size of (short)=%d\n", sizeof(b1));
  13.     printf("size of (int)=%d\n", sizeof(c1));
  14.     printf("size of (long)=%d\n", sizeof(d1));
  15.     printf("size of (float)=%d\n", sizeof(e1));
  16.     printf("size of (double)=%d\n", sizeof(f1));//sizeof()实际上是获取了数据在内存中所占用的存储空间,以字节为单位来计数。
  17.     //程序中整形类型的字节长度即与机器有关,也与编译器有关。
  18. }
复制代码


运行结果
Windows PowerShell
版权所有 (C) Microsoft Corporation。保留所有权利。
PS E:\Administrator\Documents\Visual Studio 2019> & 'c:\Users\Administrator\.vscode\extensions\ms-vscode.cpptools-0.22.1\debugAdapters\bin\WindowsDebugLauncher.exe' '--stdin=Microsoft-MIEngine-In-24houlba.0b4' '--stdout=Microsoft-MIEngine-Out-i121ef3k.sch' '--stderr=Microsoft-MIEngine-Error-crlh1xux.uns' '--pid=Microsoft-MIEngine-Pid-1n5zzs54.b04' '--dbgExe=E:\MingGW\bin\gdb.exe' '--interpreter=mi'
size of (char)=1
size of (short)=2
size of (int)=4
size of (long)=4
size of (float)=4
size of (double)=8
PS E:\Administrator\Documents\Visual Studio 2019>
***************************************************************************************************************************************************************************************************

2-2
C语言中区分字母大小写
标识符不能和C语言关键字,C语言库函数还有用户自定义的函数同名

变量名是变量值在内存空间中的地址
指针存储的是一个内存空间的地址
变量名就像门牌号,内存空间就像一栋楼房,指针就像你家的详细地址

  1. /*变量的取值范围测试*/
  2. #include<stdio.h>
  3. void main()
  4. {
  5.     int a=32766, b=-32766;
  6.     unsigned m=6553;
  7.     float t=3.4e+37;
  8.     printf("a=%d\n",a);
  9.     printf("m=%u\n",m);//[color=Magenta]u%对应的是无符号实数
  10.     printf("t=%e\n",t);//[color=Magenta]%e对应的是指数型实数

  11.     a=a+4;
  12.     b=b+6;
  13.     m=m*10+10;
  14.     t=t*10;
  15.     printf("after change:\n");
  16.     printf("a+4=%d\n",a);
  17.     printf("b-6=%d\n,",b);
  18.     printf("m*10+10=%d\n,",m);
  19.     printf("t*10=%d\n,",t);
  20. }
复制代码

=======================================================================================================

运行结果
a=32766
m=6553
t=3.400000e+037
after change:
a+4=32770
b-6=-32760
,m*10+10=65540
,t*10=-1073741824
,
PS E:\Administrator\Documents\Visual Studio 2019>
**********************************************************************************************************

2-3
/*整形常量的不同表示方法*/
  1. #include<stdio.h>
  2. void main()
  3. {
  4.     printf("十六进制0x80的十进制值为:%d\n",0x80);
  5.     printf("八进制的0200的十进制值为:%d\n",0200);
  6.     printf("十进制的128的十进制值为:%d\n",128);
  7.     printf("十进制128的十六进制值为:%x\n",128);//[color=Magenta]%x表示以十六进制输出
  8.     printf("十进制128的八进制值为:%o\n",128);//[color=Magenta]%o表示已八进制输出
  9. }
复制代码

想知道小甲鱼最近在做啥?请访问 -> ilovefishc.com
回复 支持 反对

使用道具 举报

 楼主| 发表于 2019-5-1 17:40:58 | 显示全部楼层
本帖最后由 bin554385863 于 2019-5-6 01:44 编辑

运行结果:
鍗佸叚杩涘埗0x80鐨勫崄杩涘埗鍊间负:128
鍏繘鍒剁殑0200鐨勫崄杩涘埗鍊间负:128
鍗佽繘鍒剁殑128鐨勫崄杩涘埗鍊间负:128
MMP中文输出有问题,还有一横被论坛吞了
************************************************************************************************************************************

2019年5月2日15:08:00

2-4-0

  1. /*字符常量与其顺序值得关系*/
  2. #include<stdio.h>
  3. void main()
  4. {
  5.      printf("%d---->%c\n",'A','A');
  6.      printf("%d---->%c\n",'A'+5,'A'+5);
  7.      printf("%d---->%c\n",'A'+32,'A'+32);
  8.      printf("%d----.%c\n",'A'+70,'A'+70);//字符'A'+70的值溢出
  9.    
  10. }
复制代码

=================================================================================================
结果
PS E:\Administrator\Documents\Visual Studio 2019> & 'c:\Users\Administrator\.vscode\extensions\ms-vscode.cpptools-0.22.1\debugAdapters\bin\WindowsDebugLauncher.exe' '--stdin=Microsoft-MIEngine-In-qjeqdptp.ypo' '--stdout=Microsoft-MIEngine-Out-5n5todzq.4dk' '--stderr=Microsoft-MIEngine-Error-vkib3jwx.vgn' '--pid=Microsoft-MIEngine-Pid-dyxrul3w.w13' '--dbgExe=E:\MingGW\bin\gdb.exe' '--interpreter=mi'
65---->A
70---->F
97---->a
135----.?数值超出char的取值范围导致不可预知的结果
PS E:\Administrator\Documents\Visual Studio 2019>
**********************************************************************************************************************************************************
2-4-1

  1. /*字符与字符串常量的区别*/
  2. #include<stdio.h>
  3. void main()
  4. {char a='a';
  5.     printf("size of 'a'=%d\n",sizeof(a));//由运行结果可以看出字符变量char在内存中占用一个字节
  6.     printf("size of "a"=%d\n",sizeof("a"));//字符串常量"a"占用两个字节,之所以这样是因为系统会自动在字符串常量后面加上一个串结束标志\0,所以程序中n个字符的字符串常量在内存中占用n+1个字节存储空间
  7. }
复制代码

===========================================================================================================================
结果
PS E:\Administrator\Documents\Visual Studio 2019> & 'c:\Users\Administrator\.vscode\extensions\ms-vscode.cpptools-0.22.1\debugAdapters\bin\WindowsDebugLauncher.exe' '--stdin=Microsoft-MIEngine-In-zr1fmpkr.xsg' '--stdout=Microsoft-MIEngine-Out-htfocegt.mxq' '--stderr=Microsoft-MIEngine-Error-4tifxhy4.zyz' '--pid=Microsoft-MIEngine-Pid-cwcmaprx.lzr' '--dbgExe=E:\MingGW\bin\gdb.exe' '--interpreter=mi'
size of 'a'=1
size of "a"=2
PS E:\Administrator\Documents\Visual Studio 2019>
*******************************************************************************************************************************

2-5-0

  1. #include<stdio.h>
  2. void main()//了解转义字符的作用
  3. {
  4.     printf("\a"//发出铃声
  5.     "this is a test:\n"
  6.     "Ready::"
  7.     "\bBackspace\n"//向左退一格
  8.     "\tHorizontal tab\n"//向右进到下一个制表点
  9.     "\\\n"
  10.     "\?\n"
  11.     "\'\n"
  12.     ""\n"
  13.     "\101\n"
  14.     "\x41\n");
  15.     //输出\ ? ' " A A "
  16. }
复制代码

==========================================================================================================================
结果
PS E:\Administrator\Documents\Visual Studio 2019> & 'c:\Users\Administrator\.vscode\extensions\ms-vscode.cpptools-0.22.1\debugAdapters\bin\WindowsDebugLauncher.exe' '--stdin=Microsoft-MIEngine-In-zddcj2mq.yc1' '--stdout=Microsoft-MIEngine-Out-nnimcc15.0g2' '--stderr=Microsoft-MIEngine-Error-n21mwavm.i2k' '--pid=Microsoft-MIEngine-Pid-fqrclk0e.vgj' '--dbgExe=E:\MingGW\bin\gdb.exe' '--interpreter=mi'
this is a test:
Ready:Backspace
        Horizontal tab
\
?
'
"
A
A
PS E:\Administrator\Documents\Visual Studio 2019>
**************************************************************************************************************************************************************

2-6-0

  1. /*符号常量的使用*/
  2. #include<stdio.h>
  3. #define WHO "i am a student"//#号别忘了
  4. #define HOW "that is fine"
  5. #define PI 3.141592653

  6. void main()
  7. {
  8.     printf("%s\n",WHO);
  9.     printf("%s\n",HOW);
  10.     printf("%f\n",PI);
  11. }
复制代码

=======================================================================================================
结果
PS E:\Administrator\Documents\Visual Studio 2019> & 'c:\Users\Administrator\.vscode\extensions\ms-vscode.cpptools-0.22.1\debugAdapters\bin\WindowsDebugLauncher.exe' '--stdin=Microsoft-MIEngine-In-geyi5024.ohp' '--stdout=Microsoft-MIEngine-Out-q0hkse2h.3te' '--stderr=Microsoft-MIEngine-Error-m0axfn42.gh4' '--pid=Microsoft-MIEngine-Pid-sa2whvmm.e13' '--dbgExe=E:\MingGW\bin\gdb.exe' '--interpreter=mi'
i am a student
that is fine
3.141593
PS E:\Administrator\Documents\Visual Studio 2019>
**********************************************************************************************************************************************************

2-7-0

/*了解算术运算符组成的表达式*/
  1. #include<stdio.h>
  2. void main()
  3. {
  4.     int a,b,c,d1,d2,d3,d4;
  5.     double x,y,z1,z2,z3;
  6.     a=8;
  7.     b=3;
  8.     c=10;
  9.     d1=a+b*c/a+b%c*a;
  10.     d2=a+(b*c)-(b/a)+(b%c*a);
  11.     printf("d1=%d,d2=%d\n",d1,d2);
  12.     d3=a/b;
  13.     d4=c%b;
  14.     printf("8/3=%d, 10/3=%d\n",d3,d4);
  15.     x=3.2;
  16.     y=2.4;
  17.     z1=x+y/x-y;
  18.     z2=x+(y/x)-y;
  19.     printf("z1=%f, z2=%f\n",z1,z2);
  20.     z3=y/b;
  21.     printf("2.4/3=%f\n",z3);

  22. }
复制代码

===========================================================================================================
结果
                                               > & 'c:\Users\Administrator\.vscode\extensions\ms-vscode.cpptools-0.22.1\debugAdapters\bin\WindowsDebugLauncher.exe' '--stdin=Microsoft-MIEngine-In-hxkcipfm.ocx' '--stdout=Microsoft-MIEngine-Out-zgwgudkb.5ol' '--stderr=Microsoft-MIEngine-Error-erpzitpv.hsy' '--pid=Microsoft-MIEngine-Pid-frmujo4n.rdl' '--dbgExe=E:\MingGW\bin\gdb.exe' '--interpreter=mi'
d1=35,d2=62
8/3=2, 10/3=1
z1=1.550000, z2=1.550000
2.4/3=0.800000
PS E:\Administrator\Documents\Visual Studio 2019>
**************************************************************************************************************************

2019年5月2日22:11:20

2-8-0
  1. /*了解前置++/--和后置++/--的作用*/
  2. #include<stdio.h>
  3. void main()
  4. {
  5.     int a,b;
  6.     double x,y,x1,y1,x2,y2,z;
  7.     a=16;
  8.     x=12.6;
  9.     x1=++a;
  10.     y1=++x;
  11.     printf("1---->x1=%d, y1=%2.2lf\n",a,x);
  12.     x2=a++;
  13.     y2=x++;
  14.     printf("2---->x2=%d, y2=%2.2lf\n",a,x);
  15.     b=a++;
  16.     y=x++;   
  17.     printf("3---->b=%d, y=%2.2lf\n4---->a=%d, x=%2.2lf\n",b,y,a,x);

  18. }
复制代码

==============================================================================================================================
结果
                                               > & 'c:\Users\Administrator\.vscode\extensions\ms-vscode.cpptools-0.22.1\debugAdapters\bin\WindowsDebugLauncher.exe' '--stdin=Microsoft-MIEngine-In-2naobsea.l1o' '--stdout=Microsoft-MIEngine-Out-u5jtre31.mue' '--stderr=Microsoft-MIEngine-Error-zjskygnu.3gf' '--pid=Microsoft-MIEngine-Pid-r1g15eob.t1u' '--dbgExe=E:\MingGW\bin\gdb.exe' '--interpreter=mi'
1---->x1=17, y1=13.60  说明++a是先自增后赋值
2---->x2=18, y2=14.60
3---->b=18, y=14.60  说明a++是先赋值在自增(我也不知道咋看出来的
4---->a=19, x=15.60
PS E:\Administrator\Documents\Visual Studio 2019>
想知道小甲鱼最近在做啥?请访问 -> ilovefishc.com
回复 支持 反对

使用道具 举报

 楼主| 发表于 2019-5-3 14:31:42 | 显示全部楼层
本帖最后由 bin554385863 于 2019-5-5 16:39 编辑

2019年5月3日14:31:42

2-15-0

  1. /*三目运算符和逻辑运算符的使用。或‖--->一真全真;且&&(书里翻译叫做与,我更喜欢叫它且)一假全假。*/
  2. #include<stdio.h>
  3. #include<stdlib.h>
  4. void main()
  5. {
  6.     int a;
  7.     int b,c;
  8.     int d,e,f;
  9.     scanf("%d\n%d",&a,&b);
  10.     c=(a>=b)?a:b;
  11.     d=a&&!((a-b)>0);  
  12.     f=!(a-b);
  13.     printf("max of %d and %d is:%d\t\n",a,b,c);
  14.     printf("%d&&(!(%d-%d)>0)=%d\t\n",a,a,b,d);
  15.     printf("!(%d-%d)=%d\t\n",a,b,f);

  16. }
复制代码

==========================================================================================================
结果
                                               > & 'c:\Users\Administrator\.vscode\extensions\ms-vscode.cpptools-0.22.1\debugAdapters\bin\WindowsDebugLauncher.exe' '--stdin=Microsoft-MIEngine-In-puz5tgoa.cvq' '--stdout=Microsoft-MIEngine-Out-eqz01tog.ewz' '--stderr=Microsoft-MIEngine-Error-xvrwkfc2.dx3' '--pid=Microsoft-MIEngine-Pid-skaxczb2.gd0' '--dbgExe=E:\MingGW\bin\gdb.exe' '--interpreter=mi'
456
654
max of 456 and 654 is:654
1&&(!(456-654)>0)=0
!(456-654)=0
PS E:\Administrator\Documents\Visual Studio 2019>
************************************************************************************************************************

2-20-0

  1. /*了解数据输出的常用格式*/
  2. #include<stdio.h>
  3. void main()
  4. {
  5.     int a=1234, b=23456;
  6.     long c=345678;
  7.     unsigned int d=4567890;
  8.     char e='A';
  9.     double f=314.15926535898;

  10.     printf("\t1---->Output data in default format:\n\t");//按默认格式输出数据
  11.     printf("a=%d,  b=%d,  c=%ld,  d=%u\n\t",a,b,c,d);
  12.     printf("e=%c,  f=%f,  f=%e\n\t------------------------------------\n\t",e,f,f);

  13.     printf("2---->Output data by specified width:\n\t");//按指定宽度输出字符
  14.     printf("e=%3c,  e=%03c\n\t---------------------------------------\n\t",e,e);

  15.     printf("3---->Output integers by specified width\n\t");//按指定宽度输出整形数
  16.     printf("a=%3d,  b=%4d,  c=%4ld,  d=%5u\n\t",a,b,c,d);
  17.     printf("a=%7d,  b=%7d,  c=%8ld,  d=%9u\n\t",a,b,c,d);
  18.     printf("a=%07d,  b=%07d,  c=%08ld,  d=%09u\n\t--------------------------------------------\n\t",a,b,c,d);

  19.     printf("4---->Output floating-point type number by specified width");//按指定宽度输出浮点型数
  20.     printf("f=%8.2f,  f=%12.2e\n\t",f,f);
  21.     printf("f=%4.2f,  f=%4.2e\n\t",f,f);

  22. }
复制代码

============================================================================================================
结果
PS E:\Administrator\Documents\Visual Studio 2019> & 'c:\Users\Administrator\.vscode\extensions\ms-vscode.cpptools-0.22.1\debugAdapters\bin\WindowsDebugLauncher.exe' '--stdin=Microsoft-MIEngine-In-hyupllrw.4vk' '--stdout=Microsoft-MIEngine-Out-ntqnlv1a.vzv' '--stderr=Microsoft-MIEngine-Error-kkco1fkj.2j5' '--pid=Microsoft-MIEngine-Pid-dj4r2bkj.oht' '--dbgExe=E:\MingGW\bin\gdb.exe' '--interpreter=mi'
        1---->Output data in default format:
        a=1234,  b=23456,  c=345678,  d=4567890
        e=A,  f=314.159265,  f=3.141593e+002
        ------------------------------------
        2---->Output data by specified width:
        e=  A,  e=00A
        ---------------------------------------
        3---->Output integers by specified width
        a=1234,  b=23456,  c=345678,  d=4567890
        a=   1234,  b=  23456,  c=  345678,  d=  4567890
        a=0001234,  b=0023456,  c=00345678,  d=004567890
        --------------------------------------------
        4---->Output floating-point type number by specified widthf=  314.16,  f=   3.14e+002
        f=314.16,  f=3.14e+002

PS E:\Administrator\Documents\Visual Studio 2019>
看得我头大,用了十几分钟来记住熟悉
********************************************************************************************************************************************************

2-25-0

  1. //字符输入输出函数的用法
  2. #include<stdio.h>
  3. #include<conio.h>
  4. void main()
  5. {
  6.     char ch1,ch2;
  7.     ch1=getch();//读取键盘输入的字符存在缓冲区且不在屏幕显示
  8.     ch2=getchar();//读取键盘输入的字符存在缓冲区且显示在屏幕上
  9.     putch(ch1);//从缓冲区读取数据并显示在屏幕上
  10.     printf(\n);
  11.     putchar(ch2);//将ch2的值输出在屏幕上
  12.     printf("\nch1=%c\nch2=%c\n",ch1,ch2);
  13. }
复制代码

========================================================================================================================
                                               > & 'c:\Users\Administrator\.vscode\extensions\ms-vscode.cpptools-0.22.1\debugAdapters\bin\WindowsDebugLauncher.exe' '--stdin=Microsoft-MIEngine-In-jigkrari.f3t' '--stdout=Microsoft-MIEngine-Out-eaubwoxf.aau' '--stderr=Microsoft-MIEngine-Error-0bks4lkt.4mk' '--pid=Microsoft-MIEngine-Pid-jtcwfukk.wnr' '--dbgExe=E:\MingGW\bin\gdb.exe' '--interpreter=mi'
2//输入的ch1没在屏幕显示
1
2
ch1=1//显示了ch1
ch2=2
PS E:\Administrator\Documents\Visual Studio 2019>
*********************************************************************************************************************

2-26-0
栗子1

计算两个复数的差
  1. #include<stdio.h>
  2. void main()
  3. {
  4.     float a,b,x,y;
  5.     printf("Please input the a and b of first complex:\n");
  6.     scanf("%f\n%f",&a,&b);
  7.     printf("The first complex is:(%5.2f+%5.2fi)",a,b);
  8.     printf("Please input the x and y of second complex:\n");
  9.     scanf("%f\n%f",&x,&y);
  10.     printf("The second complex is:(%5.2f+%5.2fi)",x,y);
  11.     printf("Diffrence is :\n(%6.2f+%6.2fi)\n",a-x,b-y);

  12. }
复制代码

==========================================================================================================
PS E:\Administrator\Documents\Visual Studio 2019> & 'c:\Users\Administrator\.vscode\extensions\ms-vscode.cpptools-0.22.1\debugAdapters\bin\WindowsDebugLauncher.exe' '--stdin=Microsoft-MIEngine-In-dkkpixvl.wsb' '--stdout=Microsoft-MIEngine-Out-03ci2cnr.3kx' '--stderr=Microsoft-MIEngine-Error-zs01jor4.ckh' '--pid=Microsoft-MIEngine-Pid-yrtigsbj.53j' '--dbgExe=E:\MingGW\bin\gdb.exe' '--interpreter=mi'
Please input the a and b of first complex:
4
5
The first complex is:( 4.00+ 5.00i)Please input the x and y of second complex:
3
1
The second complex is:( 3.00+ 1.00i)Diffrence is :
(  1.00+  4.00i)
PS E:\Administrator\Documents\Visual Studio 2019>
*************************************************************************************************
栗子2

  1. //求三个数的平均值
  2. #include<stdio.h>
  3. void main()
  4. {
  5.     float a,b,c,average;
  6.     printf("please input three numbers:\n");
  7.     scanf("%f%f%f",&a,&b,&c);
  8.     average=(a+b+c)/3;
  9.     printf("The average value is:%7.2f\n",average);

  10. }
复制代码

=======================================================================================================
PS E:\Administrator\Documents\Visual Studio 2019> & 'c:\Users\Administrator\.vscode\extensions\ms-vscode.cpptools-0.22.1\debugAdapters\bin\WindowsDebugLauncher.exe' '--stdin=Microsoft-MIEngine-In-ybbmgz0j.t5b' '--stdout=Microsoft-MIEngine-Out-ecxglkya.055' '--stderr=Microsoft-MIEngine-Error-io1qbrlc.o3o' '--pid=Microsoft-MIEngine-Pid-yrpxo33v.zso' '--dbgExe=E:\MingGW\bin\gdb.exe' '--interpreter=mi'
please input three numbers:
9.321
2.258
5555
The average value is:1855.53
PS E:\Administrator\Documents\Visual Studio 2019>
*********************************************************************************************************************

习题
1,输入两个数求它们的和差商积余。

  1. #include<stdio.h>
  2. void main()
  3. {
  4.     int x,y;
  5.     printf("please input two numbers\n");
  6.     scanf("%d%d",&x,&y);//MMP总是忘记地址操作符
  7.     printf("%d-%d=%d\n",x,y,x-y);
  8.     printf("%d+%d=%d\n",x,y,x+y);
  9.     printf("%d/%d=%d---%d\n",x,y,x/y,x%y);
  10.     printf("%d*%d=%d",x,y,x*y);
  11. }
复制代码

============================================================================================================
                                               > & 'c:\Users\Administrator\.vscode\extensions\ms-vscode.cpptools-0.22.1\debugAdapters\bin\WindowsDebugLauncher.exe' '--stdin=Microsoft-MIEngine-In-gd50f43m.uel' '--stdout=Microsoft-MIEngine-Out-qzpfba0d.tby' '--stderr=Microsoft-MIEngine-Error-2gbu255m.oom' '--pid=Microsoft-MIEngine-Pid-g4a4d13k.4jf' '--dbgExe=E:\MingGW\bin\gdb.exe' '--interpreter=mi'
please input two numbers
98
25
98-25=73
98+25=123
98/25=3---23
98*25=2450
PS E:\Administrator\Documents\Visual Studio 2019>
*********************************************************************************************************

练习

输入三角形的三条边求它的面积
  1. //输入三条三角形边长,计算它的面积

  2. #include<stdio.h>
  3. #include<math.h>
  4. void main()
  5. {
  6.     float a,b,c,x,y,Area;
  7.     scanf("%f%f%f",&a,&b,&c);
  8.     if (a<=0||b<=0||c<=0)
  9.     {
  10.         printf("你家的三角形边长是负数的吗?");
  11.     }
  12.     else if ((a+b)>c&&(a+c)>b&&(b+c)>a)
  13.     {
  14.         x=(a*a+b*b-c*c)/(2*a*b);
  15.         y=sqrt(1-x*x);
  16.         Area=(a*b*y)/2;
  17.         printf("The area of a triangle is:%.3f",Area);
  18.     }
  19.         else
  20.         {
  21.             printf("你确定这能画出三角形?");
  22.         }
  23.       
  24. }
复制代码

想知道小甲鱼最近在做啥?请访问 -> ilovefishc.com
回复 支持 反对

使用道具 举报

 楼主| 发表于 2019-5-4 14:21:25 | 显示全部楼层
本帖最后由 bin554385863 于 2019-5-5 16:20 编辑

2019年5月4日13:14:07

小结:
        一个C语言程序中只能存在一个main()函数,语句结束后要用分号;结束,一对花括号括起来的的代码表示一个语句块,且所有符号都必须是英文的。
       
        %(m)d        将数据以宽度为m的整形输出,宽度不足空格补齐。

        %0md        将数据以宽度为m的整形输出,宽度不足以0补齐。

        %m.nf        将数据以宽度为m,保留n位小数的单浮点数输出,宽度包含小数点。

        %m.nlf        将数据以宽度为m,保留n位小数的双浮点数输出,宽度包含小数点。

        %o                将数据以八进制输出。
       
        %x                将数据以十六进制输出。

        %u                将数据以无符号整型输出

        %e        %E        科学计数法输出数据.
       
        %g        %G        使用%f和%e表示中的总的位数表示最短的来表示浮点数 G 同g格式,但表示为指数.

        %c                 输出单个字符.

        %s                 输出字符串.

**************对于数字都可以以%[宽度包括小数点] . [有效数字位数][格式字符]的形式输出,宽度不够默认以空格补充,宽度前面加0后表示以0补充.
  1. #include<stdio.h>
  2. void main()
  3. {
  4.     float a = 3.141592653;
  5.     printf("%f\n%0.2f\n%09.4f\n%5.4f\n",a,a,a,a);
  6. }
复制代码


                                               > & 'c:\Users\Administrator\.vscode\extensions\ms-vscode.cpptools-0.22.1\debugAdapters\bin\WindowsDebugLauncher.exe' '--stdin=Microsoft-MIEngine-In-nfekeleo.d1r' '--stdout=Microsoft-MIEngine-Out-24n1j5g0.eew' '--stderr=Microsoft-MIEngine-Error-3qomv2yt.loh' '--pid=Microsoft-MIEngine-Pid-vdlejhm5.hvj' '--dbgExe=E:\MingGW\bin\gdb.exe' '--interpreter=mi'
3.141593        默认float输出.
3.14                保留两位有效小数输出.
0003.1416        宽度为9,保留4为有效数字输出,数字不够以0补齐.
3.1416        当有效数字加上小数点>=指定宽度时,默认宽度输出.

PS E:\Administrator\Documents\Visual Studio 2019>

  1. #include<stdio.h>
  2. void main()
  3. {
  4.     int a = 965775;
  5.     printf("%o\n%x\n%X\n",a,a,a,a);
  6. }
复制代码


                                               > & 'c:\Users\Administrator\.vscode\extensions\ms-vscode.cpptools-0.22.1\debugAdapters\bin\WindowsDebugLauncher.exe' '--stdin=Microsoft-MIEngine-In-3hdd3pmt.dsh' '--stdout=Microsoft-MIEngine-Out-4urtulyi.hnw' '--stderr=Microsoft-MIEngine-Error-fztkeeck.c2s' '--pid=Microsoft-MIEngine-Pid-aiyjlau0.xiy' '--dbgExe=E:\MingGW\bin\gdb.exe' '--interpreter=mi'
3536217        %o八进制输出
ebc8f     %x小写字母十六进制输出
EBC8F   %X大写字母十六进制输出

PS E:\Administrator\Documents\Visual Studio 2019>
***************************************************************************************************************************************************************************************************************************
        scanf("%格式字符 %格式字符 ..... %格式字符" ,  &变量 ,& 变量 , ......& 变量),变量的形式必须与格式字符一一对应,多个变量用逗号隔开且地址操作符&不能少)
scan()双引号内最后不要加入普通字符,否则输入的时候也应该输入相应的字符


%*[格式字符]                表示跳过读取的数据
想知道小甲鱼最近在做啥?请访问 -> ilovefishc.com
回复 支持 反对

使用道具 举报

 楼主| 发表于 2019-5-4 15:29:27 | 显示全部楼层
本帖最后由 bin554385863 于 2019-5-5 16:47 编辑

2019年5月4日15:25:10

第三章

3.1.0
        输入三个数并求取其中最小值
.先输入三个数分别赋值给变量a,b,c
.比较a和b的值把较小的那个数赋值给变量min中
.比较min与c的值,把较小的值赋值给min
.输出min的值

  1. /*输入三个数并找出其中最小的*/
  2. #include<stdio.h>
  3. void main()
  4. {
  5.     int a , b , c , min , max ;
  6.     scanf("%d%d%d",&a,&b,&c);
  7.     if ((a == b)||(b == c)||(a == c))
  8.     {
  9.         printf("Dot input same number");
  10.     }
  11.     else if ((b < a)&&(b < c))
  12.     {
  13.         min = b ;
  14.         printf("min = %d",min);
  15.     }
  16.     else if ((a < b)&&(a < c))
  17.     {
  18.         min = a ;
  19.         printf("min = %d",min);
  20.     }
  21.     else if ((c<a)&&(c < b))
  22.     {
  23.         min = c ;
  24.         printf("min = %d",min);
  25.     }
  26. }
复制代码
想知道小甲鱼最近在做啥?请访问 -> ilovefishc.com
回复 支持 反对

使用道具 举报

 楼主| 发表于 2019-5-4 19:00:28 | 显示全部楼层
本帖最后由 bin554385863 于 2019-5-5 16:44 编辑

2019年5月4日19:00:51

3.7.0

  1. /*输入百分制成绩,按五分制输出*/
  2. #include<stdio.h>
  3. void main()
  4. {
  5.     int s ;
  6.     scanf("%d",&s);

  7.     if ((s < 0) || (s > 100))
  8.     {
  9.         printf("The full score is 100! Please enter valid results!");
  10.     }
  11.     else if (s>=90)
  12.         {
  13.             printf("%c",'A');
  14.         }
  15.         else if ((s < 90) && (s >= 80))
  16.             {
  17.                 printf("%c",'B');
  18.             }
  19.             else if ((s < 80) && (s >= 70))
  20.                 {
  21.                     printf("%c",'C');
  22.                 }
  23.                 else if ((s < 70) && (s >= 60))
  24.                     {
  25.                         printf("%c",'D');
  26.                     }
  27.                     else if (s <= 60)
  28.                         {
  29.                             printf("%c",'E');
  30.                         }
  31. }
复制代码

====================================================================================
PS E:\Administrator\Documents\Visual Studio 2019> & 'c:\Users\Administrator\.vscode\extensions\ms-vscode.cpptools-0.22.1\debugAdapters\bin\WindowsDebugLauncher.exe' '--stdin=Microsoft-MIEngine-In-a44xkxdy.tzr' '--stdout=Microsoft-MIEngine-Out-d3m2atdv.rhy' '--stderr=Microsoft-MIEngine-Error-t0h0fmkf.kky' '--pid=Microsoft-MIEngine-Pid-dihnxps2.po1' '--dbgExe=E:\MingGW\bin\gdb.exe' '--interpreter=mi'
200
The full score is 100! Please enter valid results!
PS E:\Administrator\Documents\Visual Studio 2019>
****************************************************************************************************************

3.8.0

判断一个数是不是素数
  1. /*从键盘输入一个整数,判断他是不是素数*/
  2. #include<stdio.h>
  3. #include<math.h>
  4. void main()
  5. {
  6.     int num , a ;
  7.     scanf("%d",&num) ;
  8.     for(int i = 2; i < num; i++)
  9.     {
  10.         if (num%i==0)
  11.         {
  12.             a++;//当num不是素数时,a的值增加
  13.         }
  14.     }   
  15.         if (a == 0)
  16.         {
  17.             printf("%d is a prime number",num) ;//a=0表示num就是素数
  18.         }
  19.         else
  20.         {
  21.             printf("%d is not a prime number",num) ;
  22.         }
  23.    
  24. }
复制代码

----------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------
PS E:\Administrator\Documents\Visual Studio 2019> & 'c:\Users\Administrator\.vscode\extensions\ms-vscode.cpptools-0.22.1\debugAdapters\bin\WindowsDebugLauncher.exe' '--stdin=Microsoft-MIEngine-In-fjcbh5pn.h5j' '--stdout=Microsoft-MIEngine-Out-awserv5o.a0k' '--stderr=Microsoft-MIEngine-Error-bxssw0il.rc1' '--pid=Microsoft-MIEngine-Pid-gaejplxu.xyo' '--dbgExe=E:\MingGW\bin\gdb.exe' '--interpreter=mi'
9999
9999 is not a prime number
PS E:\Administrator\Documents\Visual Studio 2019>
*******************************************************************************************************************************************************************

  1. /*输入一个年份,判断其是不是闰年*/
  2. #include<stdio.h>
  3. void main()
  4. {
  5.     int year ;
  6.     scanf("%d",&year) ;
  7.     if (year <= 0)
  8.     {
  9.         printf("Please enter the correct year ");
  10.     }
  11.     else if ((year%100==0) || ((year%4==0) && (year%100!=0)))
  12.     {
  13.         printf("%d is leap year",year);
  14.     }
  15.         else
  16.         {
  17.             printf("%d is  not leap year",year);
  18.         }
  19. }
复制代码

--------------------------------------------------------------------------------------------------------------------------------------------------------
PS E:\Administrator\Documents\Visual Studio 2019> & 'c:\Users\Administrator\.vscode\extensions\ms-vscode.cpptools-0.22.1\debugAdapters\bin\WindowsDebugLauncher.exe' '--stdin=Microsoft-MIEngine-In-l2qx0ncs.nbc' '--stdout=Microsoft-MIEngine-Out-iwa2jejm.4vi' '--stderr=Microsoft-MIEngine-Error-0drctcss.cgj' '--pid=Microsoft-MIEngine-Pid-hztxowha.ah1' '--dbgExe=E:\MingGW\bin\gdb.exe' '--interpreter=mi'
1579
1579 is  not leap year
PS E:\Administrator\Documents\Visual Studio 2019>

**********************************************************************************************************************
输入四个整型数字并对其排序

  1. /*输入a,b,c,d四个数并对其排序,最笨的办法*/
  2. #include<stdio.h>
  3. int bigger(int x, int y)
  4. {
  5.     return((x>=y)?x:y);
  6. }

  7. int smaller(int x, int y)
  8. {
  9.     return((x<y)?x:y);
  10. }

  11. void main()
  12. {
  13.     int a, b, c, d, x, y, z, w ;
  14.     int x1, y1, z1, w1, x2, y2, z2, w2 ;

  15.     scanf("%d%d%d%d",&a,&b,&c,&d);
  16.     x = bigger(a,b);
  17.     y = smaller(a,b);
  18.     z = bigger(c,d);
  19.     w = smaller(c,d);

  20.     x1 = bigger(a,c);
  21.     y1 = smaller(a,c);
  22.     z1 = bigger(b,d);
  23.     w1 = smaller(b,d);

  24.     x2 = bigger(a,d);
  25.     y2 = smaller(a,d);
  26.     z2 = bigger(b,c);
  27.     w2 = smaller(b,c);

  28.     if (y>=z)
  29.     {
  30.         printf("%d>=%d>=%d>=%d",x,y,z,w);
  31.     }
  32.     else if (w>=x)
  33.     {
  34.         printf("%d>=%d>=%d>=%d",z,w,x,y);
  35.     }
  36.     else if (y1 >= z1)
  37.     {
  38.         printf("%d>=%d>=%d>=%d",x1,y1,z1,w1);
  39.     }
  40.     else if (w1 >= x1)
  41.     {
  42.         printf("%d>=%d>=%d>=%d",z1,w1,x1,y1);
  43.     }
  44.     else if (y2 >= z2)
  45.     {
  46.          printf("%d>=%d>=%d>=%d",x2,y2,z2,w2);
  47.     }
  48.     else if (w2 >= x2)
  49.     {
  50.          printf("%d>=%d>=%d>=%d",z2,w2,x2,y2);
  51.     }
  52.    
  53.    
  54. }
复制代码

========================================================================
PS E:\Administrator\Documents\Visual Studio 2019> & 'c:\Users\Administrator\.vscode\extensions\ms-vscode.cpptools-0.22.1\debugAdapters\bin\WindowsDebugLauncher.exe' '--stdin=Microsoft-MIEngine-In-s4e5wi21.t32' '--stdout=Microsoft-MIEngine-Out-svb34scz.a3l' '--stderr=Microsoft-MIEngine-Error-b4xmqb4b.xdq' '--pid=Microsoft-MIEngine-Pid-4ugzz45q.bro' '--dbgExe=E:\MingGW\bin\gdb.exe' '--interpreter=mi'
2322
8885
475
1
8885>=2322>=475>=1
PS E:\Administrator\Documents\Visual Studio 2019>
=============================================================================
  1. /*四数排序简洁版*/
  2. #include<stdio.h>
  3. void main()
  4. {
  5.     int a, b, c, d, e ;//e是数据交换的中间数
  6.     scanf("%d%d%d%d",&a,&b,&c,&d);
  7.     if (a<b)
  8.     {
  9.         e=a;//数字交换从e开始
  10.         a=b;
  11.         b=e;//也必须以e结尾,否则会出现错误的结果
  12.     }
  13.     if (a<c)
  14.     {
  15.         e=a;
  16.         a=c;
  17.         c=e;
  18.     }
  19.     if (a<d)
  20.     {
  21.         e=a;
  22.         a=d;
  23.         d=e;
  24.     }
  25.     if (b<c)
  26.     {
  27.         e=b;
  28.         b=c;
  29.         c=e;
  30.     }
  31.     if (b<d)
  32.     {
  33.         e=b;
  34.         b=d;
  35.         d=e;
  36.     }
  37.     if (c<d)
  38.     {
  39.         e=c;
  40.         c=d;
  41.         d=e;
  42.     }
  43.     printf("大小顺序是:%d %d %d %d",a,b,c,d);
  44. }
复制代码


想知道小甲鱼最近在做啥?请访问 -> ilovefishc.com
回复 支持 反对

使用道具 举报

 楼主| 发表于 2019-5-5 17:09:05 | 显示全部楼层
本帖最后由 bin554385863 于 2019-5-6 01:36 编辑

2019年5月5日17:09:20

第四章

4.1
  1. /*从小到大保存a和b的值*/
  2. #include<stdio.h>
  3. void main()
  4. {
  5.     float a, b, t ;
  6.     printf("请输入a和b的值\n");
  7.     scanf("%f%f",&a,&b);
  8.     if (a>b)
  9.     {
  10.         t = a;
  11.         a = b;
  12.         b = t;
  13.         
  14.     }
  15.     printf("a = %f\nb = %f",a,b);
  16. }
复制代码

=====================================================================================
6
3
a = 3.000000
b = 6.000000
PS E:\Administrator\Documents\Visual Studio 2019> & 'c:\Users\Administrator\.vscode\extensions\ms-vscode.cpptools-0.22.1\debugAdapters\bin\WindowsDebugLauncher.exe' '--stdin=Microsoft-MIEngine-In-erwpygem.lnf' '--stdout=Microsoft-MIEngine-Out-stdgthml.gxj' '--stderr=Microsoft-MIEngine-Error-gdfjweyc.vde' '--pid=Microsoft-MIEngine-Pid-ce0yywk5.lpx' '--dbgExe=E:\MingGW\bin\gdb.exe' '--interpreter=mi'
请输入a和b的值
3.2
3.2
a = 3.200000
b = 3.200000
PS E:\Administrator\Documents\Visual Studio 2019>
***********************************************************************************************************************************************

4.2

  1. /*程序中对浮点型数字使用了if(a>b)的形式,实际上关系运算符并不适合浮点数,当两个个浮点数相差不大时,有可能出现与程序运行结果实际情况不符合.*/
  2. #include<stdio.h>
  3. void main()
  4. {
  5.     double a = 0.2 ;
  6.     double b = a+0.1;
  7.     b = b-0.1;
  8.     printf("a = %f b = %f\n",a,b);
  9.     if (a==b)
  10.     {
  11.         printf("a==b---正确\n");
  12.     }
  13.     else if (a > b)
  14.     {
  15.         printf("a > b---错误\n");
  16.     }
  17.     else
  18.     {
  19.         printf("a < b---错误\n");
  20.     }
  21.    
  22.    
  23.    
  24. }
复制代码

===================================================================================
PS E:\Administrator\Documents\Visual Studio 2019> & 'c:\Users\Administrator\.vscode\extensions\ms-vscode.cpptools-0.22.1\debugAdapters\bin\WindowsDebugLauncher.exe' '--stdin=Microsoft-MIEngine-In-iyaijbkp.r2l' '--stdout=Microsoft-MIEngine-Out-mgsjfznt.cnt' '--stderr=Microsoft-MIEngine-Error-rf1oqwnv.ikt' '--pid=Microsoft-MIEngine-Pid-rslv125r.ezn' '--dbgExe=E:\MingGW\bin\gdb.exe' '--interpreter=mi'
a = 0.200000 b = 0.200000
a < b---错误
PS E:\Administrator\Documents\Visual Studio 2019>

显然运行结果和实际情况并不符合
如何比较浮点数大小,请参考书籍资料
******************************************************************************************************************************

4.3

  1. /*输入一个整数,求它的绝对值*/
  2. #include<stdio.h>
  3. void main()
  4. {
  5.     int a ;
  6.     scanf("%d",&a);
  7.     if (a < 0)
  8.    
  9.         a = -a;
  10.    
  11.     printf("|a| = %d",a);
  12. }
复制代码

==================================================================
PS E:\Administrator\Documents\Visual Studio 2019> & 'c:\Users\Administrator\.vscode\extensions\ms-vscode.cpptools-0.22.1\debugAdapters\bin\WindowsDebugLauncher.exe' '--stdin=Microsoft-MIEngine-In-udygli0l.wna' '--stdout=Microsoft-MIEngine-Out-5wh5ela2.zpq' '--stderr=Microsoft-MIEngine-Error-v342qse5.m04' '--pid=Microsoft-MIEngine-Pid-gff1zrx1.20w' '--dbgExe=E:\MingGW\bin\gdb.exe' '--interpreter=mi'
-90
|a| = 90
PS E:\Administrator\Documents\Visual Studio 2019>

虽然当if后面只有一个语句是 可以不加花括号,但是我并不建议这样做.
************************************************************************************************************

4.3
猜数字游戏

  1. /*通过rand()获得计算机的伪随机数字*/
  2. #include<stdio.h>
  3. #include<stdlib.h>
  4. void main()
  5. {
  6.     int num, rNum ;
  7.     scanf("%d",&num);
  8.     rNum = rand();
  9.     if (num == rNum)
  10.     {
  11.         printf("恭喜你猜对了\n答案是: %d",rNum);
  12.     }
  13.     else
  14.     {
  15.         printf("很抱歉你猜错了\n答案是: %d",rNum);
  16.     }
  17.    
  18. }
复制代码

=================================================================================================================
41
恭喜你猜对了
答案是: 41
PS E:\Administrator\Documents\Visual Studio 2019> & 'c:\Users\Administrator\.vscode\extensions\ms-vscode.cpptools-0.22.1\debugAdapters\bin\WindowsDebugLauncher.exe' '--stdin=Microsoft-MIEngine-In-orzyo0dn.4yd' '--stdout=Microsoft-MIEngine-Out-ftlujrvu.o1v' '--stderr=Microsoft-MIEngine-Error-jvzlqqzm.4kv' '--pid=Microsoft-MIEngine-Pid-0ipytmfc.vus' '--dbgExe=E:\MingGW\bin\gdb.exe' '--interpreter=mi'
41
恭喜你猜对了
答案是: 41
PS E:\Administrator\Documents\Visual Studio 2019> & 'c:\Users\Administrator\.vscode\extensions\ms-vscode.cpptools-0.22.1\debugAdapters\bin\WindowsDebugLauncher.exe' '--stdin=Microsoft-MIEngine-In-ysxv3bpy.t5d' '--stdout=Microsoft-MIEngine-Out-vaazjjp3.c3c' '--stderr=Microsoft-MIEngine-Error-kbafrlec.t3h' '--pid=Microsoft-MIEngine-Pid-lfdokeb3.gum' '--dbgExe=E:\MingGW\bin\gdb.exe' '--interpreter=mi'
41
恭喜你猜对了
答案是: 41
PS E:\Administrator\Documents\Visual Studio 2019>

每次产生的随机数都是一样的!!!!!

如要产生[m,n]范围内的随机数num,可用:int num=rand()%(n-m+1)+m
-------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------
  1. /*通过srang()和time()获得真正的随机数*/
  2. #include<stdio.h>
  3. #include<stdlib.h>
  4. void main()
  5. {
  6.     int num, rnum ;
  7.     scanf("%d",&num);
  8.     srand(time(NULL));
  9.     rnum = rand();
  10.     if (num == rnum)
  11.     {
  12.         printf("恭喜你猜对了");
  13.     }
  14.     else
  15.     {
  16.         printf("很遗憾,你猜错了\n答案是:%d",rnum);
  17.     }
  18.    
  19.    
  20. }
复制代码

=========================================================================================================
PS E:\Administrator\Documents\Visual Studio 2019> & 'c:\Users\Administrator\.vscode\extensions\ms-vscode.cpptools-0.22.1\debugAdapters\bin\WindowsDebugLauncher.exe' '--stdin=Microsoft-MIEngine-In-fct4xo0h.x3o' '--stdout=Microsoft-MIEngine-Out-4rer5hxn.zha' '--stderr=Microsoft-MIEngine-Error-pz0mdweb.1mr' '--pid=Microsoft-MIEngine-Pid-rr4u5exf.h1l' '--dbgExe=E:\MingGW\bin\gdb.exe' '--interpreter=mi'
8533
很遗憾,你猜错了
答案是:5632
PS E:\Administrator\Documents\Visual Studio 2019> & 'c:\Users\Administrator\.vscode\extensions\ms-vscode.cpptools-0.22.1\debugAdapters\bin\WindowsDebugLauncher.exe' '--stdin=Microsoft-MIEngine-In-ds35odrz.whi' '--stdout=Microsoft-MIEngine-Out-r3ryczek.31s' '--stderr=Microsoft-MIEngine-Error-ctiba13h.z5d' '--pid=Microsoft-MIEngine-Pid-n2kaazcl.5e5' '--dbgExe=E:\MingGW\bin\gdb.exe' '--interpreter=mi'
41
很遗憾,你猜错了
答案是:5864
PS E:\Administrator\Documents\Visual Studio 2019>
提到的函数:
rand()
srand()
time()

------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------
  1. /*猜数字游戏-----自定义的随机数范围*/
  2. #include<stdio.h>
  3. #include<stdlib.h>
  4. #include<math.h>
  5. void main()
  6. {
  7.     int num, rnum, a, b, x ;
  8.     printf("请输入随机数的上限和下限\n");
  9.     scanf("%d%d",&a,&b);
  10.     if ((abs(a - b) < 10) || (a <= 0) || (b <= 0))
  11.     {
  12.         printf("数值差距过小或不是正整数");
  13.     }  
  14.     else if (a < b)
  15.         {
  16.             x = b;
  17.             b = a;
  18.             a = x;
  19.             srand(time(NULL));
  20.             rnum = rand()%(a-b+1)+b;
  21.             printf("请输入猜测的数字");
  22.             scanf("%d",&num);
  23.             if (num == rnum)
  24.             {
  25.                 printf("恭喜你猜对了");
  26.             }
  27.             else
  28.             {
  29.                 printf("很遗憾,你猜错了\n答案是:%d",rnum);
  30.             }  
  31.         }
  32.    
  33. }
复制代码

===================================================================================================================================
-9
-100
数值差距过小或不是正整数
PS E:\Administrator\Documents\Visual Studio 2019> & 'c:\Users\Administrator\.vscode\extensions\ms-vscode.cpptools-0.22.1\debugAdapters\bin\WindowsDebugLauncher.exe' '--stdin=Microsoft-MIEngine-In-3gko5qg5.jid' '--stdout=Microsoft-MIEngine-Out-jxml1hmu.zum' '--stderr=Microsoft-MIEngine-Error-5p3zwwse.hgi' '--pid=Microsoft-MIEngine-Pid-uunh3t0s.ttm' '--dbgExe=E:\MingGW\bin\gdb.exe' '--interpreter=mi'
请输入随机数的上限和下限
10
-10
数值差距过小或不是正整数
PS E:\Administrator\Documents\Visual Studio 2019> & 'c:\Users\Administrator\.vscode\extensions\ms-vscode.cpptools-0.22.1\debugAdapters\bin\WindowsDebugLauncher.exe' '--stdin=Microsoft-MIEngine-In-bygb4oom.kff' '--stdout=Microsoft-MIEngine-Out-uw5huyeq.45g' '--stderr=Microsoft-MIEngine-Error-fjb4ufqt.jo2' '--pid=Microsoft-MIEngine-Pid-15gjbxqy.sni' '--dbgExe=E:\MingGW\bin\gdb.exe' '--interpreter=mi'
请输入随机数的上限和下限
20
30
请输入猜测的数字2
很遗憾,你猜错了
答案是:22
PS E:\Administrator\Documents\Visual Studio 2019>

abs(int):求整形数的绝对值
fabs(float):求浮点数的绝对值

********************************************************************************************************************
4.11

求一元二次方程ax^2+bx+c=0的根

  1. /*求一元二次方程的根*/
  2. #include<stdio.h>
  3. #include<math.h>
  4. void main()
  5. {
  6.     double a, b, c, x1, x2, derta, y, z ;
  7.     printf("请输入方程系数\n");
  8.     scanf("%lf%lf%lf",&a,&b,&c);
  9.     if ((a != 0) && (fabs(a) > (1e-6)))//避免实数转化成计算机浮点数时产生的误差
  10.     {
  11.         derta = b*b-4*a*c ;
  12.         if (derta >= 0)
  13.         {
  14.             x1 = (-b + sqrt(derta)) / (2 * a);
  15.             x2 = (-b - sqrt(derta)) / (2 * a);
  16.             printf("方程%0.2lfx^2+%0.2lfx+%0.2lf=0的根为\nx1 = %0.2lf\nx2 = %0.2lf\n", a, b, c, x1, x2);
  17.         }
  18.         else
  19.         {
  20.             y = -b / (2 * a);
  21.             z = (sqrt(-derta)) / (2 * a);
  22.             printf("判别式小于0,此方程根为两个共轭复数\n");
  23.             printf("x1 = %0.2lf + %0.2lfi\n", y, z);
  24.             printf("X2 = %0.2lf - %0.2lfi\n", y, z);
  25.         }
  26.     }
  27.     else
  28.     {
  29.         printf("首项不能为0");
  30.     }
  31. }
复制代码

===================================================================================================
想知道小甲鱼最近在做啥?请访问 -> ilovefishc.com
回复 支持 反对

使用道具 举报

 楼主| 发表于 2019-5-6 01:41:00 | 显示全部楼层
本帖最后由 bin554385863 于 2019-5-6 02:55 编辑


2019年5月6日01:38:08

习题

用switch对百分制成绩分级排序ABCDE

  1. /*用switch对百分成绩排序分级*/
  2. #include<stdio.h>
  3. void main()
  4. {
  5.     int num ;
  6.     printf("请输入你的成绩");
  7.     scanf("%d",&num);
  8.     if ((num >= 0)&&(num <= 100))
  9.     {
  10.         switch (num/10)
  11.         {
  12.             case 10 :
  13.             case 9 : printf("你的成绩是%c",'A');
  14.             break;
  15.             case 8 : printf("你的成绩是%c",'B');
  16.             break;
  17.             case 7 : printf("你的成绩是%c",'C');
  18.             break;
  19.             case 6 : printf("你的成绩是%c",'D');
  20.             break;
  21.             case 5 :
  22.             case 4 :
  23.             case 3 :
  24.             case 2 :
  25.             case 1 :
  26.             case 0 : printf("你的成绩是%c",'E');
  27.             break;
  28.         }
  29.     }
  30.     else
  31.     {
  32.         printf("请输入有效成绩");
  33.     }
  34.    
  35. }
复制代码

============================================================================================
PS E:\Administrator\Documents\Visual Studio 2019> & 'c:\Users\Administrator\.vscode\extensions\ms-vscode.cpptools-0.22.1\debugAdapters\bin\WindowsDebugLauncher.exe' '--stdin=Microsoft-MIEngine-In-foyssg0w.hwf' '--stdout=Microsoft-MIEngine-Out-uchyvsih.dwf' '--stderr=Microsoft-MIEngine-Error-3rjvn45z.yhi' '--pid=Microsoft-MIEngine-Pid-rstp1qux.pzs' '--dbgExe=E:\MingGW\bin\gdb.exe' '--interpreter=mi'
请输入你的成绩32
你的成绩是E
PS E:\Administrator\Documents\Visual Studio 2019>
******************************************************************************************************************************

  1. #include<stdio.h>
  2. #include<math.h>
  3. void main()
  4. {
  5.     double x, y, z ;
  6.     printf("请输入参数x,y的值\n");
  7.     scanf("%lf%lf",&x,&y);
  8.     if ((fabs(x) >= (1e-6))&&(fabs(y) >= (1e-6)))
  9.     {
  10.         if ((x >= 0) && (y > 0))
  11.         {
  12.             z = y - y / (pow(x,2)+2) ;
  13.             printf("%5.2lf - (%5.2lf / (pow(%5.2lf , 2) + 2)) = %5.2lf",y,y,x,z);
  14.         }
  15.         if ((x > 0) && (y <= 0))
  16.         {
  17.             z = (x - 2)/(pow(y,2)+1);
  18.             printf("(%5.2lf - 2) / (pow(%5.2lf , 2) + 1) = %5.2lf",x,y,z);
  19.         }
  20.         if (x < 0)
  21.         {
  22.             z = x + y;
  23.             printf("%5.2lf + (%5.2lf) = %5.2lf",x,y,z);
  24.         }  
  25.     }
  26.     else
  27.     {
  28.         printf("请输入有效数字");
  29.     }
  30.    
  31.    
  32. }
复制代码

==============================================================================================================
请输入参数x,y的值
99
1
1.00 - ( 1.00 / (pow(99.00 , 2) + 2)) =  1.00
PS E:\Administrator\Documents\Visual Studio 2019> & 'c:\Users\Administrator\.vscode\extensions\ms-vscode.cpptools-0.22.1\debugAdapters\bin\WindowsDebugLauncher.exe' '--stdin=Microsoft-MIEngine-In-ismc1e5p.v5u' '--stdout=Microsoft-MIEngine-Out-fng1olco.cri' '--stderr=Microsoft-MIEngine-Error-dm2davho.yx2' '--pid=Microsoft-MIEngine-Pid-ksuzs5tx.lpe' '--dbgExe=E:\MingGW\bin\gdb.exe' '--interpreter=mi'
请输入参数x,y的值
20
-20
(20.00 - 2) / (pow(-20.00 , 2) + 1) =  0.04
PS E:\Administrator\Documents\Visual Studio 2019> & 'c:\Users\Administrator\.vscode\extensions\ms-vscode.cpptools-0.22.1\debugAdapters\bin\WindowsDebugLauncher.exe' '--stdin=Microsoft-MIEngine-In-2awuigke.tsr' '--stdout=Microsoft-MIEngine-Out-eedg20fp.rpo' '--stderr=Microsoft-MIEngine-Error-jlt1hg0q.q5k' '--pid=Microsoft-MIEngine-Pid-nh50cpif.wrf' '--dbgExe=E:\MingGW\bin\gdb.exe' '--interpreter=mi'
请输入参数x,y的值
66
3
3.00 - ( 3.00 / (pow(66.00 , 2) + 2)) =  3.00
PS E:\Administrator\Documents\Visual Studio 2019>
*************************************************************************************************************************************
想知道小甲鱼最近在做啥?请访问 -> ilovefishc.com
回复 支持 反对

使用道具 举报

 楼主| 发表于 2019-5-6 19:15:51 | 显示全部楼层
本帖最后由 bin554385863 于 2019-5-7 00:01 编辑

2019年5月6日19:14:36

练习

  1. /*计算个人所得税,800以下不交,800~1200交超过800的5%,1200~2000交超过1200的8%,2000~∞交超过2000的20%,要求switch语句*/
  2. #include <stdio.h>
  3. #define i 400
  4. void main()
  5. {
  6.     int w, d;
  7.     double v;
  8.     printf("请输入你的收入:\n");
  9.     scanf("%d", &w);
  10.     if (w > 2000)
  11.     {
  12.         v = 0.20;
  13.         d = w - 2000;
  14.         printf("税率 : %5.2lf\n交税:%5.2lf\n", v,v*d);
  15.     }
  16.     else
  17.     {
  18.         switch (w / i)
  19.         {
  20.         case 1:
  21.             v = 0;
  22.             printf("税率 : %5.2lf\n交税:%5.2lf\n", 0, 0);
  23.             break;

  24.         case 2:
  25.             if (w == 800)
  26.             {
  27.                 printf("税率  %5.2lf\n交税:%5.2lf\n", 0, 0);
  28.             }
  29.             else
  30.             {
  31.                 v = 0.05;
  32.                 d = w - 800;
  33.                 printf("税率 : %5.2lf\n交税:%5.2lf\n", v, v * d);
  34.             }

  35.             break;

  36.         case 3:
  37.         case 4:
  38.             if (w == 1200)
  39.             {
  40.                 v = 0.05;
  41.                 d = w - 800;
  42.                 printf("税率 : %5.2lf\n交税:%5.2lf\n", v, v * d);
  43.             }
  44.             else
  45.             {
  46.                 v = 0.08;
  47.                 d = w - 1200;

  48.                 printf("税率 : %5.2lf\n交税:%5.2lf\n", v, v * d);
  49.             }

  50.             break;
  51.         }
  52.     }
  53.     if (w == 2000)
  54.     {
  55.         v = 0.08;
  56.         d = w - 1200;

  57.         printf("税率 : %5.2lf\n交税:%5.2lf\n", v, v * d);
  58.     }
  59.     else if (w <= 800)
  60.     {
  61.         printf("你的收入真的这么少?");
  62.     }
  63. }
复制代码

==================================================================================================================
请输入你的收入:
500
税率 :  0.00
交税: 0.00
你的收入真的这么少?
PS E:\Administrator\Documents\Visual Studio 2019> & 'c:\Users\Administrator\.vscode\extensions\ms-vscode.cpptools-0.22.1\debugAdapters\bin\WindowsDebugLauncher.exe' '--stdin=Microsoft-MIEngine-In-iei5stvx.zua' '--stdout=Microsoft-MIEngine-Out-bc2w4jny.lqx' '--stderr=Microsoft-MIEngine-Error-3jyyxuxw.nip' '--pid=Microsoft-MIEngine-Pid-xnawfrdn.r1l' '--dbgExe=E:\MingGW\bin\gdb.exe' '--interpreter=mi'
请输入你的收入:
1200
税率 :  0.05
交税:20.00
PS E:\Administrator\Documents\Visual Studio 2019> & 'c:\Users\Administrator\.vscode\extensions\ms-vscode.cpptools-0.22.1\debugAdapters\bin\WindowsDebugLauncher.exe' '--stdin=Microsoft-MIEngine-In-ey3w10a1.yt3' '--stdout=Microsoft-MIEngine-Out-s5hy3ygs.k4k' '--stderr=Microsoft-MIEngine-Error-lp2xmhzu.rlx' '--pid=Microsoft-MIEngine-Pid-m52ll1ow.msv' '--dbgExe=E:\MingGW\bin\gdb.exe' '--interpreter=mi'
请输入你的收入:
2000
税率 :  0.08
交税:64.00
PS E:\Administrator\Documents\Visual Studio 2019>
************************************************************************************************************************************
练习

自己写的代码
  1. /*从键盘输入字符,如果是小写字母则输出大写,如果是大写字符则输出小写,其他字符原样输出*/
  2. #include<stdio.h>
  3. #include<conio.h>
  4. #include<ctype.h>
  5. void main()
  6. {
  7.     char ch1;
  8.     int i, j;
  9.     printf("请输入一个字符\n");
  10.     ch1 = getchar();
  11.     for (j = 0, i = 0; i <= 127; i++)
  12.     {
  13.         ch1 % i == 1;//判断ch1是不是ASC表中的字符
  14.         j++;//如果是则j+1
  15.     }
  16.     switch (!j)
  17.     {
  18.     case 1:
  19.         printf("请输入合法字符");//输入两个以上的字符或者两位以上的数字都是非法
  20.         break;
  21.     case 0:
  22.         if (ch1 >= 'A' && ch1 <= 'Z')
  23.         {
  24.             printf("%c", ch1 + 32);
  25.         }
  26.         if (ch1 >= 'a' && ch1 <= 'z')
  27.         {
  28.             printf("%c", ch1 - 32);
  29.         }
  30.         else
  31.         {
  32.             printf("%c", ch1);
  33.         }
  34.         break;
  35.     default:
  36.         break;
  37.     }

  38. }
复制代码

-----------------------------------------------------------------------------------------------------------------------------------------------------------
t=Microsoft-MIEngine-Out-2jmmjze5.t14' '--stderr=Microsoft-MIEngine-Error-ucsuoa1i.psa' '--pid=Microsoft-MIEngine-Pid-2eyindh4.mbi' '--dbgExe=E:\MingGW\bin\gdb.exe' '--interpreter=mi'
请输入一个字符
66
6
PS E:\Administrator\Documents\Visual Studio 2019>
无法达到我想要的想法
-----------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------
感谢Croper
提供的帮助
  1. /*从键盘输入字符,如果是小写字母则输出大写,如果是大写字符则输出小写,其他字符原样输出*/
  2. #include<stdio.h>
  3. #include<ctype.h>
  4. #include<conio.h>
  5. int main()
  6. {
  7.          char c=getchar();
  8.          int flg=0;
  9.          if (getchar()!='\n')
  10.                  flg=1;
  11.          if (c<0 || c>127)  
  12.                  flg=1;  //在中文环境下,这一句是不需要的
  13.          if (flg){
  14.                  printf("请输入合法字符");
  15.                  return;
  16.         }
  17.         if (isalpha(c)) c^=32;
  18.         printf("%c",c);
  19. }
复制代码

==============================================================================================================
PS E:\Administrator\Documents\Visual Studio 2019> & 'c:\Users\Administrator\.vscode\extensions\ms-vscode.cpptools-0.22.1\debugAdapters\bin\WindowsDebugLauncher.exe' '--stdin=Microsoft-MIEngine-In-yiysj53l.dd0' '--stdout=Microsoft-MIEngine-Out-fc2m1x4l.xl5' '--stderr=Microsoft-MIEngine-Error-z0rcmudg.0v0' '--pid=Microsoft-MIEngine-Pid-tnkkhycr.3cu' '--dbgExe=E:\MingGW\bin\gdb.exe' '--interpreter=mi'
66
请输入合法字符
PS E:\Administrator\Documents\Visual Studio 2019> & 'c:\Users\Administrator\.vscode\extensions\ms-vscode.cpptools-0.22.1\debugAdapters\bin\WindowsDebugLauncher.exe' '--stdin=Microsoft-MIEngine-In-xptiixfc.5fb' '--stdout=Microsoft-MIEngine-Out-kdeidk1m.yw4' '--stderr=Microsoft-MIEngine-Error-4ozsbsuq.jvj' '--pid=Microsoft-MIEngine-Pid-0ojceihd.zg5' '--dbgExe=E:\MingGW\bin\gdb.exe' '--interpreter=mi'
h
H
PS E:\Administrator\Documents\Visual Studio 2019> & 'c:\Users\Administrator\.vscode\extensions\ms-vscode.cpptools-0.22.1\debugAdapters\bin\WindowsDebugLauncher.exe' '--stdin=Microsoft-MIEngine-In-yihpibka.kjk' '--stdout=Microsoft-MIEngine-Out-yfzunhgd.0ws' '--stderr=Microsoft-MIEngine-Error-21qtkion.25g' '--pid=Microsoft-MIEngine-Pid-tetf2ufk.bvo' '--dbgExe=E:\MingGW\bin\gdb.exe' '--interpreter=mi'
jj
请输入合法字符
PS E:\Administrator\Documents\Visual Studio 2019> & 'c:\Users\Administrator\.vscode\extensions\ms-vscode.cpptools-0.22.1\debugAdapters\bin\WindowsDebugLauncher.exe' '--stdin=Microsoft-MIEngine-In-vmjlx3ea.oen' '--stdout=Microsoft-MIEngine-Out-k3h5v3in.wbk' '--stderr=Microsoft-MIEngine-Error-x5aswmgv.gfc' '--pid=Microsoft-MIEngine-Pid-dn4ltrcu.hcc' '--dbgExe=E:\MingGW\bin\gdb.exe' '--interpreter=mi'
2
2
PS E:\Administrator\Documents\Visual Studio 2019>
完美达到了效果
include<ctype.h>    isalpha()  判断字符是不是字母
-----------------------------------------------------------------------------------
^=
异或运算是二进制数按位做异或运算再赋值,其运算规则是:

0^0=0;   0^1=1;   1^0=1;   1^1=0

即:如果两个相应位为“异”(值不同),则该位结果为1,否则为0。

同与运算相同,参与异或运算的两个操作数,位数必须相同,也就是数据类型必须相同。不同类型的数据做与运算,需要强制转换为同一类型。

**********************************************************************************************************************************************
练习for的使用

  1. /*使用for循环计算(|a|,|b|)累加和*/
  2. #include <stdio.h>
  3. void main()
  4. {
  5.         int a, b, sum, s, i;
  6.         printf("请输入a和b的值\n");
  7.         scanf("%d%d", &a, &b);
  8.         if (b <= a)
  9.         {
  10.                 i = b;
  11.                 b = a;
  12.                 a = i;
  13.                 for (s = a; s <= b; s++)
  14.                 {
  15.                         sum = sum + s;
  16.                 }
  17.                 printf("sum(%d , %d) = %d\n", a, b, sum);
  18.         }
  19.         else
  20.         {
  21.                 for (s = a; s <= b; s++)
  22.                 {
  23.                         sum = sum + s;
  24.                 }
  25.                 printf("sum(%d , %d) = %d\n", a, b, sum);
  26.         }
  27. }
复制代码

------------------------------------------------------------------------------------------------------------------------------------------------------------
请输入a和b的值
50
50
sum(50 , 50) = 50
PS E:\Administrator\Documents\Visual Studio 2019> & 'c:\Users\Administrator\.vscode\extensions\ms-vscode.cpptools-0.22.1\debugAdapters\bin\WindowsDebugLauncher.exe' '--stdin=Microsoft-MIEngine-In-h11exuqp.5kn' '--stdout=Microsoft-MIEngine-Out-zvtxoj2s.iil' '--stderr=Microsoft-MIEngine-Error-jw1s2hah.wpf' '--pid=Microsoft-MIEngine-Pid-5mxdhx3l.0px' '--dbgExe=E:\MingGW\bin\gdb.exe' '--interpreter=mi'
请输入a和b的值
50
30
sum(30 , 50) = 840
PS E:\Administrator\Documents\Visual Studio 2019> & 'c:\Users\Administrator\.vscode\extensions\ms-vscode.cpptools-0.22.1\debugAdapters\bin\WindowsDebugLauncher.exe' '--stdin=Microsoft-MIEngine-In-wt1qodgs.b0e' '--stdout=Microsoft-MIEngine-Out-1gzpi12l.ip3' '--stderr=Microsoft-MIEngine-Error-xefnl5r3.edw' '--pid=Microsoft-MIEngine-Pid-rfnvokqv.wa5' '--dbgExe=E:\MingGW\bin\gdb.exe' '--interpreter=mi'
请输入a和b的值
PS E:\Administrator\Documents\Visual Studio 2019> & 'c:\Users\Administrator\.vscode\extensions\ms-vscode.cpptools-0.22.1\debugAdapters\bin\WindowsDebugLauncher.exe' '--stdin=Microsoft-MIEngine-In-qhciwkla.0yw' '--stdout=Microsoft-MIEngine-Out-ysr1gibt.anj' '--stderr=Microsoft-MIEngine-Error-cdxo5uoy.fux' '--pid=Microsoft-MIEngine-Pid-bgzorkky.zku' '--dbgExe=E:\MingGW\bin\gdb.exe' '--interpreter=mi'
请输入a和b的值
-50
-30
sum(-50 , -30) = -840
PS E:\Administrator\Documents\Visual Studio 2019>
===================================================================================================================
第五章
------------------------------------------------------------
For循环

  1. /*使用*循环打印几何图案*/
  2. #include <stdio.h>
  3. void main()
  4. {
  5.         int i, j, m, n;
  6.         printf("请输入m和n的值\n");
  7.         scanf("%d%d", &m, &n);
  8.         if ((m > 0) && (n > 0))
  9.         {
  10.                 for (i = 1; i <= m; i++)
  11.                 {
  12.                         for (j = 1; j <= n; j++)
  13.                         {
  14.                                 printf("*");//里层循环控制竖列
  15.                                 
  16.                         }
  17.                         printf("\n");//外层循环横列
  18.                 }
  19.         }
  20.         else
  21.         {
  22.                 printf("请输入正确的数字");
  23.         }
  24. }
复制代码

----------------------------------------------------------------------------------------------------------------
PS E:\Administrator\Documents\Visual Studio 2019> & 'c:\Users\Administrator\.vscode\extensions\ms-vscode.cpptools-0.22.1\debugAdapters\bin\WindowsDebugLauncher.exe' '--stdin=Microsoft-MIEngine-In-pptuufe2.5w5' '--stdout=Microsoft-MIEngine-Out-soymft2l.czu' '--stderr=Microsoft-MIEngine-Error-p3cfkpwn.zvx' '--pid=Microsoft-MIEngine-Pid-kw4qcmuh.fvb' '--dbgExe=E:\MingGW\bin\gdb.exe' '--interpreter=mi'
请输入m和n的值
8
7
*******
*******
*******
*******
*******
*******
*******
*******
PS E:\Administrator\Documents\Visual Studio 2019>
-----------------------------------------------------------------------------------------------------------------------------
打印一个左对齐的倒直角三角形
  1. /*打印一个左对齐的倒直角三角形*/
  2. #include <stdio.h>
  3. void main()
  4. {
  5.     int i, j;
  6.     for (i = 1; i <= 8; i++)
  7.     {
  8.         for (j = i; j <= 8; j++)
  9.         {
  10.             printf("*");
  11.         }
  12.         printf("\n");
  13.     }
  14. }
复制代码

--------------------------------------------------------------------------------------------------
PS E:\Administrator\Documents\Visual Studio 2019> & 'c:\Users\Administrator\.vscode\extensions\ms-vscode.cpptools-0.22.1\debugAdapters\bin\WindowsDebugLauncher.exe' '--stdin=Microsoft-MIEngine-In-bazkeozo.0uv' '--stdoustdout=Microsoft-MIEngine-Out-cffozh4r.zuh' '--stderr=Microsoft-MIEngine-Error-lzlwn0y5.t2m' '--pid=Microsoft-MIEngine-Pid-amhta0d3.45q' '--dbgExe=E:\MingGW\bin\gdb.exe' '--interpreter=mi'
********
*******
******
*****
****
***
**
*
PS E:\Administrator\Documents\Visual Studio 2019>
想知道小甲鱼最近在做啥?请访问 -> ilovefishc.com
回复 支持 反对

使用道具 举报

 楼主| 发表于 2019-5-7 00:02:56 | 显示全部楼层
本帖最后由 bin554385863 于 2019-5-8 02:11 编辑

2019年5月7日00:02:45

打印一个右对齐的直角三角形
  1. /*打印一个右对齐的直角三角形*/
  2. #include <stdio.h>
  3. void main()
  4. {
  5.     int i, j, k;
  6.     for (i = 1; i <= 8; i++) //换行
  7.     {
  8.         for (j = 1; j <= (8 - i); j++) //打印空格

  9.             printf(" ");

  10.         for (k = 1; k <= i; k++)//打印*

  11.             printf("*");
  12.         printf("\n");
  13.     }
  14. }
复制代码

-------------------------------------------------------------------------------------------------------------
PS E:\Administrator\Documents\Visual Studio 2019> & 'c:\Users\Administrator\.vscode\extensions\ms-vscode.cpptools-0.22.1\debugAdapters\bin\WindowsDebugLauncher.exe' '--stdin=Microsoft-MIEngine-In-4d44dejs.xil' '--stdout=Microsoft-MIEngine-Out-vvmz1aim.e5b' '--stderr=Microsoft-MIEngine-Error-fkleu5c0.qwc' '--pid=Microsoft-MIEngine-Pid-wrabbjtz.ion' '--dbgExe=E:\MingGW\bin\gdb.exe' '--interpreter=mi'
             *
           **
         ***
        ****
      *****
    ******
  *******
********
PS E:\Administrator\Documents\Visual Studio 2019>
=======================================================================
for打印一个等腰三角形
  1. /*for打印一个等腰三角形*/
  2. #include <stdio.h>
  3. void main()
  4. {
  5.     int i, j, k;
  6.     for (i = 1; i <= 7; i++)
  7.     {
  8.         for (j = i; j <= 7; j++)
  9.             printf(" ");
  10.         for (k = 1; k <= (2 * i - 1); k++)
  11.             printf("*");
  12.         printf("\n");
  13.     }
  14. }
复制代码

-----------------------------------------------------------------------------------------------------------------------------------------------------------------
PS E:\Administrator\Documents\Visual Studio 2019> & 'c:\Users\Administrator\.vscode\extensions\ms-vscode.cpptools-0.23.0\debugAdapters\bin\WindowsDebugLauncher.exe' '--stdin=Microsoft-MIEngine-In-y1mgxlbo.obs' '--stdout=Microsoft-MIEngine-Out-3mkfpwb1.hg4' '--stderr=Microsoft-MIEngine-Error-xyslzhsg.e4m' '--pid=Microsoft-MIEngine-Pid-l1o05xjl.h03' '--dbgExe=E:\MingGW\bin\gdb.exe' '--interpreter=mi'
       *
      ***
     *****
    *******
   *********
  ***********
*************
PS E:\Administrator\Documents\Visual Studio 2019>
====================================================================================
if        for        switch混合使用
和差商积运算

  1. /*和差商积循环计算*/
  2. #include <stdio.h>
  3. void main()
  4. {
  5.     int a, b, s;
  6.     int input, i, m, n, r, x;
  7.     char c;
  8.     printf("请输入你想算的总题数\n");
  9.     scanf("%d", &m);
  10.     printf("请输入你想计算的数值范围\n");
  11.     scanf("%d", &n);

  12.     if ((m < 10) || (n < 100))
  13.     {
  14.         printf("请输入大于10的总题数和大于100的计算范围");
  15.     }
  16.     else
  17.     {
  18.         for (i = 0; i < m; i++)
  19.         {
  20.             srand(time(NULL));
  21.             a = rand() % n;
  22.             b = rand() % n;
  23.             s = rand() % 3;
  24.             switch (s)
  25.             {
  26.             case 0:
  27.                 c = '+';
  28.                 r = (a + b);
  29.                 printf("%d %c %d\n", a, c, b);
  30.                 break;
  31.             case 1:
  32.                 c = '*';
  33.                 r = (a * b);
  34.                 printf("%d %c %d\n", a, c, b);
  35.                 break;
  36.             case 2:
  37.                 c = '-';
  38.                 if (a >= b)
  39.                 {
  40.                     r = (a - b);
  41.                     printf("%d %c %d\n", a, c, b);
  42.                 }
  43.                 else
  44.                 {
  45.                     r = (b - a);
  46.                     printf("%d %c %d\n", b, c, a);
  47.                 }
  48.                 break;
  49.             case 3:
  50.                 c = '/';
  51.                 if ((a >= b) && ((a % b == 0)))
  52.                 {
  53.                     r = (a / b);
  54.                     printf("%d %c %d\n", a, c, b);
  55.                 }
  56.                 else if ((a < b) && ((b % a) == 0))
  57.                 {
  58.                     r = (b % a);
  59.                     printf("%d %c %d\n", b, c, a);
  60.                 }

  61.                 break;
  62.             }
  63.             scanf("%d", &input);
  64.             if (r == input)
  65.             {
  66.                 printf("正确\n");
  67.                 x++;
  68.             }
  69.             else if (r != input)
  70.             {
  71.                 printf("错误\n");
  72.             }
  73.         }
  74.         printf("你做对了%d道题\n做错了%d道题", x, m - x);
  75.     }
  76. }
复制代码

-----------------------------------------------------------------------------------------------------------------------------------------------------
错误
84 - 72
0
错误
88 * 52
0
错误
91 * 1
0
错误
91 * 1
0
错误
你做对了0道题
做错了10道题
PS E:\Administrator\Documents\Visual Studio 2019>
================================================================================================
九九乘法表
  1. /*九九乘法表*/
  2. #include <stdio.h>
  3. void main()
  4. {
  5.     int i, j;
  6.     for (i = 1; i <= 9; i++)
  7.     {
  8.         for (j = 1; j <= i; j++)
  9.         {
  10.             printf("%2d * %d = %2d ", i, j, i * j);
  11.         }

  12.         printf("\n");
  13.     }
  14. }
复制代码

------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------
PS E:\Administrator\Documents\Visual Studio 2019> & 'c:\Users\Administrator\.vscode\extensions\ms-vscode.cpptools-0.23.0\debugAdapters\bin\WindowsDebugLauncher.exe' '--stdin=Microsoft-MIEngine-In-d53uf5pc.4y0' '--stdout=Microsoft-MIEngine-Out-nj1v2p2j.qae' '--stderr=Microsoft-MIEngine-Error-zdhutrst.qek' '--pid=Microsoft-MIEngine-Pid-umfalpgf.pbb' '--dbgExe=E:\MingGW\bin\gdb.exe' '--interpreter=mi'
1 * 1 =  1
2 * 1 =  2  2 * 2 =  4
3 * 1 =  3  3 * 2 =  6  3 * 3 =  9
4 * 1 =  4  4 * 2 =  8  4 * 3 = 12  4 * 4 = 16
5 * 1 =  5  5 * 2 = 10  5 * 3 = 15  5 * 4 = 20  5 * 5 = 25
6 * 1 =  6  6 * 2 = 12  6 * 3 = 18  6 * 4 = 24  6 * 5 = 30  6 * 6 = 36
7 * 1 =  7  7 * 2 = 14  7 * 3 = 21  7 * 4 = 28  7 * 5 = 35  7 * 6 = 42  7 * 7 = 49
8 * 1 =  8  8 * 2 = 16  8 * 3 = 24  8 * 4 = 32  8 * 5 = 40  8 * 6 = 48  8 * 7 = 56  8 * 8 = 64
9 * 1 =  9  9 * 2 = 18  9 * 3 = 27  9 * 4 = 36  9 * 5 = 45  9 * 6 = 54  9 * 7 = 63  9 * 8 = 72  9 * 9 = 81
PS E:\Administrator\Documents\Visual Studio 2019>
==========================================================================================================
5.2
while语句

用while输出n的阶乘N!
  1. /*用while输出n的阶乘N!*/
  2. #include <stdio.h>
  3. void main()
  4. {
  5.     int n, s, i;
  6.     printf("请输入一个正整数\n");
  7.     scanf("%d", &n);
  8.     if (n >= 0)
  9.     {
  10.         s = 1;
  11.         i = 1;
  12.         while (i <= n)
  13.         {
  14.             s = s * i;
  15.             i++;
  16.         }
  17.         printf("%d! = %d", n, s);
  18.     }
  19.     else
  20.     {
  21.         printf("请输入一个正整数");
  22.     }
  23. }
复制代码

----------------------------------------------------------------------------------------------------------------------------------
ohwsfx.bpp' '--stdout=Microsoft-MIEngine-Out-iujqujth.o4p' '--stderr=Microsoft-MIEngine-Error-lsz0mt0b.ph4' '--pid=Microsoft-MIEngine-Pid-yy2rbuqs.i34' '--dbgExe=E:\MingGW\bin\gdb.exe' '--interpreter=mi'
请输入一个正整数
0
0! = 1
PS E:\Administrator\Documents\Visual Studio 2019> & 'c:\Users\Administrator\.vscode\extensions\ms-vscode.cpptools-0.23.0\debugAdapters\bin\WindowsDebugLauncher.exe' '--stdin=Microsoft-MIEngine-In-rny3ewys.grc' '--stdout=Microsoft-MIEngine-Out-to3qgqog.5vs' '--stderr=Microsoft-MIEngine-Error-r0pdzgxk.hvu' '--pid=Microsoft-MIEngine-Pid-mix1ct3n.k1w' '--dbgExe=E:\MingGW\bin\gdb.exe' '--interpreter=mi'
请输入一个正整数
-8
请输入一个正整数
PS E:\Administrator\Documents\Visual Studio 2019> & 'c:\Users\Administrator\.vscode\extensions\ms-vscode.cpptools-0.23.0\debugAdapters\bin\WindowsDebugLauncher.exe' '--stdin=Microsoft-MIEngine-In-g0gug30l.ole' '--stdout=Microsoft-MIEngine-Out-sixsu1pe.chw' '--stderr=Microsoft-MIEngine-Error-j0c2b5x5.iga' '--pid=Microsoft-MIEngine-Pid-xfjzqyks.lir' '--dbgExe=E:\MingGW\bin\gdb.exe' '--interpreter=mi'
请输入一个正整数
6
6! = 720
PS E:\Administrator\Documents\Visual Studio 2019>
========================================================================================
do-----while用法
  1. /*使用do--while计算累加和*/
  2. #include <stdio.h>
  3. void main()
  4. {
  5.     int i, j;
  6.     i = 1;
  7.     j = 0;
  8.     do
  9.     {
  10.         j = j + i;
  11.         i++;
  12.     } while (i <= 100);
  13.     printf("j = %d", j);
  14. }
复制代码

--------------------------------------------------------------------------------------------------------------------------
PS E:\Administrator\Documents\Visual Studio 2019> & 'c:\Users\Administrator\.vscode\extensions\ms-vscode.cpptools-0.23.0\debugAdapters\bin\WindowsDebugLauncher.exe' '--stdin=Microsoft-MIEngine-In-hjgrgnat.qbc' '--stdout=Microsoft-MIEngine-Out-yfpzipcx.uth' '--stderr=Microsoft-MIEngine-Error-xqe3yi1d.0qf' '--pid=Microsoft-MIEngine-Pid-t10hwgqv.sre' '--dbgExe=E:\MingGW\bin\gdb.exe' '--interpreter=mi'
j = 5050
PS E:\Administrator\Documents\Visual Studio 2019>
=====================================================================================
想知道小甲鱼最近在做啥?请访问 -> ilovefishc.com
回复 支持 反对

使用道具 举报

 楼主| 发表于 2019-5-9 03:12:22 | 显示全部楼层
本帖最后由 bin554385863 于 2019-5-9 23:33 编辑

2019年5月9日03:07:29
分支循环语句和break和continue的练习
----------------------------------------------------------------------------------------------
[1]
求两个个正整数的最小公倍数和最大公约数.

  1. #include <stdio.h>
  2. /*求两个个数的最大公约数和最小公倍数*/
  3. void main()
  4. {
  5.     int a, b, c, t;
  6.     printf("请输入任意两个正整数\n");
  7.     scanf("%d%d", &a, &b);
  8.     if ((a > 0) && (b > 0))
  9.     {
  10.         if (a <= b)
  11.         {
  12.             t = a;
  13.             a = b;
  14.             b = t;
  15.         }
  16.         for (c = b; c > 0; c--)
  17.         {
  18.             if (((a % c) == 0) && ((b % c) == 0))
  19.             {
  20.                 printf("%d和%d的最大公约数是:%d\n", a, b, c);
  21.                 printf("%d和%d的最小公倍数是:%d\n", a, b, (a*b)/c);
  22.                 break;
  23.             }
  24.         }

  25.     }
  26.     else
  27.     {
  28.         printf("请输入大于0的整数");
  29.     }
  30. }
复制代码

---------------------------------------------------------------------------------------------------------------------------
请输入任意两个正整数
20
40
40和20的最大公约数是:20
40和20的最小公倍数是:40
PS E:\Administrator\Documents\Visual Studio 2019> & 'c:\Users\Administrator\.vscode\extensions\ms-vscode.cpptools-0.23.0\debugAdapters\bin\WindowsDebugLauncher.exe' '--stdin=Microsoft-MIEngine-In-1xenkt03.da5' '--stdout=Microsoft-MIEngine-Out-tytcbuvo.e0r' '--stderr=Microsoft-MIEngine-Error-d5ozafg2.nje' '--pid=Microsoft-MIEngine-Pid-zlyag0ze.xce' '--dbgExe=E:\MingGW\bin\gdb.exe' '--interpreter=mi'
请输入任意两个正整数
77
33
77和33的最大公约数是:11
77和33的最小公倍数是:231
PS E:\Administrator\Documents\Visual Studio 2019> & 'c:\Users\Administrator\.vscode\extensions\ms-vscode.cpptools-0.23.0\debugAdapters\bin\WindowsDebugLauncher.exe' '--stdin=Microsoft-MIEngine-In-itkplkhc.2hh' '--stdout=Microsoft-MIEngine-Out-kpnuch0q.3kh' '--stderr=Microsoft-MIEngine-Error-lfk03dvy.awv' '--pid=Microsoft-MIEngine-Pid-ovmku5i4.udr' '--dbgExe=E:\MingGW\bin\gdb.exe' '--interpreter=mi'
请输入任意两个正整数
97
35
97和35的最大公约数是:1
97和35的最小公倍数是:3395
PS E:\Administrator\Documents\Visual Studio 2019> & 'c:\Users\Administrator\.vscode\extensions\ms-vscode.cpptools-0.23.0\debugAdapters\bin\WindowsDebugLauncher.exe' '--stdin=Microsoft-MIEngine-In-mtaqc2uv.onw' '--stdout=Microsoft-MIEngine-Out-2r3dnxmd.zcc' '--stderr=Microsoft-MIEngine-Error-jzvfjzol.xqe' '--pid=Microsoft-MIEngine-Pid-w4bcx0p3.qgv' '--dbgExe=E:\MingGW\bin\gdb.exe' '--interpreter=mi'
请输入任意两个正整数
-9
5
请输入大于0的整数
PS E:\Administrator\Documents\Visual Studio 2019>
==================================================================================================================
goto的作用

goto可以直接跳出多层循环,break只能跳出一层

[2]
设a,b,c,d,e为0~9的数字,求满足abc*e=dcba,最大的五个数字且a不为,e不为0和1.
  1. #include <stdio.h>
  2. #include <math.h>
  3. /*设a,b,c,d,e为0~9的数字求满足abc*e=dcba,a!=0&&e!=0&&e!=1,最大的a,b,c,d,e五个数*/
  4. int main()
  5. {
  6.     int a, b, c, d, e;
  7.     for (a = 9; a > 0; a--)
  8.     {
  9.         for (b = 9; b >= 0; b--)
  10.         {
  11.             for (c = 9; c >= 0; c--)
  12.             {
  13.                 for (d = 9; d >= 0; d--)
  14.                 {
  15.                     for (e = 9; e > 1; e--)
  16.                     {
  17.                         if ((a * 100 + b * 10 + c) * e == (d * 1000 + c * 100 + b * 10 + a))
  18.                         {
  19.                             printf("符合条件的五个数是:a = %d b = %d c = %d d = %d e = %d\n", a, b, c, d, e);
  20.                             printf("组成的四位数是: %d\n", d * 1000 + c * 100 + b * 10 + a);
  21.                             goto Over;
  22.                         }
  23.                     }
  24.                 }
  25.             }
  26.         }
  27.     }
  28. Over:
  29.     return 0;
  30. }
复制代码

----------------------------------------------------------------------------------------------------------------------------------
                                               > & 'c:\Users\Administrator\.vscode\extensions\ms-vscode.cpptools-0.23.0\debugAdapters\bin\WindowsDebugLauncher.exe' '--stdin=Microsoft-MIEngine-In-iwxmfow2.1j2' '--stdout=Microsoft-MIEngine-Out-5g5o4mae.byb' '--stderr=Microsoft-MIEngine-Error-q1wfomum.sir' '--pid=Microsoft-MIEngine-Pid-cpqtz5pq.x0i' '--dbgExe=E:\MingGW\bin\gdb.exe' '--interpreter=mi'
符合条件的五个数是:a = 9 b = 6 c = 7 d = 6 e = 7
组成的四位数是: 6769
PS E:\Administrator\Documents\Visual Studio 2019>
=========================================================================================================
[3]
利用循环结构计算π的近似值,
公式是a(n) = (-1^(n+1))/(2n+1)的前n项和S(n)*4

  1. #include <stdio.h>
  2. #include <math.h>
  3. /*循环结构计算π的近似值*/
  4. void main()
  5. {
  6.     double pi, PI, a;
  7.     int b, n;
  8. Start:
  9.     printf("请输入n的值,n越大精确度越高\n");
  10.     scanf("%d", &n);
  11.     if (n >= 1)
  12.     {
  13.         for (pi = 0, b = n; b >= 1; b--)
  14.         {
  15.             a = (double)pow(-1, (b + 1)) / (2 * b - 1);
  16.             if (fabs(a) > (1e-6))
  17.             {
  18.                 pi = pi + a;
  19.                 PI = pi * 4;
  20.             }
  21.         }
  22.         printf("π ≈ %10.8lf\n", PI);
  23.     }
  24.     else
  25.     {
  26.         printf("请输入大于0的正整数");
  27.         goto Start;
  28.     }
  29. }
复制代码

----------------------------------------------------------------------------------------------------------------------------------------------------
PS E:\Administrator\Documents\Visual Studio 2019> & 'c:\Users\Administrator\.vscode\extensions\ms-vscode.cpptools-0.23.0\debugAdapters\bin\WindowsDebugLauncher.exe' '--stdin=Microsoft-MIEngine-In-r4osipnv.vgm' '--stdout=Microsoft-MIEngine-Out-ipk2veu5.dat' '--stderr=Microsoft-MIEngine-Error-21lotohm.sae' '--pid=Microsoft-MIEngine-Pid-dqrrh432.bkn' '--dbgExe=E:\MingGW\bin\gdb.exe' '--interpreter=mi'
请输入n的值,n越大精确度越高
0
请输入大于0的正整数请输入n的值,n越大精确度越高
-9
请输入大于0的正整数请输入n的值,n越大精确度越高
50
π ≈ 3.12159465
PS E:\Administrator\Documents\Visual Studio 2019>
===============================================================================================
[4]切木头
一根133m的木头,需要切成19m和23m的木料,求两种木料且多少根时剩下的木料最少
  1. #include<stdio.h>
  2. /*一根133m的木头,需要切成19m和23m的木料,求两种木料且多少根时剩下的木料最少*/
  3. int main()
  4. {
  5.     int a, b, c;
  6.     for (c = 1; c < 19; c++)
  7.     {
  8.         for (a = 133; a >= 1; a--)
  9.         {
  10.             for (b = 133; b >=1; b--)
  11.             {
  12.                 if ((19 * a) + (23 * b) + c == 133)
  13.                 {
  14.                     printf("19m的木料%d根\n23m的木料%d根,剩下%dm\n", a, b, c);
  15.                     goto End;
  16.                 }
  17.             }
  18.         }
  19.     }
  20.     End :
  21.     return 0;
  22. }
复制代码

---------------------------------------------------------------------------------------------------
PS E:\Administrator\Documents\Visual Studio 2019> & 'c:\Users\Administrator\.vscode\extensions\ms-vscode.cpptools-0.23.0\debugAdapters\bin\WindowsDebugLauncher.exe' '--stdin=Microsoft-MIEngine-In-ufqieqr5.vjj' '--stdout=Microsoft-MIEngine-Out-k4xpuwnu.oaz' '--stderr=Microsoft-MIEngine-Error-ljzd2iw0.3zv' '--pid=Microsoft-MIEngine-Pid-irngfpnu.abl' '--dbgExe=E:\MingGW\bin\gdb.exe' '--interpreter=mi'
19m的木料2根
23m的木料4根,剩下3m
PS E:\Administrator\Documents\Visual Studio 2019>
========================================================================================================
[5]求1~10000,真因子之和等于它本身的数
  1. #include <stdio.h>
  2. /*求1~10000,真因子之和等于它本身的数*/
  3. void main()
  4. {
  5.     int i, j, sum;
  6.     for (i = 10000; i >= 1; i--)
  7.     {
  8.         for (sum = 0, j = 1; j <= (i / 2); j++)
  9.         {
  10.             if (i % j == 0)
  11.             {
  12.                 sum = sum + j;
  13.                 if (i == sum)
  14.                 {
  15.                     printf("%d\n", i);
  16.                 }
  17.             }
  18.         }
  19.     }
  20. }
复制代码

------------------------------------------------------------------------------------------------------------------
PS E:\Administrator\Documents\Visual Studio 2019> & 'c:\Users\Administrator\.vscode\extensions\ms-vscode.cpptools-0.23.0\debugAdapters\bin\WindowsDebugLauncher.exe' '--stdin=Microsoft-MIEngine-In-p5ba2yih.pxe' '--stdout=Microsoft-MIEngine-Out-cevwv4fv.o1h' '--stderr=Microsoft-MIEngine-Error-iaexl5y4.0nh' '--pid=Microsoft-MIEngine-Pid-eybgs5lq.yd5' '--dbgExe=E:\MingGW\bin\gdb.exe' '--interpreter=mi'
8190
8128
2016
496
28
24
6
PS E:\Administrator\Documents\Visual Studio 2019>
想知道小甲鱼最近在做啥?请访问 -> ilovefishc.com
回复 支持 反对

使用道具 举报

 楼主| 发表于 2019-5-10 21:53:29 | 显示全部楼层
本帖最后由 bin554385863 于 2019-5-10 23:49 编辑

2019年5月10日21:33:46
第六章
函数

自定义函数形式:
[存储类型符] [返回值类型符] 自定义函数名 [形式参数列表]
{
        函数体;
        返回值;
}
----------------------------------------------------------------------------------
[存储类型符]说明了函数的作用范围,它只有两种形式:
        static 内部函数 表面函数只在源文件内部起作用,不可被外部调用;

        extern 外部函数        表面函函数可以被外部调用.

        函数默认都是extern类型

[返回值类型符]
        没有返回值可以用void定义返回值类型.

        其他返回值类型参考数据类型.

        返回值类型默认为int.

函数名
        可以是任何合法标识符构成.

[形式参数列表]
        说明了参数的类型;
       
        说明了参数的个数.

函数体:

        实现函数功能的语句表达式

返回值

        返回函数表达式的值.
--------------------------------------------------------------------------------------------
自定义函数的两种调用方式
[1,先声明,后调用,再定义]
  1. #include<stdio.h>
  2. [声明自定义函数]; ---------------------------->注意分号]
  3. int main()
  4. {
  5.         函数体;
  6.         调用[自定义函数]
  7.         return 0;
  8. }
  9. [自定义函数方法的定义]
  10. {
  11.         方法定义
  12.         return (函数表达式);
  13. }
复制代码


[2,先定义后调用不声明]


  1. #include<stdio.h>
  2. [自定义函数方法的定义]
  3. {
  4.         方法定义
  5.         return (函数表达式);
  6. }
  7. int main()
  8. {
  9.         函数体;
  10.         调用[自定义函数]
  11.         return 0;
  12. }
复制代码

======================================================================================================
变量的作用域和存储类型

[变量的作用域]

        局部变量 static------------------->在函数或者某个语句块定义的变量就叫局部变量,它的作用域只限于本函数内部或语句块内部,增强了模块的独立性.

        全局变量  extern----------------->函数体外面定义的变量就是叫全局变量,它的作用域是整个源文件.

------------------------------------------------------------------------------------------------------------------------------
[变量的存储类型]

        static        型变量和extern型变量都存储在静态存储区;
       
        register型变量存储在寄存器;

        auto型变量存储在动态存储区

        auto和register只能定义局部变量.

        static可以定义局部变量和内部变量,函数外部和语句块外部的变量默认都是extern全局变量.
---------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------
计算n的阶乘比较用static和不用的的区别
  1. #include <stdio.h>
  2. /*计算n的阶乘比较用static和不用的的区别*/
  3. long funca(int n)//自定义函数funcb() ,形式参数n
  4. {
  5.     static int f = 1;//局部变量f
  6.     f = f * n;
  7.     return (f);
  8. }
  9. long funcb(int n)//自定义函数funca(),形式参数n
  10. {
  11.     int i, m;
  12.     for (i = 1, m = 1; i <= n; i++)//局部变量i,m
  13.     {
  14.         m = m * i;
  15.     }
  16.     return (m);
  17. }
  18. void main()
  19. {
  20.     int y, k;//全局变量y,k
  21.     printf("请输入y的值");
  22.     scanf("%d", &y);
  23.     if (y >= 1)
  24.     {   
  25.         for (k = 1; k <= y; k++)
  26.         {
  27.             printf("funca(%d) = %d\n", k, funca(k));//调用funca()
  28.         }
  29.         printf("\nfuncb(%d) = %d\n", y, funcb(y));//调用funcb()
  30.     }
  31. }
复制代码

------------------------------------------------------------------------------------------------------------------
wj000.jrh' '--stdout=Microsoft-MIEngine-Out-s1zfebzu.bfk' '--stderr=Microsoft-MIEngine-Error-gzc2wyaz.0qk' '--pid=Microsoft-MIEngine-Pid-k44ucn2w.41f' '--dbgExe=E:\MingGW\bin\gdb.exe' '--interpreter=mi'
请输入y的值8
funca(1) = 1
funca(2) = 2
funca(3) = 6
funca(4) = 24
funca(5) = 120
funca(6) = 720
funca(7) = 5040
funca(8) = 40320

funcb(8) = 40320
PS E:\Administrator\Documents\Visual Studio 2019>
========================================================================================================
想知道小甲鱼最近在做啥?请访问 -> ilovefishc.com
回复 支持 反对

使用道具 举报

 楼主| 发表于 2019-5-11 22:38:50 | 显示全部楼层
2019年5月11日22:36:10
[A]
编写函数求C(a,n)组合数
  1. #include <stdio.h>
  2. /*编写函数求C(a,n)组合数*/
  3. long fun_Nn(int n)
  4. {
  5.     long Nn = 1;
  6.     if (n == 0 || n == 1)
  7.     {
  8.         Nn = 1;
  9.     }
  10.     else if (n > 1)
  11.     {
  12.         Nn = n * fun_Nn(n - 1);
  13.     }
  14.     else
  15.     {
  16.         printf("请输入正整数\n");
  17.     }
  18.     return (Nn);
  19. }
  20. long fun_Cn(int n, int k)
  21. {
  22.     long Cn = 1;
  23.     if (n == k || k == 0)
  24.     {
  25.         Cn = 1;
  26.     }
  27.     else if ((n > k) && (k > 0))
  28.     {
  29.         Cn = fun_Nn(n) / (fun_Nn(k) * fun_Nn(n - k));
  30.     }
  31.     else
  32.     {
  33.         printf("数字存在负数或大小顺序错误\n");
  34.     }
  35.     return (Cn);
  36. }
  37. void main()
  38. {
  39.     int a, b;
  40.     printf("请输入组合数\n");
  41.     scanf("%d%d", &a, &b);
  42.     if ((a >= b ) && (b > 0))
  43.     {
  44.         printf("从%d中抽出%d的组合有%d种方法\n", a, b, fun_Cn(a, b));
  45.     }
  46.     else
  47.     {
  48.         printf("数字存在负数或大小顺序错误\n");
  49.     }
  50.    
  51. }
复制代码

--------------------------------------------------------------------------------------------------------------------------------------
9
6
从9中抽出6的组合有84种方法
PS E:\Administrator\Documents\Visual Studio 2019> & 'c:\Users\Administrator\.vscode\extensions\ms-vscode.cpptools-0.23.0\debugAdapters\bin\WindowsDebugLauncher.exe' '--stdin=Microsoft-MIEngine-In-hunyosj3.err' '--stdout=Microsoft-MIEngine-Out-n1kcljc2.sb1' '--stderr=Microsoft-MIEngine-Error-fymrjzzc.z1w' '--pid=Microsoft-MIEngine-Pid-5usb423r.ny5' '--dbgExe=E:\MingGW\bin\gdb.exe' '--interpreter=mi'
请输入组合数
9
45
数字存在负数或大小顺序错误
PS E:\Administrator\Documents\Visual Studio 2019> & 'c:\Users\Administrator\.vscode\extensions\ms-vscode.cpptools-0.23.0\debugAdapters\bin\WindowsDebugLauncher.exe' '--stdin=Microsoft-MIEngine-In-fkxkxuwc.bm2' '--stdout=Microsoft-MIEngine-Out-c45hycvk.3nu' '--stderr=Microsoft-MIEngine-Error-shviffuw.xd5' '--pid=Microsoft-MIEngine-Pid-x55pqnud.khl' '--dbgExe=E:\MingGW\bin\gdb.exe' '--interpreter=mi'
请输入组合数
-9
12
数字存在负数或大小顺序错误
PS E:\Administrator\Documents\Visual Studio 2019>
想知道小甲鱼最近在做啥?请访问 -> ilovefishc.com
回复 支持 反对

使用道具 举报

 楼主| 发表于 2019-5-13 00:47:21 | 显示全部楼层
本帖最后由 bin554385863 于 2019-5-19 08:51 编辑

2019年5月13日00:43:09

第七章 数组
2019-5-15 22:03:21
因为工作耽误了一段时间

[A]

数组的特点:

[1]

[1.1]可以把数组看成是一种所有数据类型相同的变量的集合;
[1.2]同一数组中的元素在内存占据的地址空间是练血的;
[1.3]数组的大小必须在定义时确定,在程序中不可改变;
[1.4]数组名代表的是数组在内存中的首地址.

[1.5]数组的定义:
[存储类型] [数据类型] 数组名[数组大小]
如: static int array[10].


[1.6]数组不能越界否则可能造成不可预料的错误

[2]一维数组

[2.1]数组的初始化

        全元素初始化,如:a[5] = {1,2,3,4,5}        .

        部分元素初始化,如:a[10] = {1,2,3       }.

        手动输入初始化,如:
  1. #include <stdio.h>
  2.         int main ()
  3.         {
  4.                 int i, arr[10];
  5.                 printf("请输入10个数字");
  6.                 for (i = 0; i <10; i++)
  7.                 {
  8.                         scanf("%d", &arr[i]);
  9.                 }
  10.                 for (i = 0; i <10; i++)
  11.                 {
  12.                         printf("arr[%D]  = %d",i,  arr[i]);
  13.                 }
  14.                 return 0;
  15.         }
  16.        
复制代码

----------------------------------------------------------------------------------------------------------------
E:\Administrator\Documents\My C>cmd /C "c:\Users\Administrator\.vscode\extensions\ms-vscode.cpptools-0.23.1\debugAdapters\bin\WindowsDebugLauncher.exe --stdin=Microsoft-MIEngine-In-4cljommd.a54 --stdout=Microsoft-MIEngine-Out-ekgymgu4.p10 --stderr=Microsoft-MIEngine-Error-4h5uvham.11y --pid=Microsoft-MIEngine-Pid-gpa0fdc3.xyd --dbgExe=E:\MinGW\bin\gdb.exe --interpreter=mi "
请输入10个数字
30
20
10
90
40
50
60
20
10
80
arr[0]  = 30
arr[1]  = 20
arr[2]  = 10
arr[3]  = 90
arr[4]  = 40
arr[5]  = 50
arr[6]  = 60
arr[7]  = 20
arr[8]  = 10
arr[9]  = 80

E:\Administrator\Documents\My C>
============================================================================
2019年5月17日07:46:25
设数组有个N个整形元素,将从数组下标为m的元素开始连续n个元素与钱m个元素调换位置

  1. #include <stdio.h>
  2. /*设数组有个N个整形元素,将从数组下标为m的元素开始连续n个元素与钱m个元素调换位置*/
  3. #include <stdlib.h>
  4. #include <time.h>
  5. #define N 10
  6. void move(int t[], int m, int n)
  7. {
  8.     int temp[N], i;
  9.     for (i = 0; i < n; i++)
  10.     {
  11.         temp[i] = t[m + i]; /*将连续m个元素放到临时数组*/
  12.     }
  13.     for (i = 0; i < m; i++)
  14.     {
  15.         t[(m + n - 1) - i] = t[(m - 1) - i]; /*原数组m个元素后移*/
  16.     }
  17.     for (i = 0; i < n; i++)
  18.     {
  19.         t[i] = temp[i]; /*将临时数组的元素移回数组前部*/
  20.     }
  21. }
  22. void main()
  23. {
  24.     int a[N], m, n, i;
  25.     srand(time(NULL));
  26.     for (i = 0; i < N; i++)
  27.     {
  28.         a[i] = rand() % 100; /*随机生成元素值*/
  29.     }
  30.     printf("原始数组的值:\n");
  31.     for (i = 0; i < N; i++)
  32.     {
  33.         printf("%d ", a[i]);
  34.     }
  35.     printf("\n");
  36.     printf("请输入数组移动开始位置\n");
  37.     scanf("%d", &m);
  38.     printf("请输入移动的元素个数\n");
  39.     scanf("%d", &n);
  40.     move(a, m, n);
  41.     for (i = 0; i < N; i++)
  42.     {
  43.         printf("%d ", a[i]);
  44.     }
  45.     printf("\n");
  46. }
复制代码

---------------------------------------------------------------------------------------------------------------------------------------------
Microsoft Windows [版本 6.1.7601]
版权所有 (c) 2009 Microsoft Corporation。保留所有权利。

E:\Administrator\Documents\My C>cmd /C "c:\Users\Administrator\.vscode\extensions\ms-vscode.cpptools-0.23.1\debugAdapters\bin\WindowsDebugLauncher.exe --stdin=Microsoft-MIEngine-In-jpaxwjbs.smr --stdout=Microsoft-MIEngine-Out-lh1m4qoq.dkl --stderr=Microsoft-MIEngine-Error-ysigoo05.cnm --pid=Microsoft-MIEngine-Pid-pn3pyeyt.dhy --dbgExe=E:\MinGW\bin\gdb.exe --interpreter=mi "
原始数组的值:
73 55 42 4 50 79 49 74 61 90
请输入数组移动开始位置
3
请输入移动的元素个数
5
4 50 79 49 74 73 55 42 61 90

E:\Administrator\Documents\My C>
===========================================================================================================

2019年5月18日22:22:34

****
数组冒泡排序
  1. /*冒泡排序*/
  2. #include <stdio.h>
  3. int main()
  4. {
  5.     int a[10] = {1, 32, 66, 585, 55, 865, 55, 99, 12, 0};
  6.     int i, j, temp;
  7.     printf("排序之前\n");
  8.     for (i = 0; i < 10; i++)
  9.     {
  10.         printf("a[%d] = %d\n", i, a[i]);
  11.     }
  12.     printf("\n");
  13.     /*排序算法*/
  14.     for (i = 0; i < 10; i++)
  15.     {
  16.         for (j = 0; j < 9; j++)
  17.         {
  18.             if (a[j] > a[j + 1])
  19.             {
  20.                 temp = a[j];
  21.                 a[j] = a[j + 1];
  22.                 a[j + 1] = temp;
  23.             }
  24.         }
  25.     }
  26.     printf("排序之后\n");
  27.     for (i = 0; i < 10; i++)
  28.     {
  29.         printf("a[%d] = %d\n", i, a[i]);
  30.     }
  31.     printf("\n");

  32.     return 0;
  33. }
复制代码

-------------------------------------------------------------------------------------------------------------------------------------------------------
Microsoft Windows [版本 6.1.7601]
版权所有 (c) 2009 Microsoft Corporation。保留所有权利。

E:\Administrator\Documents\My C>cmd /C "c:\Users\Administrator\.vscode\extensions\ms-vscode.cpptools-0.23.1\debugAdapters\bin\WindowsDebugLauncher.exe --stdin=Microsoft-MIEngine-In-qxsfwl0h.ltp --stdout=Microsoft-MIEngine-Out-zct4enou.qmi --stderr=Microsoft-MIEngine-Error-0c0jyykw.or5 --pid=Microsoft-MIEngine-Pid-w5ovd4z0.ryq --dbgExe=E:\MinGW\bin\gdb.exe --interpreter=mi "
排序之前
a[0] = 1
a[1] = 32
a[2] = 66
a[3] = 585
a[4] = 55
a[5] = 865
a[6] = 55
a[7] = 99
a[8] = 12
a[9] = 0

排序之后
a[0] = 0
a[1] = 1
a[2] = 12
a[3] = 32
a[4] = 55
a[5] = 55
a[6] = 66
a[7] = 99
a[8] = 585
a[9] = 865


E:\Administrator\Documents\My C>
=========================================================================
数组元素个数的确定:
sizeof(a[]) / sizeof(a[0]),用这句代码确定数组元素个数永远不会错,且容易对数组元素个数做修改

  1. /*用sizeof确定数组元素个数*/
  2. #include <stdio.h>
  3. int main()
  4. {
  5.     int arr[] = {6, 85666, 856, 86, 88, 68, 5, 65, 588, 6, 58, 8, 6, 5, 88, 6, 85, 8, 69};
  6.     for (int i = 0; i < (sizeof(arr) / sizeof(arr[0])); i++)
  7.     {
  8.         printf("arr[%d] = %d\n", i, arr[i]);
  9.     }
  10.     printf("\n");

  11.     return 0;
  12. }
复制代码

----------------------------------------------------------------------------------------------------------------------------
Microsoft Windows [版本 6.1.7601]
版权所有 (c) 2009 Microsoft Corporation。保留所有权利。

E:\Administrator\Documents\My C>cmd /C "c:\Users\Administrator\.vscode\extensions\ms-vscode.cpptools-0.23.1\debugAdapters\bin\WindowsDebugLauncher.exe --stdin=Microsoft-MIEngine-In-m3r1o445.4ep --stdout=Microsoft-MIEngine-Out-1nlq35yy.wa5 --stderr=Microsoft-MIEngine-Error-rbxxrd3c.zht --pid=Microsoft-MIEngine-Pid-bf2helce.yah --dbgExe=E:\MinGW\bin\gdb.exe --interpreter=mi "
arr[0] = 6
arr[1] = 85666
arr[2] = 856
arr[3] = 86
arr[4] = 88
arr[5] = 68
arr[6] = 5
arr[7] = 65
arr[8] = 588
arr[9] = 6
arr[10] = 58
arr[11] = 8
arr[12] = 6
arr[13] = 5
arr[14] = 88
arr[15] = 6
arr[16] = 85
arr[17] = 8
arr[18] = 69


E:\Administrator\Documents\My C>
============================================================================================


想知道小甲鱼最近在做啥?请访问 -> ilovefishc.com
回复 支持 反对

使用道具 举报

 楼主| 发表于 2019-5-19 09:58:41 | 显示全部楼层
本帖最后由 bin554385863 于 2019-5-19 20:18 编辑

2019年5月19日09:43:44
[4]
字符数组

[4.1.1]
        字符数组逐一元素初始化,末尾要加\0表示结束,如:
char-array[] = {'a','d','f'.........\0}.
[4.1.2]
        给字符数组赋值字符串,如:
char word[11]={"hello world"}
char word[    ]={"hello world"}
char word[    ]="hello world"

[4.2]

scanf("%c", &array[n])        逐一赋值;

scanf("%s", &array)                字符串赋值.

[4.3]
        用scanf()输入字符串时空格和换行符会作为字符串之间的符号,且不需要用双引号括起来

[4.4]
        gets()函数能够读取所有字符,回车结束.

scanf()
  1. /*scanf()和gets()函数的不同*/
  2. #include <stdio.h>
  3. int main()
  4. {
  5.     char arr1[50], arr2[50];
  6.     printf("请输入字符串\n");
  7.     scanf("%s", &arr1);
  8.     printf("arr1 = %s\n", arr1);
  9.     return 0;
  10. }
复制代码

-----------------------------------------------------------------------------------------------------------
E:\Administrator\Documents\My C>cmd /C "c:\Users\Administrator\.vscode\extensions\ms-vscode.cpptools-0.23.1\debugAdapters\bin\WindowsDebugLauncher.exe --stdin=Microsoft-MIEngine-In-ah42eebq.s20 --stdout=Microsoft-MIEngine-Out-xiam5lkz.wfy --stderr=Microsoft-MIEngine-Error-wzd3krlp.wku --pid=Microsoft-MIEngine-Pid-4h5fg1um.gt5 --dbgExe=E:\MinGW\bin\gdb.exe --interpreter=mi "
请输入字符串
hello world
arr1 = hello

E:\Administrator\Documents\My C>
-------------------------------------------------------------------------------------------------------------------
gets()
  1. /*scanf()和gets()函数的不同*/
  2. #include <stdio.h>
  3. int main()
  4. {
  5.     char arr1[50], arr2[50];
  6.     printf("请输入字符串\n");
  7.    
  8.     gets(arr2);
  9.     printf("arr2 = %s\n", arr2);
  10.     return 0;
  11. }
复制代码

--------------------------------------------------------------------------------------------------------
E:\Administrator\Documents\My C>cmd /C "c:\Users\Administrator\.vscode\extensions\ms-vscode.cpptools-0.23.1\debugAdapters\bin\WindowsDebugLauncher.exe --stdin=Microsoft-MIEngine-In-uq1s4lgi.oyz --stdout=Microsoft-MIEngine-Out-ukrnz2ix.yl5 --stderr=Microsoft-MIEngine-Error-ulxtsyhz.i4q --pid=Microsoft-MIEngine-Pid-ue5kjfxg.bf0 --dbgExe=E:\MinGW\bin\gdb.exe --interpreter=mi "
请输入字符串
hello world
arr2 = hello world

E:\Administrator\Documents\My C>
========================================================================================================
字符数组操作函数

[5.1]

strcopy(array[], "strings")        将""string""字符串复制到字符数组array[].

strcopy(arr1[], arr2[])        将字符数组arr2[]复制到arr1[].arr2不能小于arr1.

[5.2]

strcat(arr1, arr2)        将arr2拼接到arr1尾部.

------------------------------------------------------------------------------------------------------------------------------------
  1. /*字符数组复制函数strcpy()和字符数组拼接函数strcat()*/
  2. #include <stdio.h>
  3. #include <string.h>
  4. int main()
  5. {
  6.     char arr1[] = "WindowsMcrosoft", arr2[] = "abcde";
  7.     strcpy(arr1, arr2);
  8.     printf("arr1:\n%s\n", arr1);
  9.     strcpy(arr2, "zxcv");
  10.     printf("arr2:\n%s\n", arr2);
  11.     strcat(arr1, arr2);
  12.     printf("拼接arr2到arr1:\n%s\n", arr1);
  13.     return 0;
  14. }
复制代码

-------------------------------------------------------------------------------------------------------------------------------------------
Microsoft Windows [版本 6.1.7601]
版权所有 (c) 2009 Microsoft Corporation。保留所有权利。

E:\Administrator\Documents\My C>cmd /C "c:\Users\Administrator\.vscode\extensions\ms-vscode.cpptools-0.23.1\debugAdapters\bin\WindowsDebugLauncher.exe --stdin=Microsoft-MIEngine-In-u2vdhgyq.n3b --stdout=Microsoft-MIEngine-Out-f2pcwkwm.5kh --stderr=Microsoft-MIEngine-Error-44nz20sp.gll --pid=Microsoft-MIEngine-Pid-4pqkbaf5.f5p --dbgExe=E:\MinGW\bin\gdb.exe --interpreter=mi "
arr1:                        strcpy()会把\0也复制过去因此改变过后的arr[1]后面的字符无法输出,改变后的arr1[]="abcde\0sMicrosoft"
abcde
arr2:
zxcv
拼接arr2到arr1:
abcdezxcv

E:\Administrator\Documents\My C>
----------------------------------------------------------------------------------------------------------------------------------------

[5.4]
       
        字符串比较函数:strcmp(字符串1 , 字符串2)
字符串1 = 2        返回值=0
字符串1 < 2        返回值=随机负整数
字符串1 > 2        返回值=随机正整数
--------------------------------------------------------------------------------------------------------------------------------------

[5.5]字符串大小写转换函数

str="a................................n"

strupr(str)        转换大写

strlwr(str)                转换小写.

---------------------------------------------------------------------------------------------------------------------------------------------------
程序范例
从键盘输入八种商品价格,求其最高价最低价和平均价并把高于平均价的 商品打印出来
  1. /*从键盘输入八种商品价格,求其最高价最低价和平均价并把高于平均价的 商品打印出来*/
  2. /*从键盘输入八种商品价格,求其最高价最低价和平均价并把高于平均价的 商品打印出来*/
  3. #include <stdio.h>
  4. float highestp(float p[]) //寻找最大值
  5. {
  6.     int i;
  7.     float hp;
  8.     hp = p[0];
  9.     for (i = 0; i < 7; i++)
  10.     {
  11.         if (hp < p[i + 1])
  12.         {
  13.             hp = p[i + 1];
  14.         }
  15.     }
  16.     return (hp);
  17. }

  18. float lowestp(float p[]) //寻找最小值
  19. {
  20.     int i;
  21.     float lp;
  22.     lp = p[0];
  23.     for (i = 0; i < 7; i++)
  24.     {
  25.         if (lp > p[i + 1])
  26.         {
  27.             lp = p[i + 1];
  28.         }
  29.     }
  30.     return (lp);
  31. }

  32. float average(float p[]) //计算平均值
  33. {
  34.     float sum = 0.0, aver;
  35.     for (int i = 0; i < 8; i++)
  36.     {
  37.         sum = sum + p[i];
  38.         aver = sum / 8;
  39.     }
  40.     return (aver);
  41. }
  42. void readp(float p[8])
  43. {
  44.     for (int i = 0; i < 8; i++)
  45.     {
  46.         scanf("%f", &p[i]);
  47.     }
  48.     printf("物价是\n");
  49.     for (int i = 0; i < 8; i++)
  50.     {
  51.         printf("%6.2f\t", p[i]);
  52.     }
  53.     printf("\n");
  54.     return;
  55. }
  56. int ppp(float p[8], float aver)
  57. {
  58.     for (int i = 0; i < 8; i++)
  59.     {
  60.         if (p[i] > aver)
  61.         {
  62.             printf("%6.2f", p[i]);
  63.         }
  64.     }
  65.     return 0;
  66. }
  67. void main()
  68. {
  69.     float hp, lp, ap;
  70.     float p[8];
  71.     readp(p);
  72.     ap = average(p);
  73.     hp = highestp(p);
  74.     lp = lowestp(p);
  75.     printf("最高价 = %6.2f\n最低价 = %6.2f\n平均价 = %6.2f\n", hp, lp, ap);
  76.     printf("高于平均价的价格");
  77.     ppp(p, average(p));
  78. }
复制代码

---------------------------------------------------------------------------------------------------------------------
E:\Administrator\Documents\My C>cmd /C "c:\Users\Administrator\.vscode\extensions\ms-vscode.cpptools-0.23.1\debugAdapters\bin\WindowsDebugLauncher.exe --stdin=Microsoft-MIEngine-In-ltojbysc.lwo --stdout=Microsoft-MIEngine-Out-qth3ge1g.bm5 --stderr=Microsoft-MIEngine-Error-s50debj0.4oc --pid=Microsoft-MIEngine-Pid-eivff3sa.2cr --dbgExe=E:\MinGW\bin\gdb.exe --interpreter=mi "
23
36
65
15
2
45
32
66
物价是
23.00   36.00   65.00   15.00    2.00   45.00   32.00   66.00
最高价 =  66.00
最低价 =   2.00
平均价 =  35.50
高于平均价的价格 36.00 65.00 45.00 66.00
E:\Administrator\Documents\My C>
=============================================================================
想知道小甲鱼最近在做啥?请访问 -> ilovefishc.com
回复 支持 反对

使用道具 举报

发表于 2019-5-21 16:53:11 | 显示全部楼层
你好 请问你有这方面的视频完整的视频吗 我在论坛里发现好多0基础C语言都打开不了.有的话可否发份链接给我 或者发送至我的邮箱 690135978@qq.com 多谢了
想知道小甲鱼最近在做啥?请访问 -> ilovefishc.com
回复 支持 反对

使用道具 举报

 楼主| 发表于 2019-5-21 20:23:55 | 显示全部楼层
B站有老甲鱼的视频
还有好多其他人的视频
想知道小甲鱼最近在做啥?请访问 -> ilovefishc.com
回复 支持 反对

使用道具 举报

发表于 2019-5-21 20:42:41 | 显示全部楼层
#include<iostream>
#include<string>
using namespace std;
int main()
{
   string nums;
    int target;
    int i,j;
    cin>>nums;
    cin>>target;
    for(i=0;i<nums.size()-1;i++)
         for(j=i;j<nums.size()-1;j++)
       if(nums[i]+nums[j]==target)
           cout<<i<<j<<endl;
   return 0;
}

为什么不能输出
想知道小甲鱼最近在做啥?请访问 -> ilovefishc.com
回复 支持 反对

使用道具 举报

 楼主| 发表于 2019-5-21 20:54:07 | 显示全部楼层
小快艇 发表于 2019-5-21 20:42
#include
#include
using namespace std;

你这是C#吧

我学的是C
想知道小甲鱼最近在做啥?请访问 -> ilovefishc.com
回复 支持 反对

使用道具 举报

您需要登录后才可以回帖 登录 | 立即注册

本版积分规则

小黑屋|手机版|Archiver|鱼C工作室 ( 粤ICP备18085999号-1 | 粤公网安备 44051102000585号)

GMT+8, 2024-3-28 17:39

Powered by Discuz! X3.4

© 2001-2023 Discuz! Team.

快速回复 返回顶部 返回列表