我编写了一个连接到服务器并从中接收代码行的程序,然后将所有代码行打印到一个文本文件中,问题是,服务器发送的所有代码行都不按顺序,我的意思是是在包含代码行的文本文件中没有顺序,它可以是第 55 行,然后是第 33 行,我正在尝试编写一个函数来对文件进行排序,以便代码行按顺序排列,我知道我需要使用冒泡排序并将字符串中的行号转换为 int,但我以前从未尝试过对文本文件进行冒泡排序,这是我的代码:(忽略注释)
#define _WINSOCK_DEPRECATED_NO_WARNINGS
#include<stdio.h>
#include<stdlib.h>
#include<winsock2.h>
#include<windows.h>
#include<string.h>
#define LEN 1024
void sortcode(FILE *fp);
int main(void)
{
FILE *fp;
fp = fopen("theCode.txt", "wt");
int i;
WSADATA info;
char str[LEN];
str[LEN - 1] = NULL;
char str2[LEN];
str2[LEN - 1] = NULL;
char temp[8] = "5000000"; // the row number
int j = strlen(temp) - 1;// the index of the temp string
int k = 0;
int err;
err = WSAStartup(MAKEWORD(2, 0), &info);
if (err != 0)
{
printf("WSAStartup failed with error: %d\n", err);
exit(1);
}
int s = socket(AF_INET, SOCK_STREAM, IPPROTO_TCP);
if (s == INVALID_SOCKET)
{
printf("Error creating socket = %d\n", WSAGetLastError());
}
else
{
printf("Socket function succeeded\n");
}
struct sockaddr_in clientService;
clientService.sin_family = AF_INET;
clientService.sin_addr.s_addr = inet_addr("54.152.161.133");
clientService.sin_port = htons(6714);
int cResult = connect(s, (struct socketaddr*)&clientService, sizeof(clientService));
if (cResult == SOCKET_ERROR)
{
printf("Connect function failed with error: %d\n", WSAGetLastError());
cResult = closesocket(cResult);
if (cResult == SOCKET_ERROR)
{
printf("Close socket function closed with an error: %1d\n", WSAGetLastError());
}
WSACleanup();
//return 1;
}
//Until this part, it's all taken from the slideshow.
send(s, "100", LEN, 0); //Sending code 100: Requesting to connect.
printf("Request to connect was sent using 100\n");
recv(s, str, LEN, 0); //Recieving a code to the string str.
printf("Code recieved: %s\n", str);
if (strcmp("101", str) == 0)
{
printf("Connection was successful\n");
}
else
{
printf("The connection failed\n");
}
send(s, "400", LEN, 0); //Sending a request for the number of code lines.
printf("Request for the amount of code lines was sent using 400\n");
recv(s, str, LEN, 0); //Recieving the answer on str, you'll get code 401+The number of lines for example 4010079.
printf("String recieved: %s\n", str);
printf("The amount of code lines: 0079\n");
printf("%s", str);
for (k = 1; k <= 7; k++)
{
for (i = 0; i <= 9; i++)
{
temp[j] = i + 0x30;
send(s, temp, LEN, 0);
recv(s, str, LEN, 0);
fprintf(fp, str);
fprintf(fp, "\n");
}
temp[j - 1] = k + 0x30;
temp[j] = 0 + 0x30;
}
//You need to add the part with the files where you print all the lines including the code in them to a txt file.
//Good Luck, first try to solve that i to string conversion.
system("PAUSE");
return (0);
}
void sortcode(FILE *fp)
{
int i, j, k;
char str2[LEN];
fp = fopen("theCode.c", "rt");
for (i = 0; i < 79; i++)
{
for (j = 3; j < 7; j++)
{
}
}
}
最佳答案
即使您决定编写自己的排序函数而不是使用 qsort(),冒泡排序算法对于少数项目来说也是一个非常糟糕的选择。
如果您愿意在内存中缓冲所有行,直到您读取所有行并对其进行排序(这可能是您最好的选择),那么您应该考虑边做边排序。每次你阅读一个新行时,在已经阅读过的行中找到它的位置,并将其插入那里。这本质上是一种插入排序。
如果您希望您的行大部分按顺序排列,则从后到前线性搜索每个插入点。这是标准的插入排序,对于近似有序的输入,它表现得非常好。对于没有特定顺序的大量行,您可以考虑使用二进制搜索来查找每个插入点。
如果您使用链表来保存线条(如果您事先不知道会有多少行,这是一个合理的选择),那么二分搜索替代方案可以很好地扩展到大输入。
关于c - C中的冒泡排序文件,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/31078000/
总的来说,我对ruby还比较陌生,我正在为我正在创建的对象编写一些rspec测试用例。许多测试用例都非常基础,我只是想确保正确填充和返回值。我想知道是否有办法使用循环结构来执行此操作。不必为我要测试的每个方法都设置一个assertEquals。例如:describeitem,"TestingtheItem"doit"willhaveanullvaluetostart"doitem=Item.new#HereIcoulddotheitem.name.shouldbe_nil#thenIcoulddoitem.category.shouldbe_nilendend但我想要一些方法来使用
我有一个Ruby程序,它使用rubyzip压缩XML文件的目录树。gem。我的问题是文件开始变得很重,我想提高压缩级别,因为压缩时间不是问题。我在rubyzipdocumentation中找不到一种为创建的ZIP文件指定压缩级别的方法。有人知道如何更改此设置吗?是否有另一个允许指定压缩级别的Ruby库? 最佳答案 这是我通过查看rubyzip内部创建的代码。level=Zlib::BEST_COMPRESSIONZip::ZipOutputStream.open(zip_file)do|zip|Dir.glob("**/*")d
我试图在一个项目中使用rake,如果我把所有东西都放到Rakefile中,它会很大并且很难读取/找到东西,所以我试着将每个命名空间放在lib/rake中它自己的文件中,我添加了这个到我的rake文件的顶部:Dir['#{File.dirname(__FILE__)}/lib/rake/*.rake'].map{|f|requiref}它加载文件没问题,但没有任务。我现在只有一个.rake文件作为测试,名为“servers.rake”,它看起来像这样:namespace:serverdotask:testdoputs"test"endend所以当我运行rakeserver:testid时
我的目标是转换表单输入,例如“100兆字节”或“1GB”,并将其转换为我可以存储在数据库中的文件大小(以千字节为单位)。目前,我有这个:defquota_convert@regex=/([0-9]+)(.*)s/@sizes=%w{kilobytemegabytegigabyte}m=self.quota.match(@regex)if@sizes.include?m[2]eval("self.quota=#{m[1]}.#{m[2]}")endend这有效,但前提是输入是倍数(“gigabytes”,而不是“gigabyte”)并且由于使用了eval看起来疯狂不安全。所以,功能正常,
作为我的Rails应用程序的一部分,我编写了一个小导入程序,它从我们的LDAP系统中吸取数据并将其塞入一个用户表中。不幸的是,与LDAP相关的代码在遍历我们的32K用户时泄漏了大量内存,我一直无法弄清楚如何解决这个问题。这个问题似乎在某种程度上与LDAP库有关,因为当我删除对LDAP内容的调用时,内存使用情况会很好地稳定下来。此外,不断增加的对象是Net::BER::BerIdentifiedString和Net::BER::BerIdentifiedArray,它们都是LDAP库的一部分。当我运行导入时,内存使用量最终达到超过1GB的峰值。如果问题存在,我需要找到一些方法来更正我的代
Rails2.3可以选择随时使用RouteSet#add_configuration_file添加更多路由。是否可以在Rails3项目中做同样的事情? 最佳答案 在config/application.rb中:config.paths.config.routes在Rails3.2(也可能是Rails3.1)中,使用:config.paths["config/routes"] 关于ruby-on-rails-Rails3中的多个路由文件,我们在StackOverflow上找到一个类似的问题
对于具有离线功能的智能手机应用程序,我正在为Xml文件创建单向文本同步。我希望我的服务器将增量/差异(例如GNU差异补丁)发送到目标设备。这是计划:Time=0Server:hasversion_1ofXmlfile(~800kiB)Client:hasversion_1ofXmlfile(~800kiB)Time=1Server:hasversion_1andversion_2ofXmlfile(each~800kiB)computesdeltaoftheseversions(=patch)(~10kiB)sendspatchtoClient(~10kiBtransferred)Cl
我正在寻找执行以下操作的正确语法(在Perl、Shell或Ruby中):#variabletoaccessthedatalinesappendedasafileEND_OF_SCRIPT_MARKERrawdatastartshereanditcontinues. 最佳答案 Perl用__DATA__做这个:#!/usr/bin/perlusestrict;usewarnings;while(){print;}__DATA__Texttoprintgoeshere 关于ruby-如何将脚
使用带有Rails插件的vim,您可以创建一个迁移文件,然后一次性打开该文件吗?textmate也可以这样吗? 最佳答案 你可以使用rails.vim然后做类似的事情::Rgeneratemigratonadd_foo_to_bar插件将打开迁移生成的文件,这正是您想要的。我不能代表textmate。 关于ruby-使用VimRails,您可以创建一个新的迁移文件并一次性打开它吗?,我们在StackOverflow上找到一个类似的问题: https://sta
我需要从一个View访问多个模型。以前,我的links_controller仅用于提供以不同方式排序的链接资源。现在我想包括一个部分(我假设)显示按分数排序的顶级用户(@users=User.all.sort_by(&:score))我知道我可以将此代码插入每个链接操作并从View访问它,但这似乎不是“ruby方式”,我将需要在不久的将来访问更多模型。这可能会变得很脏,是否有针对这种情况的任何技术?注意事项:我认为我的应用程序正朝着单一格式和动态页面内容的方向发展,本质上是一个典型的网络应用程序。我知道before_filter但考虑到我希望应用程序进入的方向,这似乎很麻烦。最终从任何