实验六(代码片段)

q1831726125 q1831726125     2022-12-20     447

关键词:

 

1.验证性实验

技术图片
#include <iostream>
#include <fstream>   
#include <string>
#include <cstdlib>
using namespace std;

int main() 
    string filename1, filename2, newfilename;
    
    cout << "输入要合并的两个文件名: " ;
    cin >> filename1 >> filename2;
    cout << "输入合并后新文件名: " ;
    cin >> newfilename;
    
    ofstream fout;        // 输出文件流对象 
    ifstream fin;        // 输入文件流对象 
    
    
    fin.open(filename1);  // 将输入文件流对象fin与文件filename1建立关联 
    if(!fin.is_open())   // 如果打开文件失败,则输出错误提示信息并退出 
        cerr << "fail to open file " << filename1 << endl;
        system("pause");
        exit(0);    
    
    
    fout.open(newfilename);    // 将输出文件流对象fout与文件newfilename建立关联 
    if(!fin.is_open())   // 如果创建/打开文件失败,输出错误提示信息并退出  
        cerr << "fail to open file " << newfilename << endl;
        system("pause");
        exit(0);
    
    
    char ch;
    
    // 从文件输入流对象fin中获取字符,并将其插入到文件输出流对象fout中 
    while(fin.get(ch)) 
        fout << ch;
    
    fin.close();  // 关闭文件输入流对象fin与文件filename1的关联 
    
    fout << endl; // 向文件输出流对象fout中插入换行 
    
    
    fin.open(filename2);  // 将输入文件流对象fin与文件filename2建立关联 
    if(!fin.is_open())   // 如果打开文件失败,则输出错误提示信息并退出
        cerr << "fail to open file " << filename2 << endl;
        system("pause");
        exit(0);    
    
    
    // 从文件输入流对象fin中获取字符,并将其插入到文件输出流对象fout中
    while(fin.get(ch))
        fout << ch;
    
    fin.close();     // 关闭文件输入流对象fin与文件filename2的关联
    fout.close();    // 关闭文件输出流对象fout与文件newfilename的关联

    system("pause");
    
    return 0;
 
main

技术图片

2.随机抽出同学

//这个头文件里包含了可用工具函数的声明 

#include <string>
using std::string;

// 函数声明
// 返回当前系统时间,格式诸如20190611
string getCurrentDate();
技术图片
#include "utils.h"
#include <ctime>
using std::string;

const int SIZE = 20;

// 函数功能描述:返回当前系统日期 
// 参数描述:无参数
// 返回值描述:以string类型返回系统当前日期,格式诸如20190611 
string getCurrentDate() 
    
    time_t now = time(0);  // 获取当前系统日历时间
    
    struct tm *local_time = localtime(&now);  // 把系统日历时间转换为当地时间
    
    char date[SIZE];

    strftime(date, SIZE, "%Y%m%d", local_time);
    
    return (string(date));
 
utils.cpp
#include <iostream>
#include <string>
#include <iostream>
#include <string>
#include <cstdlib>
#include<fstream>
#include<time.h>
#include "utils.h"

using namespace std;


int main()

    int d,num;
    ifstream fin;
    ofstream out;
    string filename;
    cin>>filename>>d;
    fin.open(filename,ios::in);
    if(!fin)
    
        cout<<"open file fail!";
    else cout<<"open file success!"<<endl;
    string str[1000];

    int mid = 0;
    int count =0;
    int max=0;
    while(!fin.eof())
    
        getline(fin,str[count],\\n);
         count++;  
    
    fin.close();
    filename=getCurrentDate() + ".txt";
    out.open(filename);
    while(d--)
    
        int a[100]=0;
        while(a[num]!=1)
        
        num = rand() % count;    
        cout<<str[num]<<endl;
        out<<str[num]<<endl;
        a[num]=1;
        
    

    return 0;

技术图片

 技术图片

3.统计单词行数字符数

#include <iostream>
#include <fstream>   
#include <string>
using namespace std;
int main() 
    string filename;  
     char ch;
    int ca=0,word =1,line=1;
    cout<<"输入要统计的文本文件名:" ;
    cin>>filename;
    ifstream fin;
    ofstream out;
    fin.open(filename);
    if(!fin.is_open()) 
        cerr << "fail to open file "<<endl;
        system("pause");
        exit(0);    
    
    
    while(    fin.get(ch))
        
        if(ch== ||ch==\\n)
        
            word++;
        
        if(ch==\\n)
        
            line++;
        
        if(ch== ||(ch!=\\n&&ch!= ))
        
            ca++;
        
    
        cout<<"字符数:"<<ca<<endl;
        cout<<"单词数:"<<word<<endl;
        cout<<"行数:"<<line<<endl;         
        fin.close();
  

技术图片

实验让我熟悉了get() getline()的各种形式,格式用法,刚开始看觉得一头雾水,比较乱,后面慢慢理解了。

 

实验六(代码片段)

fromturtleimport*defsquare(size=50,rbg=\'blue\'):pencolor(rbg)foriinrange(4):fd(size)left(90)setup(800,600)speed(0)foriinrange(10):square()left(36)hideturtle()done()fromturtleimport*setup(800,600)penc 查看详情

实验六(代码片段)

#实验三fromturtleimport*#绘制正方形#参数size指定边长,rgb指定画笔颜色#如果没有给参数,采用默认值defsquare(size=50,rgb=\'orange\'):pencolor(rgb)foriinrange(4):fd(size)left(90)defmain():setup(800,600)speed(0)foriinrange(10):square(80)le 查看详情

实验六(代码片段)

Part1#include<stdio.h>constintN=5;typedefstructstudentlongno;charname[20];intscore;STU;voidinput(STUs[],intn);intfindMinlist(STUs[],STUt[],intn);voidoutput(STUs[],intn);intmain()STUstu[N],min 查看详情

实验六(代码片段)

part1#include<stdio.h>constintN=5;typedefstructstudentlongno;charname[20];intscore;STU;voidinput(STUs[],intn);intfindMinlist(STUs[],STUt[],intn);voidoutput(STUs[],intn);intmain()STUstu[N],min 查看详情

实验六(代码片段)

实验任务1#include<stdio.h>#include<stdlib.h>#include<string.h>#defineN3//运行程序输入测试时,可以把N改小一些输入测试typedefstructstudentintid;/*学生学号*/charname[20];/*学生姓名*/charsubject[20];/*考试科目*/floatperf;/ 查看详情

实验六(代码片段)

实验任务1:#include<stdio.h>#include<stdlib.h>#include<string.h>#defineN3//运行程序输入测试时,可以把N改小一些输入测试typedefstructstudentintid;/*学生学号*/charname[20];/*学生姓名*/charsubject[20];/*考试科目*/floatperf; 查看详情

实验六(代码片段)

part1结构体类型与编程应用#include<stdio.h>constintN=5;typedefstructstudentlongno;charname[20];intscore;STU;voidinput(STUs[],intn);intfindminlist(STUs[],STUt[],intn);voidoutput(STUs[],intn);intmain()STU 查看详情

实验六(代码片段)

 1.验证性实验#include<iostream>#include<fstream>#include<string>#include<cstdlib>usingnamespacestd;intmain()stringfilename1,filename2,newfilename;cout<<"输入要合并的两个文件名:";ci 查看详情

实验六(代码片段)

part1.#include<stdio.h>constintN=5;//定义结构体类型structstudent,并定义STU为其别名typedefstructstudentlongno;charname[20];intscore;STU;//函数声明voidinput(STUs[],intn);intfindMinlist(STUs[],STUt[],intn);voidout 查看详情

实验六(代码片段)

part1.#include<stdio.h>constintN=5;//定义结构体类型structstudent,并定义STU为其别名typedefstructstudent longno; charname[20]; intscore; STU;//函数声明voidinput(STUs[],intn);intfindMinlist(STUs[],STUt[],intn);voi 查看详情

实验六(代码片段)

fromturtleimport*defsquare(size=50,rgb=\'orange\'):pencolor(rgb)foriinrange(4):fd(size)left(90)defmain():setup(800,600)speed(0)foriinrange(10):square(80)right(36)hideturtle()done()if__name__==\'__main 查看详情

实验六(代码片段)

part1ex1constintN=5;//定义结构体类型structstudent,并定义STU为其别名typedefstructstudentlongno;charname[20];intscore;STU;//函数声明voidinput(STUs[],intn);intfindMinlist(STUs[],STUt[],intn);voidoutput(STUs[],intn);intm 查看详情

实验六(代码片段)

part1:补足程序1:补足程序如下:#include<stdio.h>constintN=5;//定义结构体类型structstudent,并定义STU为其别名typedefstructstudentlongno;charname[20];intscore;STU;//函数声明voidinput(STUs[],intn);intfindMinlist(STUs[],STUt[], 查看详情

实验六(代码片段)

一,ex1-2#include<stdio.h>constintN=5;//定义结构体类型structstudent,并定义STU为其别名typedefstructstudentlongno;charname[20];intscore;STU;//函数声明voidinput(STUs[],intn);intfindMinlist(STUs[],STUt[],intn);voidou 查看详情

实验六(代码片段)

fromturtleimport*defsquare(size=50,rgb=\'orange\'):pencolor(rgb)foriinrange(4):fd(size)left(90)defmain():setup(800,600)speed(0)foriinrange(10):square(80)left(36)hideturtle()done()if__name__==\'__main_ 查看详情

实验六(代码片段)

#include<stdio.h>#include<stdlib.h>#include<string.h>#defineN4typedefstructstudentintid;/*学生学号*/charname[20];/*学生姓名*/charsubject[20];/*考试科目*/floatperf;/*平时成绩*/floatmid;/*期中成绩*/float 查看详情

实验六(代码片段)

验证性实验基础练习#include<iostream>#include<fstream>usingnamespacestd;intmain()ofstreama("3.txt",ios_base::app);if(!"3.txt")cout<<"failtoopen"<<endl;return1;a<<‘\\n‘<<"m 查看详情

实验六(代码片段)

defmain():passtask3fromturtleimport*defsquare(size=50,rgb=\'black\'):pencolor(rgb)foriinrange(4):fd(size)left(90)foriinrange(11):square(80)left(360/11)hideturtle()done()if__name__==\'__main__\':main() 查看详情