本文目录一览:
C语言课程设计写了一个可以画地图的程序 求取一个好听的名字= ̄ω ̄=
名字的话最好取英文而且是不能输入空格的 我给你的建议是 drawingmap DIYmap Imap 望采纳!
求编程c语言
我以前写过的实验,但是是c++的。稍微改一下头文件和输入输出的格式就可以了。我们快熄灯了,没时间帮你改了。报告太长了,就不发上来了,要是需要的话再说。
#include<iostream>
#include<cmath>
#include<cstdlib>
using namespace std;
#define MAX 1000
struct save1 {
float n[MAX];
int top;
} stack1;
struct save2 {
char n[MAX];
int top;
} stack2;
// stack1存储数字,stack2存储运算符号.
bool stackempty(save1 s) //判断是否为空
{
if (s.top == -1)
return 1;
else
return 0;
}
bool stackempty2(save2 s) //判断是否为空
{
if (s.top == -1)
return 1;
else
return 0;
}
void push(save1 s, float e) //将e入栈
{
if (s.top == MAX - 1) {
cout << "栈已满" << endl;
return;
}
s.top++;
s.n[s.top] = e;
}
void push2(save2 s, char e) //将e入栈
{
if (s.top == MAX - 1) {
cout << "栈已满" << endl;
return;
}
s.top++;
s.n[s.top] = e;
}
void pop(save1 s, float e) //将栈顶元素出栈,存到e中
{
if (s.top == -1) {
cout << "栈为空" << endl;
} else {
e = s.n[s.top];
s.top--;
}
}
void pop2(save2 s, char e) //将栈顶元素出栈,存到e中
{
if (s.top == -1) {
cout << "栈为空" << endl;
} else {
e = s.n[s.top];
s.top--;
}
}
int in(char e) //e在栈内的优先级别
{
if (e == '-' || e == '+') return 2;
if (e == '*' || e == '/') return 4;
if (e == '^') return 5;
if (e == '(') return 0;
if (e == ')') return 7;
return -1;
}
int out(char e) //e在栈外的优先级别
{
if (e == '-' || e == '+') return 1;
if (e == '*' || e == '/') return 3;
if (e == '^') return 6;
if (e == '(') return 7;
if (e == ')') return 0;
return -1;
}
void count(float a, char ope, float b) //进行计算并将计算结果入栈
{
float sum;
if (ope == '+') sum = a + b;
if (ope == '-') sum = a - b;
if (ope == '*') sum = a * b;
if (ope == '/') sum = a / b;
if (ope == '^') sum = pow(a, b);
push(stack1, sum);
}
int main()
{
int i = 0, len, j, nofpoint, g = 0; // len表示输入式子的长度。 g表示读入的字符是否是字母变量、数字以及运算符。
float a, b; // a、b用来存储操作数栈中弹出的操作数,便于代入函数中进行计算。
char line[MAX], operate, temp[20];
cout << "请输入表达式" << endl;
cin >> line;
len = strlen(line);
stack1.top = -1; // 将栈置为空
stack2.top = -1; // 将栈置为空
while (1) {
g = 0;
if (isdigit(line[i])) // 若读入的字符为数字,则继续判断下一个字符,直到下一个字符不是数字或者不是小数点,即可保证该操作数是完整的小数,然后将该数入操作数栈。
{
j = 0;
nofpoint = 0; // 记录所存的数中小数点的个数
while (isdigit(line[i]) || line[i] == '.') {
if (line[i] == '.') nofpoint++;
temp[j++] = line[i];
i++;
if (i == len) break;
}
if (nofpoint > 1 || (i < len && line[i] != '-' && line[i] != '+' && line[i] != '*' && line[i] != '/' && line[i] != '^' && line[i] != ')')) {
cout << "表达式有错" << endl;
return 0;
} // 所存数中含有不止一个小数点,或者数字后面跟的不是“+、-、*、/、^、)”,则为错误
temp[j] = '\0';
b = atof(temp);
push(stack1, b);
if (i == len) break;
} else {
if (line[i] == '-' || line[i] == '+' || line[i] == '*' || line[i] == '/' || line[i] == '^' || line[i] == '(' || line[i] == ')') // 若读入的字符为运算符的情况
{
g = 1;
if (line[i] == '(' && i == len) {
cout << "表达式有错" << endl;
return 0;
} // “(”放表达式最后面,错误
if (line[i] == '-' || line[i] == '+' || line[i] == '*' || line[i] == '/' || line[i] == '^') {
if (i == len) {
cout << "表达式有错" << endl;
return 0;
} // “+、-、*、/、^”放在表达式最后面,错误
if ((!isdigit(line[i + 1])) && (!isalpha(line[i + 1])) && line[i + 1] != '(') // “+、-、*、/、^”后面跟的不是数字或者变量,错误
{
cout << "表达式有错" << endl;
return 0;
}
}
if (line[i] == '-' && (i == 0 || line[i - 1] == '(')) // 运算符是负号
{
push(stack1, 0);
push2(stack2, line[i]);
i++;
} else { // 读入的运算符与运算符栈的栈顶元素相比,并进行相应的操作
if (in(stack2.n[stack2.top]) < out(line[i]) || stackempty2(stack2)) {
push2(stack2, line[i]);
i++;
} else if (in(stack2.n[stack2.top]) == out(line[i])) {
i++;
stack2.top--;
} else if (in(stack2.n[stack2.top]) > out(line[i])) {
pop(stack1, a);
pop(stack1, b);
pop2(stack2, operate);
count(b, operate, a);
}
if (i == len) break;
}
} else {
if (isalpha(line[i])) // 读入的字符是字母变量的情况
{
g = 1;
cout << "请输入变量";
while (isalpha(line[i])) {
cout << line[i];
i++;
}
cout << "的值" << endl;
cin >> b;
push(stack1, b);
if (i == len) break;
if (line[i] != '-' && line[i] != '+' && line[i] != '*' && line[i] != '/' && line[i] != '^' && line[i] != ')') // 变量后面跟的不是“+、-、*、/、^、)”,则为错误
{
cout << "表达式有错" << endl;
return 0;
}
}
}
}
if (g == 0) {
cout << "表达式有错" << endl;
return 0;
} // g=0表示该字符是不符合要求的字符
}
while (stack2.top != -1) // 读入结束后,继续进行操作,直到运算符栈为空
{
pop(stack1, a);
pop(stack1, b);
pop2(stack2, operate);
if (operate == '(' || operate == ')') // 括号多余的情况
{
cout << "表达式有错" << endl;
return 0;
}
count(b, operate, a);
}
cout << stack1.n[stack1.top] << endl;
return 0;
}
简单的c语言
对,是数据溢出了, 有符号int 类型的数据最对只能从 负的 2的15次方即(-32768) 到正的 2的15次方-1即32767.
所以32769 超过的部分得从 -32768算起 就是-32767
够详细的吧
\b
删除前一个字符,\'
输出单引号 \\
输出 \
\n
是换行嘛
所以输出 re'hi'you
经过实际运行
如何快速理解C语言
新手如何学习c语言 第一:一些概念。 c语言是一门程序设计语言,有一些标准,比较重要的是ansi c(好像是c89)和c99。 数据结构包括逻辑结构和物理结构。逻辑结构是数据元素集合和定义在集合上的关系。物理结构是逻辑结构在计算机中的实现。 lcc、vc、tc、gcc都是c语言编译器,一般包括集成开发环境,编译器和链接器及辅助工具。我们书写的是c源程序,源程序通过编译器编译为中间文件,中间文件经链接器链接生成可执行文件。不同操作系统可执行文件不同。中间文件也有几个标准,微软使用的和linux下通用的有差异。 第二:学习什么。 个人认为程序设计学习的重点放在数据结构的学习上,但是这种学习要有一个平台,比如c语言。 学习c语言首先要掌握基本语法,常量、变量、类型、及顺序结构、分支结构和循环结构的意义及用法。进一步学习构造类型如指针、结构、函数的意义和用法。 c语言提供一些标准函数以减轻程序设计工作量,这些函数我们自己也可以实现。即使不依靠函数库,只有编译器,理论上就足够了。事实上,提供的标准函数效率都很高,使用很频繁,没有自己实现的必要,所以掌握常用函数是非常必要的,但是要注意函数的适用范围。 继续学习因人而异,应该可以独立选择了。 第三:如何学习。 强调多实践,c语言的学习要经常上机,多写程序才能逐步提高。 推荐书籍:《C Programming Language》。有中译本,但最好看英文版。