zoukankan      html  css  js  c++  java
  • 第五周作业

    本周作业头:
    这个作业属于那个课程 C语言程序设计II
    这个作业要求在哪里 https://edu.cnblogs.com/campus/zswxy/computer-scienceclass3-2018/homework/2827
    我在这个课程的目标是 对字符串的灵活使用
    这个作业在那个具体方面帮助我实现目标 让我对字符串更加了解和使用。对数组也用更深刻认识了
    参考文献 csnd博客
    基础作业
    7-1 英文单词排序 (25 分)
    本题要求编写程序,输入若干英文单词,对这些单词按长度从小到大排序后输出。如果长度相同,按照输入的顺序不变。

    输入格式:
    输入为若干英文单词,每行一个,以#作为输入结束标志。其中英文单词总数不超过20个,英文单词为长度小于10的仅由小写英文字母组成的字符串。

    输出格式:
    输出为排序后的结果,每个单词后面都额外输出一个空格。

    输入样例:
    blue
    red
    yellow
    green
    purple

    输出样例:
    red blue green yellow purple

    1.实验代码

    include <stdio.h>

    include <string.h>

    main()
    {
    char str[20][10],t[20],str1[10];
    int i,j,n=0;
    while(1)
    {
    scanf("%s",str1);
    if(str1[0]=='#')
    {
    break;
    }
    else
    {
    strcpy(str[n],str1);
    n++;
    }
    }
    for(i=0;i<n-1;i++)
    for(j=0;j<n-i-1;j++)
    {
    if(strlen(str[j])>strlen(str[j+1]))
    {
    strcpy(t,str[j]);
    strcpy(str[j],str[j+1]);
    strcpy(str[j+1],t);
    }
    }
    for(i=0;i<n;i++)
    {
    printf("%s ",str[i]);
    }
    }
    文件代码

    include <stdio.h>

    include <string.h>

    include <stdlib.h>

    int main(void) {
    FILE * fp;
    char input[21][11] = {''};
    char snap[11] = {''};
    int i = 0;
    if ((fp=fopen("D: empxzy.txt","a+")) == NULL)
    {
    printf ("File open error! ");
    exit (0);
    }

    while (1) {
        
        fscanf(fp,"%s", input[i]);
        printf("%s
    ", input[i]);
        if (input[i][0] == '!'){
            break;
        }
        i++; 
     }
    
    input[i][0] = '';
    int len = i;
    int j = 0; 
    
    
    for (i = 0; i < len; i++) {
        for (j = 1; j < len - i; j++) {
            if (strlen(input[j - 1]) > strlen(input[j])) {
                strcpy(snap, input[j - 1]); 
                strcpy(input[j - 1], input[j]); 
                strcpy(input[j], snap); 
            }
        }
    }
    fprintf(fp,"
    ");
    for (i = 0; i < len; i++){
        fprintf(fp,"%s
    ", input[i]);
        printf("%s
    ", input[i]);
    }   
        if(fclose(fp) )
    {
        printf("Can not close the file!
    ");
        exit(0);
    }
    
    return 0;
    

    }

    2.设计思路

    3. 遇到问题
    对字符串不太熟悉 对上周所学内容有所遗忘

    运行截图


    -1 统计一行文本的单词个数 (15 分)
    本题目要求编写程序统计一行字符中单词的个数。所谓“单词”是指连续不含空格的字符串,各单词之间用空格分隔,空格数可以是多个。

    输入格式:
    输入给出一行字符。

    输出格式:
    在一行中输出单词个数。

    输入样例:
    Let's go to room 209.
    输出样例:
    5
    实验代码、

    include <stdio.h>

    int main()
    {
    char a;
    int cnt=0,countword=0;

    while(1){
    	scanf("%c",&a);
    	
    	if(a=='
    '){    
    		break;
    	}else if(a!=' '){ 
    		if(a>='a'&&a<='z'||a>='A'&&a<='Z'||a>='0'&&a<='9')	{
    			countword++;   
    			cnt++;    
    		}
    			while(1){
    				scanf("%c",&a);
    				if(a==' '||a=='
    '){ 
    					break;
    				}
    				cnt++;
    			}
    	 
    		if(cnt>1){
    			if(a>='a'&&a<='z'||a>='A'&&a<='Z'||a>='0'&&a<='9')	countword++; 
    		}
    		if(a=='
    '){
    			break; 
    		}
    	} 
    }
    
    printf("%d",countword);
    return 0;
    

    }
    2.思路

    3.。这题主要是对数组和字符串的掌握 ,自己对这个掌握的不是太好
    4 运行结果



    总结
    本周要求我们对字符串的掌握以及对上周的数组的运用 这周自己终于在电脑上自己画了流程图,虽然表达的不是很清楚,但是自己在尝试去做还是蛮高兴的,自己还得加强字符串 和数组的运用

  • 相关阅读:
    「洛谷 NOIP 计划 2021」【学习1】降维技巧
    组合数取模 合集
    浅谈并查集
    四边形不等式优化 dp (doing)
    qbxt数学五一Day4
    qbxt五一数学Day3
    qbxt五一数学Day2
    qbxt五一数学Day1
    浅谈拉格朗日插值
    10-交换排序:冒泡排序
  • 原文地址:https://www.cnblogs.com/liualiu/p/10621411.html
Copyright © 2011-2022 走看看