【救命一命】帮我把这c++代码改写成c代码,课程设计弄错了!

骨头教主 骨头教主 墨衍会员 · AI 创作全网分发 墨衍智能分发 通过墨衍一键同步至各平台 可同步平台 CSDN微信公众号微博知乎掘金百家号博客园抖音小红书 你的文章也可以这样分发 墨衍会员 ¥399起/年,写一次发全网 我也要 + 加入社区
后天就要答辩,自己改来不及了,很少50块辛苦费,但还是希望大家帮帮我。我生活费不多了,另外求求版主手下留情。帮帮我这个无知少年,好心第一个帮我改出来的最后请附上支付宝。下面是c++的代码,功能是一个简单的餐厅点餐系统,对餐单进行增加修改删除,客户点餐最后结账,代码如下
#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;
}
int action;
cout << "当前菜式的信息:\n" ;
cout << "编号\t菜名\t价格\t分类\n" ;
cout<< p << "\t" << foods[p].getName() + "\t" + foods[p].numberToString(foods[p].getPrice()) + "\t" +foods[p].getKind()<< endl;
cout<< "请选择:"<<endl;
cout<<"1.修改菜名"<<endl;
cout<<"2.修改价格"<<endl;
cout<<"3.修改分类"<<endl;

cin >> action ;
string name;
string kind;
int price;
switch(action)
{
case 1:cout << "请输入新菜名 :" ;

cin >>name ;
foods[p].setName(name);break;
case 2:cout << "请输入它的价格 :" ;
cin >>price ;
foods[p].setPrice(price);break;
case 3:cout << "请输入它的分类 :" ;
cin >>kind ;
foods[p].setKind(kind);break;

default:cout<<"输入错误\n"<<endl;
}
}
/*
删除菜式
*/
void deleteFood(int p)
{
if(p<0||p>=FoodsOfnumbr)
{
cout << "没有菜单号为" <<p<<"的菜\n";return;
}
for (int i = p; i < FoodsOfnumbr; i++)
{
foods[i] = foods[i+1];
}
FoodsOfnumbr--;
cout << "删除成功!\n";
}
/*
增加菜式
*/
void addFood()
{
string n = "";
string k = "";
int p;
bool word;
cout << "输入新的菜式的信息: " << endl;
do
{
cout << "菜名 : ";
cin >> n;
word = false ;
for(int i=0;i<FoodsOfnumbr;i++)
if(n == foods[i].getName()) {word = true;cout << "已经有" <<n<<"这个菜了!请重新输入!\n";break;}
}
while(word);
cout << "价格 :";
cin >> p;
cout << "分类 :";
cin >> k;
foods[FoodsOfnumbr] = Food(n,k,p);
FoodsOfnumbr++;
}
};
class Mangement
{
public:
void showDish()
{
int action = 0;
int pos = 0;
Dish d;
do
{
d.printFoods();
cout<<"\n"<<endl;
cout<< "1.修改菜单"<<endl;
cout<<"2.删除菜式"<<endl;
cout<<"3.添加菜式"<<endl;
cout << "4.退出"<<endl;
cin >> action;
switch (action)
{
case 1:
cout <<"输入要修改的菜 :";
cin >> pos ;
d.changeFood(pos);
break;
case 2:
cout <<"输入要删除的菜 :";
cin >> pos ;
d.deleteFood(pos);
break;
case 3:
d.addFood();
break;
case 4: return;
default:
cout << "非法操作!";
}

}
while (1);


}

};

class client
{
private : int money;
int rpMoney;
int b[100];
int number;

public :
client()
{
number=0;
money=0;
}
void add()
{
int p ;
cout << "请出入你想点的菜单号:";
cin >> p ;
for(int i=0;i<number;i++)
if(p==b[i])
{
cout << "您已经点过这个菜了!\n" ;
return ;
}
b[number++] = p ;

money+=foods[p].getPrice();

}

void deletefood()
{
int p ;
int i;
if(money == 0) {cout<<"您还没有点菜\n";
return;
}
for(i=0;i<number;i++)
{
cout << b[i] <<"\t"<< foods[b[i]].getName() + "\t" + foods[b[i]].numberToString(foods[b[i]].getPrice())<<endl;
}
cout << "共" << money << "元\n" ;
cout << "请出入你想撤销的菜单号:";
cin >> p ;

for( i=0;i<=number;i++)
{
if(b[i] == p) { b[i] = b[number--] ; money-=foods[p].getPrice();

}
}
if(i>number) cout<<"没有点这个菜\n";
}

void show()
{
int i;
if(money==0) return ;
cout << "您已点:" <<endl;
for(i=0;i<number;i++)
{
cout << foods[b[i]].getName() + "\t" + foods[b[i]].numberToString(foods[b[i]].getPrice())<<"元"<<endl;
}
cout << "共" << money << "元\n" ;
}

void showclient()
{
int pos;
Dish d;
d.printFoods();
cout<<"\n"<<endl;
do
{

cout<<"请选择:"<<endl;
cout<<"1.点菜"<<endl;
cout<<"2.取消"<<endl;
cout<<"3.结账"<<endl;
cout<<"4.退出"<<endl;
cin >> pos ;
switch(pos)
{

case 1: add();break;
case 2: deletefood();break;
case 3: cout << "你共消费了" << money << "元钱。\n欢迎您下次光临!\n"; return ;
case 4 : return ;
default:
cout << "非法操作!\n";
}
show();
}
while(1);
}

};

int main()
{
Dish dish;
client c;
Mangement mangement;
int i;
dish.readFood();

while(1)
{
cout << " ***************************欢迎使用点餐管理系统!****************************" <<endl;
cout << "------------------------------1.用户点餐-------------------------------------"<<endl;
cout << "------------------------------2.菜单管理-------------------------------------"<<endl;
cout << "------------------------------3.退出-----------------------------------------"<<endl;
cin >> i;
switch(i)
{
case 1:c.showclient();break;
case 2:mangement.showDish();break;
case 3:dish.writeFood();system("pause");return 0;
default: cout<<"输入错误,请重新选择!"<<endl;break;
}

}
return 0;
}
...全文

相关推荐

如何把C++的源代码改写成C代码的方法

C++解释器比C语言解释器占用的存储空间要大,想要在某些特定场合兼容C++代码,同时为了节省有限的存储空间,降低成本,也为了提高效率,将用C++语言写的源程序用C语言改写是很有必要的。 C++与C区别最大的就是C++中的类的概念和特性,将C++改为C的问题,就转换成如何将类化去的问题。 方法有两种: 将C++中的面向对象特征去掉,先全部理解源代码的逻辑,然后改写; 是在C中保留面向对象的部分特征,用结构体实现类的功能。 第一种方法,对于类的数目很少的情况还可以,如果类的数目比较多,全部理解源代码,然后重写就很耗时间,而且很容易出错,更甚者,如果遇到大的项目想全部理解源代码几乎是不可能的

weixin_38621272

cpp代码-C++基础

cpp代码-C++基础

weixin_38645379

33个c++重要知识总结(cpp代码)

c++基础知识复习,包含了33个cpp文件,每个文件包含特定主题的知识回顾,使用特定案例重温某些不常用但是重要的算法或函数的使用方法,含注释。

GodFather47

C C++ 课程设计 《三子连珠》

用C C++ 编写的三子连珠程序, 内含两种版本,一个是流水式的,一个是调用函数的, 但方法一样, 结构清晰, 简单易懂。

zhang1987110

cpp代码-C和C++:一些符号 || && ! :: . -> %

cpp代码-C和C++:一些符号 || && ! :: . -> %

weixin_38704922
评论 9
收藏 已收藏
分享
返回顶部
骨头教主
C++ 语言 65,213 成员 250,513 内容