C++编程学习笔记 week3

文件

3.1 数的组合 文件**

读文件并5个一行输出的屏幕上。

data.txt

123 124 125 126 132
134 135 136 142 143
145 146 152 153 154
156 162 163 164 165
213 214 215 216 231
234 235 236 241 243
245 246 251 253 254
256 261 263 264 265
312 314 315 316 321
324 325 326 341 342
345 346 351 352 354
356 361 362 364 365
412 413 415 416 421
423 425 426 431 432
435 436 451 452 453
456 461 462 463 465
512 513 514 516 521
523 524 526 531 532
534 536 541 542 543
546 561 562 563 564
612 613 614 615 621
623 624 625 631 632
634 635 641 642 643
645 651 652 653 654

#include<iostream>   
using namespace std;   
int main()   
{   
    FILE * pf;   
    pf=fopen("data.txt","r");   
    int a,count=0;   
    while(!feof(pf))   
    {   
        int r=fscanf(pf,"%d",&a);   
        if(r!=1) break;   
        cout<<a<<" ";   
        count++;   
        if(count%5==0) cout<<endl;   
    }   
    fclose(pf);   
    return 0;   
}  

3.2 素数 文件***

键盘上输入n,从文件11.2中读出小于n的所有素数,输出到屏幕。

11.2

2,3,5,7,11,13,17,19,23,29,31,37,41,43,47,53,59,61,67,71,73,79,83,89,97,101,103,107,109,113,127,131,137,139,149,151,157,163,167,173,179,181,191,193,197,199,211,223,227,229,233,239,241,251,257,263,269,271,277,281,283,293,307,311,313,317,331,337,347,349,353,359,367,373,379,383,389,397,401,409,419,421,431,433,439,443,449,457,461,463,467,479,487,491,499,503,509,521,523,541,547,557,563,569,571,577,587,593,599,601,607,613,617,619,631,641,643,647,653,659,661,673,677,683,691,701,709,719,727,733,739,743,751,757,761,769,773,787,797,809,811,821,823,827,829,839,853,857,859,863,877,881,883,887,907,911,919,929,937,941,947,953,967,971,977,983,991,997,

#include<iostream>   
  
using namespace std;   
int main()   
{   
    FILE *pf;   
    pf=fopen("11.2","r");   
    int n;   
    cin>>n;   
       
    int a;   
    int count=0;   
    while(!feof(pf))   
    {   
        int r=fscanf(pf,"%d,",&a);   
        if(r==NULL||a>=n) break;   
        if(a<n) cout<<a<<" ";   
           
        count++;   
        if(count%5==0) cout<<endl;   
    }   
  
    if(count%5!=0) cout<<endl;   
  
    fclose(pf);   
    return 0;   
}  

3.3 字母的频度 文件数组****

输出文件11.15中所有字母出现的次数,不区分大小写。

11.15

Beijing Technology and Business University (BTBU) is a key state-run
university with comprehensive disciplines covering Arts, Sciences,
Engineering, Law, Economics, History, Philosophy and Management.

The university lays the equal emphasis on teaching and scientific and
technological research. In 2007 BTBU accepted the teaching quality
evaluation from the Ministry of Education and was awarded the rank
Excellence.

Under the university there are 9 schools, 1 department (Research
Institute) and 1 education center, within which 43 undergraduate
majors, 35 postgraduate majors, 2 master degree programs and some
joint doctoral programs are offered. Among those majors and programs
16 are awarded the Beijing Municipal and Preferential-construction
disciplines or majors. Also at BTBU there exist 2 national industrial
test centers, 1 national experimental center for Practical Teaching, 1
Beijing Municipal Key Lab and 2 Beijing Municipal Research Bases.

Beijing Technology and Business University takes an area of 840,000
square meters, 475,000 square meters of buildings and a total fixed
asset of RMB1.27 billion. Since the 10th Five-Year Program, the
university has increased its investment in infrastructures, completing
the reconstruction of the Fucheng Road Campus and the first stage
construction of Liangxiang Campus (245,900㎡). Besides, the university
has a library of 25,000㎡, with 2.03 millions of books in stock,
including over 600,000 digital ones and 2600 magazines and
periodicals.

At present, the university hires 807 full-time faculty members, 48%of
whom have senior professional titles. Eight professors enjoy special
research allowance from the State Council; two are awarded as China’s
Excellent Teachers; three are Famous Teachers of Universities in
Beijing; five are Excellent Teachers in Beijing; 53 are Excellent
Young Teachers in Beijing; and 11 research teams are selected as
Academic Innovation Teams of Beijing.


    #include<iostream>   
using namespace std;   
int main()   
{   
    FILE *pf;   
    pf=fopen("11.15","r");   
    char s[1000],p;   
        int a[26]={0},i;   
    while(!feof(pf))   
    {   
        char *r=fgets(s,1000,pf);   
        if(r==0) break;   
        for(i=0;s[i]!=0;i++)   
        {   
            if(s[i]>='A'&&s[i]<='Z')   
                a[s[i]-'A']++;   
            else if(s[i]>='a'&&s[i]<='z')   
                a[s[i]-'a']++;   
        }   
    }   
    for(i=0;i<26;i++)   
    {   
        p='A'+i;   
        cout<<p<<" "<<a[i]<<endl;   
    }   
    fclose(pf);   
    return 0;   
}

3.4 国名排序 文件字符串排序****

从文件11.25中读入n个国名,按照字典顺序排序并输出。

11.25

Germany
Australia
America
Canada
Brazil
Indian
Egyptian
China
England
France

#include <iostream>  
#include <cstring>   
using namespace std;   
void exchange(char *p,char *q);   
int main()   
{   
    FILE *pf;   
    pf=fopen("11.25","r");   
    char s[10][20];   
       
    int i=0,j;   
    while(!feof(pf))   
    {   
        char *r=fgets(s[i],20,pf);   
        if(r==0) break;   
        i++;   
    }   
  
    for(i = 0; i <9; i ++)   
    {   
        for(j = i+1; j < 10; j ++)   
        {   
                   
                if(strcmp(s[i],s[j])==1)   
                {   
                    exchange(s[i],s[j]);   
                }   
               
        }   
    }   
       for(i = 0; i < 10; i ++)   
       {   
           cout<<s[i];   
       }   
       
    fclose(pf);   
    return 0;   
}   
void exchange(char *p,char *q)   
{   
    char t[20];   
    strcpy(t,p);   
    strcpy(p,q);   
    strcpy(q,t);   
}  
#include <iostream>
#include <cstring>
using namespace std;
void exchange(char *p,char *q);
int main()
{
    FILE *pf;
    pf=fopen("11.25","r");
    char s[10][20];
    
    int i=0,j;
    while(!feof(pf))
    {
        char *r=fgets(s[i],20,pf);
        if(r==0) break;
        i++;
    }

    for(i = 0; i <9; i ++)
    {
        for(j = i+1; j < 10; j ++)
        {
                
                if(strcmp(s[i],s[j])==1)
                {
                    exchange(s[i],s[j]);
                }
            
        }
    }
       for(i = 0; i < 10; i ++)
       {
           cout<<s[i];
       }
    
    fclose(pf);
    return 0;
}
void exchange(char *p,char *q)
{
    char t[20];
    strcpy(t,p);
    strcpy(p,q);
    strcpy(q,t);
} 

3.5字符串连接***

编写函数将两个字符串连接起来.

#include <iostream>   
using namespace std;   
int main()      
{      
    void addString(char *p,char *q);      
    char  a[100];      
    char  b[100];      
    gets(a);      
    gets(b);          
    addString(a,b);      
    cout<<a<<endl;    
}   
void addString(char *p,char *q)   
{   
    while(*p!=0)   
    {   
        p++;   
    }   
    while(*q!=0)   
    {   
        *p=*q;   
        p++;   
        q++;   
    }   
    *p=0;   
}

3.6反序****

给定一整型数组,若数组中某个下标值大的元素值小于某个下标值比它小的元素值,称这是一个反序。即:数组a中, 对于i < j 且 a[i] > a[j],则称这是一个反序。
要求定义名为reverseOrder的函数,函数原型为int reverseOrder(int b[],int ),函数功能是计算出数组中所有反序的个数。

#include <iostream>   
using namespace std;       
int main()      
{      
    int a[10],i;      
    int n;      
    int reverseOrder(int a[],int );      
    cin>>n;    
    for(i=0;i<n;i++)   cin>>a[i];      
    cout<<reverseOrder(a,n)<<endl;     
  
    return 1;    
}   
  int reverseOrder(int a[],int n)   
{   
    int i,j,count=0;   
    for(i=0;i<n;i++)   
    {   
       
        for(j=i+1;j<n;j++)   
        {   
            if(a[j]<a[i]) count++;   
        }   
    }   
    return count;   
}

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值