找回密码
 注册
搜索
查看: 1044|回复: 28

C语言求助,在线等

[复制链接]
发表于 2007-11-30 21:12:09 | 显示全部楼层 |阅读模式
我哥帮我做了个课设,用的C++
可我想改为C的,遇到了个问题
/****************************************
* file name  : drugManager.cpp
* author    : Andrew
* last modify : 11/27/2007
***************************************/
#include<stdio.h>

const int N = 10;
struct drug
{
int   number;
char  kind;
float  price;
int   amount;
double totalPrice;
};
void _author(void);
void _mainMenu(drug *drugs);
void _initDrug(drug *drugs);
void _checkNumber(drug *drugs, const int &i);
void _locate(drug *drugs);
void _subLocate(drug *drugs);
void _update(drug *drugs);
void _subUpdate(drug *drugs);
void _insert(drug *drugs, int &N);
void _subInsert(drug *drugs, int &N);
void _delete(drug *drugs);
void _subDelete(drug *drugs);
void _statistMenu(drug *drugs);
void _viewPrice(drug *drugs);
void _sortMenu(drug *drugs);
void _upSort(drug *drugs);
void _downSort(drug *drugs);
void _exit(drug *drugs);

int main(int argc, char **argv)
{
_author();
drug *drugs = new drug[N];
_initDrug(drugs);
return (0);
}
void _author(void)
{
cout<<\"My name is Song Zhenyuan\"<<endl;
cout<<\"My number is 123\"<<endl;
cout<<\"I am 19 years old. \"<<endl<<endl;
}
//*************************************************************
void _mainMenu(drug *drugs)
{
system(\"cls\");

cout<<\"             DRUG MANAGE SYSTEM            \"<<endl;
cout<<\"                                     \"<<endl;
cout<<\"1. Key in &#39L&#39 to locate the imformation by drug number.\"<<endl;
cout<<\"2. Key in &#39U&#39 to update the imformation by drug number.\"<<endl;
cout<<\"3. Key in &#39D&#39 to delete the imformation by drug number.\"<<endl;
cout<<\"4. Key in &#39S&#39 to view the Statistic imformations.    \"<<endl;
cout<<\"5. Key in &#39E&#39 to exit the system.\"<<endl;
cout<<endl<<\"Select one option you want:\";
char choose = &#39\\0&#39;
int  aCase  = 0;
cin>>choose;
if (choose==&#39L&#39 || choose==&#39l&#39) aCase = 1;
if (choose==&#39U&#39 || choose==&#39u&#39) aCase = 2;
if (choose==&#39D&#39 || choose==&#39d&#39) aCase = 3;
if (choose==&#39S&#39 || choose==&#39s&#39) aCase = 4;
if (choose==&#39E&#39 || choose==&#39e&#39) aCase = 5;
switch (aCase)
{
case 1: _locate(drugs); break;
case 2: _update(drugs); break;
case 3: _delete(drugs); break;
case 4: _statistMenu(drugs); break;
case 5: _exit(drugs); break;
default: cout<<\"The option can not find\"<<endl;
   cout<<\"lease put &#39enter&#39 key try again.\";
   char c;
   cin.get(c);
   cin.get(c);
   if(c==&#39\\n&#39) _mainMenu(drugs);
   break;
}
}
//*************************************************************
void _initDrug(drug *drugs)
{
cout<<\"lease enter the informations of drugs.\"<<endl;
cout<<\"************************\"<<endl;
for(int i=0; i<N; i++ )
{
  cout<<endl<<\"lease enter the drugs of \"<<i+1<<\":\"<<endl;
  _checkNumber(drugs, i);
  cout<<\"choose the drug kind:\"<<endl;
  char k = &#39\\0&#39;
  cout<<\"enter C is Chinese traditional medicine\"<<endl;
  cout<<\"enter W is Western medicine\"<<endl;
  cin>>k;
  if (k==&#39c&#39 || k==&#39C&#39) drugs.kind = &#39C&#39;
  if (k==&#39w&#39 || k==&#39W&#39) drugs.kind = &#39W&#39;
  cout<<\"Drug price(yuan):\";
  cin>>drugs.price;
  cout<<\"Drug amount:\";
  cin>>drugs.amount;
  drugs.totalPrice = drugs.price * drugs.amount;
  cout<<endl<<\"************************\"<<endl;
}
cout<<\"INPUT COMPLETE\"<<endl;
cout<<\"lease put &#39enter&#39 key to goto main menu\";
char c;
cin.get(c);
cin.get(c);
if(c==&#39\\n&#39) _mainMenu(drugs);
}
void _checkNumber(drug *drugs, const int &i)
{
cout<<\"Drug number: \";
drugs.number = -1;

cin>>drugs.number;
for(int j=0; j<i; j++)
{
  if(drugs.number == drugs[j].number)
  {
  cout<<\"Existing drug number, please enter again.\"<<endl;
  _checkNumber(drugs, i);
  }
}
}

//*************************************************************
void _locate(drug *drugs)
{
system(\"cls\");
cout<<\"         DRUG MANAGE SYSTEM LOCATE            \"<<endl;
cout<<\"                                     \"<<endl;
cout<<\"1. Key in &#39L&#39 to locate the imformation by drug number.\"<<endl;
cout<<\"2. Key in &#39R&#39 to return the main menu.            \"<<endl;
cout<<endl<<\"Select one option you want:\";
char choose = &#39\\0&#39;
int  aCase  = 0;
cin>>choose;
if (choose==&#39L&#39 || choose==&#39l&#39) aCase = 1;
if (choose==&#39R&#39 || choose==&#39r&#39) aCase = 2;
switch (aCase)
{
case 1: _subLocate(drugs); break;
case 2: _mainMenu(drugs); break;
default: cout<<\"The option can not find\"<<endl;
   cout<<\"lease put &#39enter&#39 key try again.\";
   char c;
   cin.get(c);
   cin.get(c);
   if(c==&#39\\n&#39) _locate(drugs);
   break;
}
}
void _subLocate(drug *drugs)
{
system(\"cls\");
cout<<\"Existing drug numbers:\"<<endl;
for (int i=0; i<N; i++)
{
  if (drugs.number!=-1)
  cout<<\"drug number:\"<<drugs.number<<endl;
}
cout<<endl;
int x;
cout<<\"Choose the number which you want to see: \";
cin>>x;
for (i=0; i<N; i++)
{
  if (x==drugs.number)
  {
  if(drugs.kind = &#39C&#39)
   cout<<\"The drug kind:\"<<\"Chinese traditional medicine\"<<endl;
  else
   cout<<\"The drug kind: \"<<\"Western medicine\"<<endl;
  cout<<\"Drug price: \"<<drugs.price<<\" yuan\"<<endl;
  cout<<\"Drug amount: \"<<drugs.amount<<endl;
  cout<<\"Drug total price: \"<<drugs.totalPrice<<\" yuan\"<<endl;
  
  break;
  }
}

cout<<endl<<\"lease put &#39enter&#39 key to goto main menu\";
char c;
cin.get(c);
cin.get(c);
if(c==&#39\\n&#39) _mainMenu(drugs);
}
//*************************************************************
void _update(drug *drugs)
{
system(\"cls\");
cout<<\"         DRUG MANAGE SYSTEM UPDATE            \"<<endl;
cout<<\"                                     \"<<endl;
cout<<\"1. Key in &#39U&#39 to update the imformation by drug number.\"<<endl;
cout<<\"2. Key in &#39R&#39 to return the main menu.            \"<<endl;
cout<<endl<<\"Select one option you want:\";
char choose = &#39\\0&#39;
int  aCase  = 0;
cin>>choose;
if (choose==&#39U&#39 || choose==&#39u&#39) aCase = 1;
if (choose==&#39R&#39 || choose==&#39r&#39) aCase = 2;
switch (aCase)
{
case 1: _subUpdate(drugs); break;
case 2: _mainMenu(drugs); break;
default: cout<<\"The option can not find\"<<endl;
   cout<<\"lease put &#39enter&#39 key try again.\";
   char c;
   cin.get(c);
   cin.get(c);
   if(c==&#39\\n&#39) _update(drugs);
   break;
}
}
void _subUpdate(drug *drugs)
{
system(\"cls\");
cout<<\"Existing drug numbers:\"<<endl;
for (int i=0; i<N; i++)
{
  if (drugs.number!=-1)
  cout<<\"drug number:\"<<drugs.number<<endl;
}
cout<<endl;
int x;
cout<<\"Choose the number which you want to change : \";
cin>>x;
for (i=0; i<N; i++)
{
  if (x==drugs.number)
  {
  cout<<\"choose the drug kind:\"<<endl;
  char k = &#39\\0&#39;
  cout<<\"enter C is Chinese traditional medicine\"<<endl;
  cout<<\"enter W is Western medicine\"<<endl;
  cin>>k;
  if (k==&#39c&#39 || k==&#39C&#39) drugs.kind = &#39C&#39;
  if (k==&#39w&#39 || k==&#39W&#39) drugs.kind = &#39W&#39;
  cout<<\"Drug price:\";
  cin>>drugs.price;
  cout<<\"Drug amount:\";
  cin>>drugs.amount;
  drugs.totalPrice = drugs.price * drugs.amount;
  
  break;
  }
}

cout<<\"UPDATE SUCCEED\"<<endl;
cout<<\"lease put &#39enter&#39 key to goto main menu\";
char c;
cin.get(c);
cin.get(c);
if(c==&#39\\n&#39) _mainMenu(drugs);
}
//*************************************************************
void _delete(drug *drugs)
{
system(\"cls\");
cout<<\"         DRUG MANAGE SYSTEM DELETE            \"<<endl;
cout<<\"                                     \"<<endl;
cout<<\"1. Key in &#39D&#39 to delete the imformation by drug number.\"<<endl;
cout<<\"2. Key in &#39R&#39 to return the main menu.\"<<endl;
cout<<endl<<\"Select one option you want:\";
char choose = &#39\\0&#39;
int  aCase  = 0;
cin>>choose;
if (choose==&#39D&#39 || choose==&#39d&#39) aCase = 1;
if (choose==&#39R&#39 || choose==&#39r&#39) aCase = 2;
switch (aCase)
{
case 1: _subDelete(drugs); break;
case 2: _mainMenu(drugs); break;
default: cout<<\"The option can not find\"<<endl;
   cout<<\"lease put &#39enter&#39 key try again.\";
   char c;
   cin.get(c);
   cin.get(c);
   if(c==&#39\\n&#39) _delete(drugs);
   break;
}
}
void _subDelete(drug *drugs)
{
system(\"cls\");
cout<<\"Exit drug numbers:\"<<endl;
for (int i=0; i<N; i++)
{
  if (drugs.number!=-1)
  cout<<\"drug number:\"<<drugs.number<<endl;
}
cout<<endl;
int x;
cout<<\"Choose the number which you want to delete: \";
cin>>x;
for (i=0; i<N; i++)
{
  if (x==drugs.number)
  {
  drugs.number  = -1;
  drugs.kind  = &#39\\0&#39;
  drugs.price  = 0;
  drugs.amount  = 0;
  drugs.totalPrice = 0;
  break;
  }
}

cout<<\"DELETE SUCCEED\"<<endl;
cout<<\"lease put &#39enter&#39 key to goto main menu\";
char c;
cin.get(c);
cin.get(c);
if(c==&#39\\n&#39) _mainMenu(drugs);
}
//*************************************************************
void _statistMenu(drug *drugs)
{
system(\"cls\");
cout<<\"          DRUG STATIST MENU            \"<<endl;
cout<<\"                                 \"<<endl;
cout<<\"1. Key in &#39P&#39 to view the statist price of drugs.\"<<endl;
cout<<\"2. Key in &#39S&#39 to sort the drugs by unit price.  \"<<endl;
cout<<\"3. Key in &#39R&#39 to return the main menu.\"<<endl;
cout<<endl<<\"Select one option you want:\";
char choose = &#39\\0&#39;
int  aCase  = 0;
cin>>choose;
if (choose==&#39P&#39 || choose==&#39p&#39) aCase = 1;
if (choose==&#39S&#39 || choose==&#39s&#39) aCase = 2;
if (choose==&#39R&#39 || choose==&#39r&#39) aCase = 3;
switch (aCase)
{
case 1: _viewPrice(drugs); break;
case 2: _sortMenu(drugs); break;
case 3: _mainMenu(drugs); break;
default: cout<<\"The option can not find\"<<endl;
   cout<<\"Please put &#39enter&#39 key try again.\";
   char c;
   cin.get(c);
   cin.get(c);
   if(c==&#39\\n&#39) _statistMenu(drugs);
   break;
}
}
void _viewPrice(drug *drugs)
{
system(\"cls\");
cout<<\"          DRUG STATIST VIEW            \"<<endl;
cout<<\"                                 \"<<endl;
double tPrice  = 0;
double cTotalPrice = 0;
double wTotalPrice = 0;
for (int i=0; i<N; i++)
{
  if (drugs.number != -1 && drugs.kind == &#39C&#39)
  {
  cTotalPrice += drugs.totalPrice;
  }
  if (drugs.number != -1 && drugs.kind == &#39W&#39)
  {
  wTotalPrice += drugs.totalPrice;
  }
}
tPrice = cTotalPrice + wTotalPrice;
cout<<\"The price of total drugs is \"<<tPrice<<\" yuan\"<<endl;
cout<<\"The price of Chinese drugs is \"<<cTotalPrice<<\" yuan\"<<endl;
cout<<\"The price of Westen drugs is \"<<wTotalPrice<<\" yuan\"<<endl;
cout<<endl;
cout<<endl<<\"Please put &#39enter&#39 key to goto statist menu.\";
char c;
cin.get(c);
cin.get(c);
if(c==&#39\\n&#39) _statistMenu(drugs);
}
void _sortMenu(drug *drugs)
{
system(\"cls\");
cout<<\"            DRUG UNIT PRICE SORT               \"<<endl;
cout<<\"                                        \"<<endl;
cout<<\"1. Key in &#39E&#39 to sort the drug unit from expensive to cheap.\"<<endl;
cout<<\"2. Key in &#39C&#39 to sort the drug unit from cheap to expensive.\"<<endl;
cout<<\"3. Key in &#39R&#39 to return the statist menu.             \"<<endl;
cout<<endl<<\"Select one option you want:\";
char choose = &#39\\0&#39;
int  aCase  = 0;
cin>>choose;
if (choose==&#39E&#39 || choose==&#39e&#39) aCase = 1;
if (choose==&#39C&#39 || choose==&#39c&#39) aCase = 2;
if (choose==&#39R&#39 || choose==&#39r&#39) aCase = 3;
switch (aCase)
{
case 1: _upSort(drugs); break;
case 2: _downSort(drugs); break;
case 3: _statistMenu(drugs); break;
default: cout<<\"The option can not find\"<<endl;
   cout<<\"Please put &#39enter&#39 key try again.\";
   char c;
   cin.get(c);
   cin.get(c);
   if(c==&#39\\n&#39) _sortMenu(drugs);
   break;
}
}
void _downSort(drug *drugs)
{
system(\"cls\");
int num = 0;
drug temp;
for (int i=0; i<N; i++)
{
  if (drugs.number != -1) num += 1;
}
for (int j=0; j<num-1; j++)
{
  for (i=0; i<num-1-j; i++)
  {
  if (drugs.price>drugs[i+1].price)
  {
   temp = drugs;
   drugs = drugs[i+1];
   drugs[i+1] = temp;
  }
  }
}
cout<<\"The drug unit from expensive to cheap.\"<<endl;
for (i=0; i<N; i++)
{
  if(drugs.number != -1)
  cout<<i+1<<\". The drug number: \"<<drugs.number<<endl;
}
cout<<endl;
cout<<\"Please put &#39enter&#39 key to return sort menu.\";
char c;
cin.get(c);
cin.get(c);
if(c==&#39\\n&#39) _sortMenu(drugs);

}
void _upSort(drug *drugs)
{
system(\"cls\");
int num = 0;
drug temp;
for (int i=0; i<N; i++)
{
  if (drugs.number != -1) num += 1;
}
for (int j=0; j<num-1; j++)
{
  for (i=0; i<num-1-j; i++)
  {
  if (drugs.price<drugs[i+1].price)
  {
   temp = drugs;
   drugs = drugs[i+1];
   drugs[i+1] = temp;
  }
  }
}
cout<<\"The drug unit from cheap to expensive.\"<<endl;
for (i=0; i<N; i++)
{
  if(drugs.number != -1)
  cout<<i+1<<\". The drug number: \"<<drugs.number<<endl;
}
cout<<endl;
cout<<\"Please put &#39enter&#39 key to return sort menu.\";
char c;
cin.get(c);
cin.get(c);
if(c==&#39\\n&#39) _sortMenu(drugs);
}
//*************************************************************
void _exit(drug *drugs)
{
delete []drugs;
cout<<endl<<\"Thanks for using this system. \"<<endl;
cout<<\"Copyright by Andrew 2007.\"<<endl<<endl;
}
//*************************************************************







void _mainMenu(drug *drugs);
void _initDrug(drug *drugs);
void _checkNumber(drug *drugs, const int &i);
void _locate(drug *drugs);
void _subLocate(drug *drugs);
void _update(drug *drugs);
void _subUpdate(drug *drugs);
void _insert(drug *drugs, int &N);
void _subInsert(drug *drugs, int &N);
void _delete(drug *drugs);
void _subDelete(drug *drugs);
void _statistMenu(drug *drugs);
void _viewPrice(drug *drugs);
void _sortMenu(drug *drugs);
void _upSort(drug *drugs);
void _downSort(drug *drugs);
void _exit(drug *drugs);
这些都提示语法错误,这是为什么啃
发表于 2007-11-30 21:17:50 | 显示全部楼层
惃错误信息发七来

而且C语言的控制台输入输出要用 printf() scanf()的吧  用cout cin 的都不行吧
发表于 2007-11-30 21:46:17 | 显示全部楼层
用VS2005编译了一下  果不其然 是C语言中不支持的C++语言特性 引起的错误  69个的说
 楼主| 发表于 2007-12-1 17:02:06 | 显示全部楼层
恩,好象指针那有错误
发表于 2007-12-1 22:15:06 | 显示全部楼层
看不懂,没想到楼主还这么有文化,没想到,没想到。。
发表于 2007-12-1 22:26:50 | 显示全部楼层
英语专上路过
发表于 2007-12-1 23:18:00 | 显示全部楼层
LZ的程序让我很有挫败感,实在看不出指针哪用错了,但就是有错误。也试了通过只改变接口来让程序通过,失败。按照LZ程序中指针的使用方法写了个验证指针机制的小程序,却又没有错。  败了败了。叫你哥帮你搞定还实际些。
发表于 2007-12-1 23:25:36 | 显示全部楼层
软件专上但是只学到C还米学C++的路过...
发表于 2007-12-1 23:31:53 | 显示全部楼层
LS哪的软件专上?

我们就学了C++。根据我所知道的范围,C和C++差距不大。
发表于 2007-12-1 23:51:32 | 显示全部楼层
BJUT的数字媒体技术,和LZ是一个学校的~
您需要登录后才可以回帖 登录 | 注册

本版积分规则

Archiver|手机版|小黑屋|MGCN谍海游龙

GMT+8, 2026-8-20 15:28 , Processed in 0.044192 second(s), 16 queries .

Powered by Discuz! X5.0

© 2001-2026 Discuz! Team.

快速回复 返回顶部 返回列表