前往小程序,Get更优阅读体验!
立即前往
首页
学习
活动
专区
工具
TVP
发布
社区首页 >专栏 >C语言再学习-002-+-*/><……

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

作者头像
zhangrelay
发布2022-09-23 15:53:50
1720
发布2022-09-23 15:53:50
举报
文章被收录于专栏:机器人课程与技术

案例1,两数相乘 

代码语言:javascript
复制
#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。

代码语言:javascript
复制
#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:

代码语言:javascript
复制
#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:

代码语言:javascript
复制
#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:

代码语言:javascript
复制
#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:

代码语言:javascript
复制
#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();
}

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

怎么做?


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

代码语言:javascript
复制
#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");
}

代码语言:javascript
复制
#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();
	}
}

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


本文参与 腾讯云自媒体同步曝光计划,分享自作者个人站点/博客。
原始发表:2022-08-23,如有侵权请联系 cloudcommunity@tencent.com 删除

本文分享自 作者个人站点/博客 前往查看

如有侵权,请联系 cloudcommunity@tencent.com 删除。

本文参与 腾讯云自媒体同步曝光计划  ,欢迎热爱写作的你一起参与!

评论
登录后参与评论
0 条评论
热度
最新
推荐阅读
领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档