C++的一些小程序和代码实现

1、按照要求的功能将下面的程序代码补充完整。
(1)输出字符串str以及str的地址。
(2)以科学计数法显示f,使科学计数法的指数字母以大写输出
(3)以八进制输出n,输出整数时显示基数。
(4)设置显示宽度为10,填充字符为’*’,右对齐方式显示。
(5)分别设置精度为2、3、4显示f。
(6)按16进制输入整数,然后按10进制输出。
(7)从流中读取10个字符到str,遇到’!'字符停止操作
程序的部分代码:

#include <iostream>
#include <iomanip>
using namespace std;
int main(){
char str[20]="hello world!";
int n=12;
float f=1.234;
//添加代码……
return 0;
}

下面是添加的代码

int main()
{
    char str[20] = "hello world!";
    int n = 12;
    float f = 1.234;
    cout << hex << n << endl;
    cout << setiosflags(ios::showbase) << endl;
    cout << setfill('*') << setw(10) << setiosflags
    (ios::right) << n << endl;
    cout << resetiosflags(ios::scientific);
    cout << resetiosflags(ios::showbase);
    cout << resetiosflags(ios::right);
    cout << fixed;
    cout << setprecision(2) << f << endl;
    cout << setprecision(3) << f << endl;
    cout << setprecision(4) << f << endl;
    cin >> hex >> n;
    cout << dec << n;
    cin.getline(str, 10, '!');
    cout << str << endl;
    return 0;
}

2.基本的文件读写操作。请创建C++工程,并将以下代码添加在源文件中,观察程序的运行结果。

int main()
{

   ofstream f1;//输出文件流类

   f1.open("outfile.txt");//打开文件

   f1<<"This is my outfile."<<endl;//写入文件的内容

   f1.close();//文件关闭

   ifstream f2("outfile.txt");//输入文件流类

   char ch;//定义字符

   ch=f2.get();//从f2中读取数据

   while(!f2.eof()) //while循环并判断文件有没有读完 (一般都会多读一个,但本代码不会多读一个,因为前面已经有了ch=f2.get()已经读了一个)
    {

       cout<<ch;

       ch=f2.get();//从f2中读取数据

    }

   cout<<endl;

   f2.close();//文件关闭

   return 0;

}

3.试定义图书类,并完成图书对象的文件读写。
图书类定义没有什么困难的!注意对于任意类型的数据的文件读写,需要调用C++ I/O流类库中的函数,如:

write ( char *buffer, streamsize size );
read ( char * buffer, streamsize size );

实现代码

#include <iostream>
#include <fstream>
#include <string.h>
#include <iomanip>
using namespace std;

class Book
{
private :
    int id;
    char name[20];
public :
    Book();
    Book(int xid, char* xn);
    void show();
};
Book::Book(int x, char* xn)
{
    id = x;
    strcpy(name, xn);
}
void Book::show()
{
    cout << "id = " << id << "\t";
    cout << "name = " << name << endl;

}
int main()
{
    ofstream f1;
    f1.open("outfile.txt");
    Book b1(1001,"Math");
    f1.write((char*)&b1, sizeof(b1));
    f1.close();
    ifstream f2("outfile.txt");
    Book b2(1002,"sweet");
    f2.read((char*)&b2, sizeof(b1));
    b2.show();
    f2.close();
    return 0;
}

4.该程序读入给定文本,原样输出该文本,并且输出其行数以及文本中每个英文字母出现的次数。大写字母和小写字母将被视为相同的字母。
因为总共有26种字母,所以需要一个有26个元素的数组来存储字母出现的次数。还需要一个变量存储行数。
输入:含有需要处理文本的文件。
输出:含有文本、行数和每个字母在文本中出现的次数的文件。
程序的算法是:
1.定义有关变量。
2.建立/打开输入文件和输出文件。
3.初始化变量。
4.当输入文件中还有未处理的字符时:
4.1 对于每一行中的每个字符
4.1.1 读入并且写出字符
4.1.2 将相应的字符数加1
4.2 将行数加1
5.输出行数和字母计数
6.关闭文件。
(1).新建头文件 TextCount.h

#ifndef _TEXT_COUNT
#define _TEXT_COUNT
#include "fstream.h"
void initialize(int &lc,int list[]);
void copyText(ifstream &intext,ofstream &outtext,char &ch,int list[]);
void characterCount(char ch,int list[]);
void writeTotal(ofstream &outtext,int lc,int list[]);
#endif

(2).再建一个源文件.cpp

#include "TextCount.h"
void initialize(int &lc,int list[]){
	int j;
	lc=0;
	for(j=0;j<26;j++)
		list[j]=0;
}
void copyText(ifstream &intext, ofstream &outtext,char &ch,int list[]){
	while(ch!='\n'&&!intext.eof())
{
		outtext<<ch;
		characterCount(ch,list);
		intext.get(ch);
	}
	if(intext.eof())outtext<<'\n';
	else outtext<<ch;
}
void characterCount(char ch,int list[]){
	int index;
	ch=toupper(ch);
	index=ch-65;
	if(0<=index&&index<26)
		list[index]++;
}
void writeTotal(ofstream &outtext,int lc,int list[]){
	int index;
	
	outtext<<"the number of lines="<<lc<<endl;
	for(index=0;index<26;index++){
		outtext<<char(index+65)<<" count="<<list[index]<<endl;
	}
}

(3).最后在main.cpp里添加如下代码

#include "TextCount.h"
#include <iostream>
using namespace std;
int main(){
	char ch;
	int lineCount;
	int letterCount[26];
	ifstream infile;
	ofstream outfile;
	
	infile.open("c:\\textin.txt");
	if(!infile){
		cout<<"cannot open input file."<<endl;
		return 1;
	}
	outfile.open("c:\\textout.txt");
	initialize(lineCount,letterCount);
	infile.get(ch);
	while(!infile.eof()){
		copyText(infile,outfile,ch,letterCount);
		lineCount++;
		infile.get(ch);
	}
	writeTotal(outfile,lineCount,letterCount);
	infile.close();
	outfile.close();
	return 0;
}
  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 1
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值