7:标准类库的应用和字符转换(图书销售)
先试着做一个小小的图书销售系统.
先要搞定保存图书的数据存储..
因为是刚学也就选最简单的用文本来保存吧
数据储存格式为
编号:
书名:
单价:
接下来是怎么样把这个文本给读取出来呢?
#include <fstream>
std::ifstream myfile("c:\\1.txt");
while(myfile >> word)
{
std::cout << word << std::endl;\\第一行的数据库
}
读取搞定了就简单了先分为四部份
1:图书清单
2:查询图书
3:销售图书
4:销售归零
因为没有那么高的水平呵~~占时不做销售保存 只做个金额累加。
主程序部份
#include "stdafx.h"
#include <iostream>
#include <fstream>
#include <string>//字符串型
using std::string;//字符串型
int sumje;
book _book;
int _tmain(int argc, _TCHAR* argv[])
{
int v1;
int _bookop(int v1);
while(1)
{
std::cout << "===============================================================" << std::endl;
std::cout << "1:书清单" << std::endl;
std::cout << std::endl;
std::cout << "2:书查询" << std::endl;
std::cout << std::endl;
std::cout << "3:销售" << std::endl;
std::cout << std::endl;
std::cout << "4:归零" << std::endl;
std::cout << std::endl;
std::cout << "请输入操作编号:" << std::endl;
std::cin >> v1;
_bookop(v1);
}
return 0;
}
int _bookop(int v1)
{
string s1;
int v2;
switch (v1)
{
case 1:
system("cls");
_book.booklist();
return 0;
break;
case 2:
std::cout << "===============================================================" << std::endl;
std::cout << "请输入书编号:" << std::endl;
std::cin >> s1;
system("cls");
_book.bookselect(s1);
return 0;
break;
case 3:
std::cout << "===============================================================" << std::endl;
std::cout << "请输入书编号:" << std::endl;
std::cin >> s1;
std::cout << "请输入数量:" << std::endl;
std::cin >> v2;
system("cls");
sumje +=_book.bookselect(s1)*v2;
std::cout << "销售金额:" << sumje << std::endl;
return 0;
break;
case 4:
sumje =0;
system("cls");
return 0;
break;
default:
return 1;
break;
}
}
book类头文件;
#pragma once
#include <string>//字符串型
using std::string;//字符串型
class book
{
public:
book(void);
int booklist(void);
int bookselect(string bookno);
~book(void);
};
book源文件;
#include "StdAfx.h"
#include "book.h"
#include <iostream>
#include <fstream>
#include <string>//字符串型
using std::string;//字符串型
book::book(void)
{
}
int book::booklist(void)
{
int v1=1;
string word;
string pr;
//先搞定数据储存先..
std::ifstream myfile("c:\\1.txt");
while(myfile >> word)
{
if (v1==3)
{
std::cout << pr << "售价:[" << word << "]" << std::endl;
v1=1;
pr="";
}
else
{
switch (v1)
{
case 1:
pr = "编号:[" + word + "] ";
break;
case 2:
pr = pr + "书名:[" + word + "] ";
break;
}
v1 +=1;
}
}
myfile.close();
return 0;
}
int book::bookselect(string bookno)
{
int v1=1;
int v2=0;
string word;
string pr;
//先搞定数据储存先..
std::ifstream myfile("c:\\1.txt");
while(myfile >> word)
{
if (word == bookno)
{
v2=1;
}
if (v2==1)
{
switch (v1)
{
case 1:
pr = "编号:[" + word + "] ";
break;
case 2:
pr = pr + "书名:[" + word + "] ";
break;
case 3:
std::cout << pr << "售价:[" << word << "]" << std::endl;
v2=0;
return atol(word.c_str());
break;
default:
return 0;
}
v1+=1;
}
if (v1==4)
{
return 1;
break;
}
}
myfile.close();
return 0;
}
book::~book(void)
{
}
还有 stdafx.h 类库引用
#include "book.h"
数据格式示例
0X001
春天里的小花
25
0X002
冬眠的熊
17
0X003
北方的狼
21