|
楼主 |
发表于 2019-5-3 14:31:42
|
显示全部楼层
本帖最后由 bin554385863 于 2019-5-5 16:39 编辑
2019年5月3日14:31:42
2-15-0
/*三目运算符和逻辑运算符的使用。或‖--->一真全真;且&&(书里翻译叫做与,我更喜欢叫它且)一假全假。*/
#include<stdio.h>
#include<stdlib.h>
void main()
{
int a;
int b,c;
int d,e,f;
scanf("%d\n%d",&a,&b);
c=(a>=b)?a:b;
d=a&&!((a-b)>0);
f=!(a-b);
printf("max of %d and %d is:%d\t\n",a,b,c);
printf("%d&&(!(%d-%d)>0)=%d\t\n",a,a,b,d);
printf("!(%d-%d)=%d\t\n",a,b,f);
}
==========================================================================================================
结果
> & '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
/*了解数据输出的常用格式*/
#include<stdio.h>
void main()
{
int a=1234, b=23456;
long c=345678;
unsigned int d=4567890;
char e='A';
double f=314.15926535898;
printf("\t1---->Output data in default format:\n\t");//按默认格式输出数据
printf("a=%d, b=%d, c=%ld, d=%u\n\t",a,b,c,d);
printf("e=%c, f=%f, f=%e\n\t------------------------------------\n\t",e,f,f);
printf("2---->Output data by specified width:\n\t");//按指定宽度输出字符
printf("e=%3c, e=%03c\n\t---------------------------------------\n\t",e,e);
printf("3---->Output integers by specified width\n\t");//按指定宽度输出整形数
printf("a=%3d, b=%4d, c=%4ld, d=%5u\n\t",a,b,c,d);
printf("a=%7d, b=%7d, c=%8ld, d=%9u\n\t",a,b,c,d);
printf("a=%07d, b=%07d, c=%08ld, d=%09u\n\t--------------------------------------------\n\t",a,b,c,d);
printf("4---->Output floating-point type number by specified width");//按指定宽度输出浮点型数
printf("f=%8.2f, f=%12.2e\n\t",f,f);
printf("f=%4.2f, f=%4.2e\n\t",f,f);
}
============================================================================================================
结果
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
//字符输入输出函数的用法
#include<stdio.h>
#include<conio.h>
void main()
{
char ch1,ch2;
ch1=getch();//读取键盘输入的字符存在缓冲区且不在屏幕显示
ch2=getchar();//读取键盘输入的字符存在缓冲区且显示在屏幕上
putch(ch1);//从缓冲区读取数据并显示在屏幕上
printf(\n);
putchar(ch2);//将ch2的值输出在屏幕上
printf("\nch1=%c\nch2=%c\n",ch1,ch2);
}
========================================================================================================================
> & '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
计算两个复数的差#include<stdio.h>
void main()
{
float a,b,x,y;
printf("Please input the a and b of first complex:\n");
scanf("%f\n%f",&a,&b);
printf("The first complex is:(%5.2f+%5.2fi)",a,b);
printf("Please input the x and y of second complex:\n");
scanf("%f\n%f",&x,&y);
printf("The second complex is:(%5.2f+%5.2fi)",x,y);
printf("Diffrence is :\n(%6.2f+%6.2fi)\n",a-x,b-y);
}
==========================================================================================================
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
//求三个数的平均值
#include<stdio.h>
void main()
{
float a,b,c,average;
printf("please input three numbers:\n");
scanf("%f%f%f",&a,&b,&c);
average=(a+b+c)/3;
printf("The average value is:%7.2f\n",average);
}
=======================================================================================================
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,输入两个数求它们的和差商积余。
#include<stdio.h>
void main()
{
int x,y;
printf("please input two numbers\n");
scanf("%d%d",&x,&y);//MMP总是忘记地址操作符
printf("%d-%d=%d\n",x,y,x-y);
printf("%d+%d=%d\n",x,y,x+y);
printf("%d/%d=%d---%d\n",x,y,x/y,x%y);
printf("%d*%d=%d",x,y,x*y);
}
============================================================================================================
> & '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>
*********************************************************************************************************
练习
输入三角形的三条边求它的面积//输入三条三角形边长,计算它的面积
#include<stdio.h>
#include<math.h>
void main()
{
float a,b,c,x,y,Area;
scanf("%f%f%f",&a,&b,&c);
if (a<=0||b<=0||c<=0)
{
printf("你家的三角形边长是负数的吗?");
}
else if ((a+b)>c&&(a+c)>b&&(b+c)>a)
{
x=(a*a+b*b-c*c)/(2*a*b);
y=sqrt(1-x*x);
Area=(a*b*y)/2;
printf("The area of a triangle is:%.3f",Area);
}
else
{
printf("你确定这能画出三角形?");
}
}
|
|