C++利用数组解决学生成绩排名。

C++利用数组解决学生成绩排名。,第1张

#include<IOSTREAMH>

#include<STDIOH>

#include<STRINGH>

#include<STDLIBH>

#include<CONIOH>

#define WORDLEN 50

#include <IOH>

#include<TIMEH>

#define PATH 2000

struct node{

char word[WORDLEN];

float num;

struct node pre,next;

};

clock_t start,end;

struct node headptr=(struct node)malloc(sizeof(struct node));

int main(int argc,char argv[])

{

int output(char);

void searchAllFiles(char);

strcpy(headptr->word," ");

headptr->num=0;//用来存储单词的总数

headptr->pre=headptr;

headptr->next=headptr;

char paths[PATH],patht[PATH];

// printf("请输入要查找的文件路径:\n");

//gets(paths);

if(argc<3)

{

printf("请输入程序名,搜索路径及结果文件路径:\n");

return 0;

}

strcpy(paths,argv[1]);

//printf("请输入要输出结果的文件路径:\n");

//gets(patht);

strcpy(patht,argv[2]);

start=clock();

searchAllFiles(paths);

output(patht);

return 0;

}

int searchword(char fpath)

{

char filepath[PATH];

strcpy(filepath,fpath);

FILE fptr;

fptr=fopen(filepath,"r");

char word[20]="";

char sword[2];

char ch;

int InsertAndSort(char);

while((ch=fgetc(fptr))!=EOF)

{

if(!((ch>='a'&&ch<='z')||(ch>='A'&&ch<='Z')||(ch=='')||(ch=='!')||(ch=='-'))&&(!strcmp(word,"")))//所得到的字符不是要处理的字符且word数组为空

continue;

if((ch>='a'&&ch<='z')||(ch>='A'&&ch<='Z')||(ch=='-'))//得到的字符是字母或连字符

{

sword[0]=tolower(ch);//将其变成小写

strncat(word,sword,1);

continue;

}

if(!((ch>='a'&&ch<='z')||(ch>='A'&&ch<='Z')||(ch=='-')||(ch=='')||(ch=='!'))&&(strcmp(word,"")))//所得到的字符不是要处理的字符且word数组为非空

{

InsertAndSort(word);

strcpy(word,"");//重置word数组为空

continue;

}

if(((ch=='')||(ch=='!'))&&(!strcmp(word,"")))//得到'!'或''且word数组为空

{

sword[0]=ch;

strncat(word,sword,1);

InsertAndSort(word);

strcpy(word,"");//重置word数组为空

continue;

}

if(((ch=='')||(ch=='!'))&&(strcmp(word,"")))//得到'!'或''且word数组非空

{

InsertAndSort(word);//先处理word数组

strcpy(word,"");//重置word数组为空

sword[0]=ch;

strncat(word,sword,1);

InsertAndSort(word);

strcpy(word,"");//重置word数组为空

continue;

}

continue;

}

return 0;

}

int InsertAndSort(char word)

{

struct node nodeptr;//以下部分应该抽象成一个函数

nodeptr=headptr->next;

while(nodeptr!=headptr)

{

if(!strcmp(nodeptr->word,word))//这个单词已经存在

{

nodeptr->num+=1;

headptr->num+=1;

if((nodeptr->num>nodeptr->pre->num)&&(nodeptr->pre!=headptr))//与之前的单词的数目进行对比,找到一个比其数目小的最大单词数目所属单词进行交换

{

struct node searchptr;//新建一个指针用于查找

searchptr=nodeptr->pre->pre;

while((nodeptr->num>searchptr->num)&&(searchptr!=headptr))

searchptr=searchptr->pre;

searchptr=searchptr->next;//回退一个结点

float temp; //交换单词数目

temp=nodeptr->num; //变量的生存期

nodeptr->num=searchptr->num;

searchptr->num=temp;

char tempword[WORDLEN]; //交换单词

strcpy(tempword,nodeptr->word);

strcpy(nodeptr->word,searchptr->word);

strcpy(searchptr->word,tempword);

}

break;//跳出第二个while循环

}

else

nodeptr=nodeptr->next;//继续搜索下一个单词

}

if(nodeptr!=headptr) //判断是否已经找到这个单词

return 0; //已经找到

else

{

struct node lastptr=(struct node)malloc(sizeof(struct node));//在链表的尾部新建一个结点存放该单词

strcpy(lastptr->word,word);

lastptr->num=1;

lastptr->pre=nodeptr->pre;

lastptr->next=headptr;

headptr->pre->next=lastptr;

headptr->pre=lastptr;

headptr->num+=1;

}

return 0;

}

int output(char temppath)//输出链表的数据

{

struct node tempnode;

tempnode=headptr->next;

FILE target=fopen(temppath,"w");

if(!target)

{

printf("输入结果失败!\n");

return 0;

}

else

{

while(tempnode!=headptr)

{

fprintf(target,"%42f",(tempnode->num/headptr->num)100);

fputs("%",target);

fprintf(target," %s",tempnode->word);

fputs("\n",target);

tempnode=tempnode->next;

}

end=clock();

fprintf(target,"the process time is:%2f seconds\n",(double)(end-start)/(double)CLOCKS_PER_SEC);

return 0;

}

}

void searchAllFiles( char filePath )

{

struct _finddata_t fileInfo;

char filePathCpy[PATH];

strcpy(filePathCpy, filePath);

int hfind = _findfirst(filePath, &fileInfo);

if( hfind == -1 ) //打开路径失败返回

{

return;

}

else

{

int tag=0;

while( tag != -1 ) //从第一个文件第N个文件

{

if( strcmp( fileInfoname, "" )==0 || strcmp( fileInfoname, "" )==0 ) //一点和两点分别是根目录和当前目录。

{

tag=_findnext( hfind, &fileInfo ); //查找下一个配匹的文件

continue;

}

//取得全路径

char fullPath[PATH];

strcpy( fullPath, filePathCpy ); //把上一个文夹的路径赋给当前的全部路径

fullPath[ strlen( fullPath ) - strlen( "" ) ] = '\0';//去掉\\

strcat( fullPath, fileInfoname );

if ( fileInfoattrib & _A_SUBDIR ) //非0,是一个文件夹

{

strcat( fullPath, "\\" );

searchAllFiles( fullPath ); //递归扫描该文件夹的子文件

}

else

{

searchword(fullPath);

}

tag=_findnext(hfind,&fileInfo); //查找下一个配匹的文件

}

_findclose(hfind);

}

}

C++ 获取map元素的代码如下:

#include<map>

#include<string>

#include<iostream>

int   main()

{

map<string,int>   words;

map<string,int>::iterator   it=wordsbegin();

for(;it!=wordsend();++it)

cout<<"key:"<<it->first

<<"value:"<<it->second<<end1;

return   0;

}Top

C++是C语言的继承,它既可以进行C语言的过程化程序设计,又可以进行以抽象数据类型为特点的基于对象的程序设计,还可以进行以继承和多态为特点的面向对象的程序设计。C++擅长面向对象程序设计的同时,还可以进行基于过程的程序设计,因而C++就适应的问题规模而论,大小由之。

在C++中,类是支持数据封装的工具,对象则是数据封装的实现。C++通过建立用户定义类支持数据封装和数据隐藏。

在面向对象的程序设计中,将数据和对该数据进行合法 *** 作的函数封装在一起作为一个类的定义。对象被说明为具有一个给定类的变量。每个给定类的对象包含这个类所规定的若干私有成员、公有成员及保护成员。完好定义的类一旦建立,就可看成完全封装的实体,可以作为一个整体单元使用。类的实际内部工作隐藏起来,使用完好定义的类的用户不需要知道类是如何工作的,只要知道如何使用它即可。

map是一类关联式容器。它的特点是增加和删除节点对迭代器的影响很小,除了那个 *** 作节点,对其他的节点都没有什么影响。对于迭代器来说,可以修改实值,而不能修改key。

#include <iostream>

#include <string>

#include <vector>

#include <utility>

#include <algorithm>

using namespace std;

bool compare_two_vectors ( pair<string, double> a, pair<string, double> b ) {

return ( asecond > bsecond );

}

int main () {

vector< pair<string, double> > student (2, make_pair( "Unknown", 00));

vector< pair<string, double> >::iterator it = studentbegin();

student[1]first = "Jackson Bug";

student[1]second = 880;

studentpush_back( make_pair("Yu Yeeson", 1000) );

studentpush_back( make_pair("Shen Miranda", 850) );

studentpush_back( make_pair("Chen Jamie", 790) );

cout << "input order:\n";

for ( vector< pair<string, double> >::iterator it = studentbegin(); it != studentend(); it++ ) {

cout << '\t' << it->first << endl;

cout << '\t' << it->second << endl;

} cout << "\n\n";

sort(studentbegin(), studentend(), compare_two_vectors );

cout << "sorted order:\n";

for ( vector< pair<string, double> >::iterator it = studentbegin(); it != studentend(); it++ ) {

cout << '\t' << it->first << endl;

cout << '\t' << it->second << endl;

} cout << "\n\n";

return 0; 

}

#include<map>

#include<string>

#include<iostream>

int main()

{

map<string,int> words;

map<string,int>::iterator it=wordsbegin();

for(;it!=wordsend();++it)

cout<<"key:"<<it->first

<<"value:"<<it->second<<end1;

return 0;

}Top

以上就是关于用没有高人会用C++做词频统计 ,急需 ,谢谢全部的内容,包括:用没有高人会用C++做词频统计 ,急需 ,谢谢、C++ 怎么获取 map的元素呢、C++利用数组解决学生成绩排名。等相关内容解答,如果想了解更多相关内容,可以关注我们,你们的支持是我们更新的动力!

欢迎分享,转载请注明来源:内存溢出

原文地址:https://54852.com/langs/8850510.html

(0)
打赏 微信扫一扫微信扫一扫 支付宝扫一扫支付宝扫一扫
上一篇 2023-04-22
下一篇2023-04-22

发表评论

登录后才能评论

评论列表(0条)

    保存