美文网首页C语言学习C语言杭电oj
第十章-字符串:C语言世界中的大力水手

第十章-字符串:C语言世界中的大力水手

作者: 小小Henry | 来源:发表于2019-12-17 21:17 被阅读0次

    1有趣的“回文”检测

    #include <stdio.h>
    #include <string.h>
    
    int IsPalindrome(char* a);
    
    int main()
    {
        char a[20];
        printf("Input string:");
        gets(a);
        //printf("%s", a);
        if (IsPalindrome(a))
            printf("Yes!\n");
        else
            printf("No!\n");
        return 0;
    }
    
    int IsPalindrome(char *a)
    {
        char* pStart, * pEnd;
        pStart = a;
        pEnd = a;
        for (; *pEnd != '\0'; pEnd++);
        pEnd--;
    
        while (pStart < pEnd)
        {
            if (*pStart != *pEnd)
                return 0;
            else
            {
                pStart++; pEnd--;
            }
        }
        return 1;
        
    }
    

    学生成绩管理系统V1.0

    #include <stdio.h>
    #include <string.h>
    #include <math.h>
    
    #define N 30
    
    void Display(long int *number, float *score);
    
    int main()
    {
        long int number[N];
        //long int number2[N];
        float score[N];
        //float score2[N];
        Display(number, score );
        return 0;
    }
    
    void Display(long int* number, float* score)
    {
        int numbers;
        int choice;
        printf("Input student number(n<30):\n");
        scanf("%d", &numbers);
        do
        {
            printf("Management for Students' scores\n"
                "1.Input record\n"
                "Caculate total and average score of course\n"
                "3.Sort in descending order by score\n"
                "4.Sort in ascending order by number\n"
                "5.Search by number\n"
                "6.Statistic analysis\n"
                "7.List record\n"
                "0.Exit\n"
                "Please Input your choice:\n");
            scanf("%d", &choice);
            if (choice == 1)
            {
                printf("IInput student's ID, name and score:\n");
                for (int i = 0; i < numbers; i++)
                {
                    scanf("%ld%f", number+i, score+i);
                    //*(number2+i) = *(number+i);
                    //*(score2 + i) = *(score + i);
                }
            }
            else if (choice == 2)
            {
                float sum=0;
                for (int i = 0; i < numbers; i++)
                {
                    sum += *(score + i);
                }
                printf("sum=%.0f,aver=%.2f\n", sum, sum / numbers);
    
            }
            else if (choice == 3)
            {
                long int tempi;
                float tempf;
                printf("Sort in descending order by score:\n");
                for (int i=0; i<numbers-1; i++)
                    for (int j = 0; j < numbers-i-1; j++)
                    {
                        if (*(score + j) < *(score + j + 1))
                        {
                            tempi = *(number + j); *(number + j) = *(number + j + 1); *(number + j + 1) = tempi;
                            tempf = *(score + j); *(score + j) = *(score + j + 1); *(score + j + 1) = tempf;
                        }
                    }
                for (int i = 0; i < numbers; i++)
                    printf("%ld\t%.0f\n", *(number + i), *(score + i));
            }
            else if (choice == 4)
            {
                int tempi;
                float tempf;
                printf("Sort in ascending order by number:\n");
                for (int i = 0; i < numbers - 1; i++)
                    for (int j = 0; j < numbers - i - 1; j++)
                    {
                        if (*(number + j) > *(number + j + 1))
                        {
                            tempi = *(number + j); *(number + j) = *(number + j + 1); *(number + j + 1) = tempi;
                            tempf = *(score + j); *(score + j) = *(score + j + 1); *(score + j + 1) = tempf;
                        }
                    }
                for (int i = 0; i < numbers; i++)
                    printf("%ld\t%.0f\n", *(number + i), *(score + i));
            }
            else if (choice == 5)
            {
                long int n;
                printf("Input the number you want to search:\n");
                scanf("%ld", &n);
                for (int i = 0; i < numbers; i++)
                {
                    if (n == *(number + i))
                    {
                        printf("%ld\t%.0f\n", n, *(score+i));
                        goto A;
                    }
                }
                printf("Not found!\n");
            A:;
            }
            else if (choice == 6)
            {
                int gradeA=0;
                int gradeB = 0;
                int gradeC = 0;
                int gradeD = 0;
                int gradeE = 0;
                int gradeF = 0;
                for (int i = 0; i < numbers; i++)
                {
                    if (*(score + i) < 60)
                        gradeF++;
                    else if (*(score + i) < 70)
                        gradeE++;
                    else if (*(score + i) < 80)
                        gradeD++;
                    else if (*(score + i) < 90)
                        gradeC++;
                    else if (*(score + i) < 100)
                        gradeB++;
                    else if (*(score+i) == 100)
                        gradeA++;
                }
                printf("<60\t%d\t%.2f%%\n", gradeF, (100 * (float)gradeF) / numbers);
                printf("60-69\t%d\t%.2f%%\n", gradeE, (100 * (float)gradeE) / numbers);
                printf("70-79\t%d\t%.2f%%\n", gradeD, (100 * (float)gradeD) / numbers);
                printf("80-89\t%d\t%.2f%%\n", gradeC, (100 * (float)gradeC) / numbers);
                printf("90-100\t%d\t%.2f%%\n", gradeB, (100 * (float)gradeB) / numbers);
                printf("100\t%d\t%.2f%%\n", gradeA, (100 * (float)gradeA) / numbers);
    
            }
            else if (choice == 7)
            {
            for (int i = 0; i < numbers; i++)
                printf("%ld\t%.0f\n", *(number + i), *(score + i));
            }
            else if (choice == 0);
            else
                printf("Input error!\n");
        } while (choice);
        printf("End of program!\n");
    }
    

    我合计哪儿的输出格式不对,没通过测试,然后就没继续找下去了,去复制了一段别人的代码。

    3程序改错——1

    #include <stdio.h>
    #include <string.h>
    
    char* MyStrcat(char* dest, char* source);
    
    int main(void)
    {
        char a[160], b[80];
        char* first =a, * second =b, * result;
        printf("Input the first string:\n");
        gets(first);
        printf("Input the second string:\n");
        gets(second);
        result = MyStrcat(first, second);
        printf("The result is : %s\n", result);
        return 0;
    }
    char* MyStrcat(char* dest, char* source)
    {
        int i = 0,j=0;
        while (*(dest + i) != '\0')   i++;
        for (j=0; *(source + j) != '\0'; j++)
        {
            *(dest + i+j) = *(source + j);
        }
        *(dest + i + j) = '\0';
        return dest;
    }
    

    4程序改错——2

    #include<stdio.h>
    #define  ARR_SIZE  5
    
    void  YH(int a[][ARR_SIZE], int  n);
    void  PrintYH(int a[][ARR_SIZE], int  n);
    
    int main(void)
    {
        int  a[ARR_SIZE][ARR_SIZE];
        YH(a, ARR_SIZE);
        PrintYH(a, ARR_SIZE);
        return 0;
    }
    void YH(int a[][ARR_SIZE], int n)
    {
        int  i, j;
        for (i = 1; i < n; i++)
        {
            a[i][1] = 1;
            a[i][i] = 1;
        }
        for (i = 3; i < n; i++)
        {
            for (j = 2; j <= i - 1; j++)
            {
                a[i][j] = a[i - 1][j - 1] + a[i - 1][j];
            }
        }
    }
    void PrintYH(int a[][ARR_SIZE], int n)
    {
        int i, j;
        for (i = 1; i < n; i++)
        {
            for (j = 1; j <= i; j++)
            {
                printf("%4d", a[i][j]);
            }
            printf("\n");
        }
    }
    

    5出售金鱼

    #include<stdio.h>
    
    #define  ARR_SIZE  5
    
    int Fish(int n);
    
    int main(void)
    {
        printf("There are %d fishes at first.\n", Fish(1));
        return 0;
    }
    int Fish(int  n)
    {
        if (n == 5)
            return 11;
        else
            return ((float)(n+1)/n * Fish(n + 1) +1);
    }
    

    6找最值

    #include<stdio.h>
    
    #define  ARR_SIZE  10
    
    int FindMaxMin(int a[], int n, int* pMaxPos, int* pMinPos);
    
    int main(void)
    {
        int a[ARR_SIZE];
        int pMaxPos, pMinPos;
        printf("Input 10 numbers:\n");
        for (int i = 0; i < ARR_SIZE; i++)
            scanf("%d", (a+i));
        FindMaxMin(a, ARR_SIZE, &pMaxPos, &pMinPos);
        printf("Max=%d,Position=%d,Min=%d,Position=%d\n", a[pMaxPos], pMaxPos, a[pMinPos], pMinPos);
        return 0;
    }
    
    int FindMaxMin(int a[], int n, int* pMaxPos, int* pMinPos)
    {
        int max = a[0];
        int min = a[0];
        *pMaxPos = *pMinPos = 0;
        for (int i = 0; i < n; i++)
        {
            if (*(a + i) > max)
            {
                max = *(a + i);
                *pMaxPos = i;
            }
            else if (*(a + i) < min)
            {
                min = *(a + i);
                *pMinPos = i;
            }
        }
    }
    

    7杨辉三角形

    #include<stdio.h>
    
    void YH(int n);
    
    int main(void)
    {
        int n;
        printf("Input n (n<=10):\n");
        scanf("%d", &n);
        YH(n);
        return 0;
    }
    
    void YH(int n)
    {
        int yh[10][10];
        for (int i = 0; i < n; i++)
        {
            *yh[i] = 1;
            *(yh[i] + i) = 1;
        }
        for (int i = 2; i < n; i++)
            for (int j = 1; j < i ; j++)
            {
                *(yh[i] + j) = *(yh[i - 1] + j - 1) + *(yh[i - 1] + j);
                //printf("%d\t%d\t%d\n", *(yh[i-1]+j-1), *(yh[i-1]+j), *(yh[i] + j));
            }
                
        
        for (int i = 0; i < n; i++)
        {
            for (int j = 0; j <= i; j++)
                printf("%4d", *(yh[i] + j));
            printf("\n");
        }
    }
    

    8颠倒句子中的单词顺序

    #include<stdio.h>
    #include <string.h>
    
    #define N 100
    
    int Inverse(char str1[], char str2[][N]);
    
    int main(void)
    {
        char str1[100];
        char str2[N][N];
        int cal;
        printf("Input a sentence:");
        gets(str1);
        cal = Inverse(str1, str2);
        for (int i = cal; i > 1; i--)
            printf("%s ", str2[i]);
        printf("%s", str2[1]);
        printf("%s", str2[0]);
        return 0;
    }
    
    int Inverse(char str1[], char str2[][N])
    {
        int calc = 1;
        int i, j=0;
        for (i = 0; *(str1 + i) != '\0'; i++)
        {
            if (*(str1 + i) != ' ')
            {
                *(str2[calc] + j) = *(str1 + i);
                j++;
            }
            else
            {
                *(str2[calc] + j) = '\0';
                calc++;
                j = 0;
            }
            if ((*(str1 + i) < 'a' || *(str1 + i) > 'z') && (*(str1 + i) < 'A' || *(str1 + i) > 'Z') && *(str1+i)!=' ')
            {
                *str2[0] = *(str1 + i);
                *(str2[0] + 1) = '\0';
                *(str2[calc] + j - 1) = '\0';
            }
        }
        return calc;
    }
    

    相关文章

      网友评论

        本文标题:第十章-字符串:C语言世界中的大力水手

        本文链接:https://www.haomeiwen.com/subject/tskgbctx.html