13周作业

第一题:

编程实现类似strcmp但不区分字母大小写的字符串比较函数,函数原型为:

int my_stricmp(char *str1, char *str2);

该函数仅在对两个字母进行比较时,忽略字母的大小写,只看字母在字母表中的序号大小,如'A'和'a'相同,'a'比'B'小。对于不同为字母的两个字符的比较,则规则同strcmp函数相同。

在主程序中,在两行上依次输入两个均不超过40长的字符串,调用该函数进行比较,并输出函数的返回值。

说明:按照题目的要求,my_stricmp函数的功能和系统的stricmp函数并不一致,stricmp会把所有的字母都转换成小写字母来处理。

输入样例1:

heLlO,wOrld!

HElLo,WORlD!

输出结果1:

0

输入样例2:

Fog and Rat!

dog and Cat!

输出结果2:

1

代码:

#include <stdio.h>
#include <stdlib.h>
int my_stricmp(char *str1,char *str2)
{
    int result;
    char *p1 = &str1[0];
    char *p2 = &str2[0];
    while(*p1 != '\0'||*p2 != '\0')
    {
        result = 0;
        if(*p1<=122&&*p1>=97)*p1 -=32;//这里注意把p1的值换掉
        if(*p2<=122&&*p2>=97)*p2 -=32;//这一步是把小写统一转化成大写
        if(*p1>*p2)
        {
            result = 1;
            break;//比较大小是逐个比较,一旦出现了大或者小的情况直接跳出循环,不再比较后面的字符
        }
        if(*p1 == *p2);
        if(*p1 < *p2)
        {
            result = -1;
            break;
        }
        p1++;//地址向后推进一个
        p2++;
    }
    return result;//因为前面已经定义了函数是int 形式的,所以要有返回值且返回值要在循环之外
                          //如果函数是void(空)类型的话就不需要写返回值了
}
int main()
{
    int count = 999;
    char ch1[40],ch2[40];
    gets(ch1);
    gets(ch2);
    count = my_stricmp(ch1,ch2);
    printf("%d\n",count);
    printf("Hello world!\n");
    return 0;
}
第二题:

从键盘输入10个整数,用空格或制表符间隔,把他们按如下规则排序,然后输出,仍以空格间隔:

奇数在左边,从大到小排列;偶数在右边,从小到大排列

提示:有常规的办法,也有技巧性强一点的办法。常规的办法就是分成奇数和偶数,然后分别排序。技巧性强一点的办法,是把该问题看做一个按照如下规则排序的问题:奇数比偶数“小”,所以奇数应在偶数左边;两个数同为偶数,则按其值来比较大小;同为奇数,则值大的数更“小”。

输入样例:58 43 41 73 54 75 63 20 42 17

输出结果:75 73 63 43 41 17 20 42 54 58

代码:

#include <stdio.h>
#include <stdlib.h>
void sort1 (int arr[],int len)
{
    int i,j,temp;
    for(i = 0;i<len-1;i++)
    {
        for(j = 0;j<len-1-i;j++)
        {
            if(arr[j]>arr[j+1])//sort1是从小到大进行排序
            {
                temp = arr[j];
                arr[j] = arr[j+1];
                arr[j+1] = temp;
            }
        }
    }
}
void sort2 (int arr[],int len)
{
    int i,j,temp;
    for(i = 0;i<len-1;i++)
    {
        for(j = 0;j<len-1-i;j++)
        {
            if(arr[j]<arr[j+1])//sort12是从大到小进行排序
            {
                temp = arr[j];
                arr[j] = arr[j+1];
                arr[j+1] = temp;
            }
        }
    }
}//函数类型为void,不需要返回值
int main()
{
    int numbers[10],even[10],odd[10],m=0,n=0;//m和n用于奇数偶数个数的计数
    for(int i = 0;i<10;i++)
    {
        scanf("%d",&numbers[i]);
    }
    int *p = numbers;

    while(m+n<10)//这一步在后面注意事项里解释
    {
        if(*p%2 == 0)
        {
            even[m] = *p;
            m++;
            p++;
        }
        else
        {
            odd[n] = *p;
            n++;
            p++;
        }
    }
    sort1(even,m);
    sort2(odd,n);
    for(int j = 0;j<n;j++)
    {
        printf("%d ",odd[j]);
    }
    for(int j = 0;j<m;j++)
    {
        printf("%d ",even[j]);
    }
    putchar('\n');
    return 0;
}
注意事项:

假定有十个数为1 2 3 4 5 6 7 8 9 10

列一个表格:

m+n  0   1   2   3   4   5   6   7   8   9

*p      1   2   3   4   5   6   7   8   9   10

m+n = 0的时候就已经进行了一次循环,当循环进行10次的时候m+n的值是9而不是10,因此判断条件应为m+n<10而不是m+n<11

类似的可以参考一下数组:

int arr[10];

for(int i = 0;i<10;i++)//初始值是0,存的是十个数,判断条件是i<10;

{

        scanf("%d",&arr[i]);

}

第三题:

编程计算 2+4+8+16+32+64+…,直至所有项的累加和大于100000(10万)为止,这个求和式子的每一项均为上一项的2倍。在一行上,输出最终结果以及最后一项的值,用一个逗号分隔。

输入样例:无

输出结果:略

代码:

#include <stdio.h>
#include <stdlib.h>

int main()
{
    int result = 2;
    int count = 2;
    while(result < 100000)
    {
        result = result + result*2;
        count*=2;
    }
    printf("%d,%d\n",result,count);
    printf("Hello world!\n");
    return 0;
}//注意别忘了给变量result 和 count 赋值就行

第四题:

编程实现把一个表示16进制整数的字符串转换为其数值的函数,其原型如下:

int hex_to_value(char *hex);

注意该字符串可以带正负号。在主程序中,输入一个可带正负号的16进制数,调用该函数,用printf函数打印其数值。

本题目假定输入的16进制整数的绝对值在一个int的表示范围内。

输入样例1:+FF

输出结果1:255

输入样例2:-AABB

输出结果2:-43707

代码:

#include <stdio.h>
#include <stdlib.h>
int hex_to_value(char *hex)
{
    int i = 0;
    int result = 0;
    char *p = &hex[1];
    while(*p != '\0')
    {
        if(*p == 'A'||*p == 'a')*p = 10;
        if(*p == 'B'||*p == 'b')*p = 11;
        if(*p == 'C'||*p == 'c')*p = 12;
        if(*p == 'D'||*p == 'd')*p = 13;
        if(*p == 'E'||*p == 'e')*p = 14;
        if(*p == 'F'||*p == 'f')*p = 15;
        if(*(p+1)!='\0')//*P后面肯定有东西,要么是数要么是'\0'
        result=(result+*p)*16;
        else
            result+=*p;//类似于分类讨论的方法

        p++;
        i++;
    }
     if(hex[0] == '-')result = -result;
    return result;
}
int main()
{
    char hex[100];
    gets(hex);
    printf("%d\n",hex_to_value(hex));
    printf("Hello world!\n");
    return 0;
}//注意不同进制之间的转换即可

第五题:

编程实现把一个表示实数的字符串转换为其数值的函数,其原型如下:

double real_to_value(char *real);

注意该实数字符串可以带正负号,可以带小数点(不要求科学计数法形式)。在主程序中,输入该形式的一个实数,调用该函数,用printf函数打印其数值,保留到小数点后2位小数。

本题目有如下假定,输入的实数的整数部分在int的表示范围内;如把小数点后的数看作一个整数,它也在int的表示范围内。

输入样例1:+3.1415

输出结果1:3.14

输入样例2:-2.71828

输出结果2:-2.72

技巧性强那个我不会

两种思路:

1.先分奇偶再分别排序最后合并

2.先排序(从小到大)再分奇偶,按题目要求如果是奇数的话就从右往左取值,是偶数的话就从左往右取值

这里写第一个思路:

#include <stdio.h>
#include <stdlib.h>
#include <math.h>
double real_to_value(char *real)
{
    char *p = &real[1];
    char *p1 = &real[1];
    int i = 0;
    while(*p != '.')
    {
        i++;
        p++;
    }//这里是计数,记录整数的个数
    double count1 = 0;
    if(i == 1)count1 = count1 + *p1 - '0';//这里减一个零是因为p1是char类型的变量而count1是double类型的
                                                                   // 如果不减去'0'的话count1记录的是阿斯卡码表里面字符对应的值而不是数字
    if(i>1)
    {
        for(int j = 0;j<i-1;j++)
        {
            count1 = (count1 + *p1-'0')*10;
            p1++;
        }
        count1 = count1 + *p1 - '0';
    }//count1记录的是整数部分的值
    double count2 = 0;
    p1++;
    p1++;
    int m = 1;
    while(*p1 != '\0')
    {
        count2 = count2 + (*p1 - '0')/(pow(10,m)) ;
        p1++;
        m++;
        if(m>=3)break;
    }
    if(*p1>4)count2+=0.01;//count2记录的是小数部分的值
    double count = 0;
    count = count1+count2;
    if(real[0]=='-')count = -count;
    return count;
}

int main()
{
    char real[100];
    gets(real);
    printf("%.2f\n",real_to_value(real));
    printf("Hello world!\n");
    return 0;
}
 

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

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值