哈工大C语言--SSE改错03

(568, 324, '324', 'Q300', '

*日期转换

按如下函数原型用函数编程解决如下的日期转换问题(要求考虑闰年的问题):

输入某年某月某日,计算并输出它是这一年的第几天。

/* 函数功能: 对给定的某年某月某日,计算它是这一年的第几天

函数参数: 整型变量year、month、day,分别代表年、月、日

函数返回值:这一年的第几天 */

int DayofYear(int year, int month, int day);

下面程序中存在比较隐蔽的错误,请通过分析和调试程序,发现并改正程序中的错误。

注意:请将修改正确后的完整源程序拷贝粘贴到答题区内。

对于没有错误的语句,请不要修改,修改原本正确的语句也要扣分。

当且仅当错误全部改正,且程序运行结果调试正确,才给加5分。

改错时不能改变程序原有的意图,不能改变函数原型。

#include <stdio.h>
int DayofYear(int year, int month, int day);
int dayTab[2][13] =
{
    {0, 31, 29, 31, 30, 31, 30, 31, 31, 30, 31, 30, 31},
    {0, 31, 28, 31, 30, 31, 30, 31, 31, 30, 31, 30, 31}
};

main()
{
    int year, month, day;
    printf("Please enter year, month, day:\n");
    scanf("%d,%d,%d", year, month, day);
    printf("yearDay = %d\n" DayofYear(year, month, day));
}
int DayofYear(int year, int month, int day);
{
    int i, leap;
    leap = year / 4 = 0 & year / 100 != 0 | year / 400 = 0;
    for (i = 0, i < month, i++);
    {
        day = day - dayTab[leap][i];
    }
}

', '', '', '', '', '', '', '', '

#include <stdio.h>
int DayofYear(int year, int month, int day);
int dayTab[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}
};

main()
{
    int year, month, day;
    printf("Please enter year, month, day:\n");
    scanf("%d,%d,%d",&year,&month,&day);
    printf("yearDay = %d\n",DayofYear(year, month, day));
}
int DayofYear(int year, int month, int day)
{
    int i, leap;
    leap = year % 4 == 0 && year % 100 != 0 || year % 400 == 0;
    for (i = 1;i < month; i++)
    {
        day = day + dayTab[leap][i];
    }
    return day;
}

', 2),

(569, 324, '324', 'Q2133', '

**实现strcat即字符串连接

下面函数MyStrcat实现strcat的功能,即:将两个字符串连接的功能,

将源字符串srcStr连接到目的字符串dstStr的尾部。

#include <stdio.h>
#define ARR_SIZE 80;
void MyStrcat(char dstStr[], char srcStr[]);
main()
{
    char  s[], t[];
    printf("Please enter source string:\n");
    gets(s); 
    printf("Please enter destination string:\n");
    gets(t);

    MyStrcat(s,t);
    printf("The concatenate string is:\n");
    puts(t);
}
void MyStrcat(char dstStr[], char srcStr[])
{
    int i = 0, j = 0;

    while (dstStr[i]!=''\0'')
    {
        i++;
    }
    for(;srcStr[i]!=''\0'';i++,j++)
    {
        dstStr[j] = srcStr[i];
    }
    dstStr[j]=''\0'';
}

', '', '', '', '', '', '', '', '

#include <stdio.h>
#define ARR_SIZE 80
void MyStrcat(char dstStr[], char srcStr[]);
main()
{
    char  s[ARR_SIZE], t[ARR_SIZE];
    printf("Please enter source string:\n");
    gets(s);
    printf("Please enter destination string:\n");
    gets(t);

    MyStrcat(t,s);
    printf("The concatenate string is:\n");
    puts(t);
}
void MyStrcat(char dstStr[], char srcStr[])
{
    int i = 0, j = 0;

    while (dstStr[j]!=''\0'')
    {
        j++;
    }
    for(;srcStr[i]!=''\0'';i++,j++)
    {
        dstStr[j] = srcStr[i];
    }
    dstStr[j]=''\0'';
}

', 1),

(570, 324, '324', 'Q2295', '

*各位上数字之积

程序功能:计算正整数num的各位上的数字之积,例如输入252,则输出252的积,结果为20。

请找出下面给出的程序中存在的错误,使程序能够得到正确的运行结果。

#include  <stdio.h>
main()
{
    int num;    
     int result; 
    scanf("%d", num);
    result = multiple(num);
    printf( "%d result");
}
int multiple(int num)
{
    int mul;    
     while(num > 0)
    {
        mul = mul*(num/10);
        num = num % 10;
    }
    return mul;
}

', '', '', '', '', '', '', '', '

#include  <stdio.h>
main()
{
    int num;
     int result;
    scanf("%d",&num);
    result = multiple(num);
    printf( "%d",result);
}
int multiple(int num)
{
    int mul=1;
     while(num > 0)
    {
        mul = mul*(num%10);
        num = num / 10;
    }
    return mul;
}

', 0),

(571, 324, '324', 'Q3012', ' 以下程序是利用欧几里得算法(辗转相除法)

*最大公约数

求两个正整数的最大公约数。程序有错误请改正,使之能得到正确的运行结果。

#include <stdio.h>
int main()
{
    int a, b, c;
    printf("Input a,b:");
    scanf("%d,%d\n", &a, &b);
    c = Gcd(a,b);
    if (c != -1)
    {
        printf("Greatest Common Divisor of %d and %d is %d\n", a, b, c);
    }
    else
    {
        printf("Input number should be positive!\n");
    }
    return 0;
}

int Gcd(int a, int b)
{
    int r;
    if (a < 0 || b < 0)
    {
        return -1;
    }
    do{
        r = a / b;
        a = b;
        b = r;
    }while (r=0);
    return  a;
}

#include <stdio.h>
int main()
{
    int a, b, c;
    printf("Input a,b:");
    scanf("%d,%d", &a, &b);
    c = Gcd(a,b);
    if (c != -1)
    {
        printf("Greatest Common Divisor of %d and %d is %d\n", a, b, c);
    }
    else
    {
        printf("Input number should be positive!\n");
    }
    return 0;
}

int Gcd(int a, int b)
{
    int r;
    if (a <= 0 || b <= 0)
    {
        return -1;
    }
    do{
        r = a % b;
        a = b;
        b = r;
    }while (r!=0);
    return  a;
}

', 2),

(574, 324, '324', 'Q3011', '

*奥运会国名排序

请编程实现按奥运会参赛国国名(国家名字可以带空格)在字典中的顺序对其出场顺序进行排序。假高参赛国家不超过150个。

以下程序有错误,请找出并改正。

#include  <stdio.h>
#define   MAX_LEN  10                       
#define   N     150                     
void SortString(char str[][], int n);
int main()
{ 
    int    i, n;
    char   name[N][MAX_LEN];                
    printf("How many countries?");
    scanf("%d",&n);                    
    printf("Input their names:\n");
    for (i=0; i<n; i++)   
    {
        scanf("%s",&name[i]);                           
    }
    SortString(name, n);                    
    printf("Sorted results:\n");
    for (i=0; i<n; i++)                     
    {
        puts(name[i]);                      
    }
    return 0;
}

void SortString(char str[][], int n)
{
    int    i, j;
    char  temp[MAX_LEN];
    for (i=0; i<n-1; i++)                    
    {
        for (j = i+1; j<n; j++)
        {
            if (str[j]<str[i])     
            { 
                temp=str[i];        
                str[i]=str[j];
                str[j]=temp;
            }  
        }    
    }  
}

#include  <stdio.h>
#include <string.h>
#define MAX_LEN 10
#define N 150
void SortString(char str[N][MAX_LEN], int n);
int main()
{
    int    i, n;
    char   name[N][MAX_LEN];
    printf("How many countries?");
    scanf("%d",&n);getchar();
    printf("Input their names:\n");
    for (i=0; i<n; i++)
    {
        gets(name[i]);
    }
    SortString(name, n);
    printf("Sorted results:\n");
    for (i=0; i<n; i++)
    {
        puts(name[i]);
    }
    return 0;
}

void SortString(char str[N][MAX_LEN], int n)
{
    int    i, j;
    char temp[MAX_LEN];
    for (i=0; i<n-1; i++)
    {
        for (j = i+1; j<n; j++)
        {
            if (strcmp(str[j],str[i])<0)
            {
                strcpy(temp,str[i]);
                strcpy(str[i],str[j]);
                strcpy(str[j],temp);
            }
        }
    }
}

', 0),

(577, 324, '324', 'Q2366', '

*strcat字符串连接

用指针做函数参数自己编程实现字符串连接函数strcat()的功能。

下面程序中存在比较隐蔽的错误,请通过分析和调试程序,发现并改正程序中的错误。

#include <stdio.h>
#define N = 80;
void MyStrcat(char *dstStr, char *srcStr);
main()
{
    char s[N], t[N];
    printf("Input a string:\n");
    gets(s);
    printf("Input another string:\n");
    gets(t);
    MyStrcat(s, t);
    printf("Concatenate results:%s\n", s);
}
void MyStrcat(char *dstStr, char *srcStr)
{
    while (*srcStr != ''\0'');
    {
        srcStr++;
    }
    while (*srcStr != ''\0'');
    {
        *dstStr = *srcStr;
        srcStr++;
        dstStr++;
    }
}

', '', '', '', '', '', '', '', '

#include <stdio.h>
#define N 80
void MyStrcat(char *dstStr, char *srcStr);
main()
{
    char s[N], t[N];
    printf("Input a string:\n");
    gets(s);
    printf("Input another string:\n");
    gets(t);
    MyStrcat(s,t);
    printf("Concatenate results:%s\n", s);
}
void MyStrcat(char *dstStr, char *srcStr)
{
    while (*dstStr != ''\0'')
    {
        dstStr++;
    }
    while (*srcStr != ''\0'')
    {
        *dstStr = *srcStr;
        srcStr++;
        dstStr++;
    }
    dstStr--;
    dstStr=''\0'';
}

', 0),

(578, 324, '324', 'Q189', '

*日期转换

按如下函数原型用函数编程解决如下的日期转换问题(要求考虑闰年的问题):

输入某年某月某日,计算并输出它是这一年的第几天。

/* 函数功能: 对给定的某年某月某日,计算它是这一年的第几天 函数参数: 整型变量year、month、day,分别代表年、月、日
函数返回值:这一年的第几天 */

int DayofYear(int year, int month, int day);

下面程序中存在比较隐蔽的错误,请通过分析和调试程序,发现并改正程序中的错误。

#include <stdio.h>
int DayofYear(int year, int month, int day);
int dayTab[2][13] =
{
    {0, 31, 29, 31, 30, 31, 30, 31, 31, 30, 31, 30, 31},
    {0, 31, 28, 31, 30, 31, 30, 31, 31, 30, 31, 30, 31}
};

main()
{
    int year, month, day;
    printf("Please enter year, month, day:");
    scanf("%d,%d,%d", year, month, day);
    printf("yearDay = %d\n" DayofYear(year, month, day));
}
int DayofYear(int year, int month, int day);
{
    int i, leap;
    leap = year / 4 = 0 & year / 100 != 0 | year / 400 = 0;
    for (i = 0, i < month, i++);
    {
        day = day - dayTab[leap][i];
    }
}

#include <stdio.h>
int DayofYear(int year, int month, int day);
int dayTab[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}
};

main()
{
    int year, month, day;
    printf("Please enter year, month, day:");
    scanf("%d,%d,%d",&year,&month,&day);
    printf("yearDay = %d\n",DayofYear(year, month, day));
}
int DayofYear(int year, int month, int day)
{
    int i, leap;
    leap = year % 4 == 0 && year % 100 != 0 || year % 400 == 0;
    for (i = 1;i < month; i++)
    {
        day = day + dayTab[leap][i];
    }
    return day;
}

', 0),

(579, 324, '324', 'Q1392', '

*统计字符数

程序改错。

以下程序的功能是统计字符数。判断一个由’0’ ~ ‘9’这10个字符组成的字符串中哪个字符出现的次数最多。

输入数据:第一行是测试数据的组数m,每组测试数据占1行,每行数据不超过1000个字符且非空。 输出要求:m行,每行对应一组输入,包括出现次数最多的字符和该字符出现的次数。如果有多个字符出现的次数相同且最多,那么输出ASCII码最小的那一个。

#include <stdio.h>
#include <string.h>
main( )
{
int  cases, sum[10], i, max;
  char str[1000];                          
    scanf("%d", case);                    
  while (cases > 0)
       {
                scanf("%c", str);
            for( i = 0; i < 10; i++)
                 sum[i] = 0; 
                    for(i < 0; i < strlen(str); i++)
                ++sum[str[i] – 0];
            max = 0;   
            for (i = 1; i < 10; i++)
                if(sum[i] >= sum[max]) max = i;
            printf("%c %d\n", max + ''0'', sum[0]);
            cases --;
       }
}

', '', '', '', '', '', '', '', '

#include <stdio.h>
#include <string.h>
main()
{
int  cases, sum[10], i, max;
  char str[1000];
scanf("%d",&cases);getchar();
  while (cases > 0)
       {
                scanf("%s", str);
            for( i = 0; i < 10; i++)
                 sum[i] = 0;
                    for(i = 0; i < strlen(str); i++)
            ++sum[str[i]-''0''];
            max = 0;
            for (i = 1; i < 10; i++)
                if(sum[i] > sum[max]) max = i;
            printf("%c %d\n", max + ''0'', sum[max]);
            cases --;
       }
}

', 2),

(580, 324, '324', 'Q1367', '

*折半查找

下面程序实现折半查找算法,当找到输入元素后显示其在数组中的下标。找出其中的错误,并改正之

#include <stdio.h>
main()
{
    int  up=10, low=1, mid, found, find;
    int  a[10]={1, 5, 6, 9, 11, 17, 25, 34, 38, 41};

    scanf("%d", find);
    printf("\n");

    while (up>=low  ||  !found)
    {
        mid=(up+low)/2;
        if( a[mid] = find )
        {
                    found=1; 
                break;
        }
         else  if(a[mid]>find)
                up=mid+1;
            else   
                        low=mid+1;
   }

    if(found)  printf("found  number  is  %dth", mid);
    else    printf("no  found"); 
}

', '', '', '', '', '', '', '', '

#include <stdio.h>
main()
{
    int  up=9, low=0, mid, found=0, find;
    int  a[10]={1, 5, 6, 9, 11, 17, 25, 34, 38, 41};

    scanf("%d",&find);
    printf("\n");

    while (up>=low  &&  !found)
    {
        mid=(up+low)/2;
        if( a[mid] == find )
        {
                    found=1;
                break;
        }
         else  if(a[mid]>find)
                up=mid-1;
            else
               low=mid+1;
   }

    if(found)  printf("found number is %dth", mid);
    else    printf("no  found");
}

', 1),

全部评论

相关推荐

03-19 09:58
河海大学 Java
最喜欢春天的奇亚籽很...:同学,是小红书不是小哄书,一眼就能看到的错误
投了多少份简历才上岸
点赞 评论 收藏
分享
评论
1
1
分享

创作者周榜

更多
牛客网
牛客网在线编程
牛客网题解
牛客企业服务