栏目分类:
子分类:
返回
名师互学网用户登录
快速导航关闭
当前搜索
当前分类
子分类
实用工具
热门搜索
名师互学网 > IT > 系统运维 > 运维 > Linux

2021-12-06 自动化专业C语言上机作业参考答案20

Linux 更新时间: 发布时间: IT归档 最新发布 模块sitemap 名妆网 法律咨询 聚返吧 英语巴士网 伯小乐 网商动力

2021-12-06 自动化专业C语言上机作业参考答案20

上机练习20 p312.c
 
#include 
#include 
#include 

// 定义匿名结构,给出别名SHANGPING,即商品
typedef struct
{
    char name[20]; // 品名
    char size[12]; // 规格
    long number;   // 数量
    float price;   // 单价
} SHANGPING;

int main(void)
{
    int n = 0;
    char inputName[20]; // 用于接收输入品名
    SHANGPING tmp;
    FILE *fp;

    // 输入
    printf("Please input shang pin pin ming:");
    gets(inputName);
    // 打开文件,以二进制只读方式打开文件
    fp = fopen("sp.dat", "rb");
    if (NULL == fp)
    {
        // 题设没有给要求,也可以适当给用户提示
        exit(0);
    }
    // 处理:读文件,并处理数据
    printf("ncha zhao qing kuang:n");
    // 没有到尾,就一直处理
    while (!feof(fp))
    {
        // 一次读取一个单位【即一个结构】的数据,即一个结构体大小的内存空间,使用块读写方式
        if (fread(&tmp, sizeof(SHANGPING), 1, fp) == 1)
        {
            // 如果比对,找到了,则计数并输出,使用串对比
            if (strcmp(tmp.name, inputName) == 0)
            {
                n++;	// 计数,相当于是标志,有的话就一定不会是0
                printf("%s,%s,%d,%.2fn", tmp.name, tmp.size, tmp.number, tmp.price);
            }
        }
    }
    if (0 == n) // 如果一个都没有读到
    {
        printf("mei you shang pin :%sn", inputName);
    }

    // 关闭文件
    fclose(fp);
    fp = NULL; // 防止野指针
    return 0;
}

p315.c

#include 
#include 
#include 

int main(void)
{
    
    char filename[128];
    char str[21];
    FILE *fp;
    int total, first, last;
    // 文件的行数(行之间以’n’为分隔、每行的长度不定但均≤200个字节)
    char line[201]; // 一行
    char ch;        // 一次读一个字符
    int i;          // 每一行下标计数器

    printf("input the file's name and the string: ");
    scanf("%s %s", filename, str);

    // 开文件
    fp = fopen(filename, "r");
    if (NULL == fp)
    {
        printf("nfile open error!");
        exit(1);
    }
    // 处理
    printf("------------------------File content:----------------------n");
    i = 0;			// 行上字符数计数器
    total = 0;		// 行数
    first = -1;		// 记录首次出现
    last = -1;		// 记录最后出现
    while (!feof(fp))
    {
        ch = (char)fgetc(fp); // 从文件读入一个字符,存入ch中
        if (ch != EOF && ch != 'n')
        {
            // 没有碰到回车,即一行
            line[i] = ch; // 存入一行
            i++;
        }
        else
        {
            // 换行,或是结束了
            total++;
            line[i] = '';     // 补齐一行为一个字符串,每一行做成一个串
            printf("%s", line); // 输出行
            if (ch == 'n')     // 补充输出回车符
            {
                printf("n");
            }

            // 先全部转为小写,再才做比较有没有出现,即比较时,忽略大小写
            strlwr(str);
            strlwr(line);
            if (strstr(line, str) != NULL) // 在line中找str
            {
                // 第一匹配时,做首出行行标记录
                if (first == -1)
                {
                    first = total;
                }
                last = total; // 运行过程中,last不断刷新
            }
            i = 0; // 下一行的开始
        }
    }
    printf("n------------------------File summary:----------------------n");
    printf("%d lines, first line: %d, last line: %dn", total, first, last);
    // 关文件
    fclose(fp);
    fp = NULL;

    return 0;
}

p317.c
 
#include 

int main(void)
{
    int ch;
    char infile[100], outfile[100];
    FILE *in, *out;
    // 文件名给出
    printf("Please input sourceFilename: ");
    gets(infile);
    printf("Please input destinationFilename: ");
    gets(outfile);
    // 源文件操作
    in = fopen(infile, "rb");
    // 打开源文件
    if (in == NULL)
    {
        printf("nsource File (%s) Open Error!n", infile);
        return 2;
    }
    // 打开目标文件
    out = fopen(outfile, "wb");
    if (out == NULL)
    {
        fclose(in);
        in = NULL;
        printf("ndestination File (%s) Create Error!n", outfile);
        return 3;
    }
    // 过程出错检测
    while (!feof(in))
    {
        ch = fgetc(in);	// 因为fgetc返回的是一个int,所以一定用int型变量来接收值
        if (ch != EOF)
        {
            if (fputc(ch, out) == EOF)
            {
                printf("nwriting destination File (%s) Error!n", outfile);
                fclose(in);		// 写文件时出错,也要关闭之前打开的文件
                fclose(out);
                return 4;
            }
        }
    }

    printf("ncopy %s to %s successed!n", infile, outfile);
    
    // 最后关闭文件
    fclose(in);
    fclose(out);
    // 置空,养成防止野指针的习惯
    in = out = NULL;

    return 0;
}
p318.c
 
#include 
#include 
#include 

struct goods
{
    char name[18];
    char size[12];
    long number;
    float price;
};
int main(void)
{
    int n;                 // 可能有多条,可能没有,计数用
    char inputName[18];    // 输入名字
    struct goods readGood; // 用于读入
    FILE *fp;              // 操作文件

    // 输入
    printf("Please input shang pin pin ming:");
    gets(inputName);

    // 开文件
    fp = fopen("sp38.dat", "rb");
    if (NULL == fp)
    {
        printf("file open error!n");
        exit(1);
    }
    // 处理
    // 提示:使用fread,sizeof(结构体)的方法肯定不对,应单项属性逐个fread===>不能整体结构读入
    n = 0;
    printf("ncha zhao qing kuang:n");
    while (1)
    {
        // 使用块方式读文件,一个成员一个成员地读
        fread(readGood.name, sizeof(readGood.name), 1, fp);      // 读入名称
        fread(readGood.size, sizeof(readGood.size), 1, fp);      // 读入规格
        fread(&readGood.number, sizeof(readGood.number), 1, fp); // 读入数量
        // 以最后读入的单价为准,进行判断:一个结构的成员读完整没有
        if (1 == fread(&readGood.price, sizeof(readGood.price), 1, fp)) // 读入单价
        {
            if (strcmp(readGood.name, inputName) == 0)	// 串相同比较
            {
                n++;
                printf("n%s,%s,%ld,%.2f", readGood.name, readGood.size, readGood.number, readGood.price);
            }
        }
        else //如果读不完整,就不用处理了,中止
        {
            break;
        }
    }

    // 判断没有找到的情况
    if (0 == n)
    {
        printf("mei you shang pin :%s", inputName);
    }
    // 关文件
    fclose(fp);
    fp = NULL;

    return 0;
}

p319.c
 
#include 
#include 
#include 

struct good
{
    //品名(17字节的字符串)、规格(12字节的字符串)、数量(3字节的整数)、单价(float实数)
    char name[17];
    char size[12];
    int number; // 注意,题意有意为之:三个字节的整数,即在文件中,会把三个字节存一个整数
    float price;
};
int main(void)
{
    int n;
    struct good tmp;
    char inputName[17];
    FILE *fp;

    // 输入
    printf("Please input shang pin pin ming:");
    gets(inputName);

    // 开文件
    fp = fopen("sp36.dat", "rb");
    if (NULL == fp)
    {
        printf("file open error!n");
        exit(1);
    }
    // 处理
    n = 0;
    printf("ncha zhao qing kuang:n");
    while (1)
    {
        // 块读取
        fread(tmp.name, sizeof(tmp.name), 1, fp);
        fread(tmp.size, sizeof(tmp.size), 1, fp);
        fread(&tmp.number, 3, 1, fp); // 即在文件中,会把三个字节存一个整数,也就是:直接扫描3个byte即可
        if (1 == fread(&tmp.price, sizeof(tmp.price), 1, fp))
        {
            // 读入成功,进行判断
            if (strcmp(inputName, tmp.name) == 0)
            {
                n++;
                printf("n%s,%s,%d,%.2f", tmp.name, tmp.size, tmp.number, tmp.price);
            }
        }
        else // 读入失败了,则退出循环读取
        {
            break;
        }
    }
    // 未找到呢
    if (0 == n)
    {
        printf("mei you shang pin :%s", inputName);
    }
    // 关文件
    fclose(fp);
    fp = NULL;

    return 0;
}
p329.c
 
#include 
#include 
#include 
#define N 50

// 因为按均分排序,而均分要处理得到,定义数据结构,再整体操作
typedef struct student
{
    float avg;
    int id;
    char name[7];
    char gender[3];
    int year;
    int scoreC, scoreE, scoreM;
} STU;

int main(void)
{
    FILE *fp;         // 文件
    int count = 0;    // 计数器
    STU stu[N], tStu; // 结构数组
    char tmp[500];    // 清第一行

    // 临时使用结构成员
    int id;
    char name[7];
    char gender[3];
    int year;
    int scoreC, scoreE, scoreM;

    // 排序用
    int i, j;

    // 打开文件
    fp = fopen("学生成绩.txt", "r");
    if (NULL == fp)
    {
        puts("学生成绩文件“学生成绩.txt”打开失败,请仔细检查文件名是否正确,对应文件是否存在!");
        exit(1);
    }
    // 要选读入
    // 处理第一行,不用: 学号		姓名	性别	出生年	C语言	英语	微积分
    fgets(tmp, 500, fp);

    // 读入数据行到内存结构数组中,读入时完成均分计算
    // 2008122624	崔艺丹	女	1989	83	87	83
    while (1)
    {
        if (7 == fscanf(fp, "%d %s %s %d %d %d %d", &id, name, gender, &year, &scoreC, &scoreE, &scoreM))
        {
            stu[count].id = id;			// 整型,直接赋值
            strcpy(stu[count].name, name);	// 串,用函数完成赋值,即串拷贝
            strcpy(stu[count].gender, gender);
            stu[count].year = year;
            stu[count].scoreC = scoreC;
            stu[count].scoreE = scoreE;
            stu[count].scoreM = scoreM;
            stu[count].avg = (float)((scoreC + scoreE + scoreM) / 3.0); // 完成计算,注意强转,否则有警告
            count++;
        }
        else
        {
            break;
        }
    }

    // 关闭文件
    fclose(fp);
    fp = NULL;

    // 再排序,将数据封装后进行处理,更方便做排序  降序!
    // 这里使用选择交换排序法,要特别注意起点和终点
    for (i = 0; i < count - 1; i++) //N个数,排N-1轮【0   count-1-1】,最后一个不用排
    {
        for (j = i + 1; j < count; j++) // 每一轮,一个数归到最左位【i+1    count-1】,要特别注意有没有取到等于
        {
            if (stu[i].avg < stu[j].avg)
            {
                tStu = stu[i];
                stu[i] = stu[j];
                stu[j] = tStu;
            }
        }
    }

    // 再输出。
    puts("名次  平均成绩  学号        姓名      性别  出生年  C语言  英语  微积分");
    for (i = 0; i < count; i++)
    {
        printf("%3d   %7.2f   %d  %-8s   %-2s    %d   %3d    %3d   %3dn",
               i + 1, stu[i].avg, stu[i].id, stu[i].name, stu[i].gender, stu[i].year,
               stu[i].scoreC, stu[i].scoreE, stu[i].scoreM);
    }

    return 0;
}
p337.c
 
#include 
#include 
#include 

// 学生数据


int main(void)
{
    char id[11];
    char name[9];
    char inputName[9];
    char gender[3];
    char course[15];
    int score;
    int i = 0;

    FILE *fp;

    fp = fopen("xscj.txt", "r");
    if (NULL == fp)
    {
        printf("文件打开失败,请检查文件名及路径是否正确、文件是否存在!");
        exit(0);
    }

    printf("请输入要查找成绩的学生姓名:");
    gets(inputName);
    printf("n查找结果为:n");
    while (fscanf(fp, "%s %s %s %s %d", id, name, gender, course, &score) != EOF)
    {
        if (strcmp(name, inputName) == 0)	// 串比较,进行查找
        {
            // 一般情况下,这个文件是按行写入数据的,读的时候,就是按行读出来就行
            // 格式化读入时,读满四个数据,即接着读下一个四个数据,中间合理间隔:空格,,n t都行
            i++;
            printf("%d %10s, %-8s, %s, %-14s, %dn", i, id, name, gender, course, score);
        }
    }
    if (0 == i)
    {
        printf("文件中没有找到姓名为“%s”的学生n", inputName);
    }
    fclose(fp);
    fp = NULL;

    return 0;
}
p338.c
 
#include 
#include 
#include 

int main(void)
{
    char course[15];
    char rCourse[15];
    int score;
    int i = 0;
    int sum = 0;
    double avg;
    FILE *fp;

    // 打开文件
    fp = fopen("xscj.txt", "r");
    if (NULL == fp)
    {
        printf("文件打开失败,请检查文件名及路径是否正确、文件是否存在!");
        exit(0);
    }

    // 输入
    printf("请输入要计算平均成绩的课程名称:");
    gets(course);
    printf("n计算结果为:n");

    // 读入时,前三个串弃用,只用第四个串和第五个整数
    while (fscanf(fp, "%*s %*s %*s %s %d", rCourse, &score) != EOF)
    {
        if (strcmp(rCourse, course) == 0)
        {
            sum += score;
            i++;
        }
    }
    // 分情况处理数据,得到结果
    if (0 == i)
    {
        printf("文件中没有名称为“%s”的课程n", course);
    }
    else
    {
        avg = (double)sum / i;
        printf("课程“%s”有 %d 人选学,平均成绩为%.1lf", course, i, avg);
    }

    // 关闭文件
    fclose(fp);
    fp = NULL;

    return 0;
}
p339.c
 
#include 
#include 
#include 

//学号(10个数字)、姓名(最多4个汉字)、性别、课程名称(最多7个汉字)、成绩(整数)
typedef struct student
{
    char id[11];
    char name[9];
    char gender[3];
    char course[15];
    int score;
} STU;

int main(void)
{
    int cnt = 0;
    int gt60 = 0;
    char inputCourse[15];
    STU stu; // 用于读入
    FILE *fp;

    // 输入
    printf("请输入要计算及格率的课程名称:");
    gets(inputCourse);

    // 开文件
    fp = fopen("xscj.txt", "r");
    if (NULL == fp)
    {
        printf("文件打开失败,请检查文件名及路径是否正确、文件是否存在!");
        exit(1);
    }
    // 处理,直接从头读到尾
    // 注意:fscanf返回的是实际读取的数据个数,出错或者到结尾返回EOF
    while (fscanf(fp, "%s %s %s %s %d", stu.id, stu.name, stu.gender, stu.course, &stu.score) != EOF)
    {
        if (strcmp(inputCourse, stu.course) == 0)	// 串比较,进行查找
        {
            cnt++;					// 计数
            if (stu.score > 60)		// 分数比较,及格判断
            {
                gt60++;				// 计数
            }
        }
    }
    printf("n计算结果为:n");
    // 输出统计结果
    if (cnt > 0)
    {
        printf("课程“%s”有 %d 人选学,其中及格 %d 人,及格率为%.1f", inputCourse, cnt, gt60, (float)gt60 / cnt);
    }
    else
    {
        printf("文件中没有名称为“%s”的课程n", inputCourse);
    }
    // 关文件
    fclose(fp);
    fp = NULL;
    return 0;
}
p340.c
 
#include 
#include 
#include 

int main(int argc, char const *argv[])
{
    FILE *fp;
    // 牌号(1个汉字+6个字符)、发生日期、违章类型(最多4个汉字)
    char sign[9];
    int year;		// 年月日分别用整型来接收,亦可使用一个整体字符串来搞定
    int month;
    int day;
    char type[9];
    char inputType[9];
    int flag = 0;
    int count = 0;

    fp = fopen("jtwz.txt", "r");
    if (fp == NULL)
    {
        printf("文件打开失败,请检查文件名及路径是否正确、文件是否存在!");
        exit(1);
    }
    printf("请输入要查找的违章类型:");
    gets(inputType);
    printf("n查找结果为:n");
    while (!feof(fp))
    {
        fscanf(fp, "%8s %4d.%2d.%2d %s", sign, &year, &month, &day, type);
        // printf("%8s %d.%02d.%02d %sn", sign, year, month, day, type);
        if (strcmp(inputType, type) == 0)
        {
            flag = 1;
            count++;
            printf("%d %8s, %d.%02d.%02d, %sn", count, sign, year, month, day, type);
        }
    }

    if (flag == 0)
    {
        printf("文件中没有找到“%s”的记录n", inputType);
    }

    fclose(fp);
    fp = NULL;
    return 0;
}
转载请注明:文章转载自 www.mshxw.com
本文地址:https://www.mshxw.com/it/642237.html
我们一直用心在做
关于我们 文章归档 网站地图 联系我们

版权所有 (c)2021-2022 MSHXW.COM

ICP备案号:晋ICP备2021003244-6号