第七周作业春

这个作业属于哪个课程 c语言程序设计
这个作业要求在哪里 https://edu.cnblogs.com/campus/zswxy/software-engineering-class2-2018/homework/2935
我在这个课程的目标是 指针,数组灵活运用
这个作业在哪个具体方面帮助我实现目标 作题加深理解
参考文献 http://c.harson.co/

1.基础作业 函数题:每个单词的最后一个字母改成大写

函数fun的功能是:将p所指字符串中每个单词的最后一个字母改成大写。(这里的“单词”是指由空格隔开的字符串)。

函数接口定义::

void fun( char *p );

其中 p 是用户传入的参数。函数将 p所指字符串中每个单词的最后一个字母改成大写。
裁判测试程序样例:

#include <stdio.h>
void fun( char *p );
int main()
{
 char chrstr[64];  int d ;
  gets(chrstr);
  d=strlen(chrstr) ;
  chrstr[d] = ' ' ;
  chrstr[d+1] = 0 ;
  fun(chrstr);
  printf("\nAfter changing:   %s\n", chrstr);
return 0;
}

/* 请在这里填写答案 */

输入样例:

my friend is happy

输出样例:

After changing:   mY frienD iS happY

1).实验代码

void fun(char *p)
{
  int i;                       
  for(i = 0; *(p + i) != '\0'; i++)
  {
    if(*(p + i + 1 ) == ' ')
    *(p + i) = *(p + i) - 32;
  }
}

2).设计思路

3).问题和解决方案

4).运行结果

2. 基础作业 编程题: 自动售货机

如图所示的简易自动售货机,物品架1、2上共有10样商品,按顺序进行编号分别为1-10,标有价格与名称,一个编号对应一个可操作按钮,供选择商品使用。如果物架上的商品被用户买走,储物柜中会自动取出商品送到物架上,保证物品架上一定会有商品。用户可以一次投入较多钱币,并可以选择多样商品,售货机可以一次性将商品输出并找零钱。

用户购买商品的操作方法是:

(1)从“钱币入口”放入钱币,依次放入多个硬币或纸币。钱币可支持1元(纸币、硬币)、2元(纸币)、5元(纸币)、10元(纸币),放入钱币时,控制器会先对钱币进行检验识别出币值,并统计币值总额,显示在控制器显示屏中,提示用户确认钱币放入完毕;

(2)用户确认钱币放入完毕,便可选择商品,只要用手指按对应商品外面的编号按钮即可。每选中一样商品,售货机控制器会判断钱币是否足够购买,如果钱币足够,自动根据编号将物品进行计数和计算所需钱币值,并提示余额。如果钱币不足,控制器则提示“Insufficient money”。用户可以取消购买,将会把所有放入钱币退回给用户。

输入格式:
先输入钱币值序列,以-1作为结束,再依次输入多个购买商品编号,以-1结束。
输出格式:
输出钱币总额与找回零钱,以及所购买商品名称及数量。
输入样例:

1 1 2 2 5 5 10 10 -1
1 2 3 5 1 6 9 10 -1

输出样例:

Total:36yuan,change:19yuan
Table-water:2;Table-water:1;Table-water:1;Milk:1;Beer:1;Oolong-Tea:1;Green-Tea:1;

1).实验代码

#include<stdio.h>
struct goods
{
   int num;
   char name[20];
   int price;
   int count;
};
int main()
{
   struct goods good[10]=
   {
   {1,"Table-water",1,0},
   {2,"Table-water",1,0},
   {3,"Table-water",1,0},
   {4,"Coca-Cola”",2,0},
   {5,"Milk",2,0},
   {6,"Beer",3,0},
   {7,"Orange-Juice",3,0},
   {8,"Sprite",3,0},
   {9,"Oolong-Tea",4,0},
   {10,"Green-Tea",4,0}
};

int sum = 0, num, change, total = 0, money = 0, i = 0;
while(1)
{
       scanf("%d", &money);
       if(money == -1)
         {
            break;
         }
       total = total + money;
}
while(1)
{
       scanf("%d", &num);
       if(num == -1)
       {
           break;
       }
switch(num)
{
    case 1:
        {
            sum = sum + good[0].price;
            good[0].count=good[0].count+1;
            break;
        }
    case 2:
        {
            sum = sum + good[1].price;

            good[1].count=good[1].count+1;
            break;

        }
    case 3:
        {
            sum = sum + good[2].price;

            good[2].count=good[2].count+1;
            break;
        }
         case 4:
        {
            sum = sum + good[3].price;

            good[3].count = good[3].count+1;
            break;
        }
         case 5:
        {
            sum = sum + good[4].price;

            good[4].count = good[4].count+1;
            break;
        }
         case 6:
        {
            sum = sum + good[5].price;

            good[5].count = good[5].count+1;
            break;
        }
         case 7:
        {
            sum = sum + good[6].price;

            good[6].count = good[6].count+1;
            break;
        }
         case 8:
        {
            sum = sum + good[7].price;

            good[7].count = good[7].count+1;
            break;
        }
         case 9:
        {
            sum = sum + good[8].price;
 
            good[8].count = good[8].count+1;
            break;
        }
         case 10:
        {
            sum = sum + good[9].price;

            good[9].count = good[9].count+1;
            break;
        }
    }

  }
  if(sum > total)
  {
      printf("Insufficient money");
  }
  else
  {
     change = total - sum;
     printf("Total:%dyuan,change:%dyuan\n",total, change);
     for(i=0;i<10;i++)
     {
         if(good[i].count!=0)
         {
             printf("%s:%d;",good[i].name, good[i].count);
         }
     }
  }
}

2).设计思路

3).问题和解决方案


不是很清楚,应该是数组的储存不够导致的错误,最后换了种方式写

4).运行结果

3.预习作业:使用函数删除字符串中的字符

输入一个正整数 repeat (0<repeat<10),做 repeat 次下列运算:

输入一个字符串 str,再输入一个字符 c,将字符串 str 中出现的所有字符 c 删除。

要求定义并调用函数delchar(str,c), 它的功能是将字符串 str 中出现的所有 c 字符删除,函数形参str的类型是字符指针,形参c的类型是char,函数类型是void。

输入输出示例:括号内为说明,无需输入输出

输入样例::

3               (repeat=3)
happy new year  (字符串"happy new year")
a               (待删除的字符'a')
bee             (字符串"bee")
e               (待删除的字符'e')
111211          (字符串"111211")
1               (待删除的字符'1')

输出样例:

result: hppy new yer    (字符串"happy new year"中的字符'a'都被删除)
result: b               (字符串"bee"中的字符'e'都被删除)
result: 2               (字符串"111211"中的字符'1'都被删除)

1).实验代码

#include<stdio.h>
void delchar(char str[], char c);
int main(void)
{
    int repeat;
    char str[100], c;
    scanf("%d\n", &repeat);
    while(repeat--)
    {
        gets(str);
        scanf("%s", &c);
        printf("result: ");
        delchar(str,c);
    }
    return 0;
}

 void delchar(char str[], char c)
 {
    while(*str!='\0')
    {
        if(*str!=c)
        {
            putchar(*str);
        }

    }

}

2).设计思路

3).问题和解决方案

4).运行结果

4. 挑战作业

思路:目前还是比较疑惑

学习总结

| 周/日期 | 这周所花的时间 | 代码行 | 学到的知识点 | 目前比较疑惑的问题 |
| -------- | :-----: | :----: | :-----: | :----------😐:---------😐
|3/11-3/17 | 4.5小时 | 78 | 二维数组 | 对多维数组概念模糊 |
|3/18-3/23 | 6小时 | 145 | 选择排序法,二分排序 | 暂无 |
| 3/24-3/31 | 6小时 | 138 | 字符串 | 暂无 |
| 4/1-4/7 | 4小时 | 138 | 指针 | 暂无 |
|4/8-4/15 | 5小时 | 254 | 冒泡排序 | 不能灵活运用指针、数组 |

学习感悟

要多练习才能熟练运用各种知识点来解答问题。

结对感悟

较难题目不同的观点都对了解知识,解答题目有很大的帮助。

posted @ 2019-04-12 20:51  laozhupeiqi  阅读(121)  评论(0编辑  收藏  举报