请把此c++代码改写成c语言 15
#include<iostream>#include<fstream>#include<sstream>#include<string.h>usingnamespaces...
#include <iostream>
#include <fstream>
#include <sstream>
#include <string.h>
using namespace std;
/*
菜单食物的类
*/
class Food
{
private:
string name;//菜名
string kind;//分类
int price;//价格
public:
friend class Dish;
Food(){}
Food(string n,string k,int p )
{
name = n;
price = p;
kind=k;
}
void setName(string n)
{
name = n;
}
string getName()
{
return name;
}
void setKind(string k)
{
kind = k;
}
string getKind()
{
return kind;
}
int getPrice()
{
return price;
}
void setPrice(int p)
{
price = p;
}
string numberToString (int x )
{
string result;
stringstream convert;
convert << x;
result = convert.str();
return result;
}
};
Food foods[100];
int FoodsOfnumbr =0;
/*
在磁盘文件对菜单的读写
*/
class Dish
{
public :
friend class Food;
//字符串转为数字
int stringToInt(string s)
{
int thevalue;
istringstream ss(s);
ss >> thevalue;
return thevalue;
}
//字符串分割函数
void split(string s, string fields[] )
{
istringstream ss( s );
int f = 0;
while (ss)
{
string s;
if (!getline( ss, s, '\t' )) break;
fields[f] = s;
f++;
}
}
/*
读取磁盘文件里面的菜单信息
*/
bool readFood()
{
ifstream myDish;
myDish.open("d://food.txt");//打开D盘根目录的food.txt
string line;
string members[3];
int p = 0;;
int highestId = 0;
if (myDish.is_open())
{
while ( ! myDish.eof() )
{
getline (myDish,line);
if(line.length() > 0)
{ // avoid empty lines
split(line, members);
foods[p] = Food(members[0],members[2],stringToInt(members[1]));
p++;
}
}
FoodsOfnumbr = p;
myDish.close();
return true;
}
else
{
ofstream out;
out.open("d://food.txt");//若是还没有任何菜单信息则创建一个food.txt文件
return false;
}
}
/*
吧菜单信息写入磁盘文件
*/
bool writeFood()
{
ofstream myDish;
myDish.open("d://food.txt");//打开D盘根目录的food.txt
if (myDish.is_open())
{
for (int i = 0; i < FoodsOfnumbr; i++){
myDish << foods[i].getName() + "\t" + foods[i].numberToString(foods[i].getPrice()) + "\t" +foods[i].getKind() <<"\n";
}
myDish.close();
return true;
} else return false;
}
/*
输出菜单
*/
void printFoods()
{
if(FoodsOfnumbr==0){cout<<"暂时还没有任何菜式"<<endl;return ;
}
cout << "菜单如下:" << endl;
cout << "菜单号\t菜名\t价格\t分类\n" ;
for (int i = 0; i < FoodsOfnumbr; i++)
{
cout << i << "\t" << foods[i].getName() + "\t" + foods[i].numberToString(foods[i].getPrice()) + "\t" + foods[i].getKind()<< endl;
}
}
/*
修改菜式
*/
void changeFood(int p)
{
if(p<0||p>=FoodsOfnumbr)
{
cout << "没有菜单号为" <<p<<"的菜\n";return;
}
还有一半发不了了,谁能改的留个或者告诉我怎样改也行 展开
#include <fstream>
#include <sstream>
#include <string.h>
using namespace std;
/*
菜单食物的类
*/
class Food
{
private:
string name;//菜名
string kind;//分类
int price;//价格
public:
friend class Dish;
Food(){}
Food(string n,string k,int p )
{
name = n;
price = p;
kind=k;
}
void setName(string n)
{
name = n;
}
string getName()
{
return name;
}
void setKind(string k)
{
kind = k;
}
string getKind()
{
return kind;
}
int getPrice()
{
return price;
}
void setPrice(int p)
{
price = p;
}
string numberToString (int x )
{
string result;
stringstream convert;
convert << x;
result = convert.str();
return result;
}
};
Food foods[100];
int FoodsOfnumbr =0;
/*
在磁盘文件对菜单的读写
*/
class Dish
{
public :
friend class Food;
//字符串转为数字
int stringToInt(string s)
{
int thevalue;
istringstream ss(s);
ss >> thevalue;
return thevalue;
}
//字符串分割函数
void split(string s, string fields[] )
{
istringstream ss( s );
int f = 0;
while (ss)
{
string s;
if (!getline( ss, s, '\t' )) break;
fields[f] = s;
f++;
}
}
/*
读取磁盘文件里面的菜单信息
*/
bool readFood()
{
ifstream myDish;
myDish.open("d://food.txt");//打开D盘根目录的food.txt
string line;
string members[3];
int p = 0;;
int highestId = 0;
if (myDish.is_open())
{
while ( ! myDish.eof() )
{
getline (myDish,line);
if(line.length() > 0)
{ // avoid empty lines
split(line, members);
foods[p] = Food(members[0],members[2],stringToInt(members[1]));
p++;
}
}
FoodsOfnumbr = p;
myDish.close();
return true;
}
else
{
ofstream out;
out.open("d://food.txt");//若是还没有任何菜单信息则创建一个food.txt文件
return false;
}
}
/*
吧菜单信息写入磁盘文件
*/
bool writeFood()
{
ofstream myDish;
myDish.open("d://food.txt");//打开D盘根目录的food.txt
if (myDish.is_open())
{
for (int i = 0; i < FoodsOfnumbr; i++){
myDish << foods[i].getName() + "\t" + foods[i].numberToString(foods[i].getPrice()) + "\t" +foods[i].getKind() <<"\n";
}
myDish.close();
return true;
} else return false;
}
/*
输出菜单
*/
void printFoods()
{
if(FoodsOfnumbr==0){cout<<"暂时还没有任何菜式"<<endl;return ;
}
cout << "菜单如下:" << endl;
cout << "菜单号\t菜名\t价格\t分类\n" ;
for (int i = 0; i < FoodsOfnumbr; i++)
{
cout << i << "\t" << foods[i].getName() + "\t" + foods[i].numberToString(foods[i].getPrice()) + "\t" + foods[i].getKind()<< endl;
}
}
/*
修改菜式
*/
void changeFood(int p)
{
if(p<0||p>=FoodsOfnumbr)
{
cout << "没有菜单号为" <<p<<"的菜\n";return;
}
还有一半发不了了,谁能改的留个或者告诉我怎样改也行 展开
展开全部
用到了 C++的类, 还有很多成员函数,这个要改成C语言的结构体,恐怕有点麻烦了....
更多追问追答
追问
对啊,不会改。。自己捣鼓一个月弄出来的课程设计,最后发现要用c不是c++
追答
class改struct 数据成员不用变.
把成员函数全部放到外面, 都加上一个参数吧, 参数可以是一个下标,或者是这个结构体对象的引用. 这样的方式也可以达到你的目的.
文件读写也不能用 fstream了, 使用FILE 文件指针,这样才是C语言.
本回答被网友采纳
已赞过
已踩过<
评论
收起
你对这个回答的评价是?
推荐律师服务:
若未解决您的问题,请您详细描述您的问题,通过百度律临进行免费专业咨询