请帮忙! [英] help needed please!

查看:75
本文介绍了请帮忙!的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

大家好!


任务1:编写一个程序,显示一个包含5个选项的菜单。第5个

选项退出

程序。每个选项都应该使用system()执行系统命令。


以下是我的不起眼的产品。

============ ====================================== =

#include< ; stdio.h>

#include< stdlib.h>


int menu(void);


int main(void)

{

int option;


while(1){

option = menu();

switch(选项){

case 1:

system(" dir");

休息;

案例2:

系统(帮助);

休息;

案例3:

系统(时间);

休息;

案例4:

system(type menu.c);

break;

case 5:

puts(" Quitting program");

退出(0);

默认值:

puts(不是有效选项,再试一次);

}

}

返回0;

}


int menu(void)< br $>
{

int option;


puts(" 1。列表目录);

puts(" 2.helct");

puts(" 3.time");

puts (4。类型menu.c);

put(5.退出);

scanf("%d",& option) ;


返回选项;

}

================= ================================= ========

问题:我的编译器给了我一个无法访问的代码。行警告

32(返回0;)。

程序运行正常。为什么编译器(Borland C ++ Builder 6)会发出这个

警告?


任务2:创建一个计算器程序。


以下是我的谦逊产品。

================================ ================== =================

#include< stdio.h> ;


void add(int a,int b);

void subtract(int a,int b);

void multiply(int a,int b);

void divide(int a,int b);


int main(void)

{

int a,b;

char c;


puts(输入整数);

scanf("%d",& a);

puts(输入运营商);

scanf(" %c",& c);

puts(输入另一个整数);

scanf("%d",& b);

开关(c){

case''*'':

乘以(a,b);

休息;

case''+'':

add(a,b);

break;

case'' - '':

减去(a,b);

休息;

case''/'' :

除以(a,b);

休息;

默认:

puts("运算符是无效);

}

返回0;

}


void add(int a,int b)

{

printf(" Sum is:%d",a + b);

}


void subtract(int a,int b)

{

printf(" Answer is:%d",ab); < br $>
}


void multiply(int a,int b)

{

printf("产品是:%d,a * b);

}


void divide(int a,int b)

{

printf(答案是:%d,a / b);

}

======== ========================================== ===

输出:


输入一个整数

2

输入运算符

输入另一个整数


问题:为什么程序不允许用户输入操作员

之前

直接跳到输入另一个整数?我想我很想念

非常好

这里的基础。


先谢谢你们!热爱你的工作!


巴克

-

使用M2,Opera的革命性电子邮件客户端: http://www.opera.com/m2/

解决方案

#while(1){

#option = menu();

#switch(option){

#}

#}

#return 0;


#问题:我的编译器正在给出我是一个无法访问的代码行警告

#32(返回0;)。

#程序运行正常。为什么编译器(Borland C ++ Builder 6)会给出这个

#警告?


因为它是真的吗? while条件总是正确的,并且循环没有

的断点或者循环中的getos。退出循环的唯一一个是退出

程序;无论它是永久循环还是退出程序,没有代码在

之后可以达到并执行循环。


-

Derk Gwen http://derkgwen.250free.com/html/index.html

基本上,你只是追踪。


Buck Rogers写道:

你好伙计们!

任务1:编写一个程序,显示一个包含5个选项的菜单。第5个
选项退出程序。每个选项都应该使用system()执行系统命令。

以下是我的简单产品。
==================== ============================== =
#include< stdio.h>
#include< ; stdlib.h>

int menu(void);

int main(void)
{
int option;

while(1){
option = menu();
switch(选项){
案例1:
系统(" dir");
休息;案例2:
系统(帮助);
休息;
案例3:
系统(时间);
休息;案例4:
系统(type menu.c);
休息;
案例5:
put(退出程序);
exit(0);
默认值:
puts(不是有效选项,再试一次);
}
}
返回0;
}

int menu(void)< put /> {
int option;

puts(" 1。列表目录);
puts(2. help);
puts(3。时间);
puts(4。type menu.c);
put(5.退出);
scanf("%d",& option);

返回选项;
} ================================================= = ========
问题:我的编译器给了我一个无法访问的代码。线路警告
32(返回0;)。
程序运行正常。为什么编译器(Borland C ++ Builder 6)会发出这个
警告?


你的while(1)循环的唯一出路是通过exit(0)调用,并且

并没有带你回归0;声明要么。因此

投诉。理想情况下,你应该可以用

返回0替换exit(0)调用;摆脱现有的返回语句,编译器应该很高兴。

任务2:创建计算器程序。

以下是我的谦虚提供。
============================================ ====== =================
#include< stdio.h>

void add(int a,int b);
void subtract(int a,int b);
void multiply(int a,int b);
void divide(int a,int b);

int main(void)
int a,b;
char c;

puts(输入一个整数);
scanf("%d",& a);


阅读精美的常见问题解答。特别是Q12.18:
http: //www.eskimo.com/~scs/C-faq/q12.18.html


一个修复是:

int ret ;

/ *读取一个int并丢弃其余行* /

ret = scanf("%d%* [^ \ n]",& a);

if(ret< 1){

/ * ret == 0 =>输入无效* /

/ * ret == EOF =>输入或错误结束* /

/ *适当处理* /

}

/ *读取并丢弃换行符* /

if(!feof(stdin))getchar();

puts(输入运算符);
scanf("%c"& c) ;


与上述类似的问题。可以应用类似的修复。

puts(输入另一个整数);
scanf("%d",& b);
switch(c){
案例''*'':
乘(a,b);
休息;
案例''+'':
add(a,b);
休息;
案例'' - '':
减去(a,b);
休息;
案例''/'':
除(a,b);
中断;
默认:
puts(操作员无效);
}
返回0;
}

void add(int a,int b)
{/> printf(" Sum is:%d",a + b);
}

void减法(int a,int b)
{
printf(答案是:%d,ab);
}

void multiply(int a,int b)
{/> printf(" Product is:%d",a * b);
}

void divide( int a, int b)
{
printf(答案是:%d,a / b);
}
============= ===================================== ===
输出:

输入一个整数
2
输入运算符
输入另一个整数

问题:为什么程序不允许用户输入运算符
直接跳到输入另一个整数之前?我想我很遗憾
这里非常基本的东西。


你缺少的是scanf的工作原理。在C书中浏览scanf

页面可以派上用场。


-nrk。

先谢谢你们!热爱你的工作!

巴克




-

删除电子邮件的devnull





Buck Rogers写道:

大家好!

任务1:编写一个程序提供5个选项的菜单。第5个
选项退出程序。每个选项都应该使用system()执行系统命令。

以下是我的简单产品。
==================== ============================== =
#include< stdio.h>
#include< ; stdlib.h>

int menu(void);

int main(void)
{
int option;

while(1){
option = menu();
switch(选项){
案例1:
系统(" dir");
休息;案例2:
系统(帮助);
休息;
案例3:
系统(时间);
休息;案例4:
系统(type menu.c);
休息;
案例5:
put(退出程序);
exit(0);
默认值:
puts(" not a有效选项,再试一次);
}
}
返回0;
}

int menu(void)
{
int选项;

puts(" 1。列表目录);
puts(2. help);
puts(3。时间);
puts(4。type menu.c);
put(5.退出);
scanf("%d",& option);

返回选项;
} ================================================= = ========
问题:我的编译器给了我一个无法访问的代码。线路警告
32(返回0;)。
程序运行正常。为什么编译器(Borland C ++ Builder 6)会给出这个警告?


它会发出警告,因为表达式无法访问。您可以

只是忽略警告或更改逻辑。一种方法是

将while循环更改为do-while循环。


#include< stdio.h>

int menu(void);


int main(无效)

{

int option;


做{

选项= menu();

开关(选项){

案例1:

system(dir);

break;

case 2:

system(" help" ;);

休息;

案例3:

系统(时间);

休息;

案例4:

system(type menu.c);

break;

case 5 :

put(戒烟计划);

休息;


默认:

puts(不是有效选项,再试一次);

}

} while(选项!= 5);

返回0 ;

}


int menu(无效)

{

int option;


put(1。列表目录);

puts(" 2。帮助");

put(3。时间);

puts(。type menu.c);

put(5.退出);

scanf("%d",& option);


返回选项;

}


任务2:创建一个计算器程序。

以下是我的简单产品。
== ================================================ == ===============
#include< stdio.h>

void add(int a,int b);
void subtract(int a,int b);
void multiply(int a,int b);
void divide(int a,int b);

int main(void)
{a / b;
char c;

puts(输入一个整数);
scanf("%d" ,& a);


这里添加:

getchar();

puts(输入运营商);
scanf ("%c",& c);
puts(输入另一个整数);
scanf("%d",& b);
switch(c ){
案例''*'':
乘(a,b);
休息;
案例''+'':
添加(a,b) );
休息;
案例'' - '':
减去(a,b);
休息;
案例''/'':
除(a,b);
中断;
默认:
置票(操作员无效);
}
返回0;
}

void add(int a,int b)
{/> printf(" Sum is:%d",a + b);


printf(总和是:%d \ n,a + b);

}

无效减法(int a,int b)
{
printf(" Answer is:%d",ab);


printf(答案是:%d \ n,a-b); }

void multiply(int a,int b)
{/> printf(" Product is:%d",a * b);


printf(产品是:%d \ n,a * b); }

void divide(int a,int b)
{
printf(" Answer is:%d",a / b);


printf(答案是:%d \ n,a / b); }
============================================= ===== ===
输出:

输入一个整数
2
输入运算符
输入另一个整数
才能直接跳到输入另一个整数?我想我很遗憾
这里非常基本的东西。




当你输入第一个数字时,你的输入包括

数字和换行符,''\ n'',因为你预先输入了

回车键。在第一个scanf语句中,%d说明符将

在输入''\ n''字符时停止读取输入缓冲区。

此字符保留在输入缓冲区。在下一个scanf

语句中,您使用的是%c说明符。这个说明符不会删除前导空格,所以换行符char的值是

读入变量c。运算符*,/,+或 - 以及换行符

保留在输入缓冲区中。你的下一个scanf语句使用

%d来读取运算符。看到操作员不是数字,

它会中止扫描。


简单的解决方案是在第一次扫描后放入getchar()$ /
语句将从输入

缓冲区中删除换行符,并使您能够将运算符读入变量c。


-

Al Bowers

美国佛罗里达州坦帕市

mailto: xa ****** @ myrapidsys.com (删除x发送电子邮件)
http://www.geocities.com/abowers822/


Hi guys!

Task 1: Write a program which presents a menu with 5 options. The 5th
option quits
the program. Each option should execute a system command using system().

Below is my humble offering.
================================================== =
#include <stdio.h>
#include <stdlib.h>

int menu( void );

int main( void )
{
int option;

while(1) {
option = menu();
switch(option) {
case 1:
system("dir");
break;
case 2:
system("help");
break;
case 3:
system("time");
break;
case 4:
system("type menu.c");
break;
case 5:
puts("Quitting program");
exit(0);
default:
puts("Not a valid option, try again");
}
}
return 0;
}

int menu( void )
{
int option;

puts("1. List directory");
puts("2. help");
puts("3. time");
puts("4. type menu.c");
puts("5. Quit");
scanf("%d", &option);

return option;
}
================================================== ========
Problem: My compiler is giving me an "unreachable code" warning for line
32(return 0;). The
program runs fine. Why does the compiler(Borland C++ Builder 6) give this
warning?

Task 2: Create a calculator program.

Below is my humble offering.
================================================== =================
#include <stdio.h>

void add(int a, int b);
void subtract(int a, int b);
void multiply(int a, int b);
void divide(int a, int b);

int main( void )
{
int a, b;
char c;

puts("Enter an integer");
scanf("%d", &a);
puts("Enter an operator");
scanf("%c", &c);
puts("Enter another integer");
scanf("%d", &b);
switch( c ) {
case ''*'':
multiply(a, b);
break;
case ''+'':
add(a, b);
break;
case ''-'':
subtract(a, b);
break;
case ''/'':
divide(a, b);
break;
default:
puts("Operator is not valid");
}
return 0;
}

void add(int a, int b)
{
printf("Sum is: %d", a+b);
}

void subtract(int a, int b)
{
printf("Answer is: %d", a-b);
}

void multiply(int a, int b)
{
printf("Product is: %d", a*b);
}

void divide(int a, int b)
{
printf("Answer is: %d", a/b);
}
================================================== ===
Output:

Enter an integer
2
Enter an operator
Enter another integer

Problem: Why does the program not allow the user to input the operator
before
jumping straight to "Enter another integer"? I think I am missing
something very
fundamental here.

Thanks in advance guys! Love your work!

Buck
--
Using M2, Opera''s revolutionary e-mail client: http://www.opera.com/m2/

解决方案

# while(1) {
# option = menu();
# switch(option) {
# }
# }
# return 0;

# Problem: My compiler is giving me an "unreachable code" warning for line
# 32(return 0;). The
# program runs fine. Why does the compiler(Borland C++ Builder 6) give this
# warning?

Because it''s true? The while condition is always true, and loop is devoid
of breaks or gotos out of loop. The only one to exit the loop is to exit
the program; whether it loops forever or exits the program, no code after
the loop can be reached and executed.

--
Derk Gwen http://derkgwen.250free.com/html/index.html
So basically, you just trace.


Buck Rogers wrote:

Hi guys!

Task 1: Write a program which presents a menu with 5 options. The 5th
option quits
the program. Each option should execute a system command using system().

Below is my humble offering.
================================================== =
#include <stdio.h>
#include <stdlib.h>

int menu( void );

int main( void )
{
int option;

while(1) {
option = menu();
switch(option) {
case 1:
system("dir");
break;
case 2:
system("help");
break;
case 3:
system("time");
break;
case 4:
system("type menu.c");
break;
case 5:
puts("Quitting program");
exit(0);
default:
puts("Not a valid option, try again");
}
}
return 0;
}

int menu( void )
{
int option;

puts("1. List directory");
puts("2. help");
puts("3. time");
puts("4. type menu.c");
puts("5. Quit");
scanf("%d", &option);

return option;
}
================================================== ========
Problem: My compiler is giving me an "unreachable code" warning for line
32(return 0;). The
program runs fine. Why does the compiler(Borland C++ Builder 6) give this
warning?

The only way out of your while ( 1 ) loop is through the exit(0) call, and
that doesn''t bring you to the return 0; statement either. Hence the
complaint. Ideally, you should be able to replace the exit(0) call with
return 0; get rid of the existing return statements and the compiler should
be happy.
Task 2: Create a calculator program.

Below is my humble offering.
================================================== =================
#include <stdio.h>

void add(int a, int b);
void subtract(int a, int b);
void multiply(int a, int b);
void divide(int a, int b);

int main( void )
{
int a, b;
char c;

puts("Enter an integer");
scanf("%d", &a);
Read the fine FAQ. Specifically Q12.18:
http://www.eskimo.com/~scs/C-faq/q12.18.html

One fix is:
int ret;
/* read an int and discard rest of line */
ret = scanf("%d%*[^\n]", &a);
if ( ret < 1 ) {
/* ret == 0 => invalid input */
/* ret == EOF => end of input or error */
/* handle appropriately */
}
/* read and discard newline */
if ( !feof(stdin) ) getchar();
puts("Enter an operator");
scanf("%c", &c);
Similar problem as above. Similar fix can be applied.
puts("Enter another integer");
scanf("%d", &b);
switch( c ) {
case ''*'':
multiply(a, b);
break;
case ''+'':
add(a, b);
break;
case ''-'':
subtract(a, b);
break;
case ''/'':
divide(a, b);
break;
default:
puts("Operator is not valid");
}
return 0;
}

void add(int a, int b)
{
printf("Sum is: %d", a+b);
}

void subtract(int a, int b)
{
printf("Answer is: %d", a-b);
}

void multiply(int a, int b)
{
printf("Product is: %d", a*b);
}

void divide(int a, int b)
{
printf("Answer is: %d", a/b);
}
================================================== ===
Output:

Enter an integer
2
Enter an operator
Enter another integer

Problem: Why does the program not allow the user to input the operator
before
jumping straight to "Enter another integer"? I think I am missing
something very
fundamental here.

What you''re missing is knowledge of how scanf works. A trip to the scanf
pages in your C book could come in handy.

-nrk.
Thanks in advance guys! Love your work!

Buck



--
Remove devnull for email




Buck Rogers wrote:

Hi guys!

Task 1: Write a program which presents a menu with 5 options. The 5th
option quits
the program. Each option should execute a system command using system().

Below is my humble offering.
================================================== =
#include <stdio.h>
#include <stdlib.h>

int menu( void );

int main( void )
{
int option;

while(1) {
option = menu();
switch(option) {
case 1:
system("dir");
break;
case 2:
system("help");
break;
case 3:
system("time");
break;
case 4:
system("type menu.c");
break;
case 5:
puts("Quitting program");
exit(0);
default:
puts("Not a valid option, try again");
}
}
return 0;
}

int menu( void )
{
int option;

puts("1. List directory");
puts("2. help");
puts("3. time");
puts("4. type menu.c");
puts("5. Quit");
scanf("%d", &option);

return option;
}
================================================== ========
Problem: My compiler is giving me an "unreachable code" warning for line
32(return 0;). The
program runs fine. Why does the compiler(Borland C++ Builder 6) give
this warning?

It gives the warning because the expression is unreachable. You can
simply ignore the warning or you change the logic. One way would be
change the while loop into a do-while loop.

#include <stdio.h>

int menu( void );

int main( void )
{
int option;

do {
option = menu();
switch(option) {
case 1:
system("dir");
break;
case 2:
system("help");
break;
case 3:
system("time");
break;
case 4:
system("type menu.c");
break;
case 5:
puts("Quitting program");
break;

default:
puts("Not a valid option, try again");
}
}while(option != 5);
return 0;
}

int menu( void )
{
int option;

puts("1. List directory");
puts("2. help");
puts("3. time");
puts("4. type menu.c");
puts("5. Quit");
scanf("%d", &option);

return option;
}


Task 2: Create a calculator program.

Below is my humble offering.
================================================== =================
#include <stdio.h>

void add(int a, int b);
void subtract(int a, int b);
void multiply(int a, int b);
void divide(int a, int b);

int main( void )
{
int a, b;
char c;

puts("Enter an integer");
scanf("%d", &a);
Add here:
getchar();
puts("Enter an operator");
scanf("%c", &c);
puts("Enter another integer");
scanf("%d", &b);
switch( c ) {
case ''*'':
multiply(a, b);
break;
case ''+'':
add(a, b);
break;
case ''-'':
subtract(a, b);
break;
case ''/'':
divide(a, b);
break;
default:
puts("Operator is not valid");
}
return 0;
}

void add(int a, int b)
{
printf("Sum is: %d", a+b);
printf("Sum is: %d\n", a+b);
}

void subtract(int a, int b)
{
printf("Answer is: %d", a-b);
printf("Answer is: %d\n", a-b); }

void multiply(int a, int b)
{
printf("Product is: %d", a*b);
printf("Product is: %d\n", a*b); }

void divide(int a, int b)
{
printf("Answer is: %d", a/b);
printf("Answer is: %d\n", a/b); }
================================================== ===
Output:

Enter an integer
2
Enter an operator
Enter another integer

Problem: Why does the program not allow the user to input the operator
before
jumping straight to "Enter another integer"? I think I am missing
something very
fundamental here.



When you enter the first number, your input includes the digits for
the number and the newline char, ''\n'', because you preesed the
enter key. In the first scanf statement, the %d specifier will
stop reading the input buffer when it incounters the ''\n'' character.
This character remains in the input buffer. In the next scanf
statement you are using the %c specifier. This specifier will not
remove leading whitespace, so the value of the newline char is
read into variable c. The operator,*,/,+ or -, and a newline character
remain in the input buffer. Your next scanf statement, using
the %d, reads the operator. Seeing the operator is not a digit,
it aborts scanning.

The easy solution is to put getchar() after the first scanf
statement which will remove the newline char from the input
buffer and enables you to read the operator into variable c.

--
Al Bowers
Tampa, Fl USA
mailto: xa******@myrapidsys.com (remove the x to send email)
http://www.geocities.com/abowers822/


这篇关于请帮忙!的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆