创建带有随机字符的“转置”文件

2024-02-15

我想创建一个程序,创建带有随机字符的“.txt”文件,但可以创建转置的“文件”。接下来,我按照我的意愿添加两个“.txt”文件的示例。

FILE1

A|B|C|D|E|F|G|

H|I|J|K|L|M|N|

O|P|Q|R|S|T|U|

V|W|Z|1|2|3|4|

FILE2(FILE1 转置)

A|H|O|V|

B|I|P|W|

C|J|Q|Z|

D|K|R|1|

E|L|S|2|

F|M|T|3|

G|N|U|4|

现在我把我写的代码加上去,方便大家看一下,给我一些想法,比如怎么做,修改什么等等。

#include<iostream>
#include<fstream>
#include<stdio.h>
#include<string>
#include<ctime>

using namespace std;

int main()
{
    int rows, columns, element1;

    char word[10];

    ofstream myfile ("File 1.txt");
    if(myfile)
        srand(1);
    for(rows=0;rows<10;rows++)
    {
        for(columns=0;columns<30;columns++)
        {
            element1 = rand() % 100000 + 1;
            int len = rand () % 4 + 4;
            word [len] = 0;
            while (len) word [--len] = 'A' + rand () % 58;

            myfile<<element1<<word;
            myfile<<"|";
        }
        myfile<<endl;

    }
    myfile.close();


    ofstream myfileS ("File 2.txt");
    if(myfileS)
        srand(1);
    for(columns=0;columns<30;columns++)
    {
        for(rows=0;rows<10;rows++)
        {

            element1 = rand() % 100000 + 1;
            int len = rand () % 4 + 4;
            word [len] = 0;
            while (len) word [--len] = 'A' + rand () % 58;

            myfileS<<element1<<word;
            myfileS<<"|";
        }
        myfileS<<endl;
    }
    myfile.close();



    system("pause");
    return 0;

}

感谢您的帮助!

我的疑问的解决方案

#include<iostream>
#include<fstream>
#include<stdio.h>
#include<string>
#include<ctime>
#include<sstream>

using namespace std;

int main()
{
int rows, columns, rowsMax, columnsMax; 
int element1;

    char word[10];

int a=0;

cout<<"Write the number of rows of your table: ";
cin>>rowsMax;

cout<<"Write the number of columns of your table: ";
cin>>columnsMax;

int answer;
cout<<"Do you want to create the transposed table??? (1 -> yes, 2 -> no): ";
cin>>answer;

string matriz[7][5]; // string matriz[rowsMax][columnsMax]; I should modify this in every query

string table ("Table1.txt");
ofstream myfile (table);
if(myfile.is_open())
srand(1);
myfile<<id<<"|"<<type<<"|"<<columnsMax<<endl;
for(rows=0;rows<rowsMax;rows++)
{
    for(columns=0;columns<columnsMax;columns++)
    {
        element1 = rand() % 100000 + 1;
        int len = rand () % 4 + 4;
        word [len] = 0;
        while (len) word [--len] = 'A' + rand () % 58;
        myfile<<element1<<word;
        myfile<<"|";

        std::stringstream ss;
            ss<<element1;

        string mat;
        mat +=ss.str();
        mat +=word;
        matriz[rows][columns]= mat;
    }
    myfile<<endl;

}
myfile.close();


while(answer==1)
{
    string table ("Table");
    table +=id;
    table +="(transposed)";
    table +=".txt";
    ofstream myfile (table);
    if(myfile.is_open())
    myfile<<id<<"|2|"<<columnsMax<<endl;
    for(int k=0;k<5;k++)
    {
            for(int j=0;j<7;j++)
        {
            myfile<<matriz[j][k]<<"|";
        }
            myfile<<endl;
    }
    answer=2;
}
system("pause");
return 0;

}

  • 要创建 file1.txt 的转置版本,您必须将其内容保留在内存中并以转置方式访问该内容。

  • 当你写的时候

    if(myfileS.is_open())
    srand(time(0));
    

    您只需以文件打开与否的事实为条件。不是下面的内容。我会把所有东西都戴上大括号。

  • 测试 IOStream 状态的最佳方法就是将其用作条件

    if (myfileS)
    
本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系:hwhale#tublm.com(使用前将#替换为@)

创建带有随机字符的“转置”文件 的相关文章

随机推荐

  • 在 jboss 中加载 HTML 图像

    我有 HTML 页面 放在临时文件夹 WEB INF 目录之外 中 我在 HTML 页面中使用了一些图像 我也将这些图像放入临时文件夹中 然后创建了我的 war 文件 当我在 localhost 中运行该程序时 图像不会加载到 HTML 页
  • 如何在 JavaScript 中将所有对象属性设置为 null?

    我正在使用 Vue 突然使用一些计算的 cssvuetify https vuetifyjs com不管用 我声明对象的方式是 personal info 在我的模板中 我可以这样做personal info name以及每个文本输入 v
  • 在外部网络上托管 Expo 应用程序?

    我正在编写一个应用程序create react native app CRNA 为一家公司 最终 它可能会投入生产 但出于研究原因 我需要一个可以轻松部署到同事手机 Android 和 iOS 的工作原型 由于知识产权的原因 我不得在任何外
  • C#图表控件删除条形图中条形之间的空格

    I have a bar chart made with the c net chart control that looks like the following 正如您所看到的 图表上每对红色和蓝色条之间都有一个空格 有没有办法删除这些
  • Pandas 时间戳以 30 秒为单位不一致

    我试图将 pandas DatetimeIndex 或 Timestamp 舍入到最接近的分钟 但我遇到了 30 秒时间戳的问题 有些向上舍入 有些向下舍入 这似乎是交替的 有什么建议可以解决这个问题 以便 30 总是四舍五入吗 gt gt
  • 比较两个 Doctrine_Record 对象

    我如何比较两个Doctrine Record对象看看它们是否 相等 在我正在考虑的域登录中 如果两个对象具有相同的属性值 则它们相等 除了id和created at and updated at字段 a laTimestampable 我想
  • Django 迁移卡住了

    我有一个新字段要添加到我的数据库中 所以我说 python manage py makemigrations 这正确地创建了kernel migrations 0003 auto 20150726 1911 py 我检查了内容 一切看起来都
  • PostgreSQL 9.3:函数不唯一错误

    我创建了以下具有 11 个参数的函数 如下所示 函数调用 SELECT FROM function TableDetails NULL Type Table 2671ffdb 28a4 4ce4 a226 e5a21d66509e D096
  • 在android中录制原始视频格式

    我对安卓完全陌生 是否可以在 android 中从相机录制原始视频格式 基本上我有一个与 ICS 一起运行的 pandaboard 我连接了 USB 摄像头 iball 现在我想从 USB 摄像头录制原始视频格式 该相机支持原始视频格式我已
  • 是否有必要保护 JAX-RS 请求免受 CSRF 影响?

    是否有必要保护 JAX RS 请求免受攻击CSRF https en wikipedia org wiki Cross site request forgery By 定义 https en wikipedia org wiki Repre
  • Azure Function+ARM:将应用程序设置与当前设置合并

    我的部署分为两个管道 部署基础设施 运行ARM模板 部署和配置应用程序 上传应用程序 运行脚本 我的 ARM 模板包含一个 AppSettings 数组 如下所示 schema https schema management azure c
  • 如何使 CSS 网格容器保持响应式方形大小?

    I want grid container to maintain square shape So when screen is resized it will get bigger or smaller but it s height w
  • MySQL 每组前 2 条记录

    基本上 考虑到最后的创建日期时间 我只需要获取每个用户的最后 2 条记录 id user id created datetime 1 34 2015 09 10 2 34 2015 10 11 3 34 2015 05 23 4 34 20
  • Capistrano 和 Bash:忽略命令退出状态

    我正在使用 Capistrano 运行远程任务 我的任务如下所示 task my task do run my command end 我的问题是如果my command退出状态 0 则 Capistrano 认为它失败并退出 当退出状态不
  • 更灵活的引文格式

    我喜欢在我的 r bookdown 项目中使用引用 格式不是很灵活 我可以使用 citeID来获取 作者 年份 或 citeID 来获取 作者年份 有时我只需要 年份 或 年份 或者我可能只需要 作者年份 根本不需要括号 最后 我需要添加对
  • 在 JBOSSstandalone.sh 文件中设置 JAVA_OPTS

    我在 jboss 上有一个通过 log4j 进行日志记录的应用程序 当我通过bat文件 standalone bat 运行我的服务器时 日志工作正常 但standalone sh文件下有问题 以下是我在standalone bat中设置JA
  • PHP $_SESSION 变量不会取消设置

    抱歉 我问了一个重复的问题 我在这个论坛上看到过其中一些 但没有一个回复对我有用 我正在使用 php 会话构建基本登录 这是我的新手 login php 验证 html 登录表单并开始会话 设置变量 SESSION login and SE
  • IDX10501:签名验证失败。无法匹配键

    请帮助我了解 ASP netcore 应用程序和 netcore Kestrel 托管应用程序的 JWT 令牌验证之间的区别 有两个应用程序使用源代码验证令牌 如下所示 public static IServiceCollection Ad
  • 选择哪个工作流引擎? [关闭]

    就目前情况而言 这个问题不太适合我们的问答形式 我们希望答案得到事实 参考资料或专业知识的支持 但这个问题可能会引发辩论 争论 民意调查或扩展讨论 如果您觉得这个问题可以改进并可能重新开放 访问帮助中心 help reopen questi
  • 创建带有随机字符的“转置”文件

    我想创建一个程序 创建带有随机字符的 txt 文件 但可以创建转置的 文件 接下来 我按照我的意愿添加两个 txt 文件的示例 FILE1 A B C D E F G H I J K L M N O P Q R S T U V W Z 1