C语言复习6

二维数组

类型名 数组名【行长度】 【列长度】

#include<stdio.h>
#define MAXM 6
#define MAXN 6
int main(void)
{
	int col,row,i,j,m,n;
	int a[MAXM][MAXN];
	
	printf("enter number of m ,n\n");
	scanf("%d %d",&m,&n);
	
	printf("please enter number %d :",m*n);
	
	for(i=0;i<m;i++)
	{
		for(j=0;j<n;j++)
		{
			scanf("%d",&a[i][j]);
		}
	}
	
	col=row=0;
	
	for(i=0;i<m;i++)
	{
		for(j=0;j<n;j++)
		{
			if(a[col][row]<a[i][j])
			{
				col=i;
				row=j;
			}
		}
	}
	printf("the bigst number is a[%d][%d]:%d",col,row,a[col][row]);
	return 0;
}
#include<stdio.h>
#define MAXM 6
int main(void)
{
	int i,j,n,temp;
	int a[MAXM][MAXM];
	
	printf("please enter yournumber n:");
	scanf("%d",&n);
	for(i=0;i<n;i++)
	{
		for(j=0;j<n;j++)
		{
			a[i][j]=i*n+j+1;
			
		}
	}
	
	for(i=0;i<n;i++)
	{
		for(j=0;j<n;j++)
		{
			if(i<j)
			{
				temp=a[i][j];
				a[i][j]=a[j][i];
				a[j][i]=temp;
			}
		}
	}
	for(i=0;i<n;i++)
	{
		for(j=0;j<n;j++)
		{
			printf("%4d",a[i][j]);
		}
		printf("\n");
	}
	return 0;
}

 

#include<stdio.h>
int main(void)
{
	int day_of_day(int year,int month,int day);
	int year,month,day;
	
	printf("please enter year,month and day");
	scanf("%d %d %d",&year,&month,&day);
	
	printf("it is the %d days",day_of_day(year,month,day));
 } 
 
 int day_of_day(int year,int month,int day)
 {
 	int k,leap;
 	int tab[2][13]={{0,31,28,31,30,31,30, 31 ,31, 30, 31, 30, 31},{0,31,29,31,30,31,30,31,31,30,31,30,31 }};
 	leap=(year%4==0&&year%100!=0||year%400==0);//C语言中真为1,假为0, 
 	for(k=0;k<month;k++)
 	{
 		day=day+tab[leap][k];
	 }
	 return day;
 }

字符串:

在c语言中,将字符串作为字符数组来处理,'\0'是字符串结束语。

其有效长度是有效字符的长度,而不是数组的长度。故,数组长度至少是有效长度+1.

①    ‘0’    代表    字符0  ,对应ASCII码值为   0x30 (也就是十进制 48)

②    '\0'    代表     空字符(转义字符)【输出为空】, 对应ASCII码值为   0x00(也就是十进制 0), 用作字符串结束符

③   0    代表     数字0,  若把 数字0 赋值给 某个字符,对应ASCII码值为    0x00(也就是十进制0) 

#include<stdio.h>
#define MAXN 80
#define M 26
int main(void)
{
	int i,offest;
	char str[MAXN];
		printf("enter a string:");
	i=0;
	while((str[i]=getchar())!='\n')
	{
		i++;
	}
	str[i]='\0';
	 
	printf("please enter offset: ");
	scanf("%d",&offest);
	 
	if(offest>=M)
	{
	 	offest=offest%M;
	} 
	
	for(i=0;str[i]!='\0';i++)
	{
		if(str[i]<='A'&&str[i]<='Z')
		{
			if((str[i]-'A'+offest)<M)
			{
				str[i]=str[i]+offest;
			}
			else
			{
				str[i]=str[i]-(M-offest);
			}
		}
		if(str[i]>='a'&&str[i]<='z')
		{
			if((str[i]-'a'+offest)<M)
			{
				str[i]=str[i]+offest;
			}
			else
			{
				str[i]=str[i]-(M-offest);
			}
		}
	}
	printf("after being encrypted:");
	for(i=0;str[i]!='\0';i++)
	{
		putchar(str[i]);
	}
	return 0;
	  
}
#include<stdio.h>
#define MAXN 80
#define M 26
int main(void)
{
	int i,offest;
	char str[MAXN];
		printf("enter a string:");
	i=0;
	while((str[i]=getchar())!='\n')
	{
		i++;
	}
	str[i]='\0';
	 
	printf("please enter offset: ");
	scanf("%d",&offest);
	 
	if(offest>=M)
	{
	 	offest=offest%M;
	} 
	
	for(i=0;str[i]!='\0';i++)
	{
		if(str[i]<='A'&&str[i]<='Z')
		{
			if((str[i]-'A'+offest)<M)
			{
				str[i]=str[i]+offest;
			}
			else
			{
				str[i]=str[i]-(M-offest);
			}
		}
		if(str[i]>='a'&&str[i]<='z')
		{
			if((str[i]-'a'+offest)<M)
			{
				str[i]=str[i]+offest;
			}
			else
			{
				str[i]=str[i]-(M-offest);
			}
		}
	}
	printf("after being encrypted:");
	for(i=0;str[i]!='\0';i++)
	{
		putchar(str[i]);
	}
	return 0;
	  
}

 

  字符转换:

number=number*10+str[i]-'0'

也可用C中的转换函数:

● atof():将字符串转换为双精度浮点型值。

● atoi():将字符串转换为整型值。

● atol():将字符串转换为长整型值。

#include<stdio.h>
#define MAXN 10
int main(void)
{
	int i,number;
	char str [MAXN];
	
	printf("enter a string: ");
	i=0;
	while((str[i]=getchar())!='\n')
	{
		i++;
	}
	str[i]='\0';
	
	number=0;
	for(i=0;str[i]!='\0';i++)
	{
		if(str[i]>='0'&& str[i]<='9')
		{
			number = number*10 + str[i]-'0';
		}
	}
	printf("digit=%d\n",number);
	
	return 0;
}


 

  • 1
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值