首页 > 其他分享 >实验任务6

实验任务6

时间:2022-12-27 22:11:39浏览次数:27  
标签:count include int 任务 实验 printf txt t%

#include <stdio.h>
#include <stdlib.h>
int main()
{
char cr;
FILE *fp;
long count;
if((fp=fopen("data4.txt","r"))==NULL)
{
printf("Can't open file.\n");
system("pause");
exit(1);
}
count = 0;
while((cr=fgetc(fp))!=EOF)
{
if(cr!=' '&&cr!='\n'&&cr!='\t')
count++;
}
fclose(fp);
printf("date4.txt中共有 %ld 个字符。\n",count);
system("pause");
return 0;
}

 

 

#include <stdio.h>
#include <string.h>
#include <stdlib.h>

#define N 10

typedef struct {
long int id;
char name[20];
float objective; // 客观题得分
float subjective; // 操作题得分
float sum;
char level[10];
} STU;

// 函数声明
void input(STU s[], int n);
void output(STU s[], int n);
void process(STU s[], int n);

int main() {
STU stu[N];

printf("从文件读入%d个考生信息: 准考证号,姓名,客观题得分(<=40),操作题得分(<=60)\n", N);
input(stu, N);

printf("\n对考生信息进行处理: 计算总分,确定等级\n");
process(stu, N);

printf("\n打印考生完整信息, 并保存到文件中");
output(stu, N);

return 0;
}

// 从文本文件examinee.txt读入考生信息:准考证号,姓名,客观题得分,操作题得分
void input(STU s[], int n) {
int i;
FILE *fin;

fin = fopen("examinee.txt", "r");
if (fin == NULL) {
printf("fail to open file\n");
exit(0);
}

while (!feof(fin)) {
for (i = 0; i < n; i++)
fscanf(fin, "%ld %s %f %f", &s[i].id, s[i].name, &s[i].objective, &s[i].subjective);
}

fclose(fin);
}

// 输出考生完整信息: 准考证号,姓名,客观题得分,操作题得分,总分,等级
// 不仅输出到屏幕上,还写到文本文件result.txt中
void output(STU s[], int n) {
FILE *fout;
int i;

// 输出到屏幕
printf("\n");
printf("准考证号\t姓名\t客观题得分\t操作题得分\t总分\t\t等级\n");
for (i = 0; i < n; i++)
printf("%ld\t\t%s\t%.2f\t\t%.2f\t\t%.2f\t\t%s\n", s[i].id, s[i].name, s[i].objective, s[i].subjective, s[i].sum, s[i].level);

// 保存到文件
fout = fopen("result.txt", "w");
if (!fout) {
printf("fail to open or create result.txt\n");
exit(0);
}

fprintf(fout, "准考证号\t\t姓名\t客观题得分\t操作题得分\t总分\t\t等级\n");

for (i = 0; i < n; i++)
fprintf(fout, "%ld\t\t%s\t%.2f\t\t%.2f\t\t%.2f\t\t%s\n", s[i].id, s[i].name, s[i].objective, s[i].subjective, s[i].sum, s[i].level);

fclose(fout);
}

// 对考生信息进行处理:计算总分,排序,确定等级
void process(STU s[], int n) {
// 补足代码
// ×××
int i,k;
STU t;
for(i=0;i<n;i++){
s[i].sum=s[i].objective+s[i].subjective;
}
for(i=0;i<n-1;i++){
for(int j=0;j<n-1;j++){
if(s[j+1].sum>s[j].sum){
t=s[j+1];
s[j+1]=s[j];
s[j]=t;}
}
}
for(k=0;k<n;k++){
if(k<=n*0.1-1)
strcpy(s[k].level,"优秀");
else if(k>=n*0.1-1&&k<=n*0.5-1)
strcpy(s[k].level,"合格");
else
strcpy(s[k].level,"不合格");

}
}

 

 

 

 

#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <time.h>

typedef struct
{
long int no;
char name[20];
char cla[100];
}stu;

int main()
{
FILE *fp1;
fp1 = fopen("list.txt","r");

stu a[80];
int i = 0;
while(!feof(fp1))
{
fscanf(fp1,"%ld %s %s",&a[i].no,a[i].name,a[i].cla);
i++;
}
fclose(fp1);

time_t T;
T = time(NULL);
struct tm *tmTime;
tmTime = localtime(&T);
char* format = "%Y%m%d";
char strTime[100];
strftime(strTime, sizeof(strTime), format, tmTime);
strcat(strTime,".txt");

FILE *fp2;
fp2 = fopen(strTime,"w");
srand((unsigned int)time(0));
int count,b[5],repeat = 0;
for(int i = 0;i < 5;i++)
{
count = rand() % 80;

for(int k = 0;k < i;k++)
if(b[k] == count) repeat = 1;
if(repeat)
{
i--;
repeat = 0;
continue;
}

b[i] = count;
fprintf(fp2,"%ld %s %s\n",a[count].no,a[count].name,a[count].cla);
printf("%ld %s %s\n",a[count].no,a[count].name,a[count].cla);

}
fclose(fp2);

return 0;
}

 

标签:count,include,int,任务,实验,printf,txt,t%
From: https://www.cnblogs.com/molsonxx/p/17009110.html

相关文章

  • [oeasy]python0033_任务管理_jobs_切换任务_进程树结构_fg
    查看进程回忆上次内容上次先进程查询​​ps-elf​​查看所有进程信息​​ps-lf​​查看本终端相关进程信息杀死进程​​kill-9PID​​给进程发送死亡信号运行多个......
  • C/C++《数据结构课程设计》任务书[2022-12-27]
    C/C++《数据结构课程设计》任务书[2022-12-27]《数据结构课程设计》任务书一、任务总体安排:班级 设计时间 地点 指导老师21软件开发 17周每周一至周五五六节 徐青翠......
  • 网络程序设计 实验5 图形化Ping工具
    实验5图形化Ping工具实验目的:用图形界面实现Ping操作。开发语言与工具:VC实验要求:1.使用MFC编程。2.界面上有目标地址栏,信息框和ping按钮。3.使用原始套接字实......
  • 3.1任务创建-列表&列表项&初始化
     前言:FreeRTOS内核调度大量使用了列表(list)和列表项(listitem)数据结构,对于FreeRTOS内核来说,列表就是它最基础的部分一、列表 与C语言链表类似。 (1)、(5)行是宏检查列......
  • 实验六
    #include<stdio.h>#include<stdlib.h>intmain(){charcr;FILE*fp;longcount;if((fp=fopen("data4.txt","r"))==NULL){printf(......
  • 实验6
    #include<stdio.h>#define_CRT_SECURE_NO_WARNINGS#include<stdlib.h>intmain(){charch;intcount=0;FILE*fp;fp=fopen("data4.txt","r");......
  • 实验六
    #include<stdio.h>#include<string.h>#include<stdlib.h>intmain(){ FILE*fp; fp=fopen("data4.txt","r"); charch='a'; inti=0; do { ch=fgetc(fp);......
  • 实验6
    #include<stdio.h>intmain(){FILE*fp;fp=fopen("data4.txt","r");charch;intcount=0;do{ch=fgetc(fp);......
  • Python之多任务编程概述
    一、疑问利用现学知识能够让两个函数或者方法同时执行吗?不能,因为之前所写的程序都是单任务的,也就是说一个函数或者方法执行完成另外一个函数或者方法才能执行,要想实现这种......
  • Python之多任务编程进程
    一、进程概述1.进程的介绍在Python程序中,想要实现多任务可以使用进程来完成,进程是实现多任务的一种方式。2.进程的概念一个正在运行的程序或者软件就是一个进程,它是操作系......