鱼C论坛

 找回密码
 立即注册

C语言动态分配内存--malloc函数

热度 1已有 539 次阅读2016-4-20 09:45 |个人分类:C/C++

1.很多人学习C语言的时候常常会犯这样一个错误:
先定义一个N,然后从键盘上输入N的值,接着定义一个大小为N的数组,这样做逻辑完全说得过去,恩,很有道理;
然而呢,在C99标准里这种做法是不被允许的,不过本人亲测,在codeblocks和linux下gcc编译器里是可以运行的,这又是怎么回事?
看了小甲鱼(硬广告)的课才知道这是C11标准新增的内容,这是可以运行的,不过在VC6.0和VS2010就不行了,当时为舍友做一道
学校笔试题(他要申请出国),结果也是困扰我几天(本人比较笨,大家理解下!!!)
先贴题吧:
Question 2
Write a program that:
Asks the user for number of records, N
uses one array of Strings of length N to store employee names, an array of double’s of
length N to store the hourly wage, and an array of double’s to store the number of hours
worked.
reads a list of N employee names, hourly wages, and hours worked, stores them in the
corresponding arrays
calculates the gross wages for each employee as the product of hourly wages and hours
worked
calculates the tax for each employee as 10% of the gross wages
calculates net wages as gross wages minus tax
keeps track of total gross wages, total taxes, and total net wages
prints all N employee names, gross wages, tax, net wages in a table
At the end, prints total gross wages, total taxes, and total net wages


Example session:
INPUT SECTION
Please input the number of records: 5
Record 1 Employee name: John
Record 1 Hourly wage: 10
Record 1 Hours worked: 4.5
Record 2 Employee name: Jill
Record 2 Hourly wage: 11.5
Record 2 Hours worked: 5
Record 3 Employee name: Chris
Record 3 Hourly wage: 11.75
Record 3 Hours worked: 7
Record 4 Employee name: Mitchell
Record 4 Hourly wage: 14
Record 4 Hours worked: 12.5
Record 5 Employee name: Erica
Record 5 Hourly wage: 14.5
Record 5 Hours worked: 15

OUTPUT SECTION
----------------------------------------------------------
Employee name Gross pay Taxes Net pay
----------------------------------------------------------
John $ 45.00 $ 4.50 $ 40.50
Jill $ 57.50 $ 5.75 $ 51.75
Chris $ 82.25 $ 8.23 $ 74.03
Mitchell $ 175.00 $ 17.50 $ 157.50
Erica $ 217.50 $ 21.75 $ 195.75
----------------------------------------------------------
Total $ 577.25 $ 57.73 $ 519.53


解答:
简单来说就是让你从键盘上读取一个数N,然后接着输入N个人的信息(姓名,工作时薪,工作时长),接着按照示例的格式打印出来就可以了
先贴错误示例:
#include <stdio.h>

typedef struct Record
{
      char Employee[10];
      float H_wage;
      float H_worked;
      float G_pay;
      float Taxes;
      float N_pay;
}Record;

int main()
{
      int i , N;
      float G_pay = 0, Taxes = 0, N_pay = 0;

      printf("Please input the number of record:");
      scanf("%d", &N);
      Record record[N];  //此处引用了键盘读取的N,这在C99中是不可以的

      for (i = 0; i < N; i++)
      {
            printf("Record %d Employee name:", i + 1);
            scanf("%s", &record[i].Employee);
            printf("Record %d Hourly wage:", i + 1);
            scanf("%f", &record[i].H_wage);
            printf("Record %d Hours worked:", i + 1);
            scanf("%f", &record[i].H_worked);

            record[i].G_pay = record[i].H_wage * record[i].H_worked;
            record[i].Taxes = record[i].G_pay * 0.1;
            record[i].N_pay = record[i].G_pay - record[i].Taxes;
            G_pay = G_pay + record[i].G_pay;
            Taxes = Taxes + record[i].Taxes;
            N_pay = N_pay + record[i].N_pay;
      }

      printf("\n\nOUTPUT SECTION\n");
      printf("---------------------------------------------------------\n");
      printf("Employee name    Gross pay    Taxes        Net pay\n");
      printf("---------------------------------------------------------\n");
      for (i = 0; i < N; i++)
      {
            printf("%10s       $%8.2f     $%8.2f       $%8.2f\n", record[i].Employee, record[i].G_pay, record[i].Taxes, record[i].N_pay);
      }
      printf("---------------------------------------------------------\n");

      printf("     Total       $%8.2f     $%8.2f       $%8.2f\n", G_pay, Taxes, N_pay);
      return 0;
}
正确示例:
#include <stdio.h>
#include <windows.h>
#include <iostream>

typedef struct Record
{
      char Employee[10];
      float H_wage;
      float H_worked;
      float G_pay;
      float Taxes;
      float N_pay;
}Record;

int main()
{
      int i , N;
      float G_pay = 0, Taxes = 0, N_pay = 0;

      printf("Please input the number of record:");
      scanf("%d", &N);

      Record *record = (Record *)malloc(N * sizeof(Record));    //使用malloc函数进行动态分配内存,C++中用new
  
      for (i = 0; i < N; i++)
      {
            printf("Record %d Employee name:", i + 1);
            scanf("%s", &record[i].Employee);
            printf("Record %d Hourly wage:", i + 1);
            scanf("%f", &record[i].H_wage);
            printf("Record %d Hours worked:", i + 1);
            scanf("%f", &record[i].H_worked);

            record[i].G_pay = record[i].H_wage * record[i].H_worked;
            record[i].Taxes = record[i].G_pay * 0.1;
            record[i].N_pay = record[i].G_pay - record[i].Taxes;
            G_pay = G_pay + record[i].G_pay;
            Taxes = Taxes + record[i].Taxes;
            N_pay = N_pay + record[i].N_pay;
      }

      printf("\n\nOUTPUT SECTION\n");
      printf("---------------------------------------------------------\n");
      printf("Employee name    Gross pay    Taxes        Net pay\n");
      printf("---------------------------------------------------------\n");
      for (i = 0; i < N; i++)
      {
            printf("%10s       $%8.2f     $%8.2f       $%8.2f\n", record[i].Employee, record[i].G_pay, record[i].Taxes, record[i].N_pay);
      }
      printf("---------------------------------------------------------\n");

      printf("     Total       $%8.2f     $%8.2f       $%8.2f\n", G_pay, Taxes, N_pay);

      system("pause");
      return 0;
}

总之就是酱紫喽,后面的格式打印主要是printf()函数的应用,大家可以参考我日志中的C语言学习摘要中的内容,写的不好,还请笑看哈!


路过

鸡蛋

鲜花
1

握手

雷人

刚表态过的朋友 (1 人)

评论 (0 个评论)

facelist

您需要登录后才可以评论 登录 | 立即注册

小黑屋|手机版|Archiver|鱼C工作室 ( 粤ICP备18085999号-1 | 粤公网安备 44051102000585号)

GMT+8, 2024-4-25 05:32

Powered by Discuz! X3.4

© 2001-2023 Discuz! Team.

返回顶部