您的位置:首页 > 编程语言 > C语言/C++

作业《IOS_C语言》一维数组、数组排序、字符数组

2015-08-29 11:00 627 查看
#import <Foundation/Foundation.h>

#define SIZE 10

int main(int argc, const char * argv[]) {

//1. (*)先调试,调试成功后抄写下列程序

// int a[SIZE]={0};

// for (int i=0; i<SIZE;i++) {

// a[i]=i+10;

// printf("a[%d]=%d\n",i,a[i]);

// }

//

//2. (*)将第一题中的数组a反向输出

// int a[SIZE]={0};

// for (int i=SIZE-1;i>=0;i--) {

// a[i]=i+10;

// printf("a[%d]=%d\n",i,a[i]);

// }

//3. (*)对第一题中的数组进行求和操作,打印计算结果

// int a[SIZE]={0};

// int count;

// for (int i=0; i<SIZE; i++) {

// a[i]=i+10;

// count+=a[i];

// printf("a[%d]=%d\n",i,a[i]);

// }

// printf("count=%d\n",count);

// 4. (**)计算第一题数组连减,打印计算结果

// int a[SIZE]={0};

// int count=a[0];//第一个作为减数

// for (int i=0; i<SIZE; i++) {

// a[i]=i+10;

// count-=a[i];

// printf("a[%d]=%d\n",i,a[i]);

// }

// printf("count=%d\n",count);

//5. (**)随机产生20个10~50的正整数存放到数组中,并求数组中的多有元素最大值、最小值、平均值及各元素之和

// unsigned int s1[20]={0};

// for (int i=0; i<20; i++) {

// s1[i]=arc4random()%(50-10+1)+10;

// }

// for (int i=0; i<20; i++) {

// printf("s1[%d]=%d\n",i,s1[i]);

// }

// //最大值

// int max=0;

// for (int i=0; i<20; i++) {

// if (max<s1[i]) {

// max=s1[i];

// }

// }

// printf("max=%d\n",max);

// //最小值

// int min=s1[0];

// for (int i=1; i<20; i++) {

// if (min>s1[i]) {

// min=s1[i];

// }

// }

// printf("min=%d\n",min);

// //和

// int sum=0;

// for (int i=1; i<20; i++) {

// sum+=s1[i];

// }

// printf("sum=%d\n",sum);

// //平均值

// int average=sum/20.0;

//(20.0就不会有问题,因为只能取到整数)

// printf("average=%d\n",average);

//6. 编写一个程序,输入两个包含5个元素的数组,先将两个数组升序排序,然后将这两个数组合并成一个升序数组(最好是定义一个新的数组来装新生成的数组)

// int const n=5;

// int a1
={20,23,12,45,8};

// int a2[n+5]={56,334,23,654,34};

// for (int i=0; i<n-1; i++) {

// for (int j=0; j<n-1-i; j++) {

// if (a1[j]>a1[j+1]) {

// int temp=a1[j];

// a1[j]=a1[j+1];

// a1[j+1]=temp;

// }

// }

// }

// for (int i=0; i<n-1; i++) {

// for (int j=0; j<n-1-i; j++) {

// if (a2[j]>a2[j+1]) {

// int temp=a2[j];

// a2[j]=a2[j+1];

// a2[j+1]=temp;

// }

// }

// }

//

// for (int i=0; i<n; i++) {

// printf("%d ",a1[i]);

// }

// printf("\n");

// for (int i=0; i<n; i++) {

// printf("%d ",a2[i]);

// }

// printf("\n");

// //两个数组组合成一个再升序排序

// for(int i=0;i<5;i++){

// a2[5+i]=a1[i];//把数组a1连给a2

// }

// for (int i=0; i<n+5; i++) {

// printf("%d ",a2[i]);//a2数组就增加了a1的部分

// }

// for (int i=0; i<n+5-1; i++) {//新的a2数组重新执行冒泡排序

// for (int j=0; j<n+5-1-i; j++) {

// if (a2[j]>a2[j+1]) {

// int temp=a2[j];

// a2[j]=a2[j+1];

// a2[j+1]=temp;

// }

// }

// }

// printf("\n");

// //输出冒泡排序后的a2数组

// for (int i=0; i<n+5; i++) {

// printf("%d ",a2[i]);

// }

// printf("\n");

//7. (***)给定某年某月某日,输出其为这一年的第几天。

//方法一:

// int year,month,day,date=0;

// int TodayYear;

// printf("Please input TodayYear:");

// scanf("%d",&TodayYear);

// for (int i;;i++ ) {

// printf("Please input a date:");

// scanf("%d%d%d",&year,&month,&day);

// if (year<=TodayYear) {

// if (year%400==0||(year%4==0&&year%10!=0)) {

// if (month==2) {

// if (day>=1&&day<=29) {

// printf("您输入的是润年的2月份,输入的日期合法\n");

// break;

// }else{

// printf("您输入的是润年的2月份,输入的日期不合法!请输入的日期范围是1~29\n");

// }

// }else if(month==1||month==3||month==5||month==7||month==8||month==10||month==12){

// if (day>=1&&day<=31) {

// printf("您输入的是大月份,输入的日期合法\n");

// break;

// }else{

// printf("您输入的是大月份,输入的日期不合法!请输入的日期范围是1~31\n");

//

// }

// }else if(month==4||month==6||month==9||month==11){

// if (day>=1&&day<=30) {

// printf("您输入的是小月份,输入的日期合法\n");

// break;

// }else{

// printf("您输入的是小月份,输入的日期不合法!请输入的日期范围是1~30\n");

// }

// }else{

// printf("请输入合法的月份,范围1~12月\n");

// break;

// }

//

// }else {

// if (month==2) {

// if (day>=1&&day<=28) {

// printf("您输入的是平年的2月份,输入的日期合法\n");

// break;

// }else{

// printf("您输入的是平年的2月份,输入的日期不合法!请输入的日期范围是1~28\n");

// }

// }else if(month==1||month==3||month==5||month==7||month==8||month==10||month==12){

// if (day>=1&&day<=31) {

// printf("您输入的是大月份,输入的日期合法\n");

// break;

// }else{

// printf("您输入的是大月份,输入的日期不合法!请输入的日期范围是1~31\n");

// }

// }else if(month==4||month==6||month==9||month==11){

// if (day>=1&&day<=30) {

// printf("您输入的是小月份,输入的日期合法\n");

// break;

// }else{

// printf("您输入的是小月份,输入的日期不合法!请输入的日期范围是1~30\n");

// }

// }else{

// printf("请输入合法的月份,范围1~12月\n");

// }

//

// }

//

// }else{

// printf("请输入合法的年份,必须小于等于今年%d\n!",TodayYear);

// }

// }

//

// int a[][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}};

// if (month==1&&day>0&&day<31) {

// date=day;

// }else if(month>1&&month<13){

// for (int i=1; i<month; i++) {

// if (year%400==0||(year%4==0&&year%10!=0)) {

// date+=a[0][i];

// }else{

// date+=a[1][i];

// }

// }

// date+=day;

// }else{

// printf("error!");

// }

// printf("date=%d\n",date);

//方法2:

//第七题:给定某年某月某日,输出其为这一年的第几天

int year=0,month,day=0,sum=0;

int a[12]={31,28,31,30,31,60,31,31,30,31,30,31};

printf("请输入年份:\n");

for (; ; ) {

scanf("%d",&year);

if (year>=0&&year<=2015) {

break;

}

printf("请重新输入年份:\n");

}

printf("请输入月份:\n");

for (; ; ) {

scanf("%d",&month);

if (month>=1&&month<=12) {

break;

}

printf("请重新输入月份:\n");

}

printf("请输入日子:\n");

for (; ; ) {

scanf("%d",&day);

switch (month) {

case 1:

case 3:

case 5:

case 7:

case 8:

case 10:

case 12:

if (day>=1&&day<=31) {

break;

}

printf("请重新输入日子:\n");

continue;

case 2:

if (day>=1&&day<=28) {

break;

}else if ((year%400==0||((year%4==0)&&(year%100!=0)))&&day==29){

break;

}

printf("请重新输入日子:\n");

continue;

case 4:

case 6:

case 9:

case 11:

if (day>=1&&day<=30) {

break;

}

printf("请重新输入日子:\n");

continue;

}

break;

}

for (int i=0; i<month-1; i++) {

sum=sum+a[i];

}

if ((year%400==0||(year%4==0&&year%100!=0))&&day==29) {

sum=sum+1;

}

sum=sum+day;

printf("%d\n",sum);

//8. (**)编写整型值数组排序程序(冒泡排序--升序)int a[] = {3,-9,32,77,63,-24,14,0,21,45}

// int a[] = {3,-9,32,77,63,-24,14,0,21,45};

// int count=sizeof(a)/4;//这里是把整型数组当成是字符型来计算字节数,并且整型是占4个字节的,而字符型则是占一个字节的

// printf("count=%d\n",count);

// for (int i=0; i<count-1; i++) {

// for (int j=0; j<count-1-i; j++) {

// if (a[j]>a[j+1]) {

// int temp=a[j];

// a[j]=a[j+1];

// a[j+1]=temp;

// }

// }

// }

// for (int i=0; i<count; i++) {

// printf("a[%d]=%d ",i,a[i]);

// }

// printf("\n");

//9、(***)找出下列整型数组中最大和最小值及其所在位置i。int a[] = {5,-9,32,77,64,-24,14,0,21,45};

// int a[] = {5,-9,32,77,64,-24,14,0,21,45};

// int count=sizeof(a)/4;

// printf("count=%d\n",count);

// int max=a[0],min=a[0],number1=0,number2=0;

// for (int i=0; i<count; i++) {

// if (max<a[i]) {

// max=a[i];

// number1=i;

// }

// }

// printf("max=%d,number1=%d\n",max,number1);

// for (int i=0; i<count; i++) {

// if (min>a[i]) {

// min=a[i];

// number2=i;

// }

// }

// printf("min=%d,number2=%d\n",min,number2);

//10、(*)把str1, str2, str3合并到result数组中。

// char result[50] = {0};

// char str1[] = "Lanou ";

// char str2[] = "23_class ";

// char str3[] = " is niu best!";

// 结果:“Lanou 23_class is niu best!”

// char result[50] = {0};

// char str1[] = "Lanou ";

// char str2[] = "23_class ";

// char str3[] = " is niu best!";

// strcat(result,str1);

// strcat(result,str2);

// strcat(result,str3);

// printf("%s\n",result);

//11、(**)找出下面程序的错误:

//char string[10],str1[10];

//int i;

//for (i=0; i<10; i++) {

// str1[i]='a';

//}

// strcpy(string, str1);

//纠正

// char string[11];//string[10]改成[11](因为str1[i]='a',之后str1="aaaaaaaaaa",默认后面还有一个‘\0’结束符一位,所以,string[10]把10改成11就可以了)

// char str1[10];

// int i;

// for (i=0; i<10; i++) {

// str1[i]='b';

// }

// printf("%s\n",str1);

// strcpy(string, str1);

//

// printf("%s\n",string);

//12、(**)下面这个程序执行后会有什么错误或者效果

// unsigned char str[256];

// unsigned char i;

// for (i=0; i<=255;i++) {

// str[i]=i;

// printf("%c",str[i]);

// }

//出现错误: Comparison of constant 256 with expression of type 'unsigned char' is always true

//现象是:一直输出0~255,处于死循环的过程,因为unsigned char的范围是0~255,当i=255后,i也是无符号字符型,i++后就会变回0,则一直循环下去。

return 0;

}
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: