孟津县政府领导班子:谁能给做个C++小程序?(紧急)

来源:百度文库 编辑:杭州交通信息网 时间:2024/05/07 08:52:28
不管有多小,但代码一定要有详细过程和注释,先给30分,如果答案好再补100,绝不食言!!~~~
一楼的朋友,我想要的是个EXE文件的,能不能做个稍难点的.....

二楼的朋友:我要交作业......你不是叫老师整死我吧

可能我说的夸张了....不要简单到A+B=C这种啊....晕哦

有个简单计算器的,有没有会做的噢???

我编的小型计算器程序,能计算普通+,-,*,/,和带括号的表达式,使用顺序栈的数据结构,程序如下:
//////////////////////////////////////////////
#include "iostream"
#include "string"
using namespace std;

//---------------------------------- Stack.h --------------------------
//定义Stack类
const maxsize=20;
enum Error_code { success, overflow, underflow };

template <class T>
class Stack {
public:
Stack();
bool empty() const;
bool full() const;
int size() const;
void clear();
Error_code top(T &item) const;
Error_code pop();
Error_code push(const T &item);
private:
int count;
T entry[maxsize];
};

template <class T>
Stack<T>::Stack() {
count=0;
}

template <class T>
bool Stack<T>::empty () const {
return count==0;
}

template <class T>
bool Stack<T>::full () const {
return count==maxsize;
}

template <class T>
int Stack<T>::size() const {
return count;
}

template <class T>
void Stack<T>::clear() {
count=0;
}

template <class T>
Error_code Stack<T>::top (T &item) const {
if (empty()) return underflow;
item= entry[count-1];
return success;
}

template <class T>
Error_code Stack<T>::pop () {
if (empty()) return underflow;
count--;
return success;
}

template <class T>
Error_code Stack<T>::push (const T &item) {
if (full()) return overflow;
entry[count++]=item;
return success;
}
//------------------------------------------额外函数------------------

bool user_says_yes()
{
int c;
bool initial_response = true;
do { // Loop until an appropriate input is received.
if (initial_response)
cout << " (y,n)? " << flush;
else
cout << "Respond with either y or n: " << flush;
do { // Ignore white space.
c = cin.get();
} while (c == '\n' || c ==' ' || c == '\t');
initial_response = false;
} while (c != 'y' && c != 'Y' && c != 'n' && c != 'N');
return (c == 'y' || c == 'Y');
}

//---------------------------------- Main Program ----------------------

Stack<char> sign;
Stack<double> num;
int set; // 判断程序中的异常,以便适时退出

void process(char c) { //计算两个数的 + - * / 运算
int k=0;
double a,b;
sign.pop();
if (num.top(b)==success){ //判断例外
num.pop();
if (num.top(a)==success) {
num.pop();
k=1;
}
}
if (k) {
switch (c) {
case '+': num.push(a+b); break;
case '-': num.push(a-b); break;
case '*': num.push(a*b); break;
case '/':
if (b==0) { //分母不能为0
set=4;
num.push(-1);
}
else
num.push(a/b);
break;
}
}
else {set=1;num.push(-1);}
}
//输入表达式
void get_command(string &str) {
cout<<"\n请输入要进行运算的表达式,包括\" +,-,*,/,=,(,)\"和数字,"<<endl
<<"例如:\" 3+2.5*(6-25/4)-8.32= \"."<<endl
<<"注意: 以数字开头,等号结尾,中间括号要匹配."<<endl;
cin>>str;
}
//求值 表达式
double do_command(const string &str) {
string s="";
double outcome=-1;
char c;
for (int i=0;str[i]!='\0';i++)
{
if (set!=0) break; //例外 则停止运行
while (1) { //分离数据与运算符
if (str[i]<='9' && str[i]>='0' || str[i]=='.') {
s+=str[i];
i++;
}
else {
if(s!="") {
if (num.push(atof(s.c_str ()))==overflow)
set=3;
s="";
}
break;
}
}
char ch= str[i];
switch (ch) { //处理运算的优先级,并注意例外抛出
case '*':
case '/':
if (sign.top(c)==success)
if(c=='*'||c=='/') process(c);
if (sign.push(ch)==overflow)
set=3;
break;
case '+':
case '-':
while (sign.top(c)==success) {
if (c!='(') process(c);
else break;
}
if (sign.push(ch)==overflow)
set=3;
break;
case '(':
if (sign.push(ch)==overflow)
set=3;
break;
case ')':
while (sign.top(c)==success) {
if (c!='(') process(c);
else break;
}
sign.pop();
break;
case '=':
while (sign.top(c)==success) {
if (c!='(') process(c);
else break;
}
break;
default: set=2;break;
}
}
if (num.size()==1 && sign.size()==0)
num.top(outcome);
else set=1;
if (set==0) cout<<"运算结果是:\n"<<endl; //出错时的错误信息
else {
outcome=-1;
if (set==1) cout<<"\n您输入的不匹配,有错误发生。Result lost!!"<<endl;
if (set==2) cout<<"\n您输入了非法字符 , 请重新输入,谢谢合作!"<<endl;
if (set==3) cout<<"\nStack is full, Lost result!!"<<endl;
if (set==4) cout<<"\n 分母为0,不能进行除法运算,出现溢出, Lost result!!"<<endl;
}
return outcome;
}
// 主程序main()
int main() {
do {
string str,s;
set=0;
get_command(str);
s=str;
if( str[0]=='-') str='0'+str; //处理表达式中的负号
for (int i=1;str[i]!='\0';i++) {
if (str[i]=='-' && str[i-1]=='(') {
str.insert (i,"0");
i++;
}
}
double out= do_command(str);
cout<<s<<out<<endl; //输出结果
num.clear(); //清空栈
sign.clear();
cout<<"\n还要计算其它的吗"<<flush;
}while (user_says_yes()); //允许多次执行运算
return 1;
}

////////////////////////////////////////////////
////////////////////////////////////////////////
你可以输入一个算式,比如:
3+(4.5-2*8)/4-2*6=
程序得出计算结果:
3+(4.5-2*8)/4-2*6=-11.875
请参考代码,并试验程序。。。

#include <iostream>
#include <string>

using namespace std;

int main(int argc, char *argv[])
{
int count = 0, index = 1;
string word, str = "done";

cout << "Enter words (to stop, type the word done) : " << endl;
do
{
cout << index << " : ";
getline(cin, word);
index++;
count++;
} while(word!=str);

cout << "Word(s) Count : " << count-1 << endl;
cin.get();

return 0;
}
输入单词直到输入“done”结束。并显示单词数

/*执行程序,从键盘输入姓名然后屏幕输出姓名*/

#include<iostream.h> //包含iostream.h头文件

void main(void)
{
char name[30]; //定义姓名字符串变量数组
cout<<"name:"; //输出姓名,cin是输入操作符
cin<<"name\n"<<name<<endl;
//输入名字,cout是输出操作符
cout<<"name<<endl; //多个输出名字,endl是换行操作符。
}

#include <iostream>
using namespace std;
struct student
{
int id;
int scort;
};
int main()
{
student s[10];
int i=1;
float sun=0;//定义一个浮点型储存总分
cout<<"输入10个学生的id和分数\n";
while (i<=10)
{
cout<<"学生的ID:";
cin>>s[i]. id;
cout<<"学生"<<s[i].id<<"的分数:";
cin>>s[i]. scort;
sun=sun+s[i].scort;//总分
i++;
}//获得学生的id和分数
i=1;
while (i<=10)
{
switch (s[i].scort/10)
{
case 0:
case 1:
case 2:
case 3:
case 4:
case 5:cout<<"学生"<<s[i].id<<"分数为:"<<s[i].scort<<" 等级评定:"<<"不及格"<<endl;break;
case 6:
case 7:cout<<"学生"<<s[i].id<<"分数为:"<<s[i].scort<<" 等级评定:"<<"中"<<endl;break;
case 8:cout<<"学生"<<s[i].id<<"分数为:"<<s[i].scort<<" 等级评定:"<<"良"<<endl;break;
case 9:
case 10:cout<<"学生"<<s[i].id<<"分数为:"<<s[i].scort<<" 等级评定:"<<"优"<<endl;break;
}
i++;}
cout<<"学生平均分:"<<sun/10;
getchar();
getchar();
//输入10个学生的id和成绩,计算出等级和平均数
}