Facebook Sharer
选择您要替换的背景颜色:
【农历新年】背景图片:
个性化设定
 注册  找回密码
查看: 3769|回复: 27
打印 上一主题 下一主题

很难的C!!!对菜鸟难如登天?对老鸟易如反掌?我是菜鸟!

[复制链接]

55

主题

0

好友

2027

积分

白金长老

Rank: 10

跳转到指定楼层
1#
发表于 2009-9-6 11:43 PM |只看该作者 |倒序浏览
Question1: Matrix operations using array and function.

Write a complete C program to perform Matrix operations such as Addition, Subtraction, Multiplication and Transpose according to the user’s choice. Define functions to perform these various operations and call them from the main function whenever needed. The program should display the following menu to the user for them to select one of the five options listed.

                ------------------
                    MAIN MENU
                        ------------------
1.MATRIX ADDITION
2.MATRIX SUBTRACTION
3.MATRIX MLTIPLICATION
4.MATRIX TRANSPOSE
5.TO EXIT
---------------------------
                Please enter your option <1/2/3/4/5>:

The program should ask the elements of the input matrix or matrices from the user once the required operation is selected and also should check whether the required operation can be carried out or not based on the input data. The program should produce a neat output showing both the input and the resultant matrix in matrix form. The program should not be terminated until the user selects the option number 5 (TO EXIT).  


我到这里就不会了:

#include "stdafx.h"
#include <stdio.h>

int main()
{
        int add,sub,mul,tran,opt;
        printf("---------\n");
        printf("MAIN MENU\n");
        printf("---------\n");
        printf("1-MATRIX ADDITION\n");
        printf("2-MATRIX SUBTRACTION\n");
        printf("3-MATRIX MULTIPLICATION\n");
        printf("4-MATRIX TRANPOSE\n");
        printf("5-EXIT\n");
        printf("-------------------\n");
        printf("Please enter your option <1/2/3/4/5>:\n");

        scanf("%d",&opt);
switch(opt);
{
case 1:这里开始完全不懂怎样写!



while(opt == 5);
{
        printf("---------\n");
        printf("MAIN MENU\n");
        printf("---------\n");
        printf("1-MATRIX ADDITION\n");
        printf("2-MATRIX SUBTRACTION\n");
        printf("3-MATRIX MULTIPLICATION\n");
        printf("4-MATRIX TRANPOSE\n");
        printf("5-EXIT\n");
        printf("-------------------\n");
        printf("Please enter your option <1/2/3/4/5>:\n");

        scanf("%d",&opt);
}
}


p/s:应该是C不是C++,谢谢三楼大大的提醒!

[ 本帖最后由 aiklone1314 于 2009-9-7 10:09 AM 编辑 ]




收藏收藏0

46

主题

6

好友

6456

积分

百变名嘴

Rank: 13Rank: 13Rank: 13Rank: 13

2#
发表于 2009-9-7 12:21 AM |只看该作者

回复 #1 aiklone1314 的帖子

怎么你的 C++ 怪怪的 跟我看过的使用法有点不太相同。

你的switch 开错了。

int main()
{
        int add,sub,mul,tran,opt;
        printf("---------\n");
        printf("MAIN MENU\n");
        printf("---------\n");
        printf("1-MATRIX ADDITION\n");
        printf("2-MATRIX SUBTRACTION\n");
        printf("3-MATRIX MULTIPLICATION\n");
        printf("4-MATRIX TRANPOSE\n");
        printf("5-EXIT\n");
        printf("-------------------\n");
        printf("lease enter your option <1/2/3/4/5>:\n");

scanf("%d",&opt);

switch(opt)
{
case 1:
//在这里执行动作吧;
break;

case 2:
break;

case 3:
break;

case 4:
break;

case 5:
break;
}


回复

使用道具 举报

7

主题

1

好友

5108

积分

一流名嘴

Rank: 12Rank: 12Rank: 12

3#
发表于 2009-9-7 01:07 AM |只看该作者
原帖由 aiklone1314 于 2009-9-6 11:43 PM 发表


我到这里就不会了:
[quote]#include "stdafx.h"
#include <stdio.h>

int main()
{
        int add,sub,mul,tran,opt;
        printf("---------\n");
        printf("MAIN MENU\n");
        printf("---------\n");
        printf("1-MATRIX ADDITION\n");
        printf("2-MATRIX SUBTRACTION\n");
        printf("3-MATRIX MULTIPLICATION\n");
        printf("4-MATRIX TRANPOSE\n");
        printf("5-EXIT\n");
        printf("-------------------\n");
        printf("Please enter your option <1/2/3/4/5>:\n");

        scanf("%d",&opt);
switch(opt);
{
case 1:这里开始完全不懂怎样写!


while(opt == 5);
{
        printf("---------\n");
        printf("MAIN MENU\n");
        printf("---------\n");
        printf("1-MATRIX ADDITION\n");
        printf("2-MATRIX SUBTRACTION\n");
        printf("3-MATRIX MULTIPLICATION\n");
        printf("4-MATRIX TRANPOSE\n");
        printf("5-EXIT\n");
        printf("-------------------\n");
        printf("Please enter your option <1/2/3/4/5>:\n");

        scanf("%d",&opt);
}
}

[/quote]


switch 的用法沒仔細研究或多練習
去翻翻你的筆記看看怎麼使用 switch ... case

妳的 option 沒退回 main menu 就不需要用到 while 循環




p/s: 你是在寫 C 而不是 C++


回复

使用道具 举报

55

主题

0

好友

2027

积分

白金长老

Rank: 10

4#
发表于 2009-9-7 09:59 AM |只看该作者
回复 #2 宅男-兜着走 的帖子
回复 #3 Super-Tomato 的帖子

我懂格式是要怎样,不过switch里的action要怎样打,我完全不知道。你们明白题目的意思吗?我看到不是很明白。


回复

使用道具 举报

55

主题

0

好友

2027

积分

白金长老

Rank: 10

5#
发表于 2009-9-7 10:04 AM |只看该作者

回复 #3 Super-Tomato 的帖子

因为题目是要如果按任何号码,除了5,都会从新跳回menu。
就是说计算操作完了后,会在从新逃回menu。
然后,只有按5,程式才会跳出来。


回复

使用道具 举报

7

主题

1

好友

5108

积分

一流名嘴

Rank: 12Rank: 12Rank: 12

6#
发表于 2009-9-7 12:16 PM |只看该作者
原帖由 aiklone1314 于 2009-9-7 10:04 AM 发表
因为题目是要如果按任何号码,除了5,都会从新跳回menu。
就是说计算操作完了后,会在从新逃回menu。
然后,只有按5,程式才会跳出来。



那麼你也就明白其中的條件是 "非5循環"
這樣的話你就看看 while 的條件值是甚麼

從你第一樓的 coding 來看, 妳好像完全沒去練習哦
既然這樣你就先撇開你的題目, 先在這裡說明看看 while 和 switch...case 怎麼應用吧


回复

使用道具 举报

55

主题

0

好友

2027

积分

白金长老

Rank: 10

7#
发表于 2009-9-7 09:07 PM |只看该作者
我知道是 !=5,不过当我execute时,不知道为何他是倒反的,就是说当我放!=5时他反而会从新跳回menu,放==5他就不会这样。


回复

使用道具 举报

7

主题

1

好友

5108

积分

一流名嘴

Rank: 12Rank: 12Rank: 12

8#
发表于 2009-9-7 09:52 PM |只看该作者
原帖由 aiklone1314 于 2009-9-7 09:07 PM 发表
我知道是 !=5,不过当我execute时,不知道为何他是倒反的,就是说当我放!=5时他反而会从新跳回menu,放==5他就不会这样。



那是你其他錯誤問題導致的
如果你真知道怎麼做的話, 那麼就用心繼續做吧


回复

使用道具 举报

46

主题

6

好友

6456

积分

百变名嘴

Rank: 13Rank: 13Rank: 13Rank: 13

9#
发表于 2009-9-7 09:53 PM |只看该作者

回复 #7 aiklone1314 的帖子

while(opt != 5){} 翻译成人话的话就是  当OPT 不是5 的话请执行。
while loop 不是 if else, 当然不同啊。 我没C的篇写工具也 也没 compiler , 抱歉。 所以我照我懂的跟你说。


int main()
{

        int add,sub,mul,tran,opt; // 这是什么?
        printf("---------\n");
        printf("MAIN MENU\n");
        printf("---------\n");
        printf("1-MATRIX ADDITION\n");
        printf("2-MATRIX SUBTRACTION\n");
        printf("3-MATRIX MULTIPLICATION\n");
        printf("4-MATRIX TRANPOSE\n");
        printf("5-EXIT\n");
        printf("-------------------\n");
        printf("lease enter your option <1/2/3/4/5>:\n");

        scanf("%d",&opt);

switch(opt != 5){
// 如果是使用者输入 1 的话就会一直在 case 1 循环, 记得在 switch 之前问他们是不是要退出程序, 不然就要 ALT + CRTL + DELETE 了。
//当opt 是5的时候就会退出 while loop 了。你要用do while 也是可以。
case 1:
break;

case 2:
break;

case 3:
break;

case 4:
break;
}

printf(program exit.); // 通知使用者退出program 中。


应该就是这样罢了。


回复

使用道具 举报

7

主题

1

好友

5108

积分

一流名嘴

Rank: 12Rank: 12Rank: 12

10#
发表于 2009-9-7 10:12 PM |只看该作者
原帖由 宅男-兜着走 于 2009-9-7 09:53 PM 发表
while(opt != 5){} 翻译成人话的话就是  当OPT 不是5 的话请执行。
while loop 不是 if else, 当然不同啊。 我没C的篇写工具也 也没 compiler , 抱歉。 所以我照我懂的跟你说。


int main()
{

      ...


其實 while 和 if 的參數值性質是一樣的, 只要 while(條件) 能夠滿足就會繼續執行


回复

使用道具 举报

您需要登录后才可以回帖 登录 | 注册

JBTALKS.CC |联系我们 |隐私政策 |Share

GMT+8, 2024-10-28 05:24 AM , Processed in 0.115758 second(s), 28 queries .

Powered by Discuz! X2.5

© 2001-2012 Comsenz Inc.

Ultra High-performance Dedicated Server powered by iCore Technology Sdn. Bhd.
Domain Registration | Web Hosting | Email Hosting | Forum Hosting | ECShop Hosting | Dedicated Server | Colocation Services
本论坛言论纯属发表者个人意见,与本论坛立场无关
Copyright © 2003-2012 JBTALKS.CC All Rights Reserved
合作联盟网站:
JBTALKS 马来西亚中文论坛 | JBTALKS我的空间 | ICORE TECHNOLOGY SDN. BHD.
回顶部