当前位置 : 主页 > 编程语言 > java >

C语言再学习-002-+-*/><……

来源:互联网 收集:自由互联 发布时间:2022-09-02
案例1,两数相乘 #include stdio.h main () { int x , y , m ; printf ( "Please input x and y\n" ); scanf ( "%d%d" , x , y ); m = x * y ; printf ( "%d * %d = %d\n" , x , y , m ); } 输入,x和y,输出m为x和y的乘积。 其中*可以


案例1,两数相乘 

#include <stdio.h>
main()
{
int x,y,m;
printf("Please input x and y\n");
scanf("%d%d",&x,&y);
m=x*y;
printf("%d * %d = %d\n",x,y,m);
}

输入,x和y,输出m为x和y的乘积。

其中*可以更换为+-/等,int也可以用float。


案例2,比较两数大小,输出max。

#include <stdio.h>
main()
{
float x,y,c;
printf("Please input x and y:\n");
scanf("%f%f",&x,&y);
c=x>y?x:y;
printf("MAX of (%f,%f) is %f",x,y,c);
}

比大小或者是否相同,都可以用类似方法实现。 


案例3:

#include <stdio.h>
main()
{
char ch,nch;
int count;
int k;

printf("Please input a string with a # in the end.\n");
scanf("%c",&ch);
while(ch != '#')
{
if(ch >= '0' && ch <= '9')
{
count = ch-'0'+1;
scanf("%c",&nch);
for(k=0;k<count;k++)
printf("%c",nch);
}
else
printf("%c",ch);
printf(" ");
scanf("%c",&ch);
}
printf("#\n");
}

字符串输入和输入,结束符为#。 


案例4:

#include <stdio.h>
void main()
{
printf("The bytes of the variables are:\n");
printf("int:%d bytes\n",sizeof(int));

printf("char:%d byte\n",sizeof(char));

printf("short:%d bytes\n",sizeof(short));

printf("long:%d bytes\n",sizeof(long));

printf("float:%d bytes\n",sizeof(float));

printf("double:%d bytes\n",sizeof(double));

printf("long double:%d bytes\n",sizeof(long double));
getchar();

}

显示各种变量所占用的字节数。

32位/64位系统是否有差异? 


案例5:

#include <stdio.h>
main()
{
int a=5,b,c,i=10;
b=a++;
c=++b;

printf("a = %d, b = %d, c = %d\n",a,b,c);
printf("i,i++,i++ = %d,%d,%d\n",i,i++,i++);
printf("%d\n",++i);
printf("%d\n",--i);
printf("%d\n",i++);
printf("%d\n",i--);
printf("%d\n",-i++);
printf("%d\n",-i--);
getchar();
}

所谓++,--,前前后后的各种效果。


案例6:

#include <stdio.h>
main()
{
int i,j,n;
long sum=0,temp=0;

printf("Please input a number to n:\n");
scanf("%d",&n);
if(n<1)
{
printf("The n must no less than 1!\n");
return;
}

for(i=1;i<=n;i++)
{
temp=0;
for(j=1;j<=i;j++)
temp+=j;
sum+=temp;
}
printf("The sum of the sequence(%d) is %d\n",n,sum);
getchar();
getchar();
}

简单数列求和,还可以用递归方式实现。

怎么做?


猜猜如下两段程序是啥功能?

#include <stdio.h>
#include <conio.h>
void main(void)
{
int i,j,x,y;
clrscr();
printf("\n\n multiplication table \n\n");
x=9;
y=5;
for(i=1;i<=9;i++)
{
gotoxy(x,y);
printf("%2d ",i);
x+=3;
}
x=7;
y=6;
for(i=1;i<=9;i++)
{
gotoxy(x,y);
printf("%2d ",i);
y++;
}
x=9;
y= 6;
for(i=1;i<=9;i++)
{
for(j=1;j<=9;j++)
{
gotoxy(x,y);
printf("%2d ",i*j);
y++;
}
y-=9;
x+=3;
}
printf("\n\n");
}
#include <stdio.h>
#include <conio.h>
void main()
{
int Password=0,Number=0,price=58,i=0;
clrscr();
printf("\n====This is a Number Guess Game!====\n");
while( Password != 1234 )
{
if( i >= 3 )
return;
i++;
puts("Please input Password: ");
scanf("%d",&Password);
}

i=0;
while( Number!=price )
{
do{
puts("Please input a number between 1 and 100: ");
scanf("%d",&Number);
printf("Your input number is %d\n",Number);
}while( !(Number>=1 && Number<=100) );
if( Number >= 90 )
{
printf("Too Bigger! Press any key to try again!\n");
}
else if( Number >= 70 && Number < 90 )
{
printf("Bigger!\n");
}
else if( Number >= 1 && Number <= 30 )
{
printf("Too Small! Press any key to try again!\n");
}
else if( Number > 30 && Number <= 50 )
{
printf("Small! Press any key to try again!\n");
}
else
{
if( Number == price )
{
printf("OK! You are right! Bye Bye!\n");
}
else if( Number < price )
{
printf("Sorry,Only a little smaller! Press any key to try again!\n");

}
else if( Number > price )
printf(" Sorry, Only a little bigger! Press any key to try again!\n");
}
getch();
}
}

是乘法表和猜数字吗??????


上一篇:选择屏幕执行完成返回后,屏幕出错
下一篇:没有了
网友评论