c语言程序设计实例

帮忙做一道C语言程序设计作业

可以参考

#include “stdio.h” /*I/O函数*/

#include “stdlib.h” /弊塌*其它说明*/

#include “string.h” /*字符串函数*/

#include “conio.h” /*屏幕操作函数*/

#include “mem.h” /*内存操作租御圆函数*/

#include “ctype.h” /*字符操作函数*/

#include “alloc.h” /*动态地址分配函数*/

struct score

{

int mingci;

char xuehao[8];

char mingzi[20];

float score[6];

}data,info[1000];

int i,j,k=0;

char temp[20],ch;

FILE *fp,*fp1;

void shuru()

{

if((fp=fopen(“s_score.txt”,”ab+”))==NULL)

{

printf(“cannot open this file.\n”);

getch();exit(0);

}

for(i=0;i=1000;i++)

{

printf(“\nPlease shuru xuehao:”);

gets(data.xuehao);

printf(“Please shuru mingzi:”);

gets(data.mingzi);

printf(“Please shuru yuwen score:”);

gets(temp);data.score[0]=atof(temp);

printf(“Please shuru shuxue score:”);

gets(temp);data.score[1]=atof(temp);

printf(“Please input yingyu score:”);

gets(temp);data.score[2]=atof(temp);

printf(“Please shuru wuli score:”);

gets(temp);data.score[3]=atof(temp);

printf(“Please shur huaxue score:”);

gets(temp);data.score[4]=atof(temp);

data.score[5]=data.score[0]+data.score[1]+data.score[2]+data.score[3]+data.score[4];

fwrite(data,sizeof(data),1,fp);

printf(“another?y/n”);

ch=getch();

if(ch==’n’||ch==’N’)

break;

} fclose(fp);

}

void xianshi()

{

float s;int n;

if((fp=fopen(“s_score.txt”,”rb+”))==NULL)

{

printf(“Cannot reading this file.\n”);

exit(0);

}

for(i=0;i=1000;i++)

{

if((fread(info[i],sizeof(info[i]),1,fp))!=1)

break;

}

printf(“\nxuehao mingzi yuwen shuxue yingyu wuli huauxue zhongfen\n”);

for(j=0,k=1;ji;j++,k++)

{

info[j].mingci=k;

printf(“拆纯%6s %8s %3.1f %3.1f %3.1f %3.1f %3.1f %3.1f\n”,info[j].xuehao,info[j].mingzi,info[j].score[0],info[j].score[1],info[j].score[2],info[j].score[3],info[j].score[4],

info[j].score[5]);

}

getch();

fclose(fp);

}

void xiugai()

{

if((fp=fopen(“s_score.txt”,”rb+”))==NULL||(fp1=fopen(“temp.txt”,”wb+”))==NULL)

{

printf(“Cannot open this file.\n”);

exit(0);

}

printf(“\nPLease shuru xiugai xuehao:”);

scanf(“%d”,i); getchar();

while((fread(data,sizeof(data),1,fp))==1)

{

j=atoi(data.xuehao);

if(j==i)

{

printf(“xuehao:%s\nmingzi:%s\n”,data.xuehao,data.mingzi);

printf(“Please shuru mingzi:”);

gets(data.mingzi);

printf(“Please shuru yuwen score:”);

gets(temp);data.score[0]=atof(temp);

printf(“Please shuru shuxue score:”);

gets(temp);data.score[1]=atof(temp);

printf(“Please input yingyu score:”);

gets(temp);data.score[2]=atof(temp);

printf(“Please input wuli score:”);

gets(temp);data.score[3]=atof(temp);

printf(“Please input huaxue score:”);

gets(temp);data.score[4]=atof(temp);

data.score[5]=data.score[0]+data.score[1]+data.score[2]+data.score[3]+data.score[4];

} fwrite(data,sizeof(data),1,fp1);

}

fseek(fp,0L,0);

fseek(fp1,0L,0);

while((fread(data,sizeof(data),1,fp1))==1)

{

fwrite(data,sizeof(data),1,fp);

}

fclose(fp);

fclose(fp1);

}

void chazhao()

{

if((fp=fopen(“s_score.txt”,”rb”))==NULL)

{

printf(“\nCannot open this file.\n”);

exit(0);

}

printf(“\nPLease shuru xuehao chakan:”);

scanf(“%d”,i);

while(fread(data,sizeof(data),1,fp)==1)

{

j=atoi(data.xuehao);

if(i==j)

{

printf(“xuehao:%s mingzi:%s\nyuwen:%f\n shuxue:%f\n yingyu:%f\n wuli:%f\n huaxue:%f\n “,data.xuehao,data.mingzi,data.score[0],data.score[1],data.score[2],data.score[3],data.score[4],data.score[5]);

}getch();

}

}

void shanchu()

{

if((fp=fopen(“s_score.txt”,”rb+”))==NULL||(fp1=fopen(“temp.txt”,”wb+”))==NULL)

{

printf(“\nopen score.txt was failed!”);

getch();

exit(0);

}

printf(“\nPlease input ID which you want to del:”);

scanf(“%d”,i);getchar();

while((fread(data,sizeof(data),1,fp))==1)

{

j=atoi(data.xuehao);

if(j==i)

{

printf(“Anykey will delet it.\n”);

getch();

continue;

}

fwrite(data,sizeof(data),1,fp1);

}

fclose(fp);

fclose(fp1);

remove(“s_score.txt”);

rename(“temp.txt”,”s_score.txt”);

printf(“Data delet was succesful!\n”);

printf(“Anykey will return to main.”);

getch();

}

main()

{

while(1)

{

clrscr(); /*清屏幕*/

gotoxy(1,1); /*移动光标*/

textcolor(YELLOW); /*设置文本显示颜色为黄色*/

textbackground(BLUE); /*设置背景颜色为蓝色*/

window(1,1,99,99); /* 制作显示菜单的窗口,大小根据菜单条数设计*/

clrscr();

printf(“*************welcome to use student manage******************\n”);

printf(“*************************menu********************************\n”);

printf(“* ========================================================= * \n”);

printf(“* 1shuru 2xiugai * \n”);

printf(“* 3shanchu 4chazhao * \n”);

printf(“* 5xianshi 6exit * \n”);

printf(“* * \n”);

printf(“* ——————————————————— * \n”);

printf(” Please input which you want(1-6):”);

ch=getch();

switch(ch)

{

case ‘1’:shuru();break;

case ‘2’:xiugai(); break;

case ‘3’:shanchu(); break;

case ‘4’:chazhao(); break;

case ‘5’:xianshi(); break;

case ‘6’:exit(0);

default: continue;

}

}

}

c语言程序设计例题

题目1

#include “stdio.h”

void main(void)

{

int n=5,m=10,i=1;

long sum=1;

for(;i=n;i++)

{

sum*=i;

}

printf(“\n5!=%d”,sum);

for(i=1;i=10;i++)

{

sum*=i;

}

printf(“\n10!=%d”,sum);

}

题目2

#include “stdio.h”

#include “string.h”

struct Student

{

char s_Name[25];

long n_Code;

int n_English;

int n_Math;

int n_Computer;

}student_1,student_2;

void main(void)

{

printf(“\nStudent1:\nName:”);

scanf(“%s”,student_1.s_Name);

printf(“StudentNum:”);

scanf(“%d”,student_1.n_Code);

printf(“English Score:”);

scanf(“%d”,student_1.n_English);

printf(“Math Score:”);

scanf(“%d”,student_1.n_Math);

printf(“Computer Score:”);

scanf(“%d”,student_1.n_Computer);

printf(“\nStudent2:\nName:”);

scanf(“%s”,student_2.s_Name);

printf(“StudentNum:”);

scanf(“%d”,student_2.n_Code);

printf(“English Score:”宽姿);

scanf(“%d”,student_2.n_English);

printf(“Math Score:”);

scanf(“%d”,student_2.n_Math);

printf(“Computer Score:”);

scanf(“%d”,student_2.n_Computer);

printf(“\nStudent1:\nName:%s\nStudent Number:%d\慎森绝nEnglish Score:%d\nMath Score:%d\nComputer Score:%d\春誉n”,student_1.s_Name,student_1.n_Code,student_1.n_English,student_1.n_Math,student_1.n_Computer);

printf(“\nStudent2:\nName:%s\nStudent Number:%d\nEnglish Score:%d\nMath Score:%d\nComputer Score:%d\n”,student_2.s_Name,student_2.n_Code,student_2.n_English,student_2.n_Math,student_2.n_Computer);

}

c语言程序设计实例

C语言程序设计求详解过程

首先,你要先明白,这个程序有两部分组成:函数fun(..)和主函数main();main()调用fun(..)函数实现算法运算。

首先介绍主函数main():主要负责打印输出结果,for循环由k控制,连着调用fun(..)函数两次,并打印。

fun(..)函数:实现算法运算。

第一次调用:x == 1(参数a的值), z == 4(在z++后), y==1(在y++后),所以此时返回值(x + y + z) = 6

第二次隐核腔调用:x == 1(参数a的值), z == 5(在第一次z++后,本次有执行了一次z++), y==2(在第一次y++后,本次有执行了一次y++),所以此时返回值(x + y + z) = 8

直接运行了一次你的代码,输出结果如下:

注意事项以及建议:

1、这个道题目我觉得是对static的考察。

2、static 修改的变量在整个程序生命周期内只会被定义一次,并且在程序生命周期灶衫结束前,其上次的运行结果会被保存,供下次使用。

3、在fun(..)函数中, y,z是static类型。

如果以上问题对你有帮助氏氏,请采纳一下,谢谢。

C语言程序设计

#include stdio.h

#include stdlib.h

#include string.h

#define MAX_NUM 50

#define MAX_LINE 256

struct student {

    char name[10];

    float math;

    float chinese;

    float eng;

    float aver;

    char  grade;

};

const char * student_file = “student.txt”;

const char * grade_file   = “grade.txt”;

int init(struct student * s, int n);

int fun(struct student s[], int n);

void sort(struct student * s, int n);

void display(struct student * s, int n);

void save_student(struct student * s, int n, const char * file);

void load_student(const char * file);

int main(int argc, char *argv[])

{

    int num = 0;

    struct student st[MAX_NUM];

    num = init(st, MAX_NUM);

    printf(“\n等级为D的人数: %d\n”, fun(st, num));

    save_student(st, num, student_file);

    sort(st, num);

    save_student(st, num, grade_file);

    display(st, num);

    printf(“\n所有学生信息\n”);

    load_student(student_file);

    printf(“\n排序后的学生信息\n”);

    load_student(grade_file);

    return 0;

}

/* 初始化 */

int init(struct student * s, int n)

{

    int num = 0, i = 0;

    if (0 == s || 0 = n || MAX_NUM  n)

        return 0;

    printf(“请输入学生数量[不大于50]: “);

    fflush(stdout);

    scanf(“%d”, num);

  姿尺卖  while (0 = num || n  num)

    {

        fflush(stdin);

        printf(“无效的参数,请输入学生数量[不大于50]: “);

        fflush(stdout);

        scanf(“%d”, num);

    }

    for (i = 0; i  num; ++i)

    {

        printf(“请输入第%d个学生的信息: “, i + 1);

     迹逗   fflush(stdout);

        scanf(“%9s %f %f %f”, s[i].name, s[i].math, s[i].chinese, s[i].eng);

        fflush(stdin);

    }

    return num;

}

/* 计算平均成绩并统计等级为D的人数 */

int fun(struct student s[], int n)

{

    int i = 0, cnt = 0;

    if (0 == s || 0 = n || MAX_NUM  n)

        return 0;

    for (i = 0; i  n; ++i)

    {

        s[i].aver = (s[i].math + s[i].chinese + s[i].eng) / 3.0;

        if (70 = s[i].aver  85  s[i].aver)

            s[i].grade = ‘B’;

        else if (60 = s[i].aver  70  s[i].aver)

            s[i].grade = ‘C’;

        else if (60  s[i].aver)

            s[i].grade = ‘D’;

        else

            s[i].grade = ‘A’;

        if (‘D’ == s[i].grade)

            ++cnt;

    }

    return cnt;

}

/* 降序排列 */

void sort(struct student * s, int n)

{

    int i = 0, j = 0;

    struct student tmp;

    if (0 == s || 0 困慧= n || MAX_NUM  n)

        return 0;

    for (i = 0; i  n – 1; ++i)

    {

        for (j = 0; j  n – 1 – i; ++j)

        {

            if (s[j].aver  s[j + 1].aver)

            {

                tmp = s[j];

                s[j] = s[j + 1];

                s[j + 1] = tmp;

            }

        }

    }

}

void display(struct student * s, int n)

{

    int i = 0;

    if (0 == s || 0 = n || MAX_NUM  n)

        return 0;

    printf(“所有学生的信息如下: \n”);

    for (i = 0; i != n; ++i)

    {

        printf(“%d. name[%s] math[%.1f] chinese[%.1f] eng[%.1f] aver[%.1f] grade[%c]\n”,

               i + 1, s[i].name, s[i].math, s[i].chinese, s[i].eng, s[i].aver, s[i].grade);

    }

}

/* 保存学生信息到指定文件 */

void save_student(struct student * s, int n, const char * file)

{

    int i = 0;

    FILE * fp = 0;

    char buf[MAX_LINE] = {0};

    if (0 == s || 0 = n || MAX_NUM  n || 0 == file)

        return 0;

    if (fp = fopen(file, “w”))

    {

        for (i = 0; i != n; ++i)

        {

            sprintf(buf, “%s %.1f %.1f %.1f %.1f %c\r\n”,

                    s[i].name, s[i].math, s[i].chinese, s[i].eng, s[i].aver, s[i].grade);

            fwrite(buf, 1, strlen(buf), fp);

            memset(buf, 0x00, MAX_LINE);

        }

        fclose(fp);

    }

}

/* 加载指定文件中的信息 */

void load_student(const char * file)

{

    char buf[MAX_LINE] = {0};

    FILE * fp = 0;

    if (0 == file)

        return ;

    if (fp = fopen(file, “r”))

    {

        while (fgets(buf, MAX_LINE, fp))

            printf(“%s”, buf);

        fclose(fp);

    }

}

本文来自投稿,不代表【】观点,发布者:【

本文地址: ,如若转载,请注明出处!

举报投诉邮箱:253000106@qq.com

(0)
打赏 微信扫一扫 微信扫一扫 支付宝扫一扫 支付宝扫一扫
上一篇 2024年3月22日 12:00:14
下一篇 2024年3月22日 12:08:40

相关推荐

  • linux重定向实例讲解,linux输出重定向到文件命令

    linux中的重定向问题 首先是command file将标准输出重定向到file中, 2&1 是标准错误拷贝了标准输出的行为,也就是同样被重定向到file中,最终结果就是标准输出和错误都被重定向到file中。 不管是还是2都属于追加重定向。也就说,新重定向的内容不会覆盖掉原有文件里的内容,而是另起一行。只不过,是只对正确的内容或者结果进行重定向;而…

    2024年5月23日
    4200
  • c语言程序设计视频教程,c语言程序设计视频教程下载

    哪有C语言程序设计视频教程(曾怡)的课件? C程序设计视频教程(曾怡):本套视频教程由曾怡副教授讲解,使用教材为:《C程序设计》 谭浩强 清华大学出版社出版。是难得的C语言学习视频教程。全程共30讲,每讲45分钟左右,CSF视频格式。 有大量的视频教程,什么专业的都有,c语言的很多,全部免费下载,无需注册。去首页搜 c语言 视频教程 你会找到你想要的。下载的…

    2024年5月23日
    6200
  • javaweb开发实战经典高级案例篇,java web开发实例

    关于javaweb,有哪些令你称之“惊艳”的书? 1、毛遂自荐,推荐一本书《Servlet/JSP深入详解》,惊艳不敢说,让你掌握JavaWeb开发,弄清JavaWeb程序的原理没什么问题,之后再学学SpringMVC框架就可以了。 2、《thinkinginjava》中文版叫《Java编程思想》,是Java学习领域的圣经,也是入门书。 3、《精通java …

    2024年5月22日
    3900
  • linux下r连接oracle,linux连接oracle实例

    如何在linux下安装oracle 1、用“make dep”、“make clean”、“make bzImage”命令重新编译内核,并用新内核启动计算机。 2、首先需要检查系统版本,然后从Oracle官方文档中找到对应的Linux版本。 3、创建 Oracle 组和用户帐户 接下来,创建用于安装和维护 Oracle 10g 软件的 Linux 组和用户帐…

    2024年5月22日
    4600
  • java语言程序设计第五章答案,java语言程序设计基础篇第五章答案

    java语言程序设计(第三版)清华大学出版社答案 1、请编一程序,用赋初值的方法使cl、cccc5五个变量的值分别为,’C’、’h’、’i’、’n’、’a’,经过运算,使ccccc5分别变为’G’、’l’、’m’、’r’、’e’,并输出。 2、设计一个Array类,添加一个整形数组,添加构造方法对数组赋初值。为Array类添加数组求和方法,添加返回求和值的方…

    2024年5月22日
    4000
  • linux底层驱动开发实例,linux底层驱动开发好吗

    新手学习嵌入式linux驱动开发有没有什么步骤? 1、方法:使用linux—〉linxu系统编程开发—〉驱动开发和分析linux内核。先看深,那主讲原理。看几遍后,看情景分析,对照深看,两本交叉,深是纲,情是目。剖析则是0.11版,适合学习。最后深入代码。 2、嵌入式linux上层应用,包括QT的GUI开发 嵌入式linux系统开发 嵌入式linux驱动开发…

    2024年5月22日
    4500
  • java语言程序设计吕凤翥,java语言程序设计吴倩

    推荐几本C++的顶级参考文献 c语言程序设计方面的知识,那么想要了解的文献就非常的多了,建议你还是从最基础的一些学科开始练习,因为这些基础的参考文献才是最有用的,比如说c语言入门。 食品安全文的参考文献可以参考食品安全监督管理局发表的标准。 论文常用的参考文献类型包括学术期刊文章、学术会议论文、书籍、学位论文、报告、网页和其他学术资源。下面将对这些常见的参考…

    2024年5月22日
    3900
  • java卡片布局实例,javacard开发教程

    JAVA应用布局 1、请检查您输入的网址是否正确。 如果您不能确认您输入的网址,请浏览百度更多页面,来查看您所要访问的网址。 2、用两个层就解决问题了,一个与jframe一样大,里面的一个层,设置它的margin就可以了。 3、在Java语言中,提供了各种布局管理器,以便大家能够达到这一目的。通常我们最常使用的布局管理器是:Border Layout(边框布…

    2024年5月22日
    4900
  • c语言程序设计学习网站,c语言程序设计网站有哪些

    C语言程序设计视频教程下载地址? 1、链接:提取码: f1bt 谭浩强老师C语言视频教程是2007年5月17日发布的网络课程,参考于C语言程序设计。 2、挺适合像我这样0基础的,一听就能听懂的那种,最吸引我的就是他讲的原理比喻特别好理解,比如把变量比喻成装东西的箱子,我一下就理解了当初一直不理解的变量赋值,还有很多类似这样的比喻举例,感觉挺好理解的。 3、最…

    2024年5月22日
    6600
  • 《c语言程序基础,c语言程序设计基础

    c语言程序基础 c语言入门基础知识点有:c语言的命名;变量及赋值;基本数据类型;格式化输出语句;强制类型转换;不可改变的常量;自动类型转换。C语言是一种通用的、面向过程式的计算机程序设计语言。 任何一个c语言程序都必须包括以下格式: 这是c语言的基本结构,任何一个程序都必须包含这个机构。括号内可以不写任何内容,那么该程序将不执行任何结果。 C语言的风格 由于…

    2024年5月22日
    5500

发表回复

登录后才能评论



关注微信